0026990: Compiler warnings in LDOM_OSStream.hxx
[occt.git] / src / LDOM / LDOM_OSStream.cxx
CommitLineData
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//
d5f74e42 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
973c2be1 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>
70167e69 17#include <NCollection_IncAllocator.hxx>
e071e038 18#include <Standard_Assert.hxx>
19
7fd59977 20#include <string.h>
7fd59977 21
6a38ff48 22//=======================================================================
23//function : LDOM_StringElem()
24//purpose : Constructor
25//=======================================================================
26LDOM_SBuffer::LDOM_StringElem::LDOM_StringElem
35c0599a 27 ( const int theLength, const Handle(NCollection_BaseAllocator)& theAlloc )
6a38ff48 28: buf (reinterpret_cast<char*>(theAlloc->Allocate (theLength))),
29 len (0),
30 next(0)
7fd59977 31{
6a38ff48 32}
7fd59977 33
34//=======================================================================
35//function : LDOM_SBuffer()
36//purpose :
37//=======================================================================
38LDOM_SBuffer::LDOM_SBuffer (const Standard_Integer theMaxBuf)
70167e69
RL
39 : myMaxBuf (theMaxBuf), myLength(0),
40 myAlloc (new NCollection_IncAllocator)
7fd59977 41{
70167e69
RL
42 myFirstString = new (myAlloc) LDOM_StringElem (theMaxBuf, myAlloc);
43 myCurString = myFirstString;
7fd59977 44}
45
46//=======================================================================
47//function : ~LDOM_SBuffer()
48//purpose :
49//=======================================================================
50LDOM_SBuffer::~LDOM_SBuffer ()
51{
70167e69 52 //no destruction is required as IncAllocator is used
7fd59977 53}
54
55//=======================================================================
56//function : Clear()
57//purpose :
58//=======================================================================
59void LDOM_SBuffer::Clear ()
60{
70167e69
RL
61 myAlloc = new NCollection_IncAllocator;
62 myFirstString = new (myAlloc) LDOM_StringElem (myMaxBuf, myAlloc);
63 myLength = 0;
64 myCurString = myFirstString;
7fd59977 65}
66
67//=======================================================================
68//function : str()
69//purpose :
70//=======================================================================
71Standard_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//=======================================================================
92int LDOM_SBuffer::overflow(int c)
93{
8263fcd3 94 char cc = (char)c;
e071e038 95 xsputn(&cc,1);
96 return c;
7fd59977 97}
98
99//=======================================================================
100//function : underflow
101//purpose : redefined virtual
102//=======================================================================
103
104int LDOM_SBuffer::underflow()
105{
106 return EOF;
107}
108
109//int LDOM_SBuffer::uflow()
110//{ return EOF; }
111
112//=======================================================================
113//function : xsputn()
114//purpose : redefined virtual
115//=======================================================================
e071e038 116std::streamsize LDOM_SBuffer::xsputn (const char* aStr, std::streamsize n)
7fd59977 117{
e071e038 118 Standard_ASSERT_RAISE (n < IntegerLast(), "LDOM_SBuffer cannot work with strings greater than 2 Gb");
119
120 Standard_Integer aLen = static_cast<int>(n) + 1;
121 Standard_Integer freeLen = myMaxBuf - myCurString->len - 1;
7fd59977 122 if (freeLen >= n)
123 {
124 strncpy(myCurString->buf + myCurString->len, aStr, aLen);
125 }
126 else if (freeLen <= 0)
127 {
70167e69 128 LDOM_StringElem* aNextElem = new (myAlloc) LDOM_StringElem(Max(aLen, myMaxBuf), myAlloc);
7fd59977 129 myCurString->next = aNextElem;
130 myCurString = aNextElem;
131 strncpy(myCurString->buf + myCurString->len, aStr, aLen);
132 }
133 else // 0 < freeLen < n
134 {
135 // copy string by parts
136 strncpy(myCurString->buf + myCurString->len, aStr, freeLen);
137 myCurString->len += freeLen;
138 *(myCurString->buf + myCurString->len) = '\0';
139 aLen -= freeLen;
70167e69 140 LDOM_StringElem* aNextElem = new (myAlloc) LDOM_StringElem(Max(aLen, myMaxBuf), myAlloc);
7fd59977 141 myCurString->next = aNextElem;
142 myCurString = aNextElem;
143 strncpy(myCurString->buf + myCurString->len, aStr + freeLen, aLen);
144 }
145 myCurString->len += aLen - 1;
146 *(myCurString->buf + myCurString->len) = '\0';
147
e071e038 148 myLength += static_cast<int>(n);
7fd59977 149 return n;
150}
151
152//streamsize LDOM_SBuffer::xsgetn(char* s, streamsize n)
153//{ return _IO_default_xsgetn(this, s, n); }
154
155//=======================================================================
156//function : LDOM_OSStream()
157//purpose : Constructor
158//=======================================================================
159LDOM_OSStream::LDOM_OSStream (const Standard_Integer theMaxBuf)
160 : Standard_OStream (&myBuffer), myBuffer (theMaxBuf)
161{
162 init(&myBuffer);
163}