0031668: Visualization - WebGL sample doesn't work on Emscripten 1.39
[occt.git] / src / Standard / Standard_ArrayStreamBuffer.cxx
1 // Copyright (c) 2016 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <Standard_ArrayStreamBuffer.hxx>
15
16 // =======================================================================
17 // function : Standard_ArrayStreamBuffer
18 // purpose  :
19 // =======================================================================
20 Standard_ArrayStreamBuffer::Standard_ArrayStreamBuffer (const char*  theBegin,
21                                                         const size_t theSize)
22 : myBegin  (theBegin),
23   myEnd    (theBegin + theSize),
24   myCurrent(theBegin)
25 {
26   //
27 }
28
29 // =======================================================================
30 // function : ~Standard_ArrayStreamBuffer
31 // purpose  :
32 // =======================================================================
33 Standard_ArrayStreamBuffer::~Standard_ArrayStreamBuffer()
34 {
35   //
36 }
37
38 // =======================================================================
39 // function : Init
40 // purpose  :
41 // =======================================================================
42 void Standard_ArrayStreamBuffer::Init (const char*  theBegin,
43                                        const size_t theSize)
44 {
45   myBegin   = theBegin;
46   myEnd     = theBegin + theSize;
47   myCurrent = theBegin;
48 }
49
50 // =======================================================================
51 // function : underflow
52 // purpose  :
53 // =======================================================================
54 Standard_ArrayStreamBuffer::int_type Standard_ArrayStreamBuffer::underflow()
55 {
56   if (myCurrent == myEnd)
57   {
58     return traits_type::eof();
59   }
60
61   return traits_type::to_int_type(*myCurrent);
62 }
63
64 // =======================================================================
65 // function : uflow
66 // purpose  :
67 // =======================================================================
68 Standard_ArrayStreamBuffer::int_type Standard_ArrayStreamBuffer::uflow()
69 {
70   if (myCurrent == myEnd)
71   {
72     return traits_type::eof();
73   }
74
75   return traits_type::to_int_type(*myCurrent++);
76 }
77
78 // =======================================================================
79 // function : pbackfail
80 // purpose  :
81 // =======================================================================
82 Standard_ArrayStreamBuffer::int_type Standard_ArrayStreamBuffer::pbackfail (int_type ch)
83 {
84   if (myCurrent == myBegin
85     || (ch != traits_type::eof()
86     && ch != myCurrent[-1]))
87   {
88     return traits_type::eof();
89   }
90
91   return traits_type::to_int_type(*--myCurrent);
92 }
93
94 // =======================================================================
95 // function : showmanyc
96 // purpose  :
97 // =======================================================================
98 std::streamsize Standard_ArrayStreamBuffer::showmanyc()
99 {
100   if (myCurrent > myEnd)
101   {
102     // assert
103   }
104   return myEnd - myCurrent;
105 }
106
107 // =======================================================================
108 // function : seekoff
109 // purpose  :
110 // =======================================================================
111 Standard_ArrayStreamBuffer::pos_type Standard_ArrayStreamBuffer::seekoff (off_type theOff,
112                                                                           std::ios_base::seekdir theWay,
113                                                                           std::ios_base::openmode theWhich)
114 {
115   switch (theWay)
116   {
117     case std::ios_base::beg:
118     {
119       myCurrent = myBegin + theOff;
120       if (myCurrent >= myEnd)
121       {
122         myCurrent = myEnd;
123       }
124       break;
125     }
126     case std::ios_base::cur:
127     {
128       myCurrent += theOff;
129       if (myCurrent >= myEnd)
130       {
131         myCurrent = myEnd;
132       }
133       break;
134     }
135     case std::ios_base::end:
136     {
137       myCurrent = myEnd - theOff;
138       if (myCurrent < myBegin)
139       {
140         myCurrent = myBegin;
141       }
142       break;
143     }
144     default:
145     {
146       break;
147     }
148   }
149   (void )theWhich;
150   return myCurrent - myBegin;
151 }
152
153 // =======================================================================
154 // function : seekpos
155 // purpose  :
156 // =======================================================================
157 Standard_ArrayStreamBuffer::pos_type Standard_ArrayStreamBuffer::seekpos (pos_type thePosition,
158                                                                           std::ios_base::openmode theWhich)
159 {
160   return seekoff (off_type(thePosition), std::ios_base::beg, theWhich);
161 }
162
163 // =======================================================================
164 // function : xsgetn
165 // purpose  :
166 // =======================================================================
167 std::streamsize Standard_ArrayStreamBuffer::xsgetn (char* thePtr,
168                                                     std::streamsize theCount)
169 {
170   const char* aCurrent = myCurrent + theCount;
171   if (aCurrent >= myEnd)
172   {
173     aCurrent = myEnd;
174   }
175   size_t aCopied = aCurrent - myCurrent;
176   if (aCopied == 0)
177   {
178     return 0;
179   }
180   memcpy (thePtr, myCurrent, aCopied);
181   myCurrent = aCurrent;
182   return aCopied;
183 }