Athena IO Library
FileWriter.hpp
1 // This file is part of libAthena.
2 //
3 // libAthena is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // libAthena is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with libAthena. If not, see <http://www.gnu.org/licenses/>
15 
16 #ifndef FILEWRITER_HPP
17 #define FILEWRITER_HPP
18 
19 #include "Athena/IStreamWriter.hpp"
20 #include <stdio.h>
21 
22 namespace Athena
23 {
24 namespace io
25 {
26 class FileWriter : public IStreamWriter
27 {
28 public:
29  FileWriter(const std::string& filename, bool overwrite = true);
30  virtual ~FileWriter();
31 
32  void setEndian(Endian endian);
33  Endian endian() const;
34  bool isBigEndian() const;
35  bool isLittleEndian() const;
36  void open(bool overwrite = true);
37  void close();
38  bool isOpen() const;
39  void seek(atInt64 pos, SeekOrigin origin = SeekOrigin::Current);
40  inline void seekAlign32() {seek(ROUND_UP_32(position()), SeekOrigin::Begin);}
41  bool atEnd() const;
42  atUint64 position() const;
43  atUint64 length() const;
44 
45  void writeBit(bool val);
46  void seekBit(int bit);
47  void writeUByte(atUint8 val);
48  void writeByte(atInt8 val);
49  void writeUBytes(atUint8* data, atUint64 len);
50  void writeBytes(atInt8* data, atUint64 len);
51  void writeUint16(atUint16 val);
52  void writeInt16(atInt16 val);
53  void writeUint32(atUint32 val);
54  void writeInt32(atInt32 val);
55  void writeUint64(atUint64 val);
56  void writeInt64(atInt64 val);
57  void writeDouble(double val);
58  void writeFloat(float val);
59  void writeBool(bool val);
60  void writeString(const std::string& val);
61  void writeUnicode(const std::string& str);
62  void fill(atInt8 byte, atUint64 len);
63  void fill(atUint8 byte, atUint64 len);
64 private:
65  std::string m_filename;
66  FILE* m_fileHandle;
67  Endian m_endian;
68  atUint8 m_currentByte;
69  atUint64 m_bytePosition;
70  atUint8 m_bitShift;
71  bool m_bitValid;
72 };
73 }
74 } // Athena
75 
76 #ifndef FILEWRITER_BASE
77 #define FILEWRITER_BASE() \
78 private: \
79  typedef Athena::io::FileWriter base;
80 
81 #endif // FILEWRITER_BASE
82 #endif // FILEWRITER_HPP