imacarthur wrote:
> On 30 Dec 2009, at 19:24, Greg Ercolano wrote:
>>      Solution on win32 is to change _snprintf() to _snprintf_s()
>>      with the _TRUNCATE macro as the third argument. But there's no
>>      clear way to use a macro shortcut to do this (since this is a
>>      varargs function), so a cross platform solution is tricky at best.
> 
> Well, if we can assume C99 compliance (I'm not sure that we can) and  
> that the C++ compiler also supports variadic macros (most do, but it  
> is not part of any C++ spec yet, AFAIK...)
> 
> Then, something like:
> 
> #ifdef WIN32
> #  define SNPRINTF(SS, CC, ...) snprintf(SS, CC, _TRUNCATE, __VA_ARGS__)
> #else
> #  define SNPRINTF(SS, CC, ...) snprintf(SS, CC, __VA_ARGS__)
> #endif

        If that flies on all the win32 compilers we support,
        that'd be great.. I'm just not sure it is either.

        One change to the above, though:

BEFORE: #define SNPRINTF(SS, CC, ...) snprintf(SS, CC, _TRUNCATE, __VA_ARGS__)
 AFTER: #define SNPRINTF(SS, CC, ...) _snprintf_s(SS, CC, _TRUNCATE, 
__VA_ARGS__)
                                      ^^^^^^^^^^^

        Gotta have that leading "_" and trailing "_s" on the name for it to 
work,
        as it's a different function.

        I just tried the following with the test program on 2008 VS Express
        and it worked correctly, eg:

#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#define snprintf(SS, CC, ...) _snprintf_s(SS, CC, _TRUNCATE, __VA_ARGS__)       
        // ADDED THIS
#endif /*MICROSOFT*/
int main() {
    int ret;
    char ss[20];
    strcpy(ss, "12345678XY");   // 'X' and 'Y' should never be touched or seen 
in the following
    ret=snprintf(ss, 8, "1234%s","56");     fprintf(stderr,"RET=%d, SS='%s': 
TEST RESULT: %s\n", ret, ss, 
(ss[8]=='X'&&ss[9]=='Y'&&strlen(ss)<8)?"OK":"FAIL!");
    ret=snprintf(ss, 8, "1234%s","567");    fprintf(stderr,"RET=%d, SS='%s': 
TEST RESULT: %s\n", ret, ss, 
(ss[8]=='X'&&ss[9]=='Y'&&strlen(ss)<8)?"OK":"FAIL!");
    ret=snprintf(ss, 8, "1234%s","5678");   fprintf(stderr,"RET=%d, SS='%s': 
TEST RESULT: %s\n", ret, ss, 
(ss[8]=='X'&&ss[9]=='Y'&&strlen(ss)<8)?"OK":"FAIL!");
    ret=snprintf(ss, 8, "1234%s","56789");  fprintf(stderr,"RET=%d, SS='%s': 
TEST RESULT: %s\n", ret, ss, 
(ss[8]=='X'&&ss[9]=='Y'&&strlen(ss)<8)?"OK":"FAIL!");
    ret=snprintf(ss, 8, "1234%s","567890"); fprintf(stderr,"RET=%d, SS='%s': 
TEST RESULT: %s\n", ret, ss, 
(ss[8]=='X'&&ss[9]=='Y'&&strlen(ss)<8)?"OK":"FAIL!");
    return(0);
}

        ..and the output on win32 was just like linux, making the above code
        cross platform compilable:

Z:\erco\examples\c>.\test-snprintf_s
RET=6, SS='123456': TEST RESULT: OK
RET=7, SS='1234567': TEST RESULT: OK
RET=-1, SS='1234567': TEST RESULT: OK
RET=-1, SS='1234567': TEST RESULT: OK
RET=-1, SS='1234567': TEST RESULT: OK

        Sweet!

        Should I make an STR?

_______________________________________________
fltk-dev mailing list
fltk-dev@easysw.com
http://lists.easysw.com/mailman/listinfo/fltk-dev

Reply via email to