b311480e |
1 | // Created on: 2001-10-01 |
2 | // Created by: Julia DOROVSKIKH |
3 | // Copyright (c) 2001-2012 OPEN CASCADE SAS |
4 | // |
5 | // The content of this file is subject to the Open CASCADE Technology Public |
6 | // License Version 6.5 (the "License"). You may not use the content of this file |
7 | // except in compliance with the License. Please obtain a copy of the License |
8 | // at http://www.opencascade.org and read it completely before using this file. |
9 | // |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
12 | // |
13 | // The Original Code and all software distributed under the License is |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
15 | // Initial Developer hereby disclaims all such warranties, including without |
16 | // limitation, any warranties of merchantability, fitness for a particular |
17 | // purpose or non-infringement. Please see the License for the specific terms |
18 | // and conditions governing the rights and limitations under the License. |
19 | |
7fd59977 |
20 | |
21 | #include <LDOM_OSStream.hxx> |
22 | #include <string.h> |
23 | #include <Standard_Integer.hxx> |
24 | |
25 | // One element of sequence |
26 | class LDOM_StringElem |
27 | { |
28 | char* buf; // pointer on data string |
29 | int len; // quantity of really written data |
30 | LDOM_StringElem* next; // pointer on the next element of a sequence |
31 | |
32 | LDOM_StringElem (int aLen) |
33 | { |
34 | buf = new char[aLen]; |
35 | len = 0; |
36 | next = 0; |
37 | } |
38 | |
39 | ~LDOM_StringElem () |
40 | { |
41 | delete [] buf; |
42 | if (next) delete next; |
43 | } |
44 | friend class LDOM_SBuffer; |
45 | }; |
46 | |
47 | //======================================================================= |
48 | //function : LDOM_SBuffer() |
49 | //purpose : |
50 | //======================================================================= |
51 | LDOM_SBuffer::LDOM_SBuffer (const Standard_Integer theMaxBuf) |
52 | : myMaxBuf (theMaxBuf), myLength(0) |
53 | { |
54 | myFirstString = new LDOM_StringElem (theMaxBuf); |
55 | myCurString = myFirstString; |
56 | } |
57 | |
58 | //======================================================================= |
59 | //function : ~LDOM_SBuffer() |
60 | //purpose : |
61 | //======================================================================= |
62 | LDOM_SBuffer::~LDOM_SBuffer () |
63 | { |
64 | if (myFirstString) delete myFirstString; |
65 | } |
66 | |
67 | //======================================================================= |
68 | //function : Clear() |
69 | //purpose : |
70 | //======================================================================= |
71 | void LDOM_SBuffer::Clear () |
72 | { |
73 | if (myFirstString->next) delete myFirstString->next; |
74 | myFirstString->next = 0; |
75 | myFirstString->len = 0; |
76 | myLength = 0; |
77 | myCurString = myFirstString; |
78 | } |
79 | |
80 | //======================================================================= |
81 | //function : str() |
82 | //purpose : |
83 | //======================================================================= |
84 | Standard_CString LDOM_SBuffer::str () const |
85 | { |
86 | char* aRetStr = new char [myLength + 1]; |
87 | |
88 | LDOM_StringElem* aCurElem = myFirstString; |
89 | int aCurLen = 0; |
90 | while (aCurElem) |
91 | { |
92 | strncpy(aRetStr + aCurLen, aCurElem->buf, aCurElem->len); |
93 | aCurLen += aCurElem->len; |
94 | aCurElem = aCurElem->next; |
95 | } |
96 | *(aRetStr + myLength) = '\0'; |
97 | |
98 | return aRetStr; |
99 | } |
100 | |
101 | //======================================================================= |
102 | //function : overflow() |
103 | //purpose : redefined virtual |
104 | //======================================================================= |
105 | int LDOM_SBuffer::overflow(int c) |
106 | { |
8263fcd3 |
107 | char cc = (char)c; |
7fd59977 |
108 | return xsputn(&cc,1); |
109 | } |
110 | |
111 | //======================================================================= |
112 | //function : underflow |
113 | //purpose : redefined virtual |
114 | //======================================================================= |
115 | |
116 | int LDOM_SBuffer::underflow() |
117 | { |
118 | return EOF; |
119 | } |
120 | |
121 | //int LDOM_SBuffer::uflow() |
122 | //{ return EOF; } |
123 | |
124 | //======================================================================= |
125 | //function : xsputn() |
126 | //purpose : redefined virtual |
127 | //======================================================================= |
128 | int LDOM_SBuffer::xsputn(const char* aStr, int n) |
129 | { |
130 | int aLen = n + 1; |
131 | int freeLen = myMaxBuf - myCurString->len - 1; |
132 | if (freeLen >= n) |
133 | { |
134 | strncpy(myCurString->buf + myCurString->len, aStr, aLen); |
135 | } |
136 | else if (freeLen <= 0) |
137 | { |
138 | LDOM_StringElem* aNextElem = new LDOM_StringElem(Max(aLen, myMaxBuf)); |
139 | myCurString->next = aNextElem; |
140 | myCurString = aNextElem; |
141 | strncpy(myCurString->buf + myCurString->len, aStr, aLen); |
142 | } |
143 | else // 0 < freeLen < n |
144 | { |
145 | // copy string by parts |
146 | strncpy(myCurString->buf + myCurString->len, aStr, freeLen); |
147 | myCurString->len += freeLen; |
148 | *(myCurString->buf + myCurString->len) = '\0'; |
149 | aLen -= freeLen; |
150 | LDOM_StringElem* aNextElem = new LDOM_StringElem(Max(aLen, myMaxBuf)); |
151 | myCurString->next = aNextElem; |
152 | myCurString = aNextElem; |
153 | strncpy(myCurString->buf + myCurString->len, aStr + freeLen, aLen); |
154 | } |
155 | myCurString->len += aLen - 1; |
156 | *(myCurString->buf + myCurString->len) = '\0'; |
157 | |
158 | myLength += n; |
159 | return n; |
160 | } |
161 | |
162 | //streamsize LDOM_SBuffer::xsgetn(char* s, streamsize n) |
163 | //{ return _IO_default_xsgetn(this, s, n); } |
164 | |
165 | //======================================================================= |
166 | //function : LDOM_OSStream() |
167 | //purpose : Constructor |
168 | //======================================================================= |
169 | LDOM_OSStream::LDOM_OSStream (const Standard_Integer theMaxBuf) |
170 | : Standard_OStream (&myBuffer), myBuffer (theMaxBuf) |
171 | { |
172 | init(&myBuffer); |
173 | } |