xoreos  0.0.5
foxpro.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 COMMON_FOXPRO_H
26 #define COMMON_FOXPRO_H
27 
28 #include <boost/noncopyable.hpp>
29 
30 #include "src/common/types.h"
31 #include "src/common/ptrlist.h"
32 #include "src/common/ptrvector.h"
33 #include "src/common/ustring.h"
34 
35 namespace Common {
36 
37 class SeekableReadStream;
38 class WriteStream;
39 
41 class FoxPro : boost::noncopyable {
42 public:
44  enum Type {
45  kTypeDouble = 0x42, // 'B'
46  kTypeString = 0x43, // 'C'
47  kTypeDate = 0x44, // 'D'
48  kTypeFloat = 0x46, // 'F'
49  kTypeGeneral = 0x47, // 'G'
50  kTypeInteger = 0x49, // 'I'
51  kTypeBool = 0x4C, // 'L'
52  kTypeMemo = 0x4D, // 'M'
53  kTypeNumber = 0x4E, // 'N'
54  kTypePicture = 0x50, // 'P'
55  kTypeDateTime = 0x54, // 'T'
56  kTypeCurrency = 0x59 // 'Y'
57  };
58 
60  struct Field {
62 
67 
69 
72  };
73 
75  struct Record {
76  bool deleted;
77  std::vector<byte *> fields;
78  };
79 
80  FoxPro();
81  ~FoxPro();
82 
83  void load(SeekableReadStream *dbf, SeekableReadStream *cdx = 0,
84  SeekableReadStream *fpt = 0);
85  void save(WriteStream *dbf, WriteStream *cdx = 0, WriteStream *fpt = 0) const;
86 
87  void getLastUpdate(uint16 &lastUpdateYear, uint8 &lastUpdateMonth, uint8 &lastUpdateDay) const;
88 
89  bool hasIndex() const;
90  bool hasMemo () const;
91 
92  size_t getFieldCount () const;
93  size_t getRecordCount() const;
94 
95  const std::vector<Field> &getFields () const;
96  const std::vector<Record> &getRecords() const;
97 
98  UString getString(const Record &record, size_t field) const;
99  int32 getInt (const Record &record, size_t field) const;
100  bool getBool (const Record &record, size_t field) const;
101  double getDouble(const Record &record, size_t field) const;
102 
103  void getDate(const Record &record, size_t field, uint16 &year, uint8 &month, uint8 &day);
104 
105  SeekableReadStream *getMemo(const Record &record, size_t field) const;
106 
107 
108  void deleteRecord(size_t record);
109 
110  size_t addFieldString(const UString &name, uint8 size);
111  size_t addFieldNumber(const UString &name, uint8 size, uint8 decimals);
112  size_t addFieldInt (const UString &name);
113  size_t addFieldBool (const UString &name);
114  size_t addFieldDate (const UString &name);
115  size_t addFieldMemo (const UString &name);
116 
117  size_t addRecord();
118 
119 
120  void setString(size_t record, size_t field, const UString &value);
121  void setInt (size_t record, size_t field, int32 value);
122  void setBool (size_t record, size_t field, bool value);
123  void setDouble(size_t record, size_t field, double value);
124 
125  void setDate(size_t record, size_t field, uint16 year, uint8 month, uint8 day);
126 
127  void setMemo(size_t record, size_t field, SeekableReadStream *value = 0);
128 
129 private:
133 
134  bool _hasIndex;
135  bool _hasMemo;
136 
137  std::vector<Field> _fields;
138  std::vector<Record> _records;
139 
141 
144 
145  // Loading helpers
146  void loadHeader (SeekableReadStream &dbf, uint32 &recordSize, uint32 &recordCount,
147  uint32 &firstRecordPos);
148  void loadFields (SeekableReadStream &dbf, uint32 recordSize);
149  void loadRecords(SeekableReadStream &dbf, uint32 recordSize, uint32 recordCount);
150  void loadMemos (SeekableReadStream &fpt);
151 
152  // Saving helpers
153  void saveHeader (WriteStream &dbf) const;
154  void saveFields (WriteStream &dbf) const;
155  void saveRecords(WriteStream &dbf) const;
156  void saveMemos (WriteStream &fpt) const;
157 
158  void addField(uint8 size);
159  void updateUpdate();
160 
161  static void checkName(const UString &name);
162 
163  static bool getInt(const byte *data, size_t size, int32 &i);
164 };
165 
166 } // End of namespace Common
167 
168 #endif // COMMON_FOXPRO_H
int32 getInt(const Record &record, size_t field) const
Definition: foxpro.cpp:354
static void checkName(const UString &name)
Definition: foxpro.cpp:519
bool getBool(const Record &record, size_t field) const
Definition: foxpro.cpp:378
bool deleted
Has this record been deleted?
Definition: foxpro.h:76
void loadRecords(SeekableReadStream &dbf, uint32 recordSize, uint32 recordCount)
Definition: foxpro.cpp:143
Definition: 2dafile.h:39
bool _hasIndex
Definition: foxpro.h:134
A class holding an UTF-8 string.
Definition: ustring.h:48
const std::vector< Record > & getRecords() const
Definition: foxpro.cpp:333
size_t addFieldInt(const UString &name)
Definition: foxpro.cpp:577
uint8_t uint8
Definition: types.h:200
A field description.
Definition: foxpro.h:60
void deleteRecord(size_t record)
Definition: foxpro.cpp:507
size_t getRecordCount() const
Definition: foxpro.cpp:325
size_t addRecord()
Definition: foxpro.cpp:699
void saveMemos(WriteStream &fpt) const
Definition: foxpro.cpp:294
void saveHeader(WriteStream &dbf) const
Definition: foxpro.cpp:220
A list storing pointer to objects, with automatic deletion.
void updateUpdate()
Definition: foxpro.cpp:914
A vector of pointer to objects, with automatic deletion.
Definition: ptrvector.h:44
uint8 _lastUpdateDay
Definition: foxpro.h:132
uint16_t uint16
Definition: types.h:202
A list of pointer to objects, with automatic deletion.
Definition: ptrlist.h:41
void saveFields(WriteStream &dbf) const
Definition: foxpro.cpp:249
size_t addFieldDate(const UString &name)
Definition: foxpro.cpp:629
size_t addFieldBool(const UString &name)
Definition: foxpro.cpp:603
size_t addFieldNumber(const UString &name, uint8 size, uint8 decimals)
Definition: foxpro.cpp:551
bool hasMemo() const
Definition: foxpro.cpp:317
void setDate(size_t record, size_t field, uint16 year, uint8 month, uint8 day)
Definition: foxpro.cpp:828
size_t addFieldMemo(const UString &name)
Definition: foxpro.cpp:655
Low-level type definitions to handle fixed width types portably.
void loadFields(SeekableReadStream &dbf, uint32 recordSize)
Definition: foxpro.cpp:105
A vector storing pointer to objects, with automatic deletion.
std::vector< Record > _records
Definition: foxpro.h:138
void save(WriteStream *dbf, WriteStream *cdx=0, WriteStream *fpt=0) const
Definition: foxpro.cpp:193
Generic interface for a writable data stream.
Definition: writestream.h:64
void setMemo(size_t record, size_t field, SeekableReadStream *value=0)
Definition: foxpro.cpp:849
double getDouble(const Record &record, size_t field) const
Definition: foxpro.cpp:396
void saveRecords(WriteStream &dbf) const
Definition: foxpro.cpp:280
Unicode string handling.
PtrList< byte, DeallocatorArray > _pool
Definition: foxpro.h:140
void setString(size_t record, size_t field, const UString &value)
Definition: foxpro.cpp:724
Type
A field type.
Definition: foxpro.h:44
A database in FoxPro 2.0 format.
Definition: foxpro.h:41
uint32_t uint32
Definition: types.h:204
uint16 _memoBlockSize
Definition: foxpro.h:142
uint16 _lastUpdateYear
Definition: foxpro.h:130
void loadHeader(SeekableReadStream &dbf, uint32 &recordSize, uint32 &recordCount, uint32 &firstRecordPos)
Definition: foxpro.cpp:74
SeekableReadStream * getMemo(const Record &record, size_t field) const
Definition: foxpro.cpp:459
const std::vector< Field > & getFields() const
Definition: foxpro.cpp:329
PtrVector< byte, DeallocatorArray > _memos
Definition: foxpro.h:143
UString getString(const Record &record, size_t field) const
Definition: foxpro.cpp:337
void getDate(const Record &record, size_t field, uint16 &year, uint8 &month, uint8 &day)
Definition: foxpro.cpp:434
size_t addFieldString(const UString &name, uint8 size)
Definition: foxpro.cpp:525
std::vector< Field > _fields
Definition: foxpro.h:137
size_t getFieldCount() const
Definition: foxpro.cpp:321
A record.
Definition: foxpro.h:75
void getLastUpdate(uint16 &lastUpdateYear, uint8 &lastUpdateMonth, uint8 &lastUpdateDay) const
Definition: foxpro.cpp:307
void load(SeekableReadStream *dbf, SeekableReadStream *cdx=0, SeekableReadStream *fpt=0)
Definition: foxpro.cpp:45
std::vector< byte * > fields
Raw field data.
Definition: foxpro.h:77
bool hasIndex() const
Definition: foxpro.cpp:313
void setDouble(size_t record, size_t field, double value)
Definition: foxpro.cpp:793
uint8 _lastUpdateMonth
Definition: foxpro.h:131
bool _hasMemo
Definition: foxpro.h:135
uint32 autoIncNext
Definition: foxpro.h:70
void setBool(size_t record, size_t field, bool value)
Definition: foxpro.cpp:774
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void addField(uint8 size)
Definition: foxpro.cpp:683
void setInt(size_t record, size_t field, int32 value)
Definition: foxpro.cpp:746
uint8 byte
Definition: types.h:209
int32_t int32
Definition: types.h:203
void loadMemos(SeekableReadStream &fpt)
Definition: foxpro.cpp:175