9b92084e51afa6b114a807c12f8d690ee91fa180
[occt.git] / src / Interface / Interface_LineBuffer.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 #include <Interface_LineBuffer.ixx>
19 #include <Standard_OutOfRange.hxx>
20
21
22
23 //  LineBuffer, c est une String avec une Longueur reservee fixe au depart
24 //  et une longueur effective <thelen>. theline(thelen+1) vaut '\0'
25
26
27
28 Interface_LineBuffer::Interface_LineBuffer (const Standard_Integer size)
29     : theline (size+1,' ')
30 {
31   theline.SetValue(1,'\0');
32   themax = size;  theinit = thelen = theget = thekeep = thefriz = 0;
33 }
34
35     void  Interface_LineBuffer::SetMax (const Standard_Integer max)
36 {
37   if (max > theline.Length()) Standard_OutOfRange::Raise
38     ("Interface LineBuffer : SetMax");
39   if (max <= 0) themax = theline.Length();
40   else themax = max;
41 }
42
43
44     void  Interface_LineBuffer::SetInitial (const Standard_Integer initial)
45 {
46   if (thefriz > 0) return;
47   if (initial >= themax) Standard_OutOfRange::Raise
48     ("Interface LineBuffer : SetInitial");
49   if (initial <= 0) theinit = 0;
50   else theinit = initial;
51 }
52
53     void  Interface_LineBuffer::SetKeep ()
54       {  thekeep = -thelen;  }
55
56     Standard_Boolean  Interface_LineBuffer::CanGet (const Standard_Integer more)
57 {
58   theget = more;
59   if ((thelen + theinit + more) <= themax) return Standard_True;
60   if (thekeep < 0) thekeep = -thekeep;
61   return Standard_False;
62 }
63
64     Standard_CString  Interface_LineBuffer::Content () const 
65       {  return theline.ToCString();  }
66
67     Standard_Integer  Interface_LineBuffer::Length () const 
68       {  return thelen + theinit;  }  // +theinit : longueur vraie avec blancs
69
70     void  Interface_LineBuffer::FreezeInitial ()
71       {  thefriz = theinit+1;  theinit = 0;  }
72
73     void  Interface_LineBuffer::Clear ()
74 {
75   theget = thekeep = thelen = thefriz = 0;
76   theline.SetValue(1,'\0');
77 }
78
79 // ....                        RESULTATS                        ....
80
81     void  Interface_LineBuffer::Prepare ()
82 {
83 //  ATTENTION aux blanx initiaux
84   if (theinit > 0) {
85     Standard_Integer i; // svv Jan11 2000 : porting on DEC
86     // pdn Protection
87     if( (thelen +theinit) > themax)
88       return;
89     
90     for (i = thelen + 1; i > 0; i --) {
91       theline.SetValue(i + theinit, theline.Value(i));
92     }
93     for (i = 1; i <= theinit; i ++)  theline.SetValue(i,' ');
94   }
95 //  GERER KEEP : est-il jouable ? sinon, annuler. sioui, noter la jointure
96   if (thekeep > 0) thekeep += (theinit+1);  // theinit, et +1 car Keep INCLUS
97   if (thekeep > 0)
98     {  if ((thelen + theget + theinit - thekeep) >= themax) thekeep = 0;  }
99   if (thekeep > 0)
100     { thekept = theline.Value(thekeep); theline.SetValue(thekeep,'\0');  }
101 }
102
103     void  Interface_LineBuffer::Keep ()
104 {
105 //  Si Keep, sauver de thekeep + 1  a  thelen (+1 pour 0 final)
106   if (thekeep > 0) {
107     theline.SetValue(1,thekept);
108     for (Standard_Integer i = thekeep+1; i <= thelen+theinit+1; i ++) {
109       theline.SetValue(i-thekeep+1, theline.Value(i));
110     }
111     thelen = thelen+theinit-thekeep+1;
112   }
113   else Clear();
114   theget = thekeep = 0;
115   if (thefriz > 0) {  theinit = thefriz - 1;  thefriz = 0;  }
116 }
117
118
119     void  Interface_LineBuffer::Move (TCollection_AsciiString& str)
120 {
121   Prepare();
122   str.AssignCat(theline.ToCString());
123   Keep();
124 }
125
126     void  Interface_LineBuffer::Move (const Handle(TCollection_HAsciiString)& str)
127 {
128   Prepare();
129   str->AssignCat(theline.ToCString());
130   Keep();
131 }
132
133     Handle(TCollection_HAsciiString)  Interface_LineBuffer::Moved ()
134 {
135   Prepare();
136   Handle(TCollection_HAsciiString) R =
137     new TCollection_HAsciiString(theline.ToCString());
138   Keep();
139   return R;
140 }
141
142 // ....                        AJOUTS                        ....
143
144     void  Interface_LineBuffer::Add (const Standard_CString text)
145       {  Add (text,strlen(text));  }
146
147     void  Interface_LineBuffer::Add
148   (const Standard_CString text, const Standard_Integer lntext)
149 {
150   Standard_Integer lnt =
151     (lntext > (themax-thelen-theinit) ? (themax-thelen-theinit) : lntext);
152   for (Standard_Integer i = 1; i <= lnt; i ++)
153     theline.SetValue (thelen+i, text[i-1]);
154   thelen += lnt;
155   theline.SetValue (thelen+1, '\0');
156 }
157
158     void  Interface_LineBuffer::Add (const TCollection_AsciiString& text)
159       {  Add ( text.ToCString() , text.Length() );  }
160
161     void  Interface_LineBuffer::Add (const Standard_Character text)
162 {
163   theline.SetValue (thelen+1,text);
164   thelen ++;
165   theline.SetValue (thelen+1,'\0');
166 }