1 /****************************************************************************
2 
3   GLUI User Interface Toolkit
4   ---------------------------
5 
6      glui.cpp
7 
8 
9           --------------------------------------------------
10 
11   Copyright (c) 1998 Paul Rademacher (this file, Bill Baxter 2005)
12 
13   This software is provided 'as-is', without any express or implied
14   warranty. In no event will the authors be held liable for any damages
15   arising from the use of this software.
16 
17   Permission is granted to anyone to use this software for any purpose,
18   including commercial applications, and to alter it and redistribute it
19   freely, subject to the following restrictions:
20 
21   1. The origin of this software must not be misrepresented; you must not
22   claim that you wrote the original software. If you use this software
23   in a product, an acknowledgment in the product documentation would be
24   appreciated but is not required.
25   2. Altered source versions must be plainly marked as such, and must not be
26   misrepresented as being the original software.
27   3. This notice may not be removed or altered from any source distribution.
28 
29 *****************************************************************************/
30 
31 #include "GL/glui.h"
32 #include <stdarg.h>
33 
34 #ifdef _MSC_VER
35 #define vsnprintf _vsnprintf
36 #endif
37 
glui_format_str(GLUI_String & str,const char * fmt,...)38 GLUI_String& glui_format_str(GLUI_String& str, const char* fmt, ...)
39 {
40   const size_t ISIZE = 128;
41   char stackbuf[ISIZE];
42   size_t bufsz = ISIZE;
43   char *buf = stackbuf;
44   str = "";
45   va_list arg;
46   while (1) {
47     va_start(arg, fmt);
48     int ret = vsnprintf(buf,bufsz-1,fmt,arg);
49     va_end(arg);
50     if (ret>=0) {
51       break;
52     }
53     // else make a bigger buf, try again
54     bufsz <<= 1;
55     if (buf==stackbuf) buf = (char*)malloc(sizeof(char)*bufsz);
56     else buf = (char*)realloc(buf, sizeof(char)*bufsz);
57   }
58   if (buf!=stackbuf) free(buf);
59   str=buf;
60   return str;
61 }
62