0029344: Foundation Classes, TCollection_AsciiString - replace confusing strncpy...
[occt.git] / src / Interface / Interface_LineBuffer.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14
15 #include <Interface_LineBuffer.hxx>
16 #include <Standard_OutOfRange.hxx>
17 #include <TCollection_AsciiString.hxx>
18 #include <TCollection_HAsciiString.hxx>
19
20 Interface_LineBuffer::Interface_LineBuffer (const Standard_Integer size)
21 : myLine (1, size+1)
22 {
23   myLine.SetValue (1, '\0');
24   myMax = size;
25   myInit = myLen = myGet = myKeep = myFriz = 0;
26 }
27
28 void Interface_LineBuffer::SetMax (const Standard_Integer theMax)
29 {
30   if (theMax > myLine.Length())
31   {
32     throw Standard_OutOfRange("Interface LineBuffer : SetMax");
33   }
34   if (theMax <= 0)
35   {
36     myMax = myLine.Length();
37   }
38   else
39   {
40     myMax = theMax;
41   }
42 }
43
44 void  Interface_LineBuffer::SetInitial (const Standard_Integer theInitial)
45 {
46   if (myFriz > 0)
47   {
48     return;
49   }
50   if (theInitial >= myMax)
51   {
52     throw Standard_OutOfRange("Interface LineBuffer : SetInitial");
53   }
54   if (theInitial <= 0)
55   {
56     myInit = 0;
57   }
58   else
59   {
60     myInit = theInitial;
61   }
62 }
63
64 void Interface_LineBuffer::SetKeep()
65 {
66   myKeep = -myLen;
67 }
68
69 Standard_Boolean Interface_LineBuffer::CanGet (const Standard_Integer theMore)
70 {
71   myGet = theMore;
72   if ((myLen + myInit + theMore) <= myMax)
73   {
74     return Standard_True;
75   }
76   if (myKeep < 0)
77   {
78     myKeep = -myKeep;
79   }
80   return Standard_False;
81 }
82
83 void Interface_LineBuffer::FreezeInitial()
84 {
85   myFriz = myInit + 1;
86   myInit = 0;
87 }
88
89 void Interface_LineBuffer::Clear()
90 {
91   myGet = myKeep = myLen = myFriz = 0;
92   myLine.SetValue (1, '\0');
93 }
94
95 // ....                        RESULTATS                        ....
96
97 void Interface_LineBuffer::Prepare()
98 {
99 //  ATTENTION aux blanx initiaux
100   if (myInit > 0)
101   {
102     if ((myLen + myInit) > myMax)
103     {
104       return;
105     }
106     
107     for (Standard_Integer i = myLen + 1; i > 0; --i)
108     {
109       myLine.SetValue (i + myInit, myLine.Value (i));
110     }
111     for (Standard_Integer i = 1; i <= myInit; ++i)
112     {
113       myLine.SetValue (i, ' ');
114     }
115   }
116 //  GERER KEEP : est-il jouable ? sinon, annuler. sioui, noter la jointure
117   if (myKeep > 0)
118   {
119     myKeep += (myInit + 1);  // myInit, et +1 car Keep INCLUS
120   }
121   if (myKeep > 0)
122   {
123     if ((myLen + myGet + myInit - myKeep) >= myMax)
124     {
125       myKeep = 0;
126     }
127   }
128   if (myKeep > 0)
129   {
130     myKept = myLine.Value (myKeep);
131     myLine.SetValue (myKeep, '\0');
132   }
133 }
134
135 void Interface_LineBuffer::Keep()
136 {
137 //  Si Keep, sauver de myKeep + 1  a  myLen (+1 pour 0 final)
138   if (myKeep > 0)
139   {
140     myLine.SetValue (1, myKept);
141     for (Standard_Integer i = myKeep + 1; i <= myLen + myInit + 1; ++i)
142     {
143       myLine.SetValue (i - myKeep + 1, myLine.Value (i));
144     }
145     myLen = myLen + myInit - myKeep + 1;
146   }
147   else
148   {
149     Clear();
150   }
151   myGet = myKeep = 0;
152   if (myFriz > 0)
153   {
154     myInit = myFriz - 1;
155     myFriz = 0;
156   }
157 }
158
159 void Interface_LineBuffer::Move (TCollection_AsciiString& theStr)
160 {
161   Prepare();
162   theStr.AssignCat (&myLine.First());
163   Keep();
164 }
165
166 void Interface_LineBuffer::Move (const Handle(TCollection_HAsciiString)& theStr)
167 {
168   Prepare();
169   theStr->AssignCat (&myLine.First());
170   Keep();
171 }
172
173 Handle(TCollection_HAsciiString) Interface_LineBuffer::Moved()
174 {
175   Prepare();
176   Handle(TCollection_HAsciiString) R = new TCollection_HAsciiString (&myLine.First());
177   Keep();
178   return R;
179 }
180
181 // ....                        AJOUTS                        ....
182
183 void Interface_LineBuffer::Add (const Standard_CString theText)
184 {
185   Add (theText, (Standard_Integer )strlen (theText));
186 }
187
188 void Interface_LineBuffer::Add (const Standard_CString text, const Standard_Integer lntext)
189 {
190   Standard_Integer lnt = (lntext > (myMax - myLen - myInit) ? (myMax - myLen - myInit) : lntext);
191   for (Standard_Integer i = 1; i <= lnt; ++i)
192   {
193     myLine.SetValue (myLen + i, text[i-1]);
194   }
195   myLen += lnt;
196   myLine.SetValue (myLen + 1, '\0');
197 }
198
199 void Interface_LineBuffer::Add (const TCollection_AsciiString& theText)
200 {
201   Add (theText.ToCString(), theText.Length());
202 }
203
204 void Interface_LineBuffer::Add (const Standard_Character theText)
205 {
206   myLine.SetValue (myLen + 1, theText);
207   ++myLen;
208   myLine.SetValue (myLen + 1, '\0');
209 }