Changeset 220 for cpp/common/stl-util.cpp
- Timestamp:
- 04/07/14 02:58:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/stl-util.cpp
r197 r220 9 9 #include "nonstd.h" 10 10 #include "framsg.h" 11 #include <assert.h> 11 12 12 13 string ssprintf_va(const char* format, va_list ap) 13 14 { 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 case15 long size= 1000;15 string s; //clang crashed when this declaration was in s=buf 16 long size=256; 16 17 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 } 23 43 } 24 44
Note: See TracChangeset
for help on using the changeset viewer.