7fd59977 |
1 | // File: LDOMBasicString.cxx |
2 | // Created: 26.06.01 16:52:43 |
3 | // Author: Alexander GRIGORIEV |
4 | // Copyright: OpenCascade 2001 |
5 | // History: |
6 | |
7 | |
8 | #include <LDOMBasicString.hxx> |
9 | #include <LDOM_MemManager.hxx> |
10 | #include <TCollection_AsciiString.hxx> |
11 | #include <TCollection_ExtendedString.hxx> |
12 | #include <errno.h> |
13 | |
14 | //======================================================================= |
15 | //function : LDOMString |
16 | //purpose : Create a free string (not connected to any type of container) |
17 | //======================================================================= |
18 | |
19 | LDOMBasicString::LDOMBasicString (const char * aValue) |
20 | { |
21 | if (aValue == NULL /*|| aValue[0] == '\0'*/) { |
22 | myType = LDOM_NULL; |
23 | myVal.ptr = NULL; |
24 | } else { |
25 | myType = LDOM_AsciiFree; |
26 | Standard_Integer aLen = strlen (aValue) + 1; |
27 | myVal.ptr = new char [aLen]; |
28 | memcpy (myVal.ptr, aValue, aLen); |
29 | } |
30 | } |
31 | |
32 | //======================================================================= |
33 | //function : LDOMString |
34 | //purpose : Create an Ascii string managed by LDOM_Document |
35 | //======================================================================= |
36 | |
37 | LDOMBasicString::LDOMBasicString (const char * aValue, |
38 | const Handle(LDOM_MemManager)& aDoc) |
39 | { |
40 | if (aValue == NULL /*|| aValue[0] == '\0'*/) { |
41 | myType = LDOM_NULL; |
42 | myVal.ptr = NULL; |
43 | } else { |
44 | myType = LDOM_AsciiDoc; |
45 | Standard_Integer aLen = strlen (aValue) + 1; |
46 | myVal.ptr = aDoc -> Allocate (aLen); |
47 | memcpy (myVal.ptr, aValue, aLen); |
48 | } |
49 | } |
50 | |
51 | //======================================================================= |
52 | //function : LDOMString |
53 | //purpose : Create an Ascii string managed by LDOM_Document |
54 | //======================================================================= |
55 | |
56 | LDOMBasicString::LDOMBasicString (const char * aValue, |
57 | const Standard_Integer aLen, |
58 | const Handle(LDOM_MemManager)& aDoc) |
59 | { |
60 | if (aValue == NULL || aLen == 0) { |
61 | myType = LDOM_NULL; |
62 | myVal.ptr = NULL; |
63 | } else { |
64 | myType = LDOM_AsciiDoc; |
65 | myVal.ptr = aDoc -> Allocate (aLen + 1); |
66 | memcpy (myVal.ptr, aValue, aLen); |
67 | ((char *)myVal.ptr)[aLen] = '\0'; |
68 | } |
69 | } |
70 | |
71 | //======================================================================= |
72 | //function : LDOMBasicString |
73 | //purpose : Copy constructor |
74 | //======================================================================= |
75 | |
76 | LDOMBasicString::LDOMBasicString (const LDOMBasicString& anOther) |
77 | : myType (anOther.Type()) |
78 | { |
79 | switch (myType) { |
80 | case LDOM_AsciiFree: |
81 | if (anOther.myVal.ptr) { |
82 | Standard_Integer aLen = strlen ((const char *)anOther.myVal.ptr) + 1; |
83 | myVal.ptr = new char [aLen]; |
84 | memcpy (myVal.ptr, anOther.myVal.ptr, aLen); |
85 | break; |
86 | } |
87 | case LDOM_AsciiDoc: |
88 | case LDOM_AsciiDocClear: |
89 | case LDOM_AsciiHashed: |
90 | myVal.ptr = anOther.myVal.ptr; |
91 | break; |
92 | case LDOM_Integer: |
93 | myVal.i = anOther.myVal.i; |
94 | default: ; |
95 | } |
96 | } |
97 | |
98 | //======================================================================= |
99 | //function : ~LDOMString() |
100 | //purpose : Destructor |
101 | //======================================================================= |
102 | |
103 | LDOMBasicString::~LDOMBasicString () |
104 | { |
105 | if (myType == LDOM_AsciiFree) { |
106 | if (myVal.ptr) |
107 | delete [] (char *) myVal.ptr; |
108 | } |
109 | } |
110 | |
111 | //======================================================================= |
112 | //function : operator = |
113 | //purpose : Assignment to NULL |
114 | //======================================================================= |
115 | |
116 | LDOMBasicString& LDOMBasicString::operator = (const LDOM_NullPtr *) |
117 | { |
118 | if (myType == LDOM_AsciiFree && myVal.ptr) |
119 | delete [] (char *) myVal.ptr; |
120 | myType = LDOM_NULL; |
121 | myVal.ptr = NULL; |
122 | return * this; |
123 | } |
124 | |
125 | //======================================================================= |
126 | //function : operator = |
127 | //purpose : Assignment |
128 | //======================================================================= |
129 | |
130 | LDOMBasicString& LDOMBasicString::operator = (const LDOMBasicString& anOther) |
131 | { |
132 | if (myType == LDOM_AsciiFree && myVal.ptr) |
133 | delete [] (char *) myVal.ptr; |
134 | myType = anOther.Type(); |
135 | switch (myType) { |
136 | case LDOM_AsciiFree: |
137 | if (anOther.myVal.ptr) { |
138 | Standard_Integer aLen = strlen ((const char *)anOther.myVal.ptr) + 1; |
139 | myVal.ptr = new char [aLen]; |
140 | memcpy (myVal.ptr, anOther.myVal.ptr, aLen); |
141 | break; |
142 | } |
143 | case LDOM_AsciiDoc: |
144 | case LDOM_AsciiDocClear: |
145 | case LDOM_AsciiHashed: |
146 | myVal.ptr = anOther.myVal.ptr; |
147 | break; |
148 | case LDOM_Integer: |
149 | myVal.i = anOther.myVal.i; |
150 | default: ; |
151 | } |
152 | return * this; |
153 | } |
154 | |
155 | //======================================================================= |
156 | //function : equals |
157 | //purpose : Compare two strings by content |
158 | //======================================================================= |
159 | |
160 | Standard_Boolean LDOMBasicString::equals (const LDOMBasicString& anOther) const |
161 | { |
162 | Standard_Boolean aResult = Standard_False; |
163 | switch (myType) |
164 | { |
165 | case LDOM_NULL: |
166 | return (anOther == NULL); |
167 | case LDOM_Integer: |
168 | switch (anOther.Type()) |
169 | { |
170 | case LDOM_Integer: |
171 | return (myVal.i == anOther.myVal.i); |
172 | case LDOM_AsciiFree: |
173 | case LDOM_AsciiDoc: |
174 | case LDOM_AsciiDocClear: |
175 | case LDOM_AsciiHashed: |
176 | { |
177 | long aLongOther = strtol ((const char *) anOther.myVal.ptr, NULL, 10); |
178 | return (errno == 0 && aLongOther == long(myVal.i)); |
179 | } |
180 | case LDOM_NULL: |
181 | default: |
182 | ; |
183 | } |
184 | break; |
185 | default: |
186 | switch (anOther.Type()) |
187 | { |
188 | case LDOM_Integer: |
189 | { |
190 | long aLong = strtol ((const char *) myVal.ptr, NULL, 10); |
191 | return (errno == 0 && aLong == long(anOther.myVal.i)); |
192 | } |
193 | case LDOM_AsciiFree: |
194 | case LDOM_AsciiDoc: |
195 | case LDOM_AsciiDocClear: |
196 | case LDOM_AsciiHashed: |
197 | return (strcmp ((const char *) myVal.ptr, |
198 | (const char *) anOther.myVal.ptr) == 0); |
199 | case LDOM_NULL: |
200 | default: |
201 | ; |
202 | } |
203 | } |
204 | return aResult; |
205 | } |
206 | |
207 | //======================================================================= |
208 | //function : operator TCollection_AsciiString |
209 | //purpose : |
210 | //======================================================================= |
211 | |
212 | LDOMBasicString::operator TCollection_AsciiString () const |
213 | { |
214 | switch (myType) { |
215 | case LDOM_Integer: |
216 | return TCollection_AsciiString (myVal.i); |
217 | case LDOM_AsciiFree: |
218 | case LDOM_AsciiDoc: |
219 | case LDOM_AsciiDocClear: |
220 | case LDOM_AsciiHashed: |
221 | return TCollection_AsciiString (Standard_CString(myVal.ptr)); |
222 | default: ; |
223 | } |
224 | return TCollection_AsciiString (); |
225 | } |
226 | |
227 | //======================================================================= |
228 | //function : operator TCollection_ExtendedString |
229 | //purpose : |
230 | //======================================================================= |
231 | |
232 | LDOMBasicString::operator TCollection_ExtendedString () const |
233 | { |
234 | switch (myType) { |
235 | case LDOM_Integer: |
236 | return TCollection_ExtendedString (myVal.i); |
237 | case LDOM_AsciiFree: |
238 | case LDOM_AsciiDoc: |
239 | case LDOM_AsciiDocClear: |
240 | case LDOM_AsciiHashed: |
241 | { |
242 | char buf[6] = {'\0','\0','\0','\0','\0','\0'}; |
243 | const long aUnicodeHeader = 0xfeff; |
244 | Standard_CString ptr = Standard_CString (myVal.ptr); |
245 | errno = 0; |
246 | // Check if ptr is ascii string |
247 | if (ptr[0] != '#' || ptr[1] != '#') |
248 | return TCollection_ExtendedString (ptr); |
249 | buf[0] = ptr[2]; |
250 | buf[1] = ptr[3]; |
251 | buf[2] = ptr[4]; |
252 | buf[3] = ptr[5]; |
253 | if (strtol (&buf[0], NULL, 16) != aUnicodeHeader) |
254 | return TCollection_ExtendedString (ptr); |
255 | |
256 | // convert Unicode to Extended String |
257 | ptr += 2; |
258 | Standard_Integer aLength = (strlen(ptr) / 4), j = 0; |
259 | Standard_ExtCharacter * aResult = new Standard_ExtCharacter[aLength--]; |
260 | while (aLength--) { |
261 | ptr += 4; |
262 | buf[0] = ptr[0]; |
263 | buf[1] = ptr[1]; |
264 | buf[2] = ptr[2]; |
265 | buf[3] = ptr[3]; |
266 | aResult[j++] = Standard_ExtCharacter (strtol (&buf[0], NULL, 16)); |
267 | if (errno) { |
268 | delete [] aResult; |
269 | return TCollection_ExtendedString(); |
270 | } |
271 | } |
272 | aResult[j] = 0; |
273 | TCollection_ExtendedString aResultStr (aResult); |
274 | delete [] aResult; |
275 | return aResultStr; |
276 | } |
277 | default: ; |
278 | } |
279 | return TCollection_ExtendedString(); |
280 | } |
281 | |
282 | //======================================================================= |
283 | //function : GetInteger |
284 | //purpose : Conversion to Integer |
285 | //======================================================================= |
286 | |
287 | Standard_Boolean LDOMBasicString::GetInteger (Standard_Integer& aResult) const |
288 | { |
289 | switch (myType) { |
290 | case LDOM_Integer: |
291 | aResult = myVal.i; |
292 | break; |
293 | case LDOM_AsciiFree: |
294 | case LDOM_AsciiDoc: |
295 | case LDOM_AsciiDocClear: |
296 | case LDOM_AsciiHashed: |
297 | { |
298 | char * ptr; |
299 | long aValue = strtol ((const char *)myVal.ptr, &ptr, 10); |
300 | if (ptr == myVal.ptr || errno == ERANGE || errno == EINVAL) |
301 | return Standard_False; |
302 | aResult = Standard_Integer (aValue); |
303 | break; |
304 | } |
305 | default: |
306 | return Standard_False; |
307 | } |
308 | return Standard_True; |
309 | } |
310 | |
311 | #ifdef DEB |
312 | #ifndef WNT |
313 | //======================================================================= |
314 | // Debug Function for DBX: use "print -p <Variable> or pp <Variable>" |
315 | //======================================================================= |
316 | #include <LDOM_OSStream.hxx> |
317 | #define FLITERAL 0x10 |
318 | char * db_pretty_print (const LDOMBasicString * aString, int fl, char *) |
319 | { |
320 | LDOM_OSStream out (128); |
321 | const char * str; |
322 | switch (aString -> myType) { |
323 | case LDOMBasicString::LDOM_Integer: |
324 | if ((fl & FLITERAL) == 0) out << "LDOM_Integer: "; |
325 | out << '\"' << aString -> myVal.i << '\"'; goto finis; |
326 | case LDOMBasicString::LDOM_AsciiFree: |
327 | if ((fl & FLITERAL) == 0) out << "LDOM_AsciiFree: "; |
328 | break; |
329 | case LDOMBasicString::LDOM_AsciiDoc: |
330 | if ((fl & FLITERAL) == 0) out << "LDOM_AsciiDoc: "; |
331 | break; |
332 | case LDOMBasicString::LDOM_AsciiDocClear: |
333 | if ((fl & FLITERAL) == 0) out << "LDOM_AsciiDocClear: "; |
334 | break; |
335 | case LDOMBasicString::LDOM_AsciiHashed: |
336 | if ((fl & FLITERAL) == 0) out << "LDOM_AsciiHashed: "; |
337 | break; |
338 | default: |
339 | out << "Unknown type"; |
340 | } |
341 | str = (const char *) aString -> myVal.ptr; |
342 | out << '\"'; |
343 | if (strlen (str) > 512) |
344 | out.rdbuf() -> sputn (str, 512); |
345 | else |
346 | out << str; |
347 | out << '\"'; |
348 | finis: |
349 | out << ends; |
350 | return (char *)out.str(); |
351 | } |
352 | #endif |
353 | #endif |