~hikiko/nux/arb-srgba-shader

« back to all changes in this revision

Viewing changes to NuxCore/NTemplate.cpp

  • Committer: Neil Jagdish Patel
  • Date: 2010-09-01 19:25:37 UTC
  • Revision ID: neil.patel@canonical.com-20100901192537-mfz7rm6q262pewg6
Import and build NuxCore

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "NKernel.h"
 
2
 
 
3
NAMESPACE_BEGIN
 
4
// From: http://www.jb.man.ac.uk/~slowe/cpp/itoa.html
 
5
//    John Maloney has pointed out various problems with the previous implementation.
 
6
//    One of the major issues was the amount of heap allocation going on.
 
7
//    He suggested that a lot of this be removed to speed up the algorithm.
 
8
//    Below are two versions based on his excellent suggestions. The char* version 
 
9
//    is at least 10 times faster than the code above. The new std::string version
 
10
//    is 3 times faster than before. Although the char* version is faster, you should
 
11
//    check that you have allocated enough space to hold the output.
 
12
 
 
13
// C++ version std::string style "itoa":
 
14
tstring IntegerToChar(int value, int base)
 
15
{
 
16
    enum { kMaxDigits = 35 };
 
17
    tstring buf;
 
18
    buf.reserve( kMaxDigits ); // Pre-allocate enough space.
 
19
    // check that the base if valid
 
20
    if (base < 2 || base > 16)
 
21
        return buf;
 
22
    int quotient = value;
 
23
    // Translating number to string with base:
 
24
    do
 
25
    {
 
26
        buf += TEXT("0123456789abcdef")[ std::abs( quotient % base ) ];
 
27
        quotient /= base;
 
28
    } while ( quotient );
 
29
    // Append the negative sign for base 10
 
30
    if ( value < 0 && base == 10) buf += TEXT('-');
 
31
    std::reverse( buf.begin(), buf.end() );
 
32
    return buf;
 
33
}
 
34
 
 
35
// C++ version char* style "itoa":
 
36
TCHAR* IntegerToChar( int value, TCHAR* result, int base )
 
37
{
 
38
    // check that the base if valid
 
39
    if (base < 2 || base > 16)
 
40
    { 
 
41
        *result = 0; 
 
42
        return result;
 
43
    }
 
44
    TCHAR* out = result;
 
45
    int quotient = value;
 
46
    do
 
47
    {
 
48
        *out = TEXT("0123456789abcdef")[ std::abs( quotient % base ) ];
 
49
        ++out;
 
50
        quotient /= base;
 
51
    } while ( quotient );
 
52
    // Only apply negative sign for base 10
 
53
    if ( value < 0 && base == 10) *out++ = TEXT('-');
 
54
 
 
55
    std::reverse( result, out );
 
56
    *out = 0;
 
57
    return result;
 
58
}
 
59
 
 
60
// convert an hexadecimal string to t_u32
 
61
t_u32 HexCharToInteger(const TCHAR* s)
 
62
{
 
63
    int n = 0;         // position in string
 
64
    int m = 0;         // position in digit[] to shift
 
65
    int count;         // loop index
 
66
    unsigned long intValue = 0;  // integer value of hex string
 
67
    int digit[16];      // hold values to convert
 
68
 
 
69
    const TCHAR *hexStg = s;
 
70
    if((s[0] == TEXT('0')) && ((s[1] == TEXT('X')) || (s[1] == TEXT('x'))))
 
71
    {
 
72
        hexStg = s+2;
 
73
    }
 
74
 
 
75
    while (n < 16)
 
76
    {
 
77
        if (hexStg[n] == TEXT('\0'))
 
78
            break;
 
79
        if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
 
80
            digit[n] = hexStg[n] & 0x0f;            //convert to int
 
81
        else if (hexStg[n] >= TEXT('a') && hexStg[n] <= TEXT('f')) //if a to f
 
82
            digit[n] = (hexStg[n] & 0x0f) + 9;      //convert to int
 
83
        else if (hexStg[n] >= TEXT('A') && hexStg[n] <= TEXT('F')) //if A to F
 
84
            digit[n] = (hexStg[n] & 0x0f) + 9;      //convert to int
 
85
        else break;
 
86
        n++;
 
87
    }
 
88
    count = n;
 
89
    m = n - 1;
 
90
    n = 0;
 
91
    while(n < count)
 
92
    {
 
93
        // digit[n] is value of hex digit at position n
 
94
        // (m << 2) is the number of positions to shift
 
95
        // OR the bits into return value
 
96
        intValue = intValue | (digit[n] << (m << 2));
 
97
        m--;   // adjust the position to set
 
98
        n++;   // next digit to process
 
99
    }
 
100
    return (intValue);
 
101
}
 
