Sort include order.
[gemrb.git] / gemrb / plugins / 2DAImporter / 2DAImporter.h
blob615a80b3dd9f5ed7a14e0b63c4f1203689cbb94e
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef P2DAIMPORTER_H
22 #define P2DAIMPORTER_H
24 #include "TableMgr.h"
26 #include "globals.h"
28 #include <cstring>
30 typedef std::vector< char*> RowEntry;
32 class p2DAImporter : public TableMgr {
33 private:
34 DataStream* str;
35 bool autoFree;
36 std::vector< char*> colNames;
37 std::vector< char*> rowNames;
38 std::vector< char*> ptrs;
39 std::vector< RowEntry> rows;
40 char defVal[32];
41 public:
42 p2DAImporter(void);
43 ~p2DAImporter(void);
44 bool Open(DataStream* stream, bool autoFree = true);
45 /** Returns the actual number of Rows in the Table */
46 inline ieDword GetRowCount() const
48 return ( ieDword ) rows.size();
51 inline ieDword GetColNamesCount() const
53 return (ieDword) colNames.size();
56 /** Returns the actual number of Columns in the Table */
57 inline ieDword GetColumnCount(unsigned int row = 0) const
59 if (rows.size() <= row) {
60 return 0;
62 return ( ieDword ) rows[row].size();
64 /** Returns a pointer to a zero terminated 2da element,
65 if it cannot return a value, it returns the default */
66 inline const char* QueryField(unsigned int row = 0, unsigned int column = 0) const
68 if (rows.size() <= row) {
69 return ( char * ) defVal;
71 if (rows[row].size() <= column) {
72 return ( char * ) defVal;
74 if (rows[row][column][0]=='*' && !rows[row][column][1]) {
75 return ( char * ) defVal;
77 return rows[row][column];
79 /** Returns a pointer to a zero terminated 2da element,
80 uses column name and row name to search the field */
81 inline const char* QueryField(const char* row, const char* column) const
83 int rowi, coli;
85 rowi = GetRowIndex(row);
87 if (rowi < 0) {
88 return ( char * ) defVal;
91 coli = GetColumnIndex(column);
93 if (coli < 0) {
94 return ( char * ) defVal;
97 return QueryField((unsigned int) rowi, (unsigned int) coli);
100 virtual const char* QueryDefault() const
102 return defVal;
105 inline int GetRowIndex(const char* string) const
107 for (unsigned int index = 0; index < rowNames.size(); index++) {
108 if (stricmp( rowNames[index], string ) == 0) {
109 return (int) index;
112 return -1;
115 inline int GetColumnIndex(const char* string) const
117 for (unsigned int index = 0; index < colNames.size(); index++) {
118 if (stricmp( colNames[index], string ) == 0) {
119 return (int) index;
122 return -1;
125 inline const char* GetColumnName(unsigned int index) const
127 if (index < colNames.size()) {
128 return colNames[index];
130 return "";
133 inline const char* GetRowName(unsigned int index) const
135 if (index < rowNames.size()) {
136 return rowNames[index];
138 return "";
141 inline int FindTableValue(unsigned int col, long val, int start) const
143 ieDword row, max;
145 max = GetRowCount();
146 for (row = start; row < max; row++) {
147 const char* ret = QueryField( row, col );
148 long Value;
149 if (valid_number( ret, Value ) && (Value == val) )
150 return (int) row;
152 return -1;
156 #endif