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

Revision 964, 17.8 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[964]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 FUStringConversion.h
14        This file contains the FUStringConversion class.
15*/
16
17#ifndef _FCU_STRING_CONVERSION_
18#define _FCU_STRING_CONVERSION_
19
20class FUDateTime;
21
22/**
23        Common string conversion.
24
25        This static class contains the parsing function for Unicode and 8-bit/UTF-8
26        strings into common data types: integers, booleans, floating-point values,
27        vectors, matrices, date-time, etc. and dynamically-sized array of these types.
28
29        This class can also convert common data types into an 8-bit or a Unicode string and
30        it contains conversion functions to convert string between 8-bit and Unicode.
31
32        @ingroup FUtils
33*/
34class FCOLLADA_EXPORT FUStringConversion
35{
36private: FUStringConversion() {}
37public:
38        /** Converts a 8-bit string to a Unicode string.
39                @param value The 8-bit string.
40                @return The converted Unicode string. */
41        static fstring ToFString(const char* value);
42        inline static fstring ToFString(const string& value) { return ToFString(value.c_str()); } /**< See above. */
43
44        /** Converts an Unicode string to a 8-bit string.
45                @param value The Unicode string.
46                @return The converted 8-bit string. */
47        static string ToString(const fchar* value);
48        inline static string ToString(const fstring& value) { return ToString(value.c_str()); } /**< See above. */
49
50        /** Parses a string into a boolean value.
51                @param value The string.
52                @return The parsed boolean value. */
53        static bool ToBoolean(const char* value);
54        inline static bool ToBoolean(const string& value) { return ToBoolean(value.c_str()); } /**< See above. */
55#ifdef UNICODE
56        static bool ToBoolean(const fchar* value); /**< See above. */
57        inline static bool ToBoolean(const fstring& value) { return ToBoolean(value.c_str()); } /**< See above. */
58#endif
59
60        /** Parses a string into a floating-point value.
61                @param value The string. For the string pointer versions of this function,
62                        the pointer will point to the last processed characters after the parsing.
63                @return The parsed floating-point value. */
64        static float ToFloat(const char** value);
65        inline static float ToFloat(const char* value) { return ToFloat(&value); } /**< See above. */
66        inline static float ToFloat(const string& value) { return ToFloat(value.c_str()); } /**< See above. */
67#ifdef UNICODE
68        static float ToFloat(const fchar** value); /**< See above. */
69        inline static float ToFloat(const fstring& value) { return ToFloat(value.c_str()); } /**< See above. */
70        inline static float ToFloat(const fchar* value) { return ToFloat(&value); } /**< See above. */
71#endif
72
73        /** Parses a string into a signed integer.
74                @param value The string. For the string pointer versions of this function,
75                        the pointer will point to the last processed characters after the parsing.
76                @return The parsed signed integer. */
77        static int32 ToInt32(const char** value);
78        inline static int32 ToInt32(const char* value) { return ToInt32(&value); } /**< See above. */
79        inline static int32 ToInt32(const string& value) { return ToInt32(value.c_str()); } /**< See above. */
80#ifdef UNICODE
81        static int32 ToInt32(const fchar** value); /**< See above. */
82        inline static int32 ToInt32(const fchar* value) { return ToInt32(&value); } /**< See above. */
83        inline static int32 ToInt32(const fstring& value) { return ToInt32(value.c_str()); } /**< See above. */
84#endif
85
86        /** Parses a string into an unsigned integer.
87                @param value The string. For the string pointer versions of this function,
88                        the pointer will point to the last processed characters after the parsing.
89                @return The parsed unsigned integer. */
90        static uint32 ToUInt32(const char** value);
91        inline static uint32 ToUInt32(const char* value) { return ToUInt32(&value); } /**< See above. */
92        inline static uint32 ToUInt32(const string& value) { return ToUInt32(value.c_str()); } /**< See above. */
93#ifdef UNICODE
94        static uint32 ToUInt32(const fchar** value); /**< See above. */
95        inline static uint32 ToUInt32(const fchar* value) { return ToUInt32(&value); } /**< See above. */
96        inline static uint32 ToUInt32(const fstring& value) { return ToUInt32(value.c_str()); } /**< See above. */
97#endif
98
99        /** Parses a string into an unsigned integer. The string is assumed to have
100                an unsigned integer in hexadecimal format.
101                @param value The string. For the string pointer versions of this function,
102                        the pointer will point to the last processed characters after the parsing.
103                @param count The maxmimum number of characters to parse.
104                        For example, a count of 2 will read in an 8-bit character.
105                @return The parsed unsigned integer. */
106        static uint32 HexToUInt32(const char** value, uint32 count=UINT_MAX);
107        inline static uint32 HexToUInt32(const char* value, uint32 count=UINT_MAX) { return HexToUInt32(&value, count); } /**< See above. */
108        inline static uint32 HexToUInt32(const string& value, uint32 count=UINT_MAX) { return HexToUInt32(value.c_str(), count); } /**< See above. */
109#ifdef UNICODE
110        static uint32 HexToUInt32(const fchar** value, uint32 count=UINT_MAX); /**< See above. */
111        inline static uint32 HexToUInt32(const fchar* value, uint32 count=UINT_MAX) { return HexToUInt32(&value, count); } /**< See above. */
112        inline static uint32 HexToUInt32(const fstring& value, uint32 count=UINT_MAX) { return HexToUInt32(value.c_str(), count); } /**< See above. */
113#endif
114
115        /** Parses a string into a 3D vector.
116                @param value The string. For the string pointer versions of this function,
117                        the pointer will point to the last processed characters after the parsing.
118                @param lengthFactor An optional factor that will scale the 3D vector.
119                @return The parsed 3D vector. */
120        static FMVector3 ToPoint(const char** value, float lengthFactor=1.0f);
121        inline static FMVector3 ToPoint(const char* value, float lengthFactor=1.0f) { return ToPoint(&value, lengthFactor); } /**< See above. */
122        inline static FMVector3 ToPoint(const string& value, float lengthFactor=1.0f) { return ToPoint(value.c_str(), lengthFactor); } /**< See above. */
123#ifdef UNICODE
124        static FMVector3 ToPoint(const fchar** value, float lengthFactor=1.0f); /**< See above. */
125        inline static FMVector3 ToPoint(const fchar* value, float lengthFactor=1.0f) { return ToPoint(&value, lengthFactor); } /**< See above. */
126        inline static FMVector3 ToPoint(const fstring& value, float lengthFactor=1.0f) { return ToPoint(value.c_str(), lengthFactor); } /**< See above. */
127#endif
128
129        /** Parses a string into a 4x4 matrix.
130                @param value The string. For the string pointer versions of this function,
131                        the pointer will point to the last processed characters after the parsing.
132                @param mx The matrix to be filled in.
133                @param lengthFactor An optional factor that will scale the translation column
134                        of the matrix. */
135        static void ToMatrix(const char** value, FMMatrix44& mx, float lengthFactor=1.0f);
136        inline static void ToMatrix(const char* value, FMMatrix44& mx, float lengthFactor=1.0f) { return ToMatrix(&value, mx, lengthFactor); } /**< See above. */
137        inline static void ToMatrix(const string& value, FMMatrix44& mx, float lengthFactor=1.0f) { return ToMatrix(value.c_str(), mx, lengthFactor); } /**< See above. */
138#ifdef UNICODE
139        static void ToMatrix(const fchar** value, FMMatrix44& mx, float lengthFactor=1.0f); /**< See above. */
140        inline static void ToMatrix(const fchar* value, FMMatrix44& mx, float lengthFactor=1.0f) { return ToMatrix(&value, mx, lengthFactor); } /**< See above. */
141        inline static void ToMatrix(const fstring& value, FMMatrix44& mx, float lengthFactor=1.0f) { return ToMatrix(value.c_str(), mx, lengthFactor); } /**< See above. */
142#endif
143
144        /** Parses a string into a datetime structure.
145                @param value The string.
146                @param dateTime The datetime structure to fill in. */
147        static void ToDateTime(const char* value, FUDateTime& dateTime);
148        inline static void ToDateTime(const string& value, FUDateTime& dateTime) { return ToDateTime(value.c_str(), dateTime); } /**< See above. */
149#ifdef UNICODE
150        static void ToDateTime(const fchar* value, FUDateTime& dateTime); /**< See above. */
151        inline static void ToDateTime(const fstring& value, FUDateTime& dateTime) { return ToDateTime(value.c_str(), dateTime); } /**< See above. */
152#endif
153
154#ifdef HAS_VECTORTYPES
155
156        /** Splits a string into multiple substrings.
157                The separator used here are the white-spaces.
158                @param value The string.
159                @param array A list of strings that will be filled in. */
160        static void ToFStringList(const fstring& value, FStringList& array);
161        static void ToStringList(const string& value, StringList& array); /**< See above. */
162#ifdef UNICODE
163        static void ToStringList(const fchar* value, StringList& array); /**< See above. */
164        inline static void ToStringList(const fstring& value, StringList& array) { return ToStringList(value.c_str(), array); } /**< See above. */
165#endif
166
167        /** Parses a string into a list of floating point values.
168                @param value The string.
169                @param array The list of floating point values to fill in. */
170        static void ToFloatList(const char* value, FloatList& array);
171        inline static void ToFloatList(const string& value, FloatList& array) { return ToFloatList(value.c_str(), array); } /**< See above. */
172#ifdef UNICODE
173        static void ToFloatList(const fchar* value, FloatList& array); /**< See above. */
174        inline static void ToFloatList(const fstring& value, FloatList& array) { return ToFloatList(value.c_str(), array); } /**< See above. */
175#endif
176
177        /** Parses a string into a list of signed integers.
178                @param value The string.
179                @param array The list of signed integers to fill in. */
180        static void ToInt32List(const char* value, Int32List& array);
181        inline static void ToInt32List(const string& value, Int32List& array) { return ToInt32List(value.c_str(), array); } /**< See above. */
182#ifdef UNICODE
183        static void ToInt32List(const fchar* value, Int32List& array); /**< See above. */
184        inline static void ToInt32List(const fstring& value, Int32List& array) { return ToInt32List(value.c_str(), array); } /**< See above. */
185#endif
186
187        /** Parses a string into a list of unsigned integers.
188                @param value The string.
189                @param array The list of unsigned integers to fill in. */
190        static void ToUInt32List(const char* value, UInt32List& array);
191        inline static void ToUInt32List(const string& value, UInt32List& array) { return ToUInt32List(value.c_str(), array); } /**< See above. */
192#ifdef UNICODE
193        static void ToUInt32List(const fchar* value, UInt32List& array); /**< See above. */
194        inline static void ToUInt32List(const fstring& value, UInt32List& array) { return ToUInt32List(value.c_str(), array); } /**< See above. */
195#endif
196
197        /** Parses a string containing interleaved floating-point values.
198                The values will be stored in multiple, independent lists.
199                @param value The string containing interleaved floating-point values.
200                @param arrays The lists of floating-point values to fill in. */
201        static void ToInterleavedFloatList(const char* value, const vector<FloatList*>& arrays);
202        inline static void ToInterleavedFloatList(const string& value, const vector<FloatList*>& arrays) { return ToInterleavedFloatList(value.c_str(), arrays); } /**< See above. */
203#ifdef UNICODE
204        static void ToInterleavedFloatList(const fchar* value, const vector<FloatList*>& arrays); /**< See above. */
205        inline static void ToInterleavedFloatList(const fstring& value, const vector<FloatList*>& arrays) { return ToInterleavedFloatList(value.c_str(), arrays); } /**< See above. */
206#endif
207
208        /** Parses a string into a list of matrices.
209                @param value The string.
210                @param array The list of matrices to fill in.
211                @param lengthFactor An optional factor that will scale the translation column
212                        of the matrices. */
213        static void ToMatrixList(const char* value, FMMatrix44List& array, float lengthFactor=1.0f);
214        inline static void ToMatrixList(const string& value, FMMatrix44List& array, float lengthFactor=1.0f) { return ToMatrixList(value.c_str(), array, lengthFactor); } /**< See above. */
215#ifdef UNICODE
216        static void ToMatrixList(const fchar* value, FMMatrix44List& array, float lengthFactor=1.0f); /**< See above. */
217        inline static void ToMatrixList(const fstring& value, FMMatrix44List& array, float lengthFactor=1.0f) { return ToMatrixList(value.c_str(), array, lengthFactor); } /**< See above. */
218#endif
219
220        /** Parses a string into a list of 3D points.
221                @param value The string.
222                @param array The list of 3D points to fill in.
223                @param lengthFactor An optional factor that will scale the points. */
224        static void ToPointList(const char* value, FMVector3List& array, float lengthFactor=1.0f);
225        inline static void ToPointList(const string& value, FMVector3List& array, float lengthFactor=1.0f) { return ToPointList(value.c_str(), array, lengthFactor); } /**< See above. */
226#ifdef UNICODE
227        static void ToPointList(const fchar* value, FMVector3List& array, float lengthFactor=1.0f); /**< See above. */
228        inline static void ToPointList(const fstring& value, FMVector3List& array, float lengthFactor=1.0f) { return ToPointList(value.c_str(), array, lengthFactor); } /**< See above. */
229#endif
230
231        /** Converts a list of floating-point values into a string.
232                @param builder The string builder that will contain the list of values.
233                        This string builder is not cleared of its contents and a space
234                        character will be added if it is not empty.
235                @param values The list of floating-point values to convert.
236                @param lengthFactor An optional factor that will scale all the
237                        floating-point values. */
238        static void ToString(FUSStringBuilder& builder, const FloatList& values, float lengthFactor=1.0f);
239
240        /** Converts a list of signed integers into a string.
241                @param builder The string builder that will contain the list of values.
242                        This string builder is not cleared of its contents and a space
243                        character will be added if it is not empty.
244                @param values The list of signed integers to convert. */
245        static void ToString(FUSStringBuilder& builder, const Int32List& values);
246
247        /** Converts a list of unsigned integers into a string.
248                @param builder The string builder that will contain the list of values.
249                        This string builder is not cleared of its contents and a space
250                        character will be added if it is not empty.
251                @param values The list of unsigned integers to convert. */
252        static void ToString(FUSStringBuilder& builder, const UInt32List& values);
253
254#endif // HAS_VECTORTYPES
255
256        /** Converts a 4D vector into a string.
257                @param p The 4D vector to convert.
258                @param lengthFactor An optional factor that will scale the vector.
259                @return The string containing the converted vector. */
260        static string ToString(const FMVector4& p, float lengthFactor=1.0f);
261
262        /** Converts a matrix into a string.
263                @param value The matrix to convert.
264                @param lengthFactor An optional factor that will scale the translation
265                        column of the matrix.
266                @return The string containing the converted matrix. */
267        static string ToString(const FMMatrix44& value, float lengthFactor=1.0f);
268        static fstring ToFString(const FMMatrix44& value, float lengthFactor=1.0f); /**< See above. */
269
270        /** Converts a 3D vector into a string.
271                @param value The 3D vector to convert.
272                @param lengthFactor An optional factor that will scale the vector.
273                @return The string containing the converted vector. */
274        static string ToString(const FMVector3& value, float lengthFactor=1.0f);
275        static fstring ToFString(const FMVector3& value, float lengthFactor=1.0f); /**< See above. */
276
277        /** Converts a datetime structure into a string.
278                @param dateTime The datetime structure to convert.
279                @return The string containing the converted datetime structure. */
280        static string ToString(const FUDateTime& dateTime);
281        static fstring ToFString(const FUDateTime& dateTime); /**< See above. */
282
283        /** Converts a primitive value into a string.
284                This function is templatized to use the global string builders to
285                convert most primitive value types, such as signed integers,
286                unsigned integers and single floating-point values, into strings.
287                @see FUStringBuilderT
288                @param value A primitive value.
289                @return The string containing the converted primitive value. */
290        template <typename T> static string ToString(const T& value) { globalSBuilder.set(value); return globalSBuilder.ToString(); }
291        template <typename T> static fstring ToFString(const T& value) { globalBuilder.set(value); return globalBuilder.ToString(); } /**< See above. */
292
293        /** Converts a matrix into a string.
294                @param builder The string builder that will contain the matrix.
295                        This string builder is not cleared of its contents.
296                @param value The matrix to convert.
297                @param lengthFactor An optional factor that will scale the translation
298                        column of the matrix. */
299        static void ToString(FUSStringBuilder& builder, const FMMatrix44& value, float lengthFactor=1.0f);
300        static void ToFString(FUStringBuilder& builder, const FMMatrix44& value, float lengthFactor=1.0f); /**< See above. */
301
302        /** Converts a 3D vector into a string.
303                @param builder The string builder that will contain the 3D vector.
304                        This string builder is not cleared of its contents.
305                @param value The 3D vector to convert.
306                @param lengthFactor An optional factor that will scale the vector. */
307        static void ToString(FUSStringBuilder& builder, const FMVector3& value, float lengthFactor=1.0f);
308        static void ToFString(FUStringBuilder& builder, const FMVector3& value, float lengthFactor=1.0f); /**< See above. */
309
310        /** Converts a 4D vector into a string.
311                @param builder The string builder that will contain the 4D vector.
312                        This string builder is not cleared of its contents.
313                @param p The 4D vector to convert.
314                @param lengthFactor An optional factor that will scale the vector. */
315        static void ToString(FUSStringBuilder& builder, const FMVector4& p, float lengthFactor=1.0f);
316};
317
318#endif // _FCU_STRING_CONVERSION_
Note: See TracBrowser for help on using the repository browser.