Welcome to mirror list, hosted at ThFree Co, Russian Federation.

xml_helper.cpp « XML « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4a7c1bcdac840fc334187a1440522eb0c5675b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//-----------------------------------------------------------------------------
//           Name: xml_helper.cpp
//      Developer: Wolfire Games LLC
//    Description:
//        License: Read below
//-----------------------------------------------------------------------------
//
//   Copyright 2022 Wolfire Games LLC
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
//-----------------------------------------------------------------------------

#include <Internal/error.h>
#include <Internal/common.h>
#include <Internal/filesystem.h>

#include <Memory/allocation.h>
#include <XML/xml_helper.h>
#include <Compat/fileio.h>

TiXmlElement *XmlHelper::findNode(TiXmlDocument &doc, std::string &item, TiXmlElement *element) {
    // TiXmlElement* element = NULL;
    while (item.size()) {
        if (element == NULL) {
            element = doc.FirstChildElement(item.substr(0, item.find('/')));
        } else {
            element = element->FirstChildElement(item.substr(0, item.find('/')));
        }

        if (element == NULL)
            return NULL;

        if (std::string::npos != item.find('/'))
            item = item.substr(item.find('/') + 1);
        else
            item = "";
    }

    return element;
}

TiXmlElement *XmlHelper::findNode(TiXmlDocument &doc, const char *item, TiXmlElement *element) {
    std::string i = item;
    return findNode(doc, i, element);
}

bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, std::string &text) {
    std::string istr(item);
    TiXmlElement *element = findNode(doc, istr);

    if (element != NULL) {
        text = element->GetText();
        return true;
    }

    return false;
}

bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, double &d) {
    std::string text;
    bool retval = getNodeValue(doc, item, text);

    if (retval) {
        d = ::atof(text.c_str());
    }

    return retval;
}

bool XmlHelper::getNodeValue(TiXmlDocument &doc, const char *item, float &f) {
    std::string text;
    bool retval = getNodeValue(doc, item, text);

    if (retval) {
        f = (float)(::atof(text.c_str()));
    }

    return retval;
}

bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, std::string &text) {
    std::string istr(item);
    element = findNode(doc, istr, element);

    if (element != NULL) {
        text = element->GetText();
        return true;
    }

    return false;
}

bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, double &d) {
    std::string text;
    bool retval = getNodeValue(doc, element, item, text);

    if (retval) {
        d = ::atof(text.c_str());
    }

    return retval;
}

bool XmlHelper::getNodeValue(TiXmlDocument &doc, TiXmlElement *element, const char *item, float &f) {
    std::string text;
    bool retval = getNodeValue(doc, element, item, text);

    if (retval) {
        f = (float)(::atof(text.c_str()));
    }

    return retval;
}

void GetRange(const TiXmlElement *el,
              const std::string &val_str,
              const std::string &min_str,
              const std::string &max_str,
              float &min_val,
              float &max_val) {
    int result = el->QueryFloatAttribute(val_str.c_str(), &min_val);
    if (result != TIXML_SUCCESS) {
        result = el->QueryFloatAttribute(min_str.c_str(), &min_val);
        result = el->QueryFloatAttribute(max_str.c_str(), &max_val);
    } else {
        max_val = min_val;
    }
}

bool LoadXML(TiXmlDocument &doc,
             const std::string &path,
             const std::string type) {
    char abs_path[kPathSize];
    bool retry = true;
    if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) == -1) {
        return false;
    }
    doc.LoadFile(abs_path);
    return true;
}

bool LoadXMLRetryable(TiXmlDocument &doc,
                      const std::string &path,
                      const std::string type) {
    char abs_path[kPathSize];
    bool retry = true;
    if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) == -1) {
        ErrorResponse err;
        while (retry) {
            err = DisplayError("Error",
                               (type + " file \"" + path + "\" did not load correctly.").c_str(),
                               _ok_cancel_retry);
            if (err != _retry) {
                return false;
            }
            if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1) {
                retry = false;
            }
        }
    }
    doc.LoadFile(abs_path);
    return true;
}

uint8_t *StackLoadText(const char *path, size_t *size_out) {
    uint8_t *mem = NULL;
    long file_size = 0;
    char abs_path[kPathSize];
    bool retry = true;
    if (FindFilePath(path, abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1) {
        FILE *file = my_fopen(abs_path, "rb");
        if (file) {
            fseek(file, 0, SEEK_END);
            file_size = ftell(file);
            if (file_size > 0) {
                rewind(file);

                LOG_ASSERT(file_size < (1024 * 1024));

                mem = (uint8_t *)alloc.stack.Alloc(file_size + 1);
                if (mem) {
                    size_t count = fread(mem, 1, file_size, file);
                    if ((long)count == file_size) {
                        mem[file_size] = '\0';
                    } else {
                        LOGE << "Did not read expected amount of data from file: " << abs_path << std::endl;
                        alloc.stack.Free(mem);
                        mem = NULL;
                    }
                } else {
                    LOGF << "Could not allocate " << file_size + 1 << " bytes on stack for file " << abs_path << std::endl;
                }
            }
            fclose(file);
        }
    }

    if (size_out) {
        *size_out = file_size;
    }
    return mem;
}

bool LoadText(void *&mem, const char *path) {
    FILE *file = my_fopen(path, "rb");
    if (!file) {
        return false;
    }

    fseek(file, 0, SEEK_END);
    long file_size = ftell(file);
    rewind(file);

    mem = OG_MALLOC(file_size + 1);
    if (!mem) {
        FatalError("Error", "Could not allocate memory for file: %s", path);
    }
    size_t read = fread(mem, 1, file_size, file);

    if (read != file_size) {
        LOGW << "Read less than ftell told us we would!" << std::endl;
    }

    ((char *)mem)[read] = '\0';

    fclose(file);

    return true;
}

bool LoadTextRetryable(void *&mem,
                       const std::string &path,
                       const std::string type) {
    char abs_path[kPathSize];
    bool retry = true;
    if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) == -1) {
        ErrorResponse err;
        while (retry) {
            err = DisplayError("Error",
                               (type + " file \"" + path + "\" did not load correctly.").c_str(),
                               _ok_cancel_retry);
            if (err != _retry) {
                return false;
            }
            if (FindFilePath(path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths | kAbsPath) != -1) {
                retry = false;
            }
        }
    }
    LoadText(mem, abs_path);
    return true;
}

void LoadAttribIntoString(TiXmlElement *el, const char *attrib, std::string &str) {
    const char *label = el->Attribute(attrib);
    if (label) {
        str = label;
    }
}