0029399: Optimize reading of floating point values from text strings
[occt.git] / src / Standard / Standard_GUID.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
b311480e 14
42cf5bc1 15
16#include <Standard_GUID.hxx>
7fd59977 17#include <Standard_IStream.hxx>
42cf5bc1 18#include <Standard_RangeError.hxx>
7fd59977 19
42cf5bc1 20#include <stdio.h>
7fd59977 21
22//Fixes incorrect treatment of GUID given as a string with invalid format
7fd59977 23
24//=======================================================================
25//function : Standard_GUID_MatchChar
26//purpose :
27//=======================================================================
28Standard_Integer Standard_GUID_MatchChar(const Standard_CString buffer,
29 const Standard_Character aChar)
30{
31 Standard_CString tmpbuffer = buffer;
32 Standard_Integer result = -1;
33
34 while(*tmpbuffer != '\0' && *tmpbuffer != aChar) {tmpbuffer++; result++;}
35
7fd59977 36 if(*tmpbuffer == '\0') return -1; //The searched symbol wasn't found
7fd59977 37
38 if (result >= 0) result++;
39
40 return result;
41}
42//=======================================================================
43//function : Standard_GUID_GetValue32
44//purpose :
45//=======================================================================
46Standard_PCharacter Standard_GUID_GetValue32(Standard_PCharacter tmpBuffer,
47 Standard_Integer& my32b)
48{
49 Standard_Character strtmp[Standard_GUID_SIZE_ALLOC];
50 Standard_Integer pos = 0;
51
52 pos = Standard_GUID_MatchChar(tmpBuffer,'-');
53 if (pos >= 0) {
54 strncpy(strtmp,tmpBuffer,pos);
55 strtmp[pos] = '\0';
56 my32b = (Standard_Integer) strtoul(strtmp, (char **)NULL, 16);
57 }
7fd59977 58 else return NULL;
7fd59977 59 return &tmpBuffer[pos+1];
60}
61//=======================================================================
62//function : Standard_GUID_GetValue16
63//purpose :
64//=======================================================================
65Standard_PCharacter Standard_GUID_GetValue16(Standard_PCharacter tmpBuffer,
66 Standard_ExtCharacter& my32b)
67{
68 Standard_Character strtmp[Standard_GUID_SIZE_ALLOC];
69 Standard_Integer pos = 0;
70
71 pos = Standard_GUID_MatchChar(tmpBuffer,'-');
72 if (pos >= 0) {
73 strncpy(strtmp,tmpBuffer,pos);
74 strtmp[pos] = '\0';
75 my32b = (Standard_ExtCharacter) strtoul(strtmp, (char **)NULL, 16);
76 }
7fd59977 77 else return NULL;
7fd59977 78// cout << "V16 :" << hex(my32b) << endl;
79 return &tmpBuffer[pos+1];
80}
81//=======================================================================
82//function : Standard_GUID_GetValue8
83//purpose :
84//=======================================================================
85Standard_PCharacter Standard_GUID_GetValue8(Standard_PCharacter tmpBuffer,
86 Standard_Byte& my32b)
87{
88 Standard_Character strtmp[Standard_GUID_SIZE_ALLOC];
89
90 strncpy(strtmp,tmpBuffer,2);
91 strtmp[2] = '\0';
92 my32b = (Standard_Byte) strtoul(strtmp, (char **)NULL, 16);
93// cout << "V8 :" << hex(my32b) << endl;
94 return &tmpBuffer[2];
95}
96//=======================================================================
97//function : CheckGUIDFormat
98//purpose :
99//=======================================================================
100Standard_Boolean Standard_GUID::CheckGUIDFormat(const Standard_CString aGuid)
101{
102 Standard_Boolean result = Standard_True;
103
104 if (aGuid == NULL) return Standard_False;
105
106 if (strlen(aGuid) == Standard_GUID_SIZE) {
107 Standard_Integer i;
108
109 for (i = 0; i < 8 && result; i++) {
110 if (!IsXDigit(aGuid[i])) {
111 return Standard_False;
112 }
113 }
114
115 if (aGuid[8] != '-') return Standard_False;
116
117 for (i = 9; i < 13 && result; i++) {
118 if (!IsXDigit(aGuid[i])) {
119 return Standard_False;
120 }
121 }
122
123 if (aGuid[13] != '-') return Standard_False;
124
125 for (i = 14; i < 18 && result; i++) {
126 if (!IsXDigit(aGuid[i])) {
127 return Standard_False;
128 }
129 }
130
131 if (aGuid[18] != '-') return Standard_False;
132
133 for (i = 19; i < 23; i++) {
134 if (!IsXDigit(aGuid[i])) {
135 return Standard_False;
136 }
137 }
138
139 if (aGuid[23] != '-') return Standard_False;
140
141 for (i = 24; i < 36; i++) {
142 if (!IsXDigit(aGuid[i])) {
143 return Standard_False;
144 }
145 }
146 }
147 else result = Standard_False;
148
149 return result;
150}
151
152Standard_GUID::Standard_GUID()
153: my32b ( 0),
154 my16b1 ( 0),
155 my16b2 ( 0),
156 my16b3 ( 0),
157 my8b1 ( 0),
158 my8b2 ( 0),
159 my8b3 ( 0),
160 my8b4 ( 0),
161 my8b5 ( 0),
162 my8b6 ( 0)
163{
164}
165
166Standard_GUID::Standard_GUID(const Standard_CString aGuid)
167: my32b ( 0),
168 my16b1 ( 0),
169 my16b2 ( 0),
170 my16b3 ( 0),
171 my8b1 ( 0),
172 my8b2 ( 0),
173 my8b3 ( 0),
174 my8b4 ( 0),
175 my8b5 ( 0),
176 my8b6 ( 0)
177{
178 char* tmpBuffer =(char*) aGuid;
179
9775fa61 180 if(!CheckGUIDFormat(tmpBuffer)) throw Standard_RangeError("Invalid format of GUID");
7fd59977 181
7fd59977 182 if((tmpBuffer = Standard_GUID_GetValue32(tmpBuffer,my32b)) == NULL)
9775fa61 183 throw Standard_RangeError("Invalid format of GUID");
7fd59977 184 if((tmpBuffer = Standard_GUID_GetValue16(tmpBuffer,my16b1)) == NULL)
9775fa61 185 throw Standard_RangeError("Invalid format of GUID");
7fd59977 186 if((tmpBuffer = Standard_GUID_GetValue16(tmpBuffer,my16b2)) == NULL)
9775fa61 187 throw Standard_RangeError("Invalid format of GUID");
7fd59977 188 if((tmpBuffer = Standard_GUID_GetValue16(tmpBuffer,my16b3)) == NULL)
9775fa61 189 throw Standard_RangeError("Invalid format of GUID");
7fd59977 190 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b1);
191 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b2);
192 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b3);
193 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b4);
194 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b5);
195 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b6);
196}
197
198Standard_GUID::Standard_GUID(const Standard_ExtString aGuid)
199: my32b ( 0),
200 my16b1 ( 0),
201 my16b2 ( 0),
202 my16b3 ( 0),
203 my8b1 ( 0),
204 my8b2 ( 0),
205 my8b3 ( 0),
206 my8b4 ( 0),
207 my8b5 ( 0),
208 my8b6 ( 0)
209{
6b09c923
R
210 char tpb[Standard_GUID_SIZE_ALLOC];
211 char *tmpBuffer = tpb;
7fd59977 212 Standard_Integer i = 0;
7fd59977 213 while(i < Standard_GUID_SIZE) {
214 tmpBuffer[i] = (char ) aGuid[i];
215 i++;
216 }
217
218 tmpBuffer[i] = '\0';
219
9775fa61 220 if(!CheckGUIDFormat(tmpBuffer)) throw Standard_RangeError("Invalid format of GUID");
7fd59977 221
7fd59977 222 if((tmpBuffer = Standard_GUID_GetValue32(tmpBuffer,my32b)) == NULL)
9775fa61 223 throw Standard_RangeError("Invalid format of GUID");
7fd59977 224 if((tmpBuffer = Standard_GUID_GetValue16(tmpBuffer,my16b1)) == NULL)
9775fa61 225 throw Standard_RangeError("Invalid format of GUID");
7fd59977 226 if((tmpBuffer = Standard_GUID_GetValue16(tmpBuffer,my16b2)) == NULL)
9775fa61 227 throw Standard_RangeError("Invalid format of GUID");
7fd59977 228 if((tmpBuffer = Standard_GUID_GetValue16(tmpBuffer,my16b3)) == NULL)
9775fa61 229 throw Standard_RangeError("Invalid format of GUID");
7fd59977 230 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b1);
231 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b2);
232 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b3);
233 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b4);
234 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b5);
235 tmpBuffer = Standard_GUID_GetValue8(tmpBuffer,my8b6);
236}
237
238Standard_GUID::Standard_GUID(const Standard_Integer a32b,
239 const Standard_ExtCharacter a16b1,
240 const Standard_ExtCharacter a16b2,
241 const Standard_ExtCharacter a16b3,
242 const Standard_Byte a8b1,
243 const Standard_Byte a8b2,
244 const Standard_Byte a8b3,
245 const Standard_Byte a8b4,
246 const Standard_Byte a8b5,
247 const Standard_Byte a8b6)
248{
249 my32b = a32b;
250 my16b1 = a16b1;
251 my16b2 = a16b2;
252 my16b3 = a16b3;
253 my8b1 = a8b1;
254 my8b2 = a8b2;
255 my8b3 = a8b3;
256 my8b4 = a8b4;
257 my8b5 = a8b5;
258 my8b6 = a8b6;
259}
260
261Standard_GUID::Standard_GUID(const Standard_GUID& aGuid)
262{
263 my32b = aGuid.my32b;
264 my16b1 = aGuid.my16b1;
265 my16b2 = aGuid.my16b2;
266 my16b3 = aGuid.my16b3;
267 my8b1 = aGuid.my8b1;
268 my8b2 = aGuid.my8b2;
269 my8b3 = aGuid.my8b3;
270 my8b4 = aGuid.my8b4;
271 my8b5 = aGuid.my8b5;
272 my8b6 = aGuid.my8b6;
273}
07bbde45 274
275Standard_GUID::Standard_GUID (const Standard_UUID& theUUID)
276{
277 Assign (theUUID);
278}
279
280void Standard_GUID::Assign (const Standard_UUID& theUUID)
7fd59977 281{
07bbde45 282 my32b = theUUID.Data1;
283 my16b1 = theUUID.Data2;
284 my16b2 = theUUID.Data3;
285 my16b3 = (theUUID.Data4[0] << 8) | (theUUID.Data4[1]);
286 my8b1 = theUUID.Data4[2];
287 my8b2 = theUUID.Data4[3];
288 my8b3 = theUUID.Data4[4];
289 my8b4 = theUUID.Data4[5];
290 my8b5 = theUUID.Data4[6];
291 my8b6 = theUUID.Data4[7];
7fd59977 292}
293
294//=======================================================================
295//function : ToCString
296//purpose :
297//=======================================================================
298void Standard_GUID::ToCString(const Standard_PCharacter aStrGuid) const
299{
300 sprintf(aStrGuid,"%.8x-%.4x-%.4x-%.4x-%.2x%.2x%.2x%.2x%.2x%.2x",
301 my32b,
302 (unsigned short) my16b1,
303 (unsigned short) my16b2,
304 (unsigned short) my16b3,
305 (unsigned char)my8b1,
306 (unsigned char)my8b2,
307 (unsigned char)my8b3,
308 (unsigned char)my8b4,
309 (unsigned char)my8b5,
310 (unsigned char)my8b6);
311}
312
313//=======================================================================
314//function : ToExtString
315//purpose :
316//=======================================================================
317void Standard_GUID::ToExtString(const Standard_PExtCharacter aStrGuid) const
318{
319 Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
6b09c923 320 ToCString(sguid);
7fd59977 321
6b09c923 322 for(Standard_Integer i = 0; i < Standard_GUID_SIZE; i++) {
7fd59977 323 aStrGuid[i] = (Standard_ExtCharacter)sguid[i];
324 }
325
326 aStrGuid[Standard_GUID_SIZE] = (Standard_ExtCharacter)0;
327}
328
329Standard_UUID Standard_GUID::ToUUID() const
330{
331 Standard_UUID result ;
332
333 result.Data1 = my32b ;
334 result.Data2 = my16b1 ;
335 result.Data3 = my16b2 ;
008aef40 336 result.Data4[0] = (unsigned char)(my16b3 >> 8);
337 result.Data4[1] = (char) my16b3;
7fd59977 338 result.Data4[2] = my8b1 ;
339 result.Data4[3] = my8b2 ;
340 result.Data4[4] = my8b3 ;
341 result.Data4[5] = my8b4 ;
342 result.Data4[6] = my8b5 ;
343 result.Data4[7] = my8b6 ;
344 return result ;
345}
346
347Standard_Boolean Standard_GUID::IsSame(const Standard_GUID& uid) const
348{
349 Standard_Boolean result = Standard_True;
350
351 if (my32b != uid.my32b) result = Standard_False;
352 else if (my16b1 != uid.my16b1) result = Standard_False;
353 else if (my16b2 != uid.my16b2) result = Standard_False;
354 else if (my16b3 != uid.my16b3) result = Standard_False;
355 else if (my8b1 != uid.my8b1) result = Standard_False;
356 else if (my8b2 != uid.my8b2) result = Standard_False;
357 else if (my8b3 != uid.my8b3) result = Standard_False;
358 else if (my8b4 != uid.my8b4) result = Standard_False;
359 else if (my8b5 != uid.my8b5) result = Standard_False;
360 else if (my8b6 != uid.my8b6) result = Standard_False;
361
362 return result;
363}
364
365Standard_Boolean Standard_GUID::IsNotSame(const Standard_GUID& uid) const
366{
367 return !IsSame(uid);
368}
369
370void Standard_GUID::Assign(const Standard_GUID& uid)
371{
372 my32b = uid.my32b;
373 my16b1 = uid.my16b1;
374 my16b2 = uid.my16b2;
375 my16b3 = uid.my16b3;
376 my8b1 = uid.my8b1;
377 my8b2 = uid.my8b2;
378 my8b3 = uid.my8b3;
379 my8b4 = uid.my8b4;
380 my8b5 = uid.my8b5;
381 my8b6 = uid.my8b6;
382}
383
384void Standard_GUID::ShallowDump(Standard_OStream& aStream) const
385{
386 Standard_Character sguid[Standard_GUID_SIZE_ALLOC];
6b09c923 387 ToCString(sguid);
7fd59977 388 aStream << sguid;
389}
390
391Standard_Integer Standard_GUID::HashCode(const Standard_GUID& aGuid,const Standard_Integer Upper)
392{
6b09c923 393 return aGuid.Hash(Upper);
7fd59977 394}
395
396Standard_Integer Standard_GUID::Hash(const Standard_Integer Upper) const
397{
398 if (Upper < 1){
9775fa61 399 throw Standard_RangeError("Standard_GUID::Hash: Try to apply HashCode method with negative or null argument.");
7fd59977 400 }
401
6b09c923
R
402 char sguid[Standard_GUID_SIZE_ALLOC];
403 ToCString(sguid);
7fd59977 404
6b09c923 405 return ::HashCode(sguid,Upper);
7fd59977 406}
407
408Standard_Boolean Standard_GUID::IsEqual(const Standard_GUID& aGuid1,const Standard_GUID& aGuid2)
409{
410 return (aGuid1 == aGuid2);
411}
412