~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to libaqsistypes/sstring.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.com
 
5
//
 
6
// This library is free software; you can redistribute it and/or
 
7
// modify it under the terms of the GNU General Public
 
8
// License as published by the Free Software Foundation; either
 
9
// version 2 of the License, or (at your option) any later version.
 
10
//
 
11
// This library is distributed in the hope that it will be useful,
 
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
// General Public License for more details.
 
15
//
 
16
// You should have received a copy of the GNU General Public
 
17
// License along with this library; if not, write to the Free Software
 
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
 
 
21
/** \file
 
22
                \brief Implements an extended string class, CqString, derived from std::string
 
23
                \author Paul C. Gregory (pgregory@aqsis.com)
 
24
*/
 
25
 
 
26
#include        <stdio.h>
 
27
 
 
28
#include        <stdarg.h>
 
29
 
 
30
#include        "aqsis.h"
 
31
#include        "sstring.h"
 
32
 
 
33
START_NAMESPACE( Aqsis )
 
34
 
 
35
//---------------------------------------------------------------------
 
36
/**     Expands characters which require escape sequences.
 
37
 * \return A string in which escape characters are expanded.
 
38
 */
 
39
 
 
40
CqString CqString::ExpandEscapes() const
 
41
{
 
42
    CqString    strResult;
 
43
    const TqChar*       str = c_str();
 
44
 
 
45
    if ( str != NULL )
 
46
    {
 
47
        TqChar c = *str++;
 
48
 
 
49
        while ( c != 0 )
 
50
        {
 
51
            if ( c >= ' ' )
 
52
            {
 
53
                switch ( c )
 
54
                {
 
55
                case '\\': strResult += '\\' + '\\';    break;
 
56
                case '\'': strResult += '\\' + '\'';    break;
 
57
                case '\"': strResult += '\\' + '\"';    break;
 
58
                default: strResult += c;        break;
 
59
                }
 
60
            }
 
61
            else
 
62
            {
 
63
                strResult += '\\';
 
64
 
 
65
                switch ( c )
 
66
                {
 
67
                case '\a': strResult += 'a';    break;
 
68
                case '\b': strResult += 'b';    break;
 
69
                case '\n': strResult += 'n';    break;
 
70
                case '\r': strResult += 'r';    break;
 
71
                case '\t': strResult += 't';    break;
 
72
                case '\0': strResult += '0';    break;
 
73
 
 
74
                default:
 
75
                    {
 
76
                        strResult += 'x';
 
77
                        TqInt i;
 
78
                        for ( i = 0;i < 4;i++ )
 
79
                        {
 
80
                            TqInt Value = ( ( c >> 12 ) & 0x0F );       // Get high nibble;
 
81
                            c <<= 4;
 
82
 
 
83
                            if ( Value <= 9 )
 
84
                                strResult += '0' + Value;
 
85
                            else
 
86
                                strResult += 'a' + Value;
 
87
                        }
 
88
                    }
 
89
                    break;
 
90
                }
 
91
            }
 
92
            c = *str++;
 
93
        }
 
94
    }
 
95
    return ( strResult );
 
96
}
 
97
 
 
98
 
 
99
//---------------------------------------------------------------------
 
100
/**     Translates escape sequences into their actual characters.
 
101
 * \return A string in which escape characters are translated.
 
102
 */
 
103
 
 
104
CqString CqString::TranslateEscapes() const
 
