1 // Created on: 2001-06-26
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
22 #include <LDOMBasicString.hxx>
23 #include <LDOM_MemManager.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_ExtendedString.hxx>
28 //=======================================================================
29 //function : LDOMString
30 //purpose : Create a free string (not connected to any type of container)
31 //=======================================================================
33 LDOMBasicString::LDOMBasicString (const char * aValue)
35 if (aValue == NULL /*|| aValue[0] == '\0'*/) {
39 myType = LDOM_AsciiFree;
40 Standard_Integer aLen = strlen (aValue) + 1;
41 myVal.ptr = new char [aLen];
42 memcpy (myVal.ptr, aValue, aLen);
46 //=======================================================================
47 //function : LDOMString
48 //purpose : Create an Ascii string managed by LDOM_Document
49 //=======================================================================
51 LDOMBasicString::LDOMBasicString (const char * aValue,
52 const Handle(LDOM_MemManager)& aDoc)
54 if (aValue == NULL /*|| aValue[0] == '\0'*/) {
58 myType = LDOM_AsciiDoc;
59 Standard_Integer aLen = strlen (aValue) + 1;
60 myVal.ptr = aDoc -> Allocate (aLen);
61 memcpy (myVal.ptr, aValue, aLen);
65 //=======================================================================
66 //function : LDOMString
67 //purpose : Create an Ascii string managed by LDOM_Document
68 //=======================================================================
70 LDOMBasicString::LDOMBasicString (const char * aValue,
71 const Standard_Integer aLen,
72 const Handle(LDOM_MemManager)& aDoc)
74 if (aValue == NULL || aLen == 0) {
78 myType = LDOM_AsciiDoc;
79 myVal.ptr = aDoc -> Allocate (aLen + 1);
80 memcpy (myVal.ptr, aValue, aLen);
81 ((char *)myVal.ptr)[aLen] = '\0';
85 //=======================================================================
86 //function : LDOMBasicString
87 //purpose : Copy constructor
88 //=======================================================================
90 LDOMBasicString::LDOMBasicString (const LDOMBasicString& anOther)
91 : myType (anOther.Type())
95 if (anOther.myVal.ptr) {
96 Standard_Integer aLen = strlen ((const char *)anOther.myVal.ptr) + 1;
97 myVal.ptr = new char [aLen];
98 memcpy (myVal.ptr, anOther.myVal.ptr, aLen);
102 case LDOM_AsciiDocClear:
103 case LDOM_AsciiHashed:
104 myVal.ptr = anOther.myVal.ptr;
107 myVal.i = anOther.myVal.i;
112 //=======================================================================
113 //function : ~LDOMString()
114 //purpose : Destructor
115 //=======================================================================
117 LDOMBasicString::~LDOMBasicString ()
119 if (myType == LDOM_AsciiFree) {
121 delete [] (char *) myVal.ptr;
125 //=======================================================================
126 //function : operator =
127 //purpose : Assignment to NULL
128 //=======================================================================
130 LDOMBasicString& LDOMBasicString::operator = (const LDOM_NullPtr *)
132 if (myType == LDOM_AsciiFree && myVal.ptr)
133 delete [] (char *) myVal.ptr;
139 //=======================================================================
140 //function : operator =
141 //purpose : Assignment
142 //=======================================================================
144 LDOMBasicString& LDOMBasicString::operator = (const LDOMBasicString& anOther)
146 if (myType == LDOM_AsciiFree && myVal.ptr)
147 delete [] (char *) myVal.ptr;
148 myType = anOther.Type();
151 if (anOther.myVal.ptr) {
152 Standard_Integer aLen = strlen ((const char *)anOther.myVal.ptr) + 1;
153 myVal.ptr = new char [aLen];
154 memcpy (myVal.ptr, anOther.myVal.ptr, aLen);
158 case LDOM_AsciiDocClear:
159 case LDOM_AsciiHashed:
160 myVal.ptr = anOther.myVal.ptr;
163 myVal.i = anOther.myVal.i;
169 //=======================================================================
171 //purpose : Compare two strings by content
172 //=======================================================================
174 Standard_Boolean LDOMBasicString::equals (const LDOMBasicString& anOther) const
176 Standard_Boolean aResult = Standard_False;
180 return (anOther == NULL);
182 switch (anOther.Type())
185 return (myVal.i == anOther.myVal.i);
188 case LDOM_AsciiDocClear:
189 case LDOM_AsciiHashed:
191 long aLongOther = strtol ((const char *) anOther.myVal.ptr, NULL, 10);
192 return (errno == 0 && aLongOther == long(myVal.i));
200 switch (anOther.Type())
204 long aLong = strtol ((const char *) myVal.ptr, NULL, 10);
205 return (errno == 0 && aLong == long(anOther.myVal.i));
209 case LDOM_AsciiDocClear:
210 case LDOM_AsciiHashed:
211 return (strcmp ((const char *) myVal.ptr,
212 (const char *) anOther.myVal.ptr) == 0);
221 //=======================================================================
222 //function : operator TCollection_AsciiString
224 //=======================================================================
226 LDOMBasicString::operator TCollection_AsciiString () const
230 return TCollection_AsciiString (myVal.i);
233 case LDOM_AsciiDocClear:
234 case LDOM_AsciiHashed:
235 return TCollection_AsciiString (Standard_CString(myVal.ptr));
238 return TCollection_AsciiString ();
241 //=======================================================================
242 //function : operator TCollection_ExtendedString
244 //=======================================================================
246 LDOMBasicString::operator TCollection_ExtendedString () const
250 return TCollection_ExtendedString (myVal.i);
253 case LDOM_AsciiDocClear:
254 case LDOM_AsciiHashed:
256 char buf[6] = {'\0','\0','\0','\0','\0','\0'};
257 const long aUnicodeHeader = 0xfeff;
258 Standard_CString ptr = Standard_CString (myVal.ptr);
260 // Check if ptr is ascii string
261 if (ptr[0] != '#' || ptr[1] != '#')
262 return TCollection_ExtendedString (ptr);
267 if (strtol (&buf[0], NULL, 16) != aUnicodeHeader)
268 return TCollection_ExtendedString (ptr);
270 // convert Unicode to Extended String
272 Standard_Integer aLength = (strlen(ptr) / 4), j = 0;
273 Standard_ExtCharacter * aResult = new Standard_ExtCharacter[aLength--];
280 aResult[j++] = Standard_ExtCharacter (strtol (&buf[0], NULL, 16));
283 return TCollection_ExtendedString();
287 TCollection_ExtendedString aResultStr (aResult);
293 return TCollection_ExtendedString();
296 //=======================================================================
297 //function : GetInteger
298 //purpose : Conversion to Integer
299 //=======================================================================
301 Standard_Boolean LDOMBasicString::GetInteger (Standard_Integer& aResult) const
309 case LDOM_AsciiDocClear:
310 case LDOM_AsciiHashed:
313 long aValue = strtol ((const char *)myVal.ptr, &ptr, 10);
314 if (ptr == myVal.ptr || errno == ERANGE || errno == EINVAL)
315 return Standard_False;
316 aResult = Standard_Integer (aValue);
320 return Standard_False;
322 return Standard_True;
327 //=======================================================================
328 // Debug Function for DBX: use "print -p <Variable> or pp <Variable>"
329 //=======================================================================
330 #include <LDOM_OSStream.hxx>
331 #define FLITERAL 0x10
332 char * db_pretty_print (const LDOMBasicString * aString, int fl, char *)
334 LDOM_OSStream out (128);
336 switch (aString -> myType) {
337 case LDOMBasicString::LDOM_Integer:
338 if ((fl & FLITERAL) == 0) out << "LDOM_Integer: ";
339 out << '\"' << aString -> myVal.i << '\"'; goto finis;
340 case LDOMBasicString::LDOM_AsciiFree:
341 if ((fl & FLITERAL) == 0) out << "LDOM_AsciiFree: ";
343 case LDOMBasicString::LDOM_AsciiDoc:
344 if ((fl & FLITERAL) == 0) out << "LDOM_AsciiDoc: ";
346 case LDOMBasicString::LDOM_AsciiDocClear:
347 if ((fl & FLITERAL) == 0) out << "LDOM_AsciiDocClear: ";
349 case LDOMBasicString::LDOM_AsciiHashed:
350 if ((fl & FLITERAL) == 0) out << "LDOM_AsciiHashed: ";
353 out << "Unknown type";
355 str = (const char *) aString -> myVal.ptr;
357 if (strlen (str) > 512)
358 out.rdbuf() -> sputn (str, 512);
364 return (char *)out.str();