Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Extras / glui / glui_string.cpp
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 library is free software; you can redistribute it and/or modify
14   it under the terms of the GNU Lesser General Public License as
15   published by the Free Software Foundation; either version 2.1 of the
16   License, or (at your option) any later version.
17
18   This library is distributed in the hope that it will be useful, but
19   WITHOUT ANY WARRANTY; without even the implied warranty of
20   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21   Lesser General Public License for more details.
22
23   You should have received a copy of the GNU Lesser General Public
24   License along with this library; if not, write to the Free Software
25   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26   USA
27
28   This program is -not- in the public domain.
29
30 *****************************************************************************/
31
32 #include "GL/glui.h"
33 #include <stdarg.h>
34
35 #if defined  (_MSC_VER) && (_MSC_VER < 1500)
36 #define vsnprintf _vsnprintf
37 #endif
38
39 GLUI_String& glui_format_str(GLUI_String& str, const char* fmt, ...)
40 {
41   const size_t ISIZE = 128;
42   char stackbuf[ISIZE];
43   size_t bufsz = ISIZE;
44   char *buf = stackbuf;
45   str = "";
46   va_list arg;
47   while (1) {
48     va_start(arg, fmt);
49     int ret = vsnprintf(buf,299,fmt,arg);
50     va_end(arg);
51     if (ret>=0) {
52       break;
53     }
54     // else make a bigger buf, try again
55     bufsz <<= 1;
56     if (buf==stackbuf) buf = (char*)malloc(sizeof(char)*bufsz);
57     else buf = (char*)realloc(buf, sizeof(char)*bufsz);
58   }
59   if (buf!=stackbuf) free(buf);
60   str=buf;
61   return str;
62 }