Introduced FileSystem and Out classes
[openstranded.git] / src / bbfile.cc
blobcf02db50544e8ad4adbd7ce3b0f5dc87b99b5368
1 /*
2 * This file includes the implementation of the functions of the
3 * BBFile class.
5 * Copyright (C) 2008 David Kolossa
7 * This file is part of OpenStranded.
9 * OpenStranded is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * OpenStranded is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
23 #include <iostream>
24 #include "bbfile.hh"
26 BBFile::BBFile (std::string filePath)
28 file.open (filePath.c_str(), std::fstream::binary | std::fstream::in | std::fstream::out);
29 if(!file)
31 // Try again. Read-only...
32 file.open (filePath.c_str(), std::fstream::binary | std::fstream::in);
36 BBFile::~BBFile ()
38 file.close ();
41 bool
42 BBFile::operator! ()
44 return !(this->file);
47 BBFile&
48 BBFile::operator>> (std::string &strBuf)
50 strBuf = this->readString ();
51 return *this;
54 BBFile&
55 BBFile::operator>> (uint8_t &byteBuf)
57 byteBuf = this->readByte ();
58 return *this;
61 BBFile&
62 BBFile::operator>> (uint16_t &shortBuf)
64 shortBuf = this->readShort ();
65 return *this;
68 BBFile&
69 BBFile::operator>> (int32_t &intBuf)
71 intBuf = this->readInt ();
72 return *this;
75 BBFile&
76 BBFile::operator>> (float &floatBuf)
78 floatBuf = this->readFloat ();
79 return *this;
82 BBFile&
83 BBFile::operator<< (std::string &strBuf)
85 this->writeString (strBuf);
86 return *this;
89 BBFile&
90 BBFile::operator<< (uint8_t &byteBuf)
92 this->writeByte (byteBuf);
93 return *this;
96 BBFile&
97 BBFile::operator<< (uint16_t &shortBuf)
99 this->writeShort (shortBuf);
100 return *this;
103 BBFile&
104 BBFile::operator<< (int32_t &intBuf)
106 this->writeInt (intBuf);
107 return *this;
110 BBFile&
111 BBFile::operator<< (float &floatBuf)
113 this->writeFloat (floatBuf);
114 return *this;
117 std::string
118 BBFile::readLine ()
120 std::string lineRead;
122 getline (file, lineRead, '\r');
123 file.get (); // remove \n on stream
125 return lineRead;
128 std::string
129 BBFile::readString ()
131 std::string stringRead;
132 int32_t size;
134 size = this->readInt ();
135 for (int32_t i=0; i < size; i++)
137 stringRead += (char) file.get ();
140 return stringRead;
143 uint8_t
144 BBFile::readByte ()
146 uint8_t byteRead;
148 file.read ((char*) &byteRead, sizeof(uint8_t));
149 return byteRead;
152 uint16_t
153 BBFile::readShort ()
155 uint16_t shortRead;
157 file.read ((char*) &shortRead, sizeof(uint16_t));
158 return shortRead;
161 int32_t
162 BBFile::readInt ()
164 int32_t intRead;
166 file.read ((char*) &intRead, sizeof(int32_t));
167 return intRead;
171 float
172 BBFile::readFloat ()
174 float floatRead;
176 file.read ((char*) &floatRead, sizeof(float));
177 return floatRead;
180 void
181 BBFile::readData(void *buffer, uint32_t size)
183 file.read((char*)buffer, size);
186 void
187 BBFile::writeLine (std::string &lineWritten)
189 file.write (lineWritten.c_str(), lineWritten.size ());
190 file.write ("\r\n", 2 * sizeof(char));
193 void
194 BBFile::writeString (std::string &strWritten)
196 int32_t size = (int32_t) strWritten.size ();
198 this->writeInt (size);
199 file.write (strWritten.c_str (), size);
202 void
203 BBFile::writeByte (uint8_t &byteWritten)
205 file.write ((char*) &byteWritten, sizeof(uint8_t));
208 void
209 BBFile::writeShort (uint16_t &shortWritten)
211 file.write ((char*) &shortWritten, sizeof(uint16_t));
214 void
215 BBFile::writeInt (int32_t &intWritten)
217 file.write ((char*) &intWritten, sizeof(int32_t));
220 void
221 BBFile::writeFloat (float &floatWritten)
223 file.write ((char*) &floatWritten, sizeof(float));
226 bool
227 BBFile::validLine (std::string &line)
229 return ((line.find ('\r') == std::string::npos) && (line.find ('\n') == std::string::npos));