4 #ifndef tools_wroot_wbuf
5 #define tools_wroot_wbuf
9 #include "../charp_out"
33 static void write_swap_2(
char* a_pos,
char* a_x) {
34 *a_pos = *(a_x+1);a_pos++;
35 *a_pos = *a_x;a_pos++;
37 static void write_swap_4(
char* a_pos,
char* a_x) {
39 *a_pos = *a_x;a_pos++;a_x--;
40 *a_pos = *a_x;a_pos++;a_x--;
41 *a_pos = *a_x;a_pos++;a_x--;
42 *a_pos = *a_x;a_pos++;
44 static void write_swap_8(
char* a_pos,
char* a_x) {
46 *a_pos = *a_x;a_pos++;a_x--;
47 *a_pos = *a_x;a_pos++;a_x--;
48 *a_pos = *a_x;a_pos++;a_x--;
49 *a_pos = *a_x;a_pos++;a_x--;
50 *a_pos = *a_x;a_pos++;a_x--;
51 *a_pos = *a_x;a_pos++;a_x--;
52 *a_pos = *a_x;a_pos++;a_x--;
53 *a_pos = *a_x;a_pos++;
58 static void write_nswp_2(
char* a_pos,
char* a_x) {::memcpy(a_pos,a_x,2);}
59 static void write_nswp_4(
char* a_pos,
char* a_x) {::memcpy(a_pos,a_x,4);}
60 static void write_nswp_8(
char* a_pos,
char* a_x) {::memcpy(a_pos,a_x,8);}
66 static const std::string& s_class() {
67 static const std::string s_v(
"tools::wroot::wbuf");
70 typedef void (*w_2_func)(
char*,
char*);
71 typedef void (*w_4_func)(
char*,
char*);
72 typedef void (*w_8_func)(
char*,
char*);
74 wbuf(std::ostream& a_out,
bool a_byte_swap,
const char* a_eob,
char*& a_pos)
85 mem::increment(s_class().c_str());
91 mem::decrement(s_class().c_str());
105 mem::increment(s_class().c_str());
135 if(!check_eob<unsigned char>())
return false;
141 if(!check_eob<unsigned short>())
return false;
143 m_pos +=
sizeof(
unsigned short);
148 if(!check_eob<unsigned int>())
return false;
150 m_pos +=
sizeof(
unsigned int);
155 if(!check_eob<uint64>())
return false;
162 if(!check_eob<float>())
return false;
164 m_pos +=
sizeof(float);
169 if(!check_eob<double>())
return false;
171 m_pos +=
sizeof(double);
180 bool write(
const std::string& a_x) {
182 unsigned int nchars = (
unsigned int)a_x.size();
184 if(!
check_eob(1+4,
"std::string"))
return false;
186 if(!
write(nwh))
return false;
187 if(!
write(nchars))
return false;
189 if(!
check_eob(1,
"std::string"))
return false;
190 nwh = (
unsigned char)nchars;
191 if(!
write(nwh))
return false;
193 if(!
check_eob(nchars,
"std::string"))
return false;
194 for (
unsigned int i = 0; i < nchars; i++)
m_pos[i] = a_x[i];
202 if(!a_n)
return true;
203 uint32 l = a_n *
sizeof(T);
206 for(
uint32 i=0;i<a_n;i++) {
207 if(!
write(a_a[i]))
return false;
210 ::memcpy(
m_pos,a_a,l);
217 bool write(
const std::vector<T>& a_v) {
218 if(a_v.empty())
return true;
223 if(!
write(a_v[i]))
return false;
232 m_out << s_class() <<
" : " <<
stype(T()) <<
" : "
234 <<
" try to access out of buffer " <<
sizeof(T) <<
" bytes"
243 m_out << s_class() <<
" : " << a_cmt <<
" : "
245 <<
" try to access out of buffer " << a_n <<
" bytes"