comparison src/utils.cc @ 10257:cd550069240e

assume vsnprintf from gnulib; use sstream instead of snprintf
author John W. Eaton <jwe@octave.org>
date Wed, 03 Feb 2010 06:34:29 -0500
parents 2d47356a7a1a
children 57a59eae83cc
comparison
equal deleted inserted replaced
10256:c84186ad78f3 10257:cd550069240e
1223 1223
1224 static size_t size = initial_size; 1224 static size_t size = initial_size;
1225 1225
1226 static char *buf = 0; 1226 static char *buf = 0;
1227 1227
1228 #if defined (HAVE_C99_VSNPRINTF)
1229 size_t nchars = 0;
1230 #else
1231 int nchars = 0; 1228 int nchars = 0;
1232 #endif
1233 1229
1234 if (! buf) 1230 if (! buf)
1235 buf = new char [size]; 1231 buf = new char [size];
1236 1232
1237 if (! buf) 1233 if (! buf)
1238 return 0; 1234 return 0;
1239 1235
1240 #if defined (HAVE_C99_VSNPRINTF)
1241
1242 // Note that the caller is responsible for calling va_end on args.
1243 // We will do it for saved_args.
1244
1245 va_list saved_args;
1246
1247 SAVE_ARGS (saved_args, args);
1248
1249 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF;
1250
1251 nchars = octave_raw_vsnprintf (buf, size, fmt, args);
1252
1253 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
1254
1255 if (nchars >= size)
1256 {
1257 size = nchars + 1;
1258
1259 delete [] buf;
1260
1261 buf = new char [size];
1262
1263 if (buf)
1264 {
1265 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_FOR_VSNPRINTF;
1266
1267 octave_raw_vsnprintf (buf, size, fmt, saved_args);
1268
1269 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
1270 }
1271 }
1272
1273 va_end (saved_args);
1274
1275 #else
1276
1277 while (1) 1236 while (1)
1278 { 1237 {
1279 va_list saved_args; 1238 va_list saved_args;
1280 1239
1281 SAVE_ARGS (saved_args, args); 1240 SAVE_ARGS (saved_args, args);
1286 1245
1287 va_end (saved_args); 1246 va_end (saved_args);
1288 1247
1289 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; 1248 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
1290 1249
1291 if (nchars > -1 && nchars < size-1) 1250 if (nchars > -1 && nchars < size)
1292 return buf; 1251 break;
1293 else 1252 else
1294 { 1253 {
1295 delete [] buf; 1254 delete [] buf;
1296 1255
1297 size *= 2; 1256 size = nchars + 1;;
1298 1257
1299 buf = new char [size]; 1258 buf = new char [size];
1300 1259
1301 if (! buf) 1260 if (! buf)
1302 return 0; 1261 return 0;
1303 } 1262 }
1304 } 1263 }
1305
1306 #endif
1307 1264
1308 return buf; 1265 return buf;
1309 } 1266 }
1310 1267
1311 char * 1268 char *