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