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