xoreos  0.0.5
2dafile.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef AURORA_2DAFILE_H
26 #define AURORA_2DAFILE_H
27 
28 #include <vector>
29 #include <map>
30 
31 #include <boost/noncopyable.hpp>
32 
33 #include "src/common/types.h"
34 #include "src/common/ustring.h"
35 #include "src/common/ptrvector.h"
36 
37 #include "src/aurora/aurorafile.h"
38 
39 namespace Common {
40  class SeekableReadStream;
41  class WriteStream;
42  class StreamTokenizer;
43 }
44 
45 namespace Aurora {
46 
47 class TwoDAFile;
48 class GDAFile;
49 
61 class TwoDARow : boost::noncopyable {
62 public:
64  const Common::UString &getString(size_t column) const;
66  const Common::UString &getString(const Common::UString &column) const;
67 
69  int32 getInt(size_t column) const;
71  int32 getInt(const Common::UString &column) const;
72 
74  float getFloat(size_t column) const;
76  float getFloat(const Common::UString &column) const;
77 
79  bool empty(size_t column) const;
81  bool empty(const Common::UString &column) const;
82 
83 private:
85 
86  std::vector<Common::UString> _data;
87 
88  TwoDARow(TwoDAFile &parent);
89  ~TwoDARow();
90 
91  const Common::UString &getCell(size_t n) const;
92 
93  friend class TwoDAFile;
94 
95  template<typename T>
96  friend void Common::DeallocatorDefault::destroy(T *);
97 };
98 
124 class TwoDAFile : boost::noncopyable, public AuroraFile {
125 public:
127  TwoDAFile(const GDAFile &gda);
128  ~TwoDAFile();
129 
131  size_t getRowCount() const;
132 
134  size_t getColumnCount() const;
135 
137  const std::vector<Common::UString> &getHeaders() const;
138 
140  size_t headerToColumn(const Common::UString &header) const;
141 
143  const TwoDARow &getRow(size_t row) const;
144 
146  const TwoDARow &getRow(const Common::UString &header, const Common::UString &value) const;
147 
148  // .--- 2DA file writers
150  void writeASCII(Common::WriteStream &out) const;
152  bool writeASCII(const Common::UString &fileName) const;
153 
155  void writeBinary(Common::WriteStream &out) const;
157  bool writeBinary(const Common::UString &fileName) const;
158 
160  void writeCSV(Common::WriteStream &out) const;
162  bool writeCSV(const Common::UString &fileName) const;
163  // '---
164 
165 private:
166  typedef std::map<Common::UString, size_t, Common::UString::iless> HeaderMap;
167 
171 
172  std::vector<Common::UString> _headers;
174 
177 
178  // Loading helpers
179  void load(Common::SeekableReadStream &twoda);
180  void read2a(Common::SeekableReadStream &twoda);
181  void read2b(Common::SeekableReadStream &twoda);
182 
183  // ASCII loading helpers
187 
188  // Binary loading helpers
192 
193  // GDA loading/conversion helpers
194  void load(const GDAFile &gda);
195 
196  void createHeaderMap();
197 
198  static int32 parseInt(const Common::UString &str);
199  static float parseFloat(const Common::UString &str);
200 
201  friend class TwoDARow;
202 };
203 
204 } // End of namespace Aurora
205 
206 #endif // AURORA_2DAFILE_H
Class to hold the two-dimensional array of a 2DA file.
Definition: 2dafile.h:124
void createHeaderMap()
Definition: 2dafile.cpp:336
TwoDAFile(Common::SeekableReadStream &twoda)
Definition: 2dafile.cpp:128
void readHeaders2b(Common::SeekableReadStream &twoda)
Definition: 2dafile.cpp:258
void load(Common::SeekableReadStream &twoda)
Definition: 2dafile.cpp:143
Definition: 2dafile.h:39
const Common::UString & getString(size_t column) const
Return the contents of a cell as a string.
Definition: 2dafile.cpp:59
A class holding an UTF-8 string.
Definition: ustring.h:48
std::map< Common::UString, size_t, Common::UString::iless > HeaderMap
Definition: 2dafile.h:166
void skipRowNames2b(Common::SeekableReadStream &twoda)
Definition: 2dafile.cpp:275
Tokenizes a stream.
std::vector< Common::UString > _headers
Definition: 2dafile.h:172
Common::UString _defaultString
The default string to return should a cell not exist.
Definition: 2dafile.h:168
HeaderMap _headerMap
Definition: 2dafile.h:173
size_t getRowCount() const
Return the number of rows in the array.
Definition: 2dafile.cpp:400
A vector of pointer to objects, with automatic deletion.
Definition: ptrvector.h:44
size_t getColumnCount() const
Return the number of columns in the array.
Definition: 2dafile.cpp:404
void writeCSV(Common::WriteStream &out) const
Write the 2DA data into a CSV stream.
Definition: 2dafile.cpp:622
int32 _defaultInt
The default int to return should a cell not exist.
Definition: 2dafile.h:169
void readRows2b(Common::SeekableReadStream &twoda)
Definition: 2dafile.cpp:294
Base for BioWare&#39;s Aurora engine files.
static void destroy(T *x)
Definition: deallocator.h:40
std::vector< Common::UString > _data
Definition: 2dafile.h:86
void writeASCII(Common::WriteStream &out) const
Write the 2DA data into an V2.0 ASCII 2DA.
Definition: 2dafile.cpp:443
Low-level type definitions to handle fixed width types portably.
void read2a(Common::SeekableReadStream &twoda)
Definition: 2dafile.cpp:172
void writeBinary(Common::WriteStream &out) const
Write the 2DA data into an V2.b binary 2DA.
Definition: 2dafile.cpp:515
A vector storing pointer to objects, with automatic deletion.
int32 getInt(size_t column) const
Return the contents of a cell as an int.
Definition: 2dafile.cpp:75
Generic interface for a writable data stream.
Definition: writestream.h:64
void readDefault2a(Common::SeekableReadStream &twoda, Common::StreamTokenizer &tokenize)
Definition: 2dafile.cpp:196
static float parseFloat(const Common::UString &str)
Definition: 2dafile.cpp:691
Unicode string handling.
const std::vector< Common::UString > & getHeaders() const
Return the columns&#39; headers.
Definition: 2dafile.cpp:408
bool empty(size_t column) const
Check if the cell is empty.
Definition: 2dafile.cpp:107
const TwoDARow & getRow(size_t row) const
Get a row.
Definition: 2dafile.cpp:421
TwoDARow _emptyRow
Definition: 2dafile.h:175
void readRows2a(Common::SeekableReadStream &twoda, Common::StreamTokenizer &tokenize)
Definition: 2dafile.cpp:227
Base class for most files found in games using BioWare&#39;s Aurora engine.
Definition: aurorafile.h:52
void readHeaders2a(Common::SeekableReadStream &twoda, Common::StreamTokenizer &tokenize)
Definition: 2dafile.cpp:216
Common::PtrVector< TwoDARow > _rows
Definition: 2dafile.h:176
float _defaultFloat
The default float to return should a cell not exist.
Definition: 2dafile.h:170
const Common::UString & getCell(size_t n) const
Definition: 2dafile.cpp:120
static int32 parseInt(const Common::UString &str)
Definition: 2dafile.cpp:677
TwoDAFile * _parent
The parent 2DA.
Definition: 2dafile.h:84
A row within a 2DA file.
Definition: 2dafile.h:61
void read2b(Common::SeekableReadStream &twoda)
Definition: 2dafile.cpp:190
Interface for a seekable & readable data stream.
Definition: readstream.h:265
size_t headerToColumn(const Common::UString &header) const
Translate a column header to a column index.
Definition: 2dafile.cpp:412
TwoDARow(TwoDAFile &parent)
Definition: 2dafile.cpp:53
Class to hold the GFF&#39;d two-dimensional array of a GDA file.
Definition: gdafile.h:62
float getFloat(size_t column) const
Return the contents of a cell as a float.
Definition: 2dafile.cpp:91
int32_t int32
Definition: types.h:203