c++ - _vscwprintf on Mac OS X/Linux -


i porting application on mac os x written windows.

in application, there many instances of _vscwprintf , _vscprintf.

this question helped me implement _vsprintf on mac os x. same technique _vswprintf not working.

can give alternative of _vscwprintf on mac os x? or there equivalent method this?

microsoft describes functions returning number of characters used if string formatted — note documented not including null terminator.

int _vscprintf(    const char *format,    va_list argptr  ); int _vscwprintf(    const wchar_t *format,    va_list argptr  ); 

initial answer

these functions can, therefore, emulated vsprintf() , vswprintf():

int _vscprintf(const char *format, va_list argptr) {     return(vsnprintf(0, 0, format, argptr)); } 

int _vscwprintf(const wchar_t *format, va_list argptr) {     return(vswprintf(0, 0, format, argptr)); } 

it whether remove leading underscore; would.

note _vscwprintf() implementation above flawed; see code below.


vscprintf() , scprintf()

apologies: wrote vsprintf() needed write vsnprintf() (now fixed in code above); however, vswprintf() has safer interface buffer length, there no vsnwprintf(). there's reason prefer test compile code before (or shortly after) posting — it's been irksome not having wherewithal couple of days.

here's sscce vscprintf() (and scprintf()):

#include <stdio.h> #include <stdarg.h>  extern int vscprintf(const char *format, va_list argptr); extern int scprintf(const char *format, ...);  int vscprintf(const char *format, va_list argptr) {     return(vsnprintf(0, 0, format, argptr)); }  int scprintf(const char *format, ...) {     va_list args;     va_start(args, format);     int rc = vscprintf(format, args);     va_end(args);     return rc; }  int main(void) {     int l = scprintf("%-8s %8d\n", "abc", 123);     if (l > 0)     {         char buffer[l+1];         int n = snprintf(buffer, sizeof(buffer), "%-8s %8d\n", "abc", 123);         printf("%d = %d: %s", l, n, buffer);     }     return 0; } 

output:

18 = 18: abc           123 

vscwprintf() , scwprintf()

it turns out harder simulate _vscwprintf() because vswprintf() function not helpful vsnprintf() function. specifically, vswprintf() reports error if formatted string won't fit in formatted space, whereas vsnprintf() reports number of characters have been needed in buffer if going fit. hence, have work trial , error:

#include <stdio.h> #include <stdarg.h> #include <wchar.h>  extern int vscwprintf(const wchar_t *format, va_list argptr); extern int scwprintf(const wchar_t *format, ...);  int vscwprintf(const wchar_t *format, va_list argptr) {     // unlike vsnprintf(), vswprintf() not tell how many     // characters have been written if there space enough in     // buffer - reports error when there not enough     // space.  assume moderately large machine kilobytes of wchar_t     // on stack not problem.     int buf_size = 1024;     while (buf_size < 1024 * 1024)     {         va_list args;         va_copy(args, argptr);         wchar_t buffer[buf_size];         int fmt_size = vswprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), format, args);         if (fmt_size >= 0)             return fmt_size;         buf_size *= 2;     }     return -1; }  int scwprintf(const wchar_t *format, ...) {     va_list args;     va_start(args, format);     int rc = vscwprintf(format, args);     va_end(args);     return rc; }  int main(void) {     int l = scwprintf(l"%-8ls %8d\n", l"abc", 123);     if (l > 0)     {         wchar_t buffer[l+1];         int n = swprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), l"%-8ls %8d\n", l"abc", 123);         wprintf(l"%d = %d: %ls", l, n, buffer);     }     return 0; } 

when run, produces output

18 = 18: abc           123 

(the same before).

tested on mac os x 10.8.3 using gcc 4.7.3 (which built on mac os x 10.7.5, shouldn't cause problems).


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -