Changeset 220


Ignore:
Timestamp:
04/07/14 02:58:00 (10 years ago)
Author:
Maciej Komosinski
Message:
  • moved vsnprintf-related #defines to nonstd.h
  • ssprintf_va() now works with non-C99 conformant vsnprintf(): when possible, it obtains the required number of characters in a single call instead of iterating
Location:
cpp/common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/nonstd.h

    r197 r220  
    113113//typedef char byte; //rozne srodowiska c++ definiuja byte jako unsigned char! w javie jest inaczej -> trzeba i tak zmienic w portowanych zrodlach byte na char.
    114114
     115//vsnprintf implementations support different standards, some only return -1 instead of the required number of characters
     116//these definitions are used by stl-util.cpp/ssprintf_va and SString::sprintf
     117#ifdef LINUX
     118#define VSNPRINTF_RETURNS_REQUIRED_SIZE
     119#endif
     120#if defined _WIN32 && !defined __BORLANDC__
     121#define USE_VSCPRINTF
     122#endif
     123
    115124
    116125#endif
  • cpp/common/stl-util.cpp

    r197 r220  
    99#include "nonstd.h"
    1010#include "framsg.h"
     11#include <assert.h>
    1112
    1213string ssprintf_va(const char* format, va_list ap)
    1314{
    14         string s; //clang crashed when this declaration was in s=buf - dreadful GOTO? - i doubt it... goto did not skip over any variable declarations in this case
    15         long size=1000;
     15        string s; //clang crashed when this declaration was in s=buf
     16        long size=256;
    1617        char* buf;
    17 retry:
    18         buf=(char*)malloc(size);
    19         if (vsnprintf(buf,size,format,ap)>=size) {free(buf); size*=4; goto retry;}
    20         s=buf;
    21         free(buf);
    22         return s;
     18
     19        //almost like SString::sprintf, but there is no common code to share because SString can use its directWrite to avoid double allocating/copying
     20#ifdef USE_VSCPRINTF
     21        size=_vscprintf(format, ap);
     22#endif
     23
     24        while(1)
     25                {
     26                buf=(char*)malloc(size);
     27                assert(buf!=NULL);
     28                int n=vsnprintf(buf,size,format,ap);
     29                if (n > -1 && n < size)
     30                        {
     31                        s=buf;
     32                        free(buf);
     33                        return s;
     34                        }
     35#ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE
     36                if (n > -1)    /* glibc 2.1 */
     37                        size = n+1; /* precisely what is needed */
     38                else           /* glibc 2.0 */
     39#endif
     40                        size *= 2;  /* twice the old size */
     41                free(buf);
     42                }
    2343}
    2444
Note: See TracChangeset for help on using the changeset viewer.