aboutsummaryrefslogtreecommitdiff
path: root/dep/src/g3dlite/format.cpp
blob: 13f01e26e6bd56d6645833eeb121f113365c15e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 @file format.cpp

 @author Morgan McGuire, graphics3d.com

 @created 2000-09-09
 @edited  2006-04-30
*/

#include "G3D/format.h"
#include "G3D/platform.h"
#include "G3D/System.h"

#ifdef G3D_WIN32
    #include <math.h>
    #define vsnprintf _vsnprintf
    #define NEWLINE "\r\n"
#else
    #include <stdarg.h>
    #define NEWLINE "\n"
#endif

#ifdef _MSC_VER
    // disable: "C++ exception handler used"
#   pragma warning (push)
#   pragma warning (disable : 4530)
#endif // _MSC_VER

// If your platform does not have vsnprintf, you can find a
// implementation at http://www.ijs.si/software/snprintf/

namespace G3D {

std::string format(const char* fmt,...) {
    va_list argList;
    va_start(argList,fmt);
    std::string result = vformat(fmt, argList);
    va_end(argList);

    return result;
}

#if defined(G3D_WIN32) && (_MSC_VER >= 1300)
// Both MSVC6 and 7 seem to use the non-standard vsnprintf
// so we are using vscprintf to determine buffer size, however
// only MSVC7 headers include vscprintf for some reason.
std::string vformat(const char *fmt, va_list argPtr) {
    // We draw the line at a 1MB string.
    const int maxSize = 1000000;

    // If the string is less than 161 characters,
    // allocate it on the stack because this saves
    // the malloc/free time.
    const int bufSize = 161;
    char stackBuffer[bufSize];

    int actualSize = _vscprintf(fmt, argPtr) + 1;

    if (actualSize > bufSize) {

        // Now use the heap.
        char* heapBuffer = NULL;

        if (actualSize < maxSize) {

            heapBuffer = (char*)System::malloc(maxSize + 1);
            vsnprintf(heapBuffer, maxSize, fmt, argPtr);
            heapBuffer[maxSize] = '\0';
        } else {
            heapBuffer = (char*)System::malloc(actualSize);
            vsprintf(heapBuffer, fmt, argPtr);
        }

        std::string formattedString(heapBuffer);
        System::free(heapBuffer);
        return formattedString;
    } else {

        vsprintf(stackBuffer, fmt, argPtr);
        return std::string(stackBuffer);
    }
}

#elif defined(G3D_WIN32) && (_MSC_VER < 1300)

std::string vformat(const char *fmt, va_list argPtr) {
    // We draw the line at a 1MB string.
    const int maxSize = 1000000;

    // If the string is less than 161 characters,
    // allocate it on the stack because this saves
    // the malloc/free time.
    const int bufSize = 161;
    char stackBuffer[bufSize];

    int actualWritten = vsnprintf(stackBuffer, bufSize, fmt, argPtr);

    // Not a big enough buffer, bufSize characters written
    if (actualWritten == -1) {

        int heapSize = 512;
        double powSize = 1.0;
        char* heapBuffer = (char*)System::malloc(heapSize);

        while ((vsnprintf(heapBuffer, heapSize, fmt, argPtr) == -1) &&
            (heapSize  < maxSize)) {

            heapSize = iCeil(heapSize * ::pow((double)2.0, powSize++));
            heapBuffer = (char*)System::realloc(heapBuffer, heapSize);
        }

        heapBuffer[heapSize-1] = '\0';

        std::string heapString(heapBuffer);
        System::free(heapBuffer);

        return heapString;
    } else {

        return std::string(stackBuffer);
    }
}

#else

// glibc 2.1 has been updated to the C99 standard
std::string vformat(const char* fmt, va_list argPtr) {
    // If the string is less than 161 characters,
    // allocate it on the stack because this saves
    // the malloc/free time.  The number 161 is chosen
    // to support two lines of text on an 80 character
    // console (plus the null terminator).
    const int bufSize = 161;
    char stackBuffer[bufSize];

    int numChars = vsnprintf(stackBuffer, bufSize, fmt, argPtr);

    if (numChars >= bufSize) {
      // We didn't allocate a big enough string.
      char* heapBuffer = (char*)System::malloc((numChars + 1) * sizeof(char));

      assert(heapBuffer);
      int numChars2 = vsnprintf(heapBuffer, numChars + 1, fmt, argPtr);
      assert(numChars2 == numChars);

      std::string result(heapBuffer);

      System::free(heapBuffer);

      return result;

    } else {

      return std::string(stackBuffer);

    }
}

#endif

} // namespace

#ifdef G3D_WIN32
#   undef vsnprintf
#endif

#ifdef _MSC_VER
#   pragma warning (pop)
#endif

#undef NEWLINE