source: issm/trunk-jpl/src/c/shared/Exceptions/exprintf.cpp@ 13622

Last change on this file since 13622 was 13622, checked in by Mathieu Morlighem, 12 years ago

CHG: cosmetics, removing all deboule blank lines and indent single white lines correctly

File size: 1.1 KB
Line 
1/*!\file: exprintf
2 * \brief this is a modification of the sprintf function.
3 * Instead of returning an int, it will return the char* itself.
4 * The advantage is to be able to do things like:
5 * ErrorException(exprintf("%s%i\n","test failed for id:",id));
6 */
7
8#include <stdarg.h>
9#include <stdio.h>
10#include "../Alloc/xNewDelete.h"
11#include "../Alloc/alloc.h"
12
13char* exprintf(const char* format,...){
14
15 /*returned string: */
16 char *buffer = NULL;
17 int n,size = 100;
18 int string_size;
19
20 //variable list of arguments
21 va_list args;
22
23 while(true){
24
25 /*allocate buffer for given string size*/
26 buffer=xNew<char>(size);
27
28 /* Try to print in the allocated space. */
29 va_start(args, format);
30#ifndef WIN32
31 n=vsnprintf(buffer,size,format,args);
32#else
33 n=vsnprintf(buffer,size,format,args);
34#endif
35 va_end(args);
36
37 /* If that worked, return the string. */
38 if(n>-1 && n<size) break;
39
40 /* Else try again with more space. */
41 if(n>-1) /* glibc 2.1 */
42 size=n+1; /* precisely what is needed */
43 else /* glibc 2.0 */
44 size*=2; /* twice the old size */
45
46 xDelete<char>(buffer);
47 }
48
49 return buffer;
50}
Note: See TracBrowser for help on using the repository browser.