Moved Printer class into Out class
[openstranded.git] / src / bbfile.hh
blob9561783b0ee883df30cb57ec972aae3edee02e99
1 /*
2 * This file includes the class declaration of a Blitz Basic file
4 * Copyright (C) 2008 David Kolossa
6 * This file is part of OpenStranded.
8 * OpenStranded is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * OpenStranded is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef STRANDED_BBFILE_HH
23 #define STRANDED_BBFILE_HH
25 #include <stdint.h>
26 #include <fstream>
28 /**
29 * This is a wrapper class for Blitz3d files.
30 * It is used to provide compatibility with files created by
31 * Blitz Basic 3d (e.g. save files)
33 class BBFile
35 private:
36 /**
37 * The file object for access to the Blitz Basic file
39 std::fstream file;
41 public:
42 /**
43 * The constructor.
44 * Opens a Blitz Basic compatible file.
45 * @param filePath Path to the file to be opened
47 BBFile (std::string filePath);
49 /**
50 * The destructor.
51 * Closes the opened file.
53 ~BBFile ();
58 // Check for file stream flags
61 /**
62 * Check fail flag.
64 inline bool
65 fail() { return file.fail(); }
67 /**
68 * Check EOF flag.
70 inline bool
71 eof() { return file.eof(); }
73 /**
74 * Check bad flag.
76 inline bool
77 bad() { return file.bad(); }
79 /**
80 * Check good flag.
82 inline bool
83 good() { return file.good(); }
88 // Overloading the ! operator
90 bool
91 operator!();
96 // Overloading the extraction operator
99 /**
100 * Read a string from the file.
101 * Note: You can't read lines using this operator, use readLine() instead.
102 * @param strBuf The buffer into which the string is written
103 * @return A reference to the BBFile object
104 * @see readLine()
105 * @see readString()
107 BBFile&
108 operator>> (std::string &strBuf);
111 * Read a uint8_t (In B3d: Byte) from the file.
112 * @param byteBuf The buffer into which the uint8_t is written
113 * @return A reference to the BBFile object
114 * @see readByte()
116 BBFile&
117 operator>> (uint8_t &byteBuf);
120 * Read a uint16_t (In B3d: Short) from the file.
121 * @param shortBuf The buffer into which the uint16_t is written
122 * @return A reference to the BBFile object
123 * @see readShort()
125 BBFile&
126 operator>> (uint16_t &shortBuf);
129 * Read an int32_t (In B3d: Int) from the file.
130 * @param intBuf The buffer into which the int32_t is written
131 * @return A reference to the BBFile object
132 * @see readInt()
134 BBFile&
135 operator>> (int32_t &intBuf);
138 * Read a float (In B3d: Float) from the file.
139 * @param floatBuf Th buffer into which the float is written
140 * @return A reference to the BBFile object
141 * @see readFloat()
143 BBFile&
144 operator>> (float &floatBuf);
149 // Overloading the insertion operator
153 * Write a string to the file.
154 * Note: You can't write lines with that operator, use writeLine() instead.
155 * @param strBuf The buffer from which the string is read
156 * @return A reference to the BBFile object
157 * @see writeString()
158 * @see writeLine()
160 BBFile&
161 operator<< (std::string &strBuf);
164 * Write a uint8_t (In B3d: Byte) to the file.
165 * @param byteBuf The buffer from which the uint8_t is read
166 * @return A reference to the BBFile object
167 * @see writeByte()
169 BBFile&
170 operator<< (uint8_t &byteBuf);
173 * Write a uint16_t (In B3d: Short) to the file.
174 * @param shortBuf The buffer from which the uint16_t is read
175 * @return A reference to the BBFile object
176 * @see writeShort()
178 BBFile&
179 operator<< (uint16_t &shortBuf);
182 * Write a int32_t (In B3d: Integer) to the file.
183 * @param intBuf The buffer from which the int32_t is read
184 * @return A reference to the BBFile object
185 * @see writeInt()
187 BBFile&
188 operator<< (int32_t &intBuf);
191 * Write a float (In B3d: Float) to the file.
192 * @param floatBuf The buffer from which the float is read
193 * @return A reference to the BBFile object
194 * @see writeFloat()
196 BBFile&
197 operator<< (float &floatBuf);
202 // Actual file reading methods
206 * Reads a line.
207 * A line is terminated by the ASCII symbols CR (0x0d) and LF (0x0a).
208 * The read data is removed from the stream.
209 * @return The read string
211 std::string
212 readLine ();
215 * Reads a string.
216 * A string is preceded by its length as an int32_t. This method reads the
217 * string and removes it (including length indicator) from the stream.
218 * @return The read string
220 std::string
221 readString ();
224 * Reads a uint8_t (B3d: Byte) value from the stream and removes it afterwards.
225 * @return The read uint8_t value.
227 uint8_t
228 readByte ();
231 * Reads a uint16_t (B3d: Short) value from the stream and removes it afterwards.
232 * @return The read short value.
234 uint16_t
235 readShort ();
238 * Reads an int32_t (B3d: Int) value from the stream and removes it afterwards.
239 * @return The read int32_t value.
241 int32_t
242 readInt ();
245 * Reads a float value from the stream and removes it afterwards.
246 * @return The read float value.
248 float
249 readFloat ();
252 * Reads raw data from the stream.
253 * @param buffer Buffer to which the bytes are written
254 * @param size Amount of data to be read
256 void
257 readData(void *buffer, uint32_t size);
260 // Actual file writing methods
264 * Writes a string terminated by the ASCII symbols CR (0x0d) and LF (0x0a)
265 * to the stream.
266 * @param lineWritten The line to be written
268 void
269 writeLine (std::string &lineWritten);
272 * Writes a string to the stream.
273 * @param strWritten The string to be written
275 void
276 writeString (std::string &strWritten);
279 * Writes a uint8_t (B3d: Byte) to the stream.
280 * @param byteWritten The uint8_t to be written
282 void
283 writeByte (uint8_t &byteWritten);
286 * Writes a uint16_t (B3d: Short) to the stream.
287 * @param shortWritten The uint16_t to be written
289 void
290 writeShort (uint16_t &shortWritten);
293 * Writes a int32_t (B3d: Int) to the stream.
294 * @param intWritten The int32_t to be written
296 void
297 writeInt (int32_t &intWritten);
300 * Writes a float to the stream.
301 * @param floatWritten The float to be written
303 void
304 writeFloat (float &floatWritten);
309 // Input validation
313 * Checks whether a string is a valid B3d line
314 * @param line The string to be checked
315 * @return true if line is a valid b3d line, false if not
317 bool
318 validLine (std::string &line);
322 #endif /* STRANDED_BBFILE_HH */