105
{
 
106
    CqString    strResult;
 
107
    const TqChar*       str = c_str();
 
108
 
 
109
    if ( str != NULL )
 
110
    {
 
111
        TqChar c = *str++;
 
112
 
 
113
        while ( c != 0 )
 
114
        {
 
115
            if ( c == '\\' )
 
116
            {
 
117
                c = *str++;
 
118
 
 
119
                switch ( c )
 
120
                {
 
121
                case 'a': strResult += '\a';    break;
 
122
                case 'b': strResult += '\b';    break;
 
123
                case 'n': strResult += '\n';    break;
 
124
                case 'r': strResult += '\r';    break;
 
125
                case 't': strResult += '\t';    break;
 
126
 
 
127
                case '0':
 
128
                    {
 
129
                        TqChar Value = 0;
 
130
 
 
131
                        while ( ( c >= '0' && c <= '7' ) )
 
132
                        {
 
133
                            Value = Value * 8 + c - '0';
 
134
                            c = *str++;
 
135
                        }
 
136
                        strResult += Value;
 
137
                        str--;
 
138
                    }
 
139
                    break;
 
140
 
 
141
                case 'x':
 
142
                    {
 
143
                        TqChar Value = 0;
 
144
 
 
145
                        c = *str++;
 
146
 
 
147
                        while ( ( c >= '0' && c <= '9' ) || ( c >= 'A' && c <= 'F' ) || ( c >= 'a' && c <= 'f' ) )
 
148
                        {
 
149
                            Value *= 16;
 
150
                            if ( c >= '0' && c <= '9' )
 
151
                                Value += c - '0';
 
152
                            else
 
153
                                if ( c >= 'A' && c <= 'F' )
 
154
                                    Value += c - 'A' + 10;
 
155
                                else
 
156
                                    if ( c >= 'a' && c <= 'f' )
 
157
                                        Value += c - 'a' + 10;
 
158
 
 
159
                            c = *str++;
 
160
                        }
 
161
                        strResult += Value;
 
162
                        str--;
 
163
                    }
 
164
                    break;
 
165
 
 
166
                default:
 
167
                    strResult += c;
 
168
                    break;
 
169
                }
 
170
            }
 
171
            else
 
172
                strResult += c;
 
173
 
 
174
            c = *str++;
 
175
        }
 
176
    }
 
177
 
 
178
    return ( strResult );
 
179
}
 
180
 
 
181
 
 
182
//---------------------------------------------------------------------
 
183
/** Format a string printf style
 
184
 */
 
185
 
 
186
CqString& CqString::Format( const TqChar* strFmt, ... )
 
187
{
 
188
    va_list marker;
 
189
    va_start( marker, strFmt );
 
190
 
 
191
    *this = "";
 
192
 
 
193
    TqInt i = 0;
 
194
    while ( strFmt[ i ] != '\0' )
 
195
    {
 
196
        switch ( strFmt[ i ] )
 
197
        {
 
198
        case '%':
 
199
            {
 
200
                i++;
 
201
                switch ( strFmt[ i ] )
 
202
                {
 
203
                case 'f':
 
204
                    {
 
205
                        TqFloat val = static_cast<TqFloat>( va_arg( marker, double ) );
 
206
                        *this += ToString(val);
 
207
                    }
 
208
                    break;
 
209
 
 
210
                case 'd':
 
211
                case 'i':
 
212
                    {
 
213
                        TqInt val = va_arg( marker, TqInt );
 
214
                        *this += ToString(val);
 
215
                    }
 
216
                    break;
 
217
 
 
218
                case 'x':
 
219
                    {
 
220
                        TqInt val = va_arg( marker, TqInt );
 
221
                        *this += ToString(val);
 
222
                    }
 
223
                    break;
 
224
 
 
225
                case 's':
 
226
                    *this += va_arg( marker, TqChar* );
 
227
                    break;
 
228
                }
 
229
            }
 
230
            break;
 
231
 
 
232
        default:
 
233
            *this += strFmt[ i ];
 
234
            break;
 
235
        }
 
236
        i++;
 
237
    }
 
238
 
 
239
    va_end( marker );
 
240
    return ( *this );
 
241
}
 
242
 
 
243
 
 
244
CqString& CqString::operator+=( const CqString& str )
 
245
{
 
246
    CqStringBase::operator+=( str );
 
247
    return ( *this );
 
248
}
 
249
 
 
250
CqString& CqString::operator+=( const TqChar* str )
 
251
{
 
252
    CqStringBase::operator+=( str );
 
253
    return ( *this );
 
254
}
 
255
 
 
256
CqString& CqString::operator+=( TqChar c )
 
