0025616: Avoid Classes using "new" to allocate Instances but not defining a copy...
[occt.git] / src / LDOM / LDOM_OSStream.cxx
1 // Created on: 2001-10-01
2 // Created by: Julia DOROVSKIKH
3 // Copyright (c) 2001-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License 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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <LDOM_OSStream.hxx>
17 #include <NCollection_IncAllocator.hxx>
18 #include <string.h>
19 #include <Standard.hxx>
20 #include <Standard_Integer.hxx>
21
22 //=======================================================================
23 //function : LDOM_StringElem()
24 //purpose  : Constructor
25 //=======================================================================
26 LDOM_SBuffer::LDOM_StringElem::LDOM_StringElem
27   ( const int theLength, const Handle_NCollection_BaseAllocator& theAlloc )
28 : buf (reinterpret_cast<char*>(theAlloc->Allocate (theLength))),
29   len (0),
30   next(0)
31 {
32 }
33
34 //=======================================================================
35 //function : LDOM_SBuffer()
36 //purpose  : 
37 //=======================================================================
38 LDOM_SBuffer::LDOM_SBuffer (const Standard_Integer theMaxBuf)
39      : myMaxBuf (theMaxBuf), myLength(0),
40        myAlloc (new NCollection_IncAllocator)
41 {
42   myFirstString = new (myAlloc) LDOM_StringElem (theMaxBuf, myAlloc);
43   myCurString   = myFirstString;
44 }
45
46 //=======================================================================
47 //function : ~LDOM_SBuffer()
48 //purpose  : 
49 //=======================================================================
50 LDOM_SBuffer::~LDOM_SBuffer ()
51 {
52   //no destruction is required as IncAllocator is used
53 }
54
55 //=======================================================================
56 //function : Clear()
57 //purpose  : 
58 //=======================================================================
59 void LDOM_SBuffer::Clear ()
60 {
61   myAlloc       = new NCollection_IncAllocator;
62   myFirstString = new (myAlloc) LDOM_StringElem (myMaxBuf, myAlloc);
63   myLength      = 0;
64   myCurString   = myFirstString;
65 }
66
67 //=======================================================================
68 //function : str()
69 //purpose  : 
70 //=======================================================================
71 Standard_CString LDOM_SBuffer::str () const
72 {
73   char* aRetStr = new char [myLength + 1];
74
75   LDOM_StringElem* aCurElem = myFirstString;
76   int aCurLen = 0;
77   while (aCurElem)
78   {
79     strncpy(aRetStr + aCurLen, aCurElem->buf, aCurElem->len);
80     aCurLen += aCurElem->len;
81     aCurElem = aCurElem->next;
82   }
83   *(aRetStr + myLength) = '\0';
84
85   return aRetStr;
86 }
87
88 //=======================================================================
89 //function : overflow()
90 //purpose  : redefined virtual
91 //=======================================================================
92 int LDOM_SBuffer::overflow(int c)
93 {
94   char cc = (char)c;
95   return xsputn(&cc,1);
96 }
97
98 //=======================================================================
99 //function : underflow
100 //purpose  : redefined virtual
101 //=======================================================================
102
103 int LDOM_SBuffer::underflow()
104 {
105   return EOF;
106 }
107
108 //int LDOM_SBuffer::uflow()
109 //{ return EOF; }
110
111 //=======================================================================
112 //function : xsputn()
113 //purpose  : redefined virtual
114 //=======================================================================
115 int LDOM_SBuffer::xsputn(const char* aStr, int n)
116 {
117   int aLen = n + 1;
118   int freeLen = myMaxBuf - myCurString->len - 1;
119   if (freeLen >= n)
120   {
121     strncpy(myCurString->buf + myCurString->len, aStr, aLen);
122   }
123   else if (freeLen <= 0)
124   {
125     LDOM_StringElem* aNextElem = new (myAlloc) LDOM_StringElem(Max(aLen, myMaxBuf), myAlloc);
126     myCurString->next = aNextElem;
127     myCurString = aNextElem;
128     strncpy(myCurString->buf + myCurString->len, aStr, aLen);
129   }
130   else // 0 < freeLen < n
131   {
132     // copy string by parts
133     strncpy(myCurString->buf + myCurString->len, aStr, freeLen);
134     myCurString->len += freeLen;
135     *(myCurString->buf + myCurString->len) = '\0';
136     aLen -= freeLen;
137     LDOM_StringElem* aNextElem = new (myAlloc) LDOM_StringElem(Max(aLen, myMaxBuf), myAlloc);
138     myCurString->next = aNextElem;
139     myCurString = aNextElem;
140     strncpy(myCurString->buf + myCurString->len, aStr + freeLen, aLen);
141   }
142   myCurString->len += aLen - 1;
143   *(myCurString->buf + myCurString->len) = '\0';
144
145   myLength += n;
146   return n;
147 }
148
149 //streamsize LDOM_SBuffer::xsgetn(char* s, streamsize n)
150 //{ return _IO_default_xsgetn(this, s, n); }
151
152 //=======================================================================
153 //function : LDOM_OSStream()
154 //purpose  : Constructor
155 //=======================================================================
156 LDOM_OSStream::LDOM_OSStream (const Standard_Integer theMaxBuf)
157      : Standard_OStream (&myBuffer), myBuffer (theMaxBuf)
158 {
159   init(&myBuffer);
160 }