102
 
 
103
int CharToInteger(const TCHAR* digit)
 
104
{
 
105
    int i;
 
106
    if(CharToInteger(digit, i))
 
107
    {
 
108
        return i;
 
109
    }
 
110
    nuxAssertNoEntry();
 
111
    return t_s32_max;
 
112
}
 
113
 
 
114
BOOL CharToInteger(const TCHAR* digit, INT& ret)
 
115
{
 
116
    INT result = 0;
 
117
    INT sign = +1;
 
118
 
 
119
    //--- Convert each digit char and add into result.
 
120
    t_u32 i = 0;
 
121
    if(digit[i] == TEXT('-'))
 
122
    {
 
123
        sign = -1;
 
124
        i++;
 
125
    }
 
126
    else if(digit[i] == TEXT('+'))
 
127
    {
 
128
        sign = +1;
 
129
        i++;
 
130
    }
 
131
 
 
132
    while(digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
 
133
    {
 
134
        result = (result * 10) + (digit[i] - TEXT('0'));
 
135
        i++;
 
136
    }
 
137
 
 
138
    if( digit[i] == TEXT('.'))           // test for '.' in case the characters represent a double or a float value
 
139
    {
 
140
        i++;
 
141
        while(digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
 
142
        {
 
143
            i++;
 
144
        }
 
145
    }
 
146
 
 
147
    const char* hello = "HelloWorld";
 
148
    bool null_char = (digit[i] == 0);
 
149
    bool space_char = (digit[i] == TEXT(' '));
 
150
    bool tab_char = (digit[i] == TEXT('\t'));
 
151
    bool win_end_of_line = (digit[i] == TEXT('\r')) && (digit[i+1] == TEXT('\n'));
 
152
    bool line_feed = (digit[i] == ('\n'));
 
153
    bool carriage_return = (digit[i] == ('\r'));
 
154
    //--- Check that there were no non-digits at end.
 
155
    if((!null_char) && (!tab_char) && (!space_char) && (!win_end_of_line) && (!line_feed) && (!carriage_return))
 
156
    {
 
157
        nuxDebugMsg(TEXT("[CharToInteger]: Unexpected character at the end of number %d in string: %s"), sign*result, digit);
 
158
    }
 
159
    ret = sign * result;
 
160
    return TRUE;
 
161
}
 
162
 
 
163
double CharToDouble(const TCHAR* digit)
 
164
{
 
165
    double d;
 
166
    if(CharToDouble(digit, d))
 
167
        return d;
 
168
    nuxAssertNoEntry();
 
169
    return t_f64_max;
 
170
}
 
171
 
 
172
BOOL CharToDouble(const TCHAR* digit, double& ret)
 
173
{
 
174
    int sign = 1;
 
175
 
 
176
    t_u32 i = 0;
 
177
    double result = 0;
 
178
    
 
179
    // check sign
 
180
    if (digit[i] == TEXT('-'))
 
181
    {
 
182
        sign = -1;
 
183
        i++;
 
184
    }
 
185
    else if (digit[i] == TEXT('+'))
 
186
    {
 
187
        sign = +1;
 
188
        i++;
 
189
    }
 
190
 
 
191
    //--- get integer portion
 
192
    while (digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
 
193
    {
 
194
        result = (result * 10) + digit[i] - TEXT('0');
 
195
        i++;
 
196
    }
 
197
 
 
198
    //--- get decimal point and fraction, if any.
 
199
    if (digit[i] == TEXT('.'))
 
200
    {
 
201
        digit++;
 
202
        double scale = 0.1;
 
203
        while (digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
 
204
        {
 
205
            result += (digit[i] - TEXT('0')) * scale;
 
206
            scale *= 0.1;
 
207
            i++;
 
208
        }
 
209
    }
 
210
 
 
211
    //--- error if we're not at the end of the number
 
212
    if((digit[i] != 0)
 
213
        && (digit[i] != TEXT(' '))
 
214
        && (digit[i] != TEXT('\t'))
 
215
#ifdef _WIN32
 
216
        // end of line on Windows
 
217
        && (digit[i] != TEXT('\r')) 
 
218
        && (digit[i+1] != TEXT('\n')) 
 
219
#else
 
220
        // end of line on Unix.
 
221
        && (digit[i] != TEXT('\n')) 
 
222
#endif
 
223
        )
 
224
    {
 
225
        // not NULL terminated or unexpected character;
 
226
        //nuxAssert(0);
 
227
        return FALSE;
 
228
    }
 
229
    ret = result * sign;
 
230
    return TRUE;
 
231
}
 
232
 
 
233
float CharToFloat(const TCHAR* digit)
 
234
{
 
235
    float f;
 
236
    if(CharToFloat(digit, f))
 
237
        return f;
 
238
    nuxAssertNoEntry();
 
239
    return t_f32_max;
 
240
}
 
241
 
 
242
BOOL CharToFloat(const TCHAR* digit, float& ret)
 
243
{
 
244
    int sign = 1;
 
245
 
 
246
    t_u32 i = 0;
 
247
    float result = 0.0f;
 
248
 
 
249
    // check sign
 
250
    if (digit[i] == TEXT('-'))
 
251
    {
 
252
        sign = -1;
 
253
        i++;
 
254
    }
 
255
    else if (digit[i] == TEXT('+'))
 
256
    {
 
257
        sign = +1;
 
258
        i++;
 
259
    }
 
260
 
 
261
    //--- get integer portion
 
262
    while (digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
 
263
    {
 
264
        result = (result * 10) + digit[i] - TEXT('0');
 
265
        i++;
 
266
    }
 
267
 
 
268
    //--- get decimal point and fraction, if any.
 
269
    if (digit[i] == TEXT('.'))
 
270
    {
 
271
        digit++;
 
272
        float scale = 0.1f;
 
273
        while (digit[i] >= TEXT('0') && digit[i] <= TEXT('9'))
 
274
        {
 
275
            result += (digit[i] - TEXT('0')) * scale;
 
276
            scale *= 0.1f;
 
277
            i++;
 
278
        }
 
279
    }
 
280
 
 
281
    //--- error if we're not at the end of the number
 
282
    if((digit[i] != 0)
 
283
        && (digit[i] != TEXT(' '))
 
284
        && (digit[i] != TEXT('\t'))
 
285
#ifdef _WIN32
 
286
        // end of line on Windows
 
287
        && (digit[i] != TEXT('\r')) 
 
288
        && (digit[i+1] != TEXT('\n')) 
 
289
#else
 
290
        // end of line on Unix.
 
291
        && (digit[i] != TEXT('\n')) 
 
292
#endif
 
293
        )
 
294
    {
 
295
        // not NULL terminated or unexpected character;
 
296
        //nuxAssert(0);
 
297
        return FALSE;
 
298
    }
 
299
    ret = result * sign;
 
300
    return TRUE;
 
301
}
 
302
 
 
303
NAMESPACE_END