Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
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-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.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 
35 
40 template<int N, typename T>
41 void WriteBinary(T value, std::ostream &ostr)
42 {
43  for (int i = 0; i < N; ++i)
44  {
45  unsigned char byte = (value >> (i*8)) & 0xFF;
46  ostr.write(reinterpret_cast<char*>(&byte), 1);
47  }
48 }
49 
51 
56 template<int N, typename T>
57 T ReadBinary(std::istream &istr)
58 {
59  T value = 0;
60  for (int i = 0; i < N; ++i)
61  {
62  unsigned char byte = 0;
63  istr.read(reinterpret_cast<char*>(&byte), 1);
64  value |= byte << (i*8);
65  }
66  return value;
67 }
68 
70 
73 void WriteBinaryBool(float value, std::ostream &ostr)
74 {
75  unsigned char v = value ? 1 : 0;
76  IOUtils::WriteBinary<1, unsigned char>(v, ostr);
77 }
78 
80 
83 bool ReadBinaryBool(std::istream &istr)
84 {
85  int v = IOUtils::ReadBinary<1, unsigned char>(istr);
86  return v != 0;
87 }
88 
90 
94 void WriteBinaryFloat(float value, std::ostream &ostr)
95 {
96  union { float fValue; unsigned int iValue; } u;
97  memset(&u, 0, sizeof(u));
98  u.fValue = value;
99  IOUtils::WriteBinary<4, unsigned int>(u.iValue, ostr);
100 }
101 
103 
107 float ReadBinaryFloat(std::istream &istr)
108 {
109  union { float fValue; unsigned int iValue; } u;
110  memset(&u, 0, sizeof(u));
111  u.iValue = IOUtils::ReadBinary<4, unsigned int>(istr);
112  return u.fValue;
113 }
114 
116 
120 template<int N>
121 void WriteBinaryString(const std::string &value, std::ostream &ostr)
122 {
123  int length = value.size();
124  WriteBinary<N, int>(length, ostr);
125 
126  for (int i = 0; i < length; ++i)
127  ostr.put(value[i]);
128 }
129 
131 
135 template<int N>
136 std::string ReadBinaryString(std::istream &istr)
137 {
138  int length = ReadBinary<N, int>(istr);
139 
140  std::string str;
141  char c = 0;
142  for (int i = 0; i < length; ++i)
143  {
144  istr.read(&c, 1);
145  str += c;
146  }
147 
148  return str;
149 }
150 
151 }; // namespace IOUtils
152 
Definition: ioutils.h:32