I use the p_original() -function below without problems. My code has a hundred of these calls.
Now I want to overload it to be able to append text in several steps to a character array before I print it. I know that this is easily done using String(), but this way I hope to avoid fragmenting my free ram.
All I want to achieve is to be able to supply a bool to the p() function so it can decide if to append or start from the first character.
//Original, works fine
char *p_original( const char *fmt, ... ) {
static char tmp[ 192 ]; // resulting string limited to 192 chars
va_list args;
va_start ( args, fmt );
vsnprintf(tmp, sizeof( tmp ), fmt, args );
va_end ( args );
return tmp;
}
// My intended solution with a possibility to append text
char *p( const char *fmt, ... ) {
return p ( 0, fmt, __VA_ARGS__ );
}
char *p_append( const char *fmt, ... ) {
return p ( 1, fmt, __VA_ARGS__ );
}
char *p( bool append, const char *fmt, ... ) {
static char tmp[ 224 ]; // resulting string limited to 224 chars
va_list args;
va_start ( args, fmt );
if ( append ) {
vsnprintf( tmp[ strlen(tmp) ], sizeof( tmp ), fmt, args );
}
else {
vsnprintf(tmp, sizeof( tmp ), fmt, args );
}
va_end ( args );
return tmp;
}
Below is a small part of the error-message. Many warnings and errors on ambiguity.
How do I pass this bool without ambiguity?
D:\Mina Dokument\Arduino\MQTT\mqtt_RullgardinVardagsrum_v004\pushover.ino: In function 'int8_t securePublishPUSHOVER(int8_t, const char*, const char*, int8_t, int8_t)':
D:\Mina Dokument\Arduino\MQTT\mqtt_RullgardinVardagsrum_v004\pushover.ino:158:73: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
log( 0, p( G( "securePublishPUSHOVER() using file %s" ), freeFile ) );
^
D:\Mina Dokument\Arduino\MQTT\mqtt_RullgardinVardagsrum_v004\filelogstring.ino:307:7: note: candidate 1: char* p(bool, const char*, ...)
char *p( bool append, const char *fmt, ... ) {
^
D:\Mina Dokument\Arduino\MQTT\mqtt_RullgardinVardagsrum_v004\filelogstring.ino:300:7: note: candidate 2: char* p(const char*, ...)
char *p( const char *fmt, ... ) {
^
D:\Mina Dokument\Arduino\MQTT\mqtt_RullgardinVardagsrum_v004\pushover.ino:172:81: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second: [enabled by default]
log(1, p( G( "securePublishPUSHOVER() failed using file %s" ), freeFile ) );
^
D:\Mina Dokument\Arduino\MQTT\mqtt_RullgardinVardagsrum_v004\filelogstring.ino:307:7: note: candidate 1: char* p(bool, const char*, ...)
char *p( bool append, const char *fmt, ... ) {
^
D:\Mina Dokument\Arduino\MQTT\mqtt_RullgardinVardagsrum_v004\filelogstring.ino:300:7: note: candidate 2: char* p(const char*, ...)
char *p( const char *fmt, ... ) {
^
D:\Mina Dokument\Arduino\MQTT\mqtt_RullgardinVardagsrum_v004\rullgardin.ino: In function 'int MotorControl(MotorCommand, int8_t)':
rullgardin:142:148: error: call of overloaded 'p(const char [52], const char [15], const char [15], const char [12], int8_t&)' is ambiguous
log(1, p("motorState %s -> %s using command %s where pos = %d", g_motorStates[oldState], g_motorStates[g_motorState], g_motorCommands[c], pos) );
^