source: NonGTP/FCollada/FUtils/FUStringBuilder.h @ 964

Revision 964, 11.5 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*
2        Copyright (C) 2005-2006 Feeling Software Inc.
3        MIT License: http://www.opensource.org/licenses/mit-license.php
4*/
5/*
6        Based on the FS Import classes:
7        Copyright (C) 2005-2006 Feeling Software Inc
8        Copyright (C) 2005-2006 Autodesk Media Entertainment
9        MIT License: http://www.opensource.org/licenses/mit-license.php
10*/
11
12/**
13        @file FUStringBuilder.h
14        This file contains the FUStringBuilderT template class,
15        its defined template classes and its helper classes.
16*/
17
18#ifndef _FCU_STRING_BUILDER_
19#define _FCU_STRING_BUILDER_
20
21/**
22        A dynamically-sized string object.
23        The template has two arguments: the character definition and
24        the sprintf() functor class for float to string conversions.
25
26        This class should be used for all the string operations, as it contains a
27        dynamically-resized buffer that is not directly tied to its content's length.
28
29        @ingroup FUtils
30*/
31template <class Char, class SPrintF>
32class FUStringBuilderT
33{
34private:
35        Char* buffer;
36        size_t reserved;
37        size_t size;
38
39public:
40        /** The standard string object which correspond to the builder. */
41        typedef std::basic_string<Char> String;
42
43        /** Creates a new builder with the content of the given string.
44                @param sz A string. Its content will be copied within the builder. */
45        FUStringBuilderT(const String& sz);
46
47        /** Creates a new builder with the content of the given character array.
48                @param sz A character array. Its content will be copied within the builder.
49                        It must terminate with an element containing the 'zero' value. */
50        FUStringBuilderT(const Char* sz);
51
52        /** Creates a new builder with the given character repeated multiple times over the array.
53                @param ch A character to repeat.
54                @param count The number of times to repeat the given character. */
55        FUStringBuilderT(Char ch, size_t count);
56
57        /** Creates a new builder with an empty buffer.
58                @see reserve
59                @param reserved The number of character slots to reserve within the empty buffer. */
60        FUStringBuilderT(size_t reserved);
61
62        /** Creates a new builder with an empty buffer. */
63        FUStringBuilderT();
64
65        /** Deletes the builder. Its buffer will be cleared.
66                Any pointers to its data will be dangling. */
67        ~FUStringBuilderT();
68
69        /** Reserves a given number of character slots.
70                If the builder has a buffer with a different number of character slots, a new
71                buffer will be allocated. If the builder has contents, it will be copied within
72                the new buffer. If there is more content than the new buffer can handle, it will
73                be discarded.
74                @param length The number of character slots to reserve. */
75        void reserve(size_t length);
76
77        /** Retrieves the length of the content within the builder.
78                @return The length of the string. */
79        inline size_t length() { return size; }
80
81        /** Clears the content of the builder.
82                This does not re-allocate a new buffer. */
83        void clear();
84
85        /** Retrieves whether the builder is empty.
86                A builder is considered empty when it has no content, regardless of
87                the size or allocation status of its buffer.
88                @return Whether the builder is empty. */
89        inline bool empty() { return size == 0; }
90
91        /** Appends a character to the content of the builder.
92                @param c A character. May not be the 'zero' value. */
93        void append(Char c);
94
95        /** Appends a string to the content of the builder.
96                @param sz A string. */
97        void append(const String& sz);
98
99        /** Appends a character array to the content of the builder.
100                @param sz A character array. It must terminate with an
101                        element containing the 'zero' value. */
102        void append(const Char* sz);
103
104        /** Appends the content of a builder to the content of this builder.
105                @param b A string builder. */
106        void append(const FUStringBuilderT& b);
107
108        /** Appends the integer value, after converting it to a string,
109                to the content of the builder.
110                @param i An integer value. */
111        void append(int32 i);
112        void append(uint32 i); /**< See above. */
113        void append(uint64 i); /**< See above. */
114
115        inline void append(int i) { append((int32) i); } /**< See above. */
116#ifdef _W64
117        inline void append(_W64 unsigned int i) { append((uint32) i); } /**< See above. */
118#else
119        inline void append(unsigned int i) { append((uint32) i); } /**< See above. */
120#endif
121
122        /** Appends the floating-point value, after converting it to a string,
123                to the content of the builder. If the floating-point value is the special token
124                that represents infinity, the string "INF" is appended. If it represents
125                the negative infinity, the string "-INF" is appended. If it represents the
126                impossibility, the string "NaN" is appended.
127                @param f A floating-point value. */
128        void append(float f);
129        void append(double f); /**< See above. */
130
131        /** Appends a value to the content of the builder.
132                This is a shortcut for the append function.
133                @see append
134                @param val A value. This may be numerical, a character, a character array or a string. */
135        template<typename TYPE> inline FUStringBuilderT& operator+=(const TYPE& val) { append(val); return *this; }
136
137        /** Appends a character array to the content of the builder.
138                A newline character will be appended after the character array.
139                @param sz A character array. It must terminate with an
140                        element containing the 'zero' value. */
141        void appendLine(const Char* sz);
142
143        /** Removes a section of the content of the builder.
144                Every character that occurs after the given index will be removed,
145                resulting in a shrunk string.
146                @param start An index within the content of the builder. */
147        void remove(int32 start);
148
149        /** Removes a section of the content of the builder.
150                The substring defined by the 'start' and 'end' indices will be
151                removed. The 'start' character is removed and is replaced by
152                the 'end' character.
153                @param start The index of the first character of the substring to remove.
154                @param end The index of the first character after the removed substring. */
155        void remove(int32 start, int32 end);
156
157        /** Removes the last character of the content of the builder. */
158        inline void pop_back() { if (size > 0) --size; }
159
160        /** Sets the content of the builder to a given value.
161                This clears the builder of all its content and appends the given value.
162                @param val A value. This may be numerical, a character, a character array or a string. */
163        template<typename TYPE> inline void set(const TYPE& val) { clear(); append(val); }
164        template<typename TYPE> inline FUStringBuilderT& operator=(const TYPE& val) { clear(); append(val); return *this; } /**< See above. */
165
166        /** Converts the content of the builder to a standard string.
167                @return A string with the content of the builder. */
168        String ToString();
169        operator String() { return ToString(); } /**< See above. */
170
171        /** Converts the content of the builder to a character array.
172                @return A character array with the content of the builder.
173                        This pointer is valid for the lifetime of the buffer of the builder, so
174                        do not keep it around. This character array should not be modified. */
175        const Char* ToCharPtr();
176        operator const Char*() { return ToCharPtr(); } /**< See above. */
177
178        /** Retrieves the index of the first character within the content of the builder
179                that is equivalent to the given character.
180                @param c The character to match.
181                @return The index of the first equivalent character. -1 is returned if no
182                        character matches the given character. */
183        int32 index(Char c);
184
185        /** Retrieves the index of the last character within the content of the builder
186                that is equivalent to the given character.
187                @param c The character to match.
188                @return The index of the last equivalent character. -1 is returned if no
189                        character matches the given character. */
190        int32 rindex(Char c);
191
192private:
193        void enlarge(size_t minimum);
194};
195
196/**
197        Encapsulates the 8-bit string numerical conversion functions.
198        The 'snprintf' function is used so no locale information is handled.
199*/
200class SprintF
201{
202public:
203        /** Converts a signed integer into the given constant-sized string.
204                @param output A constant-sized string.
205                @param length The size of the constant-sized string.
206                @param i A signed integer. */
207        void PrintInt32(char* output, uint32 length, int32 i) { snprintf(output, length, "%i", i); }
208
209        /** Converts an unsigned integer into the given constant-sized string.
210                @param output A constant-sized string.
211                @param length The size of the constant-sized string.
212                @param i An unsigned integer. */
213        void PrintUInt32(char* output, uint32 length, uint32 i) { snprintf(output, length, "%u", i); }
214        void PrintUInt64(char* output, uint32 length, uint64 i) { snprintf(output, length, "%u", i); } /**< See above. */
215
216        /** Converts a floating-point value into the given constant-sized string.
217                @param output A constant-sized string.
218                @param length The size of the constant-sized string.
219                @param f A floating-point value. */
220        void PrintFloat(char* output, uint32 length, double f) { snprintf(output, length, "%f", f); }
221
222        /** Retrieves the length of a constant-sized string.
223                @param in A character array which is terminated with a 'zero' element.
224                @return The number of element preceeding the 'zero' element. */
225        size_t StrLen(const char* in) { return strlen(in); }
226};
227
228/**
229        Encapsulates the Unicode string numerical conversion functions.
230        The 'fsnprintf' function is used so no locale information is handled.
231*/
232class SFprintF
233{
234public:
235        /** Converts a signed integer into the given constant-sized string.
236                @param output A constant-sized string.
237                @param length The size of the constant-sized string.
238                @param i A signed integer. */
239        void PrintInt32(fchar* output, uint32 length, int32 i) { fsnprintf(output, length, FC("%i"), i); }
240
241        /** Converts an unsigned integer into the given constant-sized string.
242                @param output A constant-sized string.
243                @param length The size of the constant-sized string.
244                @param i An unsigned integer. */
245        void PrintUInt32(fchar* output, uint32 length, uint32 i) { fsnprintf(output, length, FC("%u"), i); }
246
247        /** Converts an unsigned integer into the given constant-sized string.
248                @param output A constant-sized string.
249                @param length The size of the constant-sized string.
250                @param i An unsigned integer. */
251        void PrintUInt64(fchar* output, uint32 length, uint64 i) { fsnprintf(output, length, FC("%u"), i); }
252
253        /** Converts a floating-point value into the given constant-sized string.
254                @param output A constant-sized string.
255                @param length The size of the constant-sized string.
256                @param f A floating-point value. */
257        void PrintFloat(fchar* output, uint32 length, double f) { fsnprintf(output, length, FC("%f"), f); }
258
259        /** Retrieves the length of a constant-sized string.
260                @param in A character array which is terminated with a 'zero' element.
261                @return The number of element preceeding the 'zero' element. */
262        size_t StrLen(const fchar* in) { return fstrlen(in); }
263};
264
265typedef FUStringBuilderT<fchar, SFprintF> FUStringBuilder; /**< A Unicode string builder. */
266typedef FUStringBuilderT<char, SprintF> FUSStringBuilder;  /**< A 8-bit string builder. */
267
268/** Declares a global Unicode string builder.
269        As many functions within FCollada use the global string builders, their content is often overwritten.
270        Use this builder only for quick conversion or character accumulation. */
271FCOLLADA_EXPORT extern FUStringBuilder globalBuilder;
272
273/** Declares a global 8-bit string builder.
274        As many functions within FCollada use the global string builders, their content is often overwritten.
275        Use this builder only for quick conversion or character accumulation. */
276FCOLLADA_EXPORT extern FUSStringBuilder globalSBuilder;
277
278#include "FUtils/FUStringBuilder.hpp"
279
280#endif // _FCU_STRING_BUILDER_
281
Note: See TracBrowser for help on using the repository browser.