2 * This file includes the implementation of the functions of the
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/>.
26 BBFile::BBFile (std::string filePath
)
28 file
.open (filePath
.c_str(), std::fstream::binary
| std::fstream::in
| std::fstream::out
);
31 // Try again. Read-only...
32 file
.open (filePath
.c_str(), std::fstream::binary
| std::fstream::in
);
48 BBFile::operator>> (std::string
&strBuf
)
50 strBuf
= this->readString ();
55 BBFile::operator>> (uint8_t &byteBuf
)
57 byteBuf
= this->readByte ();
62 BBFile::operator>> (uint16_t &shortBuf
)
64 shortBuf
= this->readShort ();
69 BBFile::operator>> (int32_t &intBuf
)
71 intBuf
= this->readInt ();
76 BBFile::operator>> (float &floatBuf
)
78 floatBuf
= this->readFloat ();
83 BBFile::operator<< (std::string
&strBuf
)
85 this->writeString (strBuf
);
90 BBFile::operator<< (uint8_t &byteBuf
)
92 this->writeByte (byteBuf
);
97 BBFile::operator<< (uint16_t &shortBuf
)
99 this->writeShort (shortBuf
);
104 BBFile::operator<< (int32_t &intBuf
)
106 this->writeInt (intBuf
);
111 BBFile::operator<< (float &floatBuf
)
113 this->writeFloat (floatBuf
);
120 std::string lineRead
;
122 getline (file
, lineRead
, '\r');
123 file
.get (); // remove \n on stream
129 BBFile::readString ()
131 std::string stringRead
;
134 size
= this->readInt ();
135 for (int32_t i
=0; i
< size
; i
++)
137 stringRead
+= (char) file
.get ();
148 file
.read ((char*) &byteRead
, sizeof(uint8_t));
157 file
.read ((char*) &shortRead
, sizeof(uint16_t));
166 file
.read ((char*) &intRead
, sizeof(int32_t));
176 file
.read ((char*) &floatRead
, sizeof(float));
181 BBFile::readData(void *buffer
, uint32_t size
)
183 file
.read((char*)buffer
, size
);
187 BBFile::writeLine (std::string
&lineWritten
)
189 file
.write (lineWritten
.c_str(), lineWritten
.size ());
190 file
.write ("\r\n", 2 * sizeof(char));
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
);
203 BBFile::writeByte (uint8_t &byteWritten
)
205 file
.write ((char*) &byteWritten
, sizeof(uint8_t));
209 BBFile::writeShort (uint16_t &shortWritten
)
211 file
.write ((char*) &shortWritten
, sizeof(uint16_t));
215 BBFile::writeInt (int32_t &intWritten
)
217 file
.write ((char*) &intWritten
, sizeof(int32_t));
221 BBFile::writeFloat (float &floatWritten
)
223 file
.write ((char*) &floatWritten
, sizeof(float));
227 BBFile::validLine (std::string
&line
)
229 return ((line
.find ('\r') == std::string::npos
) && (line
.find ('\n') == std::string::npos
));