Switch to using -I to find includes, rather than relative paths.
[gemrb.git] / gemrb / plugins / INIImporter / INIImporter.cpp
blob0e156984779a1f34c49271fc5a1c6e5a28ca1f21
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 #include "win32def.h"
22 #include "INIImporter.h"
23 #include "Interface.h"
25 INIImp::INIImp(void)
27 str = NULL;
28 autoFree = false;
31 INIImp::~INIImp(void)
33 if (str && autoFree) {
34 delete( str );
36 for (unsigned int i = 0; i < tags.size(); i++)
37 delete( tags[i] );
40 bool INIImp::Open(DataStream* stream, bool autoFree)
42 if (stream == NULL) {
43 return false;
45 if (str && this->autoFree) {
46 delete( str );
48 str = stream;
49 this->autoFree = autoFree;
50 int cnt = 0;
51 char* strbuf = ( char* ) malloc( 4097 );
52 INITag* lastTag = NULL;
53 do {
54 cnt = str->ReadLine( strbuf, 4096 );
55 if (cnt == -1)
56 break;
57 if (cnt == 0)
58 continue;
59 if (strbuf[0] == ';') //Rem
60 continue;
61 if (strbuf[0] == '[') {
62 // this is a tag
63 char* sbptr = strbuf + 1;
64 char* TagName = sbptr;
65 while (*sbptr != '\0') {
66 if (*sbptr == ']') {
67 *sbptr = 0;
68 break;
70 sbptr++;
72 INITag* it = new INITag( TagName );
73 tags.push_back( it );
74 lastTag = it;
75 continue;
77 if (lastTag == NULL)
78 continue;
79 if (lastTag->AddLine( strbuf )) {
80 printMessage("INIImporter","", LIGHT_RED);
81 printf("Bad Line in file: %s, Section: [%s], Entry: '%s'\n", stream->filename, lastTag->GetTagName(), strbuf);
84 } while (true);
85 free( strbuf );
86 return true;
89 int INIImp::GetKeysCount(const char* Tag)
91 for (unsigned int i = 0; i < tags.size(); i++) {
92 const char* TagName = tags[i]->GetTagName();
93 if (stricmp( TagName, Tag ) == 0) {
94 return tags[i]->GetKeyCount();
97 return 0;
100 const char* INIImp::GetKeyNameByIndex(const char* Tag, int index)
102 for (unsigned int i = 0; i < tags.size(); i++) {
103 const char* TagName = tags[i]->GetTagName();
104 if (stricmp( TagName, Tag ) == 0) {
105 return tags[i]->GetKeyNameByIndex(index);
108 return NULL;
111 const char* INIImp::GetKeyAsString(const char* Tag, const char* Key,
112 const char* Default)
114 for (unsigned int i = 0; i < tags.size(); i++) {
115 const char* TagName = tags[i]->GetTagName();
116 if (stricmp( TagName, Tag ) == 0) {
117 return tags[i]->GetKeyAsString( Key, Default );
120 return Default;
123 int INIImp::GetKeyAsInt(const char* Tag, const char* Key,
124 const int Default)
126 for (unsigned int i = 0; i < tags.size(); i++) {
127 const char* TagName = tags[i]->GetTagName();
128 if (stricmp( TagName, Tag ) == 0) {
129 return tags[i]->GetKeyAsInt( Key, Default );
132 return Default;
135 float INIImp::GetKeyAsFloat(const char* Tag, const char* Key,
136 const float Default)
138 for (unsigned int i = 0; i < tags.size(); i++) {
139 const char* TagName = tags[i]->GetTagName();
140 if (stricmp( TagName, Tag ) == 0) {
141 return tags[i]->GetKeyAsFloat( Key, Default );
144 return Default;
147 bool INIImp::GetKeyAsBool(const char* Tag, const char* Key,
148 const bool Default)
150 for (unsigned int i = 0; i < tags.size(); i++) {
151 const char* TagName = tags[i]->GetTagName();
152 if (stricmp( TagName, Tag ) == 0) {
153 return tags[i]->GetKeyAsBool( Key, Default );
156 return Default;
159 #include "plugindef.h"
161 GEMRB_PLUGIN(0xB62F6D7, "INI File Importer")
162 PLUGIN_CLASS(IE_INI_CLASS_ID, INIImp)
163 END_PLUGIN()