Colobot
ioutils.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
25 #pragma once
26 
27 
28 #include <iostream>
29 
30 #include <cstring>
31 
32 namespace IOUtils
33 {
34 
36 
41 template<int N, typename T>
42 void WriteBinary(T value, std::ostream &ostr)
43 {
44  for (int i = 0; i < N; ++i)
45  {
46  unsigned char byte = (value >> (i*8)) & 0xFF;
47  ostr.write(reinterpret_cast<char*>(&byte), 1);
48  }
49 }
50 
52 
57 template<int N, typename T>
58 T ReadBinary(std::istream &istr)
59 {
60  T value = 0;
61  for (int i = 0; i < N; ++i)
62  {
63  unsigned char byte = 0;
64  istr.read(reinterpret_cast<char*>(&byte), 1);
65  value |= byte << (i*8);
66  }
67  return value;
68 }
69 
71 
74 inline void WriteBinaryBool(bool value, std::ostream &ostr)
75 {
76  unsigned char v = value ? 1 : 0;
77  IOUtils::WriteBinary<1, unsigned char>(v, ostr);
78 }
79 
81 
84 inline bool ReadBinaryBool(std::istream &istr)
85 {
86  int v = IOUtils::ReadBinary<1, unsigned char>(istr);
87  return v != 0;
88 }
89 
91 
95 inline void WriteBinaryFloat(float value, std::ostream &ostr)
96 {
97  union FloatCast
98  {
99  float fValue;
100  unsigned int iValue;
101  };
102  FloatCast u;
103  u.fValue = 0.0f;
104  u.iValue = 0;
105 
106  u.fValue = value;
107  IOUtils::WriteBinary<4, unsigned int>(u.iValue, ostr);
108 }
109 
111 
115 inline float ReadBinaryFloat(std::istream &istr)
116 {
117  union FloatCast
118  {
119  float fValue;
120  unsigned int iValue;
121  };
122  FloatCast u;
123  u.fValue = 0.0f;
124  u.iValue = 0;
125 
126  u.iValue = IOUtils::ReadBinary<4, unsigned int>(istr);
127  return u.fValue;
128 }
129 
131 
135 template<int N>
136 void WriteBinaryString(const std::string &value, std::ostream &ostr)
137 {
138  int length = value.size();
139  WriteBinary<N, int>(length, ostr);
140 
141  for (int i = 0; i < length; ++i)
142  ostr.put(value[i]);
143 }
144 
146 
150 template<int N>
151 std::string ReadBinaryString(std::istream &istr)
152 {
153  int length = ReadBinary<N, int>(istr);
154 
155  std::string str;
156  char c = 0;
157  for (int i = 0; i < length; ++i)
158  {
159  istr.read(&c, 1);
160  str += c;
161  }
162 
163  return str;
164 }
165 
166 } // namespace IOUtils
167 
void WriteBinaryString(const std::string &value, std::ostream &ostr)
Writes a variable binary string to output stream.
Definition: ioutils.h:136
Definition: ioutils.h:32
void WriteBinary(T value, std::ostream &ostr)
Writes a binary number to output stream.
Definition: ioutils.h:42
std::string ReadBinaryString(std::istream &istr)
Reads a variable binary string from output stream.
Definition: ioutils.h:151
bool ReadBinaryBool(std::istream &istr)
Reads a binary 1-byte boolean.
Definition: ioutils.h:84
void WriteBinaryFloat(float value, std::ostream &ostr)
Writes a binary 32-bit float to output stream.
Definition: ioutils.h:95
T ReadBinary(std::istream &istr)
Reads a binary number from input stream.
Definition: ioutils.h:58
void WriteBinaryBool(bool value, std::ostream &ostr)
Writes a binary 1-byte boolean.
Definition: ioutils.h:74
float ReadBinaryFloat(std::istream &istr)
Reads a binary 32-bit float from input stream.
Definition: ioutils.h:115