b311480e |
1 | // Created on: 2001-10-01 |
2 | // Created by: Julia DOROVSKIKH |
973c2be1 |
3 | // Copyright (c) 2001-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
973c2be1 |
7 | // This library is free software; you can redistribute it and / or modify it |
8 | // under the terms of the GNU Lesser General Public version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
16 | #include <LDOM_OSStream.hxx> |
17 | #include <string.h> |
18 | #include <Standard_Integer.hxx> |
19 | |
20 | // One element of sequence |
21 | class LDOM_StringElem |
22 | { |
23 | char* buf; // pointer on data string |
24 | int len; // quantity of really written data |
25 | LDOM_StringElem* next; // pointer on the next element of a sequence |
26 | |
27 | LDOM_StringElem (int aLen) |
28 | { |
29 | buf = new char[aLen]; |
30 | len = 0; |
31 | next = 0; |
32 | } |
33 | |
34 | ~LDOM_StringElem () |
35 | { |
36 | delete [] buf; |
37 | if (next) delete next; |
38 | } |
39 | friend class LDOM_SBuffer; |
40 | }; |
41 | |
42 | //======================================================================= |
43 | //function : LDOM_SBuffer() |
44 | //purpose : |
45 | //======================================================================= |
46 | LDOM_SBuffer::LDOM_SBuffer (const Standard_Integer theMaxBuf) |
47 | : myMaxBuf (theMaxBuf), myLength(0) |
48 | { |
49 | myFirstString = new LDOM_StringElem (theMaxBuf); |
50 | myCurString = myFirstString; |
51 | } |
52 | |
53 | //======================================================================= |
54 | //function : ~LDOM_SBuffer() |
55 | //purpose : |
56 | //======================================================================= |
57 | LDOM_SBuffer::~LDOM_SBuffer () |
58 | { |
59 | if (myFirstString) delete myFirstString; |
60 | } |
61 | |
62 | //======================================================================= |
63 | //function : Clear() |
64 | //purpose : |
65 | //======================================================================= |
66 | void LDOM_SBuffer::Clear () |
67 | { |
68 | if (myFirstString->next) delete myFirstString->next; |
69 | myFirstString->next = 0; |
70 | myFirstString->len = 0; |
71 | myLength = 0; |
72 | myCurString = myFirstString; |
73 | } |
74 | |
75 | //======================================================================= |
76 | //function : str() |
77 | //purpose : |
78 | //======================================================================= |
79 | Standard_CString LDOM_SBuffer::str () const |
80 | { |
81 | char* aRetStr = new char [myLength + 1]; |
82 | |
83 | LDOM_StringElem* aCurElem = myFirstString; |
84 | int aCurLen = 0; |
85 | while (aCurElem) |
86 | { |
87 | strncpy(aRetStr + aCurLen, aCurElem->buf, aCurElem->len); |
88 | aCurLen += aCurElem->len; |
89 | aCurElem = aCurElem->next; |
90 | } |
91 | *(aRetStr + myLength) = '\0'; |
92 | |
93 | return aRetStr; |
94 | } |
95 | |
96 | //======================================================================= |
97 | //function : overflow() |
98 | //purpose : redefined virtual |
99 | //======================================================================= |
100 | int LDOM_SBuffer::overflow(int c) |
101 | { |
8263fcd3 |
102 | char cc = (char)c; |
7fd59977 |
103 | return xsputn(&cc,1); |
104 | } |
105 | |
106 | //======================================================================= |
107 | //function : underflow |
108 | //purpose : redefined virtual |
109 | //======================================================================= |
110 | |
111 | int LDOM_SBuffer::underflow() |
112 | { |
113 | return EOF; |
114 | } |
115 | |
116 | //int LDOM_SBuffer::uflow() |
117 | //{ return EOF; } |
118 | |
119 | //======================================================================= |
120 | //function : xsputn() |
121 | //purpose : redefined virtual |
122 | //======================================================================= |
123 | int LDOM_SBuffer::xsputn(const char* aStr, int n) |
124 | { |
125 | int aLen = n + 1; |
126 | int freeLen = myMaxBuf - myCurString->len - 1; |
127 | if (freeLen >= n) |
128 | { |
129 | strncpy(myCurString->buf + myCurString->len, aStr, aLen); |
130 | } |
131 | else if (freeLen <= 0) |
132 | { |
133 | LDOM_StringElem* aNextElem = new LDOM_StringElem(Max(aLen, myMaxBuf)); |
134 | myCurString->next = aNextElem; |
135 | myCurString = aNextElem; |
136 | strncpy(myCurString->buf + myCurString->len, aStr, aLen); |
137 | } |
138 | else // 0 < freeLen < n |
139 | { |
140 | // copy string by parts |
141 | strncpy(myCurString->buf + myCurString->len, aStr, freeLen); |
142 | myCurString->len += freeLen; |
143 | *(myCurString->buf + myCurString->len) = '\0'; |
144 | aLen -= freeLen; |
145 | LDOM_StringElem* aNextElem = new LDOM_StringElem(Max(aLen, myMaxBuf)); |
146 | myCurString->next = aNextElem; |
147 | myCurString = aNextElem; |
148 | strncpy(myCurString->buf + myCurString->len, aStr + freeLen, aLen); |
149 | } |
150 | myCurString->len += aLen - 1; |
151 | *(myCurString->buf + myCurString->len) = '\0'; |
152 | |
153 | myLength += n; |
154 | return n; |
155 | } |
156 | |
157 | //streamsize LDOM_SBuffer::xsgetn(char* s, streamsize n) |
158 | //{ return _IO_default_xsgetn(this, s, n); } |
159 | |
160 | //======================================================================= |
161 | //function : LDOM_OSStream() |
162 | //purpose : Constructor |
163 | //======================================================================= |
164 | LDOM_OSStream::LDOM_OSStream (const Standard_Integer theMaxBuf) |
165 | : Standard_OStream (&myBuffer), myBuffer (theMaxBuf) |
166 | { |
167 | init(&myBuffer); |
168 | } |