Skip to content

Print::vprintf (and syntactic glue for flash) (IDFGH-8582) #7392

Closed
@dirkx

Description

@dirkx

Is your feature request related to a problem?

Although (AFAIK) the ESP32 does not need the F() or PROGMEM macro to force a const char * into flash -- a lot of code in the Arduino system contains this.

Secondly - the Print class for the ESP32 currently lacks a vprintf -- which is useful if you override the printf (e.g. for a writev() like approach).

Describe the solution you'd like.

May be useful to kill these two birds with one stone; by splitting the printf and adding the syntactic glue.

size_t Print::printf(const __FlashStringHelper *ifsh, ...) {
   va_list arg;
   va_start(arg, ifsh);
   const char * format = (reinterpret_cast<const char *>(ifsh));
   size_t ret = vprintf(format, arg);
   va_end(arg);
   return ret;
}

size_t Print::printf(const char *format, ...)
{
    va_list arg;
    va_start(arg, format);
    size_t ret = vprintf(format, arg);
    va_end(arg);
    return ret;
}

size_t Print::vprinttf(const char *format, va_list arg) {
    char loc_buf[64];
    char * temp = loc_buf;
    va_list copy;
    va_copy(copy, arg);
    int len = vsnprintf(temp, sizeof(loc_buf), format, copy);
    va_end(copy);
    if(len < 0) {
        va_end(arg);
        return 0;
    };
    if(len >= sizeof(loc_buf)){
        temp = (char*) malloc(len+1);
        if(temp == NULL) {
            va_end(arg);
            return 0;
        }
        len = vsnprintf(temp, len+1, format, arg);
    }
    va_end(arg);
    len = write((uint8_t*)temp, len);
    if(temp != loc_buf){
        free(temp);
    }
    return len;
}

Describe alternatives you've considered.

Adding a vprintf and associated in a class above Print -and inject this.

Additional context.

No response

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions