Line data Source code
1 : /*
2 : * Copyright (c) 2012-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Sebastian Reiter
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #include "binary_stream.h"
34 : #include "../log.h"
35 :
36 : using namespace std;
37 :
38 : namespace ug{
39 :
40 0 : BinaryStreamBuffer::BinaryStreamBuffer()
41 : {
42 0 : reserve(128);
43 0 : }
44 :
45 0 : void BinaryStreamBuffer::clear()
46 : {
47 0 : resize(0);
48 0 : }
49 :
50 0 : size_t BinaryStreamBuffer::size() const
51 : {
52 0 : if(egptr() < pptr())
53 0 : return pptr() - buffer();
54 0 : return egptr() - eback();
55 : }
56 :
57 0 : void BinaryStreamBuffer::reserve(size_t newSize)
58 : {
59 : size_t getPos = 0;
60 : size_t putPos = 0;
61 : size_t readEndPos = 0;
62 0 : if(!m_dataBuf.empty()){
63 0 : getPos = gptr() - eback();
64 0 : readEndPos = egptr() - eback();
65 0 : putPos = pptr() - buffer();
66 : }
67 :
68 0 : if(newSize > m_dataBuf.size())
69 0 : m_dataBuf.resize(newSize);
70 :
71 0 : setg(buffer(), buffer() + getPos, buffer() + readEndPos);
72 0 : setp(buffer() + putPos, end());
73 0 : }
74 :
75 0 : void BinaryStreamBuffer::resize(size_t newSize)
76 : {
77 : size_t getPos = 0;
78 : size_t putPos = 0;
79 0 : if(!m_dataBuf.empty()){
80 0 : getPos = gptr() - eback();
81 0 : putPos = pptr() - buffer();
82 : }
83 :
84 0 : if(newSize > m_dataBuf.size())
85 0 : m_dataBuf.resize(newSize);
86 :
87 0 : if(getPos > newSize)
88 : getPos = newSize;
89 0 : if(putPos > newSize)
90 : putPos = newSize;
91 :
92 0 : setg(buffer(), buffer() + getPos, buffer() + newSize);
93 0 : setp(buffer() + putPos, end());
94 0 : }
95 :
96 0 : void BinaryStreamBuffer::reset()
97 : {
98 0 : size_t readEndPos = egptr() - eback();
99 :
100 0 : setg(buffer(), buffer(), buffer() + readEndPos);
101 : setp(buffer(), end());
102 0 : }
103 :
104 0 : void BinaryStreamBuffer::write_jump(size_t jumpSize)
105 : {
106 0 : gbump(jumpSize);
107 0 : }
108 :
109 0 : void BinaryStreamBuffer::read_jump(size_t jumpSize)
110 : {
111 0 : pbump(jumpSize);
112 0 : }
113 :
114 0 : size_t BinaryStreamBuffer::get_read_pos() const
115 : {
116 0 : return gptr() - eback();
117 : }
118 :
119 0 : BinaryStreamBuffer::int_type BinaryStreamBuffer::overflow(int_type c)
120 : {
121 0 : if(c != traits_type::eof())
122 : {
123 0 : size_t curSize = size();
124 0 : reserve(curSize * 2);
125 0 : m_dataBuf[curSize] = c;
126 0 : setp(pptr() + 1, epptr());
127 : }
128 0 : return c;
129 : }
130 :
131 0 : BinaryStreamBuffer::int_type BinaryStreamBuffer::underflow()
132 : {
133 0 : if(egptr() < pptr()){
134 : // new data has been written since last reserve or last read operation
135 : setg(eback(), gptr(), pptr());
136 0 : if(gptr() < egptr()){
137 0 : return m_dataBuf[gptr() - eback()];
138 : }
139 : }
140 :
141 : return traits_type::eof();
142 : }
143 :
144 : }// end of namespace
|