257
{
 
258
    CqStringBase::operator+=( c );
 
259
    return ( *this );
 
260
}
 
261
 
 
262
CqString& CqString::operator+=( TqInt i )
 
263
{
 
264
    *this += ToString(i);
 
265
    return *this;
 
266
}
 
267
 
 
268
CqString& CqString::operator+=( TqFloat f )
 
269
{
 
270
    *this += ToString(f);
 
271
    return *this;
 
272
}
 
273
 
 
274
 
 
275
//---------------------------------------------------------------------
 
276
/** Concatenate two strings
 
277
 */
 
278
 
 
279
CqString operator+( const CqString& strAdd1, const CqString& strAdd2 )
 
280
{
 
281
    CqString strRes( strAdd1 );
 
282
    strRes += strAdd2;
 
283
    return ( strRes );
 
284
}
 
285
 
 
286
 
 
287
//---------------------------------------------------------------------
 
288
/** Concatenate two strings
 
289
 */
 
290
 
 
291
CqString operator+( const TqChar* strAdd1, const CqString& strAdd2 )
 
292
{
 
293
    CqString strRes( strAdd1 );
 
294
    strRes += strAdd2;
 
295
    return ( strRes );
 
296
}
 
297
 
 
298
 
 
299
//---------------------------------------------------------------------
 
300
/** Concatenate two strings
 
301
 */
 
302
 
 
303
CqString operator+( const CqString& strAdd1, const TqChar* strAdd2 )
 
304
{
 
305
    CqString strRes( strAdd1 );
 
306
    strRes += strAdd2;
 
307
    return ( strRes );
 
308
}
 
309
 
 
310
 
 
311
//---------------------------------------------------------------------
 
312
/** Append charater to end of string
 
313
 */
 
314
 
 
315
CqString operator+( const CqString& strAdd1, TqChar ch )
 
316
{
 
317
    CqString strRes( strAdd1 );
 
318
    strRes += ch;
 
319
    return ( strRes );
 
320
}
 
321
 
 
322
 
 
323
//---------------------------------------------------------------------
 
324
/** Prepend character to string
 
325
 */
 
326
 
 
327
CqString operator+( TqChar ch, const CqString& strAdd2 )
 
328
{
 
329
    CqString strRes( ch );
 
330
    strRes += strAdd2;
 
331
    return ( strRes );
 
332
}
 
333
 
 
334
 
 
335
//---------------------------------------------------------------------
 
336
/** Not really useful but needed for the templatised operation of the render engine.
 
337
 */
 
338
 
 
339
CqString operator-( const CqString& strAdd1, const CqString& strAdd2 )
 
340
{
 
341
    return ( strAdd1 );
 
342
}
 
343
 
 
344
 
 
345
//---------------------------------------------------------------------
 
346
/** Not really useful but needed for the templatised operation of the render engine.
 
347
 */
 
348
 
 
349
CqString operator/( const CqString&strAdd1, const CqString&strAdd2 )
 
350
{
 
351
    return ( strAdd1 );
 
352
}
 
353
 
 
354
 
 
355
//---------------------------------------------------------------------
 
356
/** Not really useful but needed for the templatised operation of the render engine.
 
357
 */
 
358
 
 
359
CqString operator*( const CqString& strAdd1, const CqString& strAdd2 )
 
360
{
 
361
    return ( strAdd1 );
 
362
}
 
363
 
 
364
 
 
365
//---------------------------------------------------------------------
 
366
/** Not really useful but needed for the templatised operation of the render engine.
 
367
 */
 
368
 
 
369
CqString operator*( const CqString& strAdd1, TqFloat f )
 
370
{
 
371
    return ( strAdd1 );
 
372
}
 
373
 
 
374
 
 
375
std::ostream& operator<<( std::ostream & stmOutput, const CqString& strString )
 
376
{
 
377
    stmOutput << strString.c_str();
 
378
    return ( stmOutput );
 
379
}
 
380
 
 
381
 
 
382
 
 
383
//---------------------------------------------------------------------
 
384
 
 
385
END_NAMESPACE( Aqsis )