0020716: Eliminate usage of "config.h" header file
[occt.git] / src / PCollection / PCollection_HExtendedString.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <PCollection_HExtendedString.ixx>
16 #include <PCollection_HAsciiString.hxx>
17 #include <Standard_ExtString.hxx>
18 #include <Standard_NumericError.hxx>
19 #include <Standard_NegativeValue.hxx>
20 #include <Standard_OutOfRange.hxx>
21
22 #include <stdio.h>
23
24 //------------------------------------------------------------------------
25 //  Create from an ExtendedString of TCollection
26 //------------------------------------------------------------------------
27 PCollection_HExtendedString::PCollection_HExtendedString
28                   (const TCollection_ExtendedString& S):Data(S.Length())
29 {
30    for( Standard_Integer i = 1; i <= Data.Length() ; i++) 
31                             Data.SetValue(i-1, S.Value(i)) ;   
32 }
33
34 //------------------------------------------------------------------------
35 //  Create from a ExtCharacter
36 //------------------------------------------------------------------------
37 PCollection_HExtendedString::PCollection_HExtendedString
38             (const Standard_ExtCharacter C): Data(1)
39 {
40    Data.SetValue(0, C);
41 }
42
43 //------------------------------------------------------------------------
44 //  Create from a range of an HExtendedString of PCollection
45 //------------------------------------------------------------------------
46 PCollection_HExtendedString::PCollection_HExtendedString 
47   (const Handle(PCollection_HExtendedString)& S, 
48    const Standard_Integer FromIndex, const Standard_Integer ToIndex) : Data(ToIndex-FromIndex+1)
49 {
50    for( Standard_Integer i = 0 , k = FromIndex; i < Data.Length() ; i++, k++) 
51                         Data.SetValue(i, S->Value(k));
52 }
53
54 //-----------------------------------------------------------------------
55 // Create : from a CString
56 //-----------------------------------------------------------------------
57 PCollection_HExtendedString::PCollection_HExtendedString(const Standard_CString S)
58                                                : Data((Standard_Integer) strlen(S))
59 {
60    for( Standard_Integer i = 0 ; i < Data.Length() ; i++)  {
61          Standard_ExtCharacter val = ToExtCharacter(S[i]);
62          Data.SetValue(i, val) ;
63    }
64 }
65
66 //------------------------------------------------------------------------
67 //  Create from an HAsciiString from PCollection
68 //------------------------------------------------------------------------
69 PCollection_HExtendedString::PCollection_HExtendedString 
70           (const Handle(PCollection_HAsciiString)& S) : Data(S->Length())
71  {
72    for( Standard_Integer i = 1; i <= Data.Length() ; i++)  {
73 //     convert the character i of S
74        Standard_ExtCharacter val = ToExtCharacter(S->Value(i)) ;
75        Data.SetValue(i-1,val );   
76    }   
77 }
78
79 //------------------------------------------------------------------------
80 //  Append
81 //------------------------------------------------------------------------
82 void PCollection_HExtendedString::Append 
83                    (const Handle(PCollection_HExtendedString)& S)
84 {
85    InsertAfter(Length(),S);
86 }
87
88 //------------------------------------------------------------------------
89 //  Center
90 //------------------------------------------------------------------------
91 void PCollection_HExtendedString::Center 
92            (const Standard_Integer Width, const Standard_ExtCharacter Filler)
93 {
94    if (Width < 0) Standard_NegativeValue::Raise();
95    Standard_Integer size1 = Length();
96    if(Width > size1) {
97       Standard_Integer size2 = size1 + ((Width - size1)/2);
98       LeftJustify(size2,Filler);
99       RightJustify(Width,Filler);
100    }
101 }
102
103 //------------------------------------------------------------------------
104 //  ChangeAll
105 //------------------------------------------------------------------------
106 void PCollection_HExtendedString::ChangeAll 
107      (const Standard_ExtCharacter C, const Standard_ExtCharacter NewC)
108 {
109    for( Standard_Integer i = 0 ; i < Data.Length(); i++) {
110       if(Data(i) == C) Data.SetValue(i, NewC);
111    }
112 }
113
114 //------------------------------------------------------------------------
115 //  Clear
116 //------------------------------------------------------------------------
117 void PCollection_HExtendedString::Clear ()
118 {
119    Data.Resize(0);
120 }
121
122 //------------------------------------------------------------------------
123 //  Convert
124 //------------------------------------------------------------------------
125 TCollection_ExtendedString PCollection_HExtendedString::Convert() const
126 {
127    Standard_Integer L = Length();
128    TCollection_ExtendedString TString (L,' ');
129    for (Standard_Integer i = 1 ; i <= L ; i++) {
130       TString.SetValue(i,Value(i));
131    }
132    return TString;
133 }
134
135 //------------------------------------------------------------------------
136 //  FirstLocationInSet
137 //------------------------------------------------------------------------
138 Standard_Integer PCollection_HExtendedString::FirstLocationInSet 
139               (const Handle(PCollection_HExtendedString)& Set, 
140                const Standard_Integer FromIndex, 
141                const Standard_Integer ToIndex) const
142 {
143    if (Length() == 0 || Set->Length() == 0) return 0;
144    if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex ) 
145                      Standard_OutOfRange::Raise();
146    for(Standard_Integer i = FromIndex-1 ; i <= ToIndex-1; i++)
147      for(Standard_Integer j = 1; j <= Set->Length(); j++) 
148        if(Data(i) == Set->Value(j)) return (i+1);
149    return 0;
150 }
151
152 //------------------------------------------------------------------------
153 //  FirstLocationNotInset
154 //------------------------------------------------------------------------
155 Standard_Integer PCollection_HExtendedString::FirstLocationNotInSet 
156       (const Handle(PCollection_HExtendedString)& Set, const Standard_Integer FromIndex, 
157        const Standard_Integer ToIndex) const
158 {
159    if (Length() == 0 || Set->Length() == 0) return 0;
160    if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex ) 
161                      Standard_OutOfRange::Raise();
162    Standard_Boolean find;
163    for(Standard_Integer i = FromIndex-1 ; i <= ToIndex-1; i++) {
164      find = Standard_False;
165      for(Standard_Integer j = 1; j <= Set->Length(); j++) { 
166        if (Data(i) == Set->Value(j)) find = Standard_True;
167      }
168      if (!find)  return (i+1);
169    }
170    return 0;
171 }
172
173 //------------------------------------------------------------------------
174 //  InsertAfter
175 //------------------------------------------------------------------------
176 void PCollection_HExtendedString::InsertAfter 
177            (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
178 {
179    Standard_Integer i ;
180    Standard_Integer size1 = Length();
181    Standard_Integer size2 = S->Length();
182 #ifndef NOBOUNDCHECK
183    if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
184 #endif
185    Data.Resize(size1+size2);
186    for( i = size1-1 ; i >= Index; i--) Data.SetValue(size2+i,Data(i));
187    for( i = 1 ; i <= size2; i++) Data.SetValue(Index+i-1,S->Value(i));
188 }
189
190 //------------------------------------------------------------------------
191 //  InsertBefore
192 //------------------------------------------------------------------------
193 void PCollection_HExtendedString::InsertBefore 
194            (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
195 {
196    Standard_Integer i ;
197    Standard_Integer size1 = Length();
198    Standard_Integer size2 = S->Length();
199 #ifndef NOBOUNDCHECK
200    if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
201 #endif
202    Data.Resize(size1+size2);
203    for( i = size1-1 ; i >= Index-1 ; i--) 
204                                         Data.SetValue(size2+i,Data(i));
205   for( i = 1 ; i <= size2; i++) Data.SetValue(Index+i-2, S->Value(i));
206 }
207
208 //------------------------------------------------------------------------
209 //  IsAscii
210 //------------------------------------------------------------------------
211 Standard_Boolean PCollection_HExtendedString::IsAscii() const
212 {
213    for( Standard_Integer i = 0; i < Data.Length() ; i++)  {
214              if (!IsAnAscii(Data(i))) return Standard_False;
215    }
216    return Standard_True;
217 }
218
219 //------------------------------------------------------------------------
220 //  IsDifferent
221 //------------------------------------------------------------------------
222 Standard_Boolean PCollection_HExtendedString::IsDifferent 
223                         (const Handle(PCollection_HExtendedString)& S) const
224 {
225    Standard_Integer size = Length();
226    if( size != S->Length()) return Standard_True;
227    Standard_Integer i = 1 ;
228    Standard_Boolean different = Standard_False;
229    while (i <= size && !different) {
230       if (Data(i-1) != S->Value(i)) different = Standard_True;
231       i++;
232    }
233    return different;
234 }
235
236 //------------------------------------------------------------------------
237 //  IsEmpty
238 //------------------------------------------------------------------------
239 Standard_Boolean PCollection_HExtendedString::IsEmpty () const
240 {
241    return (Data.Length() == 0);
242 }
243
244 // ----------------------------------------------------------------------------
245 // IsLess
246 // ----------------------------------------------------------------------------
247 Standard_Boolean PCollection_HExtendedString::IsLess(
248         const Handle(PCollection_HExtendedString)& other) const
249 {
250    Standard_Integer mysize = Data.Length();
251    Standard_Integer size = other->Length();
252    Standard_Integer i = 0;
253    Standard_Integer j = 1;
254    while (i < mysize && j <= size) {
255       if (Data(i) < other->Value(j)) return (Standard_True);
256       if (Data(i) > other->Value(j)) return (Standard_False);
257       i++;
258       j++;
259    }
260    if (i == mysize && j <= size) return (Standard_True);
261    return (Standard_False);
262 }
263
264 // ----------------------------------------------------------------------------
265 // IsGreater
266 // ----------------------------------------------------------------------------
267 Standard_Boolean PCollection_HExtendedString::IsGreater
268     (const Handle(PCollection_HExtendedString)& other) const
269 {
270    Standard_Integer mysize = Data.Length();
271    Standard_Integer size = other->Length();
272    Standard_Integer i = 0;
273    Standard_Integer j = 1;
274    while (i < mysize && j <= size) {
275       if (Data(i) < other->Value(j)) return (Standard_False);
276       if (Data(i) > other->Value(j)) return (Standard_True);
277       i++;
278       j++;
279    }
280    if (j == size && j < mysize) return (Standard_True);
281    return (Standard_False);
282 }
283
284 //------------------------------------------------------------------------
285 //  IsSameString
286 //------------------------------------------------------------------------
287 Standard_Boolean PCollection_HExtendedString::IsSameString 
288                         (const Handle(PCollection_HExtendedString)& S) const
289 {
290    Standard_Integer size1 = Length();
291    if( size1 != S->Length()) return Standard_False;
292    for( Standard_Integer i = 1 ; i <= size1; i++) {
293      if(Data(i-1) != S->Value(i)) return Standard_False;
294    }
295    return Standard_True;
296 }
297
298 //------------------------------------------------------------------------
299 //  LeftAdjust
300 //------------------------------------------------------------------------
301 void PCollection_HExtendedString::LeftAdjust ()
302 {
303    Standard_Integer i ;
304    
305    if (!IsAscii()) Standard_OutOfRange::Raise();
306    for ( i = 1 ; i <= Length() ; i ++) {
307       if (!IsSpace((Standard_Character)Value(i))) break;
308    }
309    if( i > 1 ) Remove(1,i-1);
310 }
311
312 //------------------------------------------------------------------------
313 //  LeftJustify
314 //------------------------------------------------------------------------
315 void PCollection_HExtendedString::LeftJustify 
316      (const Standard_Integer Width, const Standard_ExtCharacter Filler)
317 {
318    if (Width < 0) Standard_NegativeValue::Raise();
319    Standard_Integer size1 = Length();
320    if(Width > size1) {
321      Data.Resize(Width);
322      for(Standard_Integer i = size1; i < Width ; i++) Data.SetValue(i, Filler);
323    }
324 }
325
326 //------------------------------------------------------------------------
327 //  Length
328 //------------------------------------------------------------------------
329 Standard_Integer PCollection_HExtendedString::Length () const
330 {
331    return Data.Length();
332 }
333
334 //------------------------------------------------------------------------
335 //  Location
336 //------------------------------------------------------------------------
337 Standard_Integer PCollection_HExtendedString::Location 
338    (const Standard_Integer N, const Standard_ExtCharacter C, 
339     const Standard_Integer FromIndex, const Standard_Integer ToIndex) const
340 {
341    if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex ) 
342                       Standard_OutOfRange::Raise();
343    for(Standard_Integer i = FromIndex-1, icount = 0; i <= ToIndex-1; i++) 
344               if(Data(i) == C) {
345                 icount++;
346                 if ( icount == N ) return (i+1);
347               }
348    return 0 ;
349 }
350
351 //------------------------------------------------------------------------
352 //  Location
353 //------------------------------------------------------------------------
354 Standard_Integer PCollection_HExtendedString::Location 
355         (const Handle(PCollection_HExtendedString)& S, const Standard_Integer FromIndex, 
356          const Standard_Integer ToIndex) const
357 {
358    if (Length() == 0 || S->Length() == 0) return 0;
359    if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex ) 
360                       Standard_OutOfRange::Raise();
361    for(Standard_Integer i = FromIndex-1, k = 1, l = FromIndex-2; i < ToIndex; i++){
362      if(Data(i) == S->Value(k)) {
363        k++;
364        if ( k > S->Length()) return l + 2;
365      }
366      else {
367        k = 1;
368        l = i;
369      }
370   }
371   return 0;
372 }
373
374 //------------------------------------------------------------------------
375 //  Prepend
376 //------------------------------------------------------------------------
377 void PCollection_HExtendedString::Prepend 
378                     (const Handle(PCollection_HExtendedString)& S)
379 {
380    InsertAfter(0,S);
381 }
382
383
384 //------------------------------------------------------------------------
385 //  Print
386 //------------------------------------------------------------------------
387 void PCollection_HExtendedString::Print (Standard_OStream& S) const
388 {
389    Standard_Integer len = Data.Length() ;
390    for(Standard_Integer i = 0; i < len ; i++) {
391        S.width(4);
392        S.fill('0');
393        S << hex << Data(i);
394    }
395 }
396
397 //------------------------------------------------------------------------
398 //  Remove
399 //------------------------------------------------------------------------
400 void PCollection_HExtendedString::Remove (const Standard_Integer Index)
401 {
402    if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
403    Remove(Index,Index);
404 }
405
406 //------------------------------------------------------------------------
407 //  Remove
408 //------------------------------------------------------------------------
409 void PCollection_HExtendedString::Remove 
410     (const Standard_Integer FromIndex, const Standard_Integer ToIndex)
411 {
412    if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex ) 
413                      Standard_OutOfRange::Raise();
414    Standard_Integer size1 = Length();
415    for( Standard_Integer i = ToIndex, j = FromIndex-1; i < size1 ; i++, j++)
416      Data.SetValue(j,Data(i));
417    Data.Resize(size1-(ToIndex-FromIndex+1));
418 }
419
420 //------------------------------------------------------------------------
421 //  RemoveAll
422 //------------------------------------------------------------------------
423 void PCollection_HExtendedString::RemoveAll (const Standard_ExtCharacter C)
424 {
425    Standard_Integer i ;
426    Standard_Integer j ;
427    Standard_Integer size1 = Length();
428    for( i = 0, j = 0; i < size1 ; i++) {
429        if (Data(i) == C) continue;
430        Data.SetValue(j++, Data(i));
431    }
432     Data.Resize(j);
433 }
434
435 //------------------------------------------------------------------------
436 //  RightAdjust
437 //------------------------------------------------------------------------
438 void PCollection_HExtendedString::RightAdjust ()
439 {
440    Standard_Integer i ;
441    if (! IsAscii()) Standard_OutOfRange::Raise();
442    for ( i = Length() ; i >= 1 ; i --) {
443       if (!IsSpace((Standard_Character)Value(i))) break;
444    }
445    if( i < Length() ) Remove(i+1,Length());
446 }
447
448 //------------------------------------------------------------------------
449 //  RightJustify
450 //------------------------------------------------------------------------
451 void PCollection_HExtendedString::RightJustify 
452            (const Standard_Integer Width, const Standard_ExtCharacter Filler)
453 {
454    Standard_Integer i ;
455    Standard_Integer k ;
456    if (Width < 0) Standard_NegativeValue::Raise();
457    Standard_Integer size1 = Length();
458    if(Width > size1) {
459       Data.Resize(Width);
460       for ( i = size1-1, k = Width-1 ; i >= 0 ; i--, k--) 
461                Data.SetValue(k, Data(i));
462       for(; k >= 0 ; k--) Data.SetValue(k, Filler);
463    }
464 }
465
466 //------------------------------------------------------------------------
467 //  SetValue
468 //------------------------------------------------------------------------
469 void PCollection_HExtendedString::SetValue 
470                 (const Standard_Integer Index, const Standard_ExtCharacter C)
471 {
472    if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
473    Data(Index-1) = C;
474 }
475
476 //------------------------------------------------------------------------
477 //  SetValue
478 //------------------------------------------------------------------------
479 void PCollection_HExtendedString::SetValue 
480            (const Standard_Integer Index, const Handle(PCollection_HExtendedString)& S)
481 {
482
483    Standard_Integer size1 = Length();
484    Standard_Integer size2 = S->Length();
485    Standard_Integer size3 = size2 + Index - 1;
486 #ifndef NOBOUNDCHECK
487    if (Index < 0 || Index > size1) Standard_OutOfRange::Raise();
488 #endif
489    if(size1 != size3) Data.Resize(size3);
490    for( Standard_Integer i = 1 ; i <= size2; i++) Data.SetValue(Index+i-2, S->Value(i));
491 }
492
493 //------------------------------------------------------------------------
494 //  Split
495 //------------------------------------------------------------------------
496 Handle(PCollection_HExtendedString) PCollection_HExtendedString::Split 
497              (const Standard_Integer Index)
498 {
499    if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
500    Handle(PCollection_HExtendedString) S2;
501    if (Index != Length()) { 
502       S2 = SubString(Index+1,Length());
503       Data.Resize(Index);
504    }
505    else {
506 #ifndef OBJS
507       Handle(PCollection_HAsciiString) s = new PCollection_HAsciiString("");
508       S2 = new PCollection_HExtendedString(s);
509 #else
510       Handle(PCollection_HAsciiString) s = new (os_segment::of(this)) PCollection_HAsciiString("");
511       S2 = new (os_segment::of(this)) PCollection_HExtendedString(s);
512 #endif
513    }
514    return S2;
515 }
516
517 //------------------------------------------------------------------------
518 //  SubString
519 //------------------------------------------------------------------------
520 Handle(PCollection_HExtendedString) PCollection_HExtendedString::SubString 
521              (const Standard_Integer FromIndex, const Standard_Integer ToIndex) const
522 {
523    if (ToIndex > Length() || FromIndex <= 0 || FromIndex > ToIndex ) 
524                       Standard_OutOfRange::Raise();
525    Handle(PCollection_HExtendedString) S1;
526    Handle(PCollection_HExtendedString) S2;
527    S2 = this;
528 #ifndef OBJS
529    S1 = new PCollection_HExtendedString(S2,FromIndex,ToIndex);
530 #else
531    S1 = new (os_segment::of(this)) PCollection_HExtendedString(S2,FromIndex,ToIndex);
532 #endif
533    return S1;
534 }
535
536 //------------------------------------------------------------------------
537 //  UsefullLength
538 //------------------------------------------------------------------------
539 Standard_Integer PCollection_HExtendedString::UsefullLength () const
540 {
541    Standard_Integer i ;
542   if (! IsAscii()) Standard_OutOfRange::Raise();
543   for( i = Length() ; i >= 1 ; i--) 
544       if (IsGraphic((Standard_Character)Value(i))) break;
545    return (i);
546 }
547
548 //------------------------------------------------------------------------
549 //  value
550 //------------------------------------------------------------------------
551 Standard_ExtCharacter PCollection_HExtendedString::Value 
552                                (const Standard_Integer Index) const
553 {
554    if (Index < 0 || Index > Length()) Standard_OutOfRange::Raise();
555    return Data(Index-1);
556 }
557
558 //------------------------------------------------------------------------
559 //  Assign
560 //------------------------------------------------------------------------
561 void PCollection_HExtendedString::Assign
562               (const DBC_VArrayOfExtCharacter& TheField)
563
564 {
565    Data = TheField;
566 }
567
568
569
570
571
572
573