0022815: Missing delete operator for placement new
[occt.git] / src / LDOM / LDOM_OSStream.cxx
1 // File:      LDOM_OSStream.cxx
2 // Created:   01.10.01 10:57:37
3 // Author:    Julia DOROVSKIKH
4 // Copyright: Open Cascade 2001
5 // History:
6
7 #include <LDOM_OSStream.hxx>
8 #include <string.h>
9 #include <Standard_Integer.hxx>
10
11 // One element of sequence
12 class LDOM_StringElem
13 {
14   char* buf;             // pointer on data string
15   int len;               // quantity of really written data
16   LDOM_StringElem* next; // pointer on the next element of a sequence
17
18   LDOM_StringElem (int aLen)
19   {
20     buf = new char[aLen];
21     len = 0;
22     next = 0;
23   }
24
25   ~LDOM_StringElem ()
26   {
27     delete [] buf;
28     if (next) delete next;
29   }
30 friend class LDOM_SBuffer;
31 };
32
33 //=======================================================================
34 //function : LDOM_SBuffer()
35 //purpose  : 
36 //=======================================================================
37 LDOM_SBuffer::LDOM_SBuffer (const Standard_Integer theMaxBuf)
38      : myMaxBuf (theMaxBuf), myLength(0)
39 {
40   myFirstString = new LDOM_StringElem (theMaxBuf);
41   myCurString = myFirstString;
42 }
43
44 //=======================================================================
45 //function : ~LDOM_SBuffer()
46 //purpose  : 
47 //=======================================================================
48 LDOM_SBuffer::~LDOM_SBuffer ()
49 {
50   if (myFirstString) delete myFirstString;
51 }
52
53 //=======================================================================
54 //function : Clear()
55 //purpose  : 
56 //=======================================================================
57 void LDOM_SBuffer::Clear ()
58 {
59   if (myFirstString->next) delete myFirstString->next;
60   myFirstString->next = 0;
61   myFirstString->len = 0;
62   myLength = 0;
63   myCurString = myFirstString;
64 }
65
66 //=======================================================================
67 //function : str()
68 //purpose  : 
69 //=======================================================================
70 Standard_CString LDOM_SBuffer::str () const
71 {
72   char* aRetStr = new char [myLength + 1];
73
74   LDOM_StringElem* aCurElem = myFirstString;
75   int aCurLen = 0;
76   while (aCurElem)
77   {
78     strncpy(aRetStr + aCurLen, aCurElem->buf, aCurElem->len);
79     aCurLen += aCurElem->len;
80     aCurElem = aCurElem->next;
81   }
82   *(aRetStr + myLength) = '\0';
83
84   return aRetStr;
85 }
86
87 //=======================================================================
88 //function : overflow()
89 //purpose  : redefined virtual
90 //=======================================================================
91 int LDOM_SBuffer::overflow(int c)
92 {
93   char cc = c;
94   return xsputn(&cc,1);
95 }
96
97 //=======================================================================
98 //function : underflow
99 //purpose  : redefined virtual
100 //=======================================================================
101
102 int LDOM_SBuffer::underflow()
103 {
104   return EOF;
105 }
106
107 //int LDOM_SBuffer::uflow()
108 //{ return EOF; }
109
110 //=======================================================================
111 //function : xsputn()
112 //purpose  : redefined virtual
113 //=======================================================================
114 int LDOM_SBuffer::xsputn(const char* aStr, int n)
115 {
116   int aLen = n + 1;
117   int freeLen = myMaxBuf - myCurString->len - 1;
118   if (freeLen >= n)
119   {
120     strncpy(myCurString->buf + myCurString->len, aStr, aLen);
121   }
122   else if (freeLen <= 0)
123   {
124     LDOM_StringElem* aNextElem = new LDOM_StringElem(Max(aLen, myMaxBuf));
125     myCurString->next = aNextElem;
126     myCurString = aNextElem;
127     strncpy(myCurString->buf + myCurString->len, aStr, aLen);
128   }
129   else // 0 < freeLen < n
130   {
131     // copy string by parts
132     strncpy(myCurString->buf + myCurString->len, aStr, freeLen);
133     myCurString->len += freeLen;
134     *(myCurString->buf + myCurString->len) = '\0';
135     aLen -= freeLen;
136     LDOM_StringElem* aNextElem = new LDOM_StringElem(Max(aLen, myMaxBuf));
137     myCurString->next = aNextElem;
138     myCurString = aNextElem;
139     strncpy(myCurString->buf + myCurString->len, aStr + freeLen, aLen);
140   }
141   myCurString->len += aLen - 1;
142   *(myCurString->buf + myCurString->len) = '\0';
143
144   myLength += n;
145   return n;
146 }
147
148 //streamsize LDOM_SBuffer::xsgetn(char* s, streamsize n)
149 //{ return _IO_default_xsgetn(this, s, n); }
150
151 //=======================================================================
152 //function : LDOM_OSStream()
153 //purpose  : Constructor
154 //=======================================================================
155 LDOM_OSStream::LDOM_OSStream (const Standard_Integer theMaxBuf)
156      : Standard_OStream (&myBuffer), myBuffer (theMaxBuf)
157 {
158   init(&myBuffer);
159 }