Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
193 andreas 1
/*
2
 * Copyright (C) 2022 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
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.
13
 *
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 Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "tmap.h"
20
#include "texpat++.h"
21
#include "tresources.h"
22
#include "tconfig.h"
23
#include "terror.h"
24
 
25
#if __cplusplus < 201402L
26
#   error "This module requires at least C++14 standard!"
27
#else
28
#   if __cplusplus < 201703L
29
#       include <experimental/filesystem>
30
namespace fs = std::experimental::filesystem;
31
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
32
#   else
33
#       include <filesystem>
34
#       ifdef __ANDROID__
35
namespace fs = std::__fs::filesystem;
36
#       else
37
namespace fs = std::filesystem;
38
#       endif
39
#   endif
40
#endif
41
 
42
using std::string;
43
using std::vector;
44
using namespace Expat;
45
 
46
TMap::TMap(const std::string& file)
47
    : mFile(file)
48
{
49
    DECL_TRACER("TMap::TMap(std::string& file)");
50
 
51
    if (!fs::exists(file))
52
    {
53
        MSG_ERROR("File " << file << " does not exist!");
54
        mError = true;
55
        return;
56
    }
57
 
58
    bool e = readMap();
59
    mError = !e;
60
}
61
 
62
bool TMap::readMap()
63
{
64
    DECL_TRACER("TMap::readMap()");
65
 
66
    string path = makeFileName(mFile, "map.xma");
67
    vector<string> elements = { "cm", "am", "lm", "bm", "sm", "strm", "pm" };
68
 
69
    if (!isValidFile())
70
    {
71
        MSG_ERROR("File \"" << path << "\" is not a regular readable file!");
72
        return false;
73
    }
74
 
75
    TExpat xml(path);
76
    xml.setEncoding(ENC_CP1250);
77
 
78
    if (!xml.parse())
79
        return false;
80
 
81
    int depth = 0;
82
    size_t index = 0;
83
    vector<string>::iterator mapIter;
84
    size_t oldIndex = 0;
85
 
86
    if (elements.size() == 0)
87
        return false;
88
 
89
    for (mapIter = elements.begin(); mapIter != elements.end(); ++mapIter)
90
    {
91
        if ((index = xml.getElementIndex(*mapIter, &depth)) == TExpat::npos)
92
        {
93
            MSG_WARNING("Element \"" << *mapIter << "\" was not found!");
94
            continue;
95
        }
96
 
97
        MAP_T map;
98
        MAP_BM_T mapBm;
99
        MAP_PM_T mapPm;
100
        string name, content;
101
 
102
        while ((index = xml.getNextElementFromIndex(index, &name, nullptr, nullptr)) != TExpat::npos)
103
        {
104
            string el = name;
105
 
106
            if (el.compare("me") == 0)
107
            {
108
                while ((index = xml.getNextElementFromIndex(index, &name, &content, nullptr)) != TExpat::npos)
109
                {
110
                    string e = name;
111
 
112
                    if (mapIter->compare("cm") == 0 || mapIter->compare("am") == 0 ||
113
                        mapIter->compare("lm") == 0 || mapIter->compare("strm") == 0)
114
                    {
115
                        if (e.compare("p") == 0)
116
                            map.p = xml.convertElementToInt(content);
117
                        else if (e.compare("c") == 0)
118
                            map.c = xml.convertElementToInt(content);
119
                        else if (e.compare("ax") == 0)
120
                            map.ax = xml.convertElementToInt(content);
121
                        else if (e.compare("pg") == 0)
122
                            map.pg = xml.convertElementToInt(content);
123
                        else if (e.compare("bt") == 0)
124
                            map.bt = xml.convertElementToInt(content);
125
                        else if (e.compare("pn") == 0)
126
                            map.pn = content;
127
                        else if (e.compare("bn") == 0)
128
                            map.bn = content;
129
                    }
130
                    else if (mapIter->compare("bm") == 0)
131
                    {
132
                        while ((index = xml.getNextElementFromIndex(index, &name, &content, nullptr)) != TExpat::npos)
133
                        {
134
                            string im = name;
135
 
136
                            if (im.compare("i") == 0)
137
                                mapBm.i = content;
138
                            else if (im.compare("id") == 0)
139
                                mapBm.id = xml.convertElementToInt(content);
140
                            else if (im.compare("rt") == 0)
141
                                mapBm.rt = xml.convertElementToInt(content);
142
                            else if (im.compare("pg") == 0)
143
                                mapBm.pg = xml.convertElementToInt(content);
144
                            else if (im.compare("bt") == 0)
145
                                mapBm.bt = xml.convertElementToInt(content);
146
                            else if (im.compare("st") == 0)
147
                                mapBm.st = xml.convertElementToInt(content);
148
                            else if (im.compare("sl") == 0)
149
                                mapBm.sl = xml.convertElementToInt(content);
150
                            else if (im.compare("pn") == 0)
151
                                mapBm.pn = content;
152
                            else if (im.compare("bn") == 0)
153
                                mapBm.bn = content;
154
 
155
                            oldIndex = index;
156
                        }
157
 
158
                        mMap.map_bm.push_back(mapBm);
159
 
160
                        if (index == TExpat::npos)
161
                            index = oldIndex + 1;
162
                    }
163
                    else if (mapIter->compare("sm") == 0)
164
                    {
165
                        if (e.compare("i") == 0)
166
                            mMap.map_sm.push_back(content);
167
                    }
168
                    else if (mapIter->compare("pm") == 0)
169
                    {
170
                        if (e.compare("a") == 0)
171
                            mapPm.a = xml.convertElementToInt(content);
172
                        else if (e.compare("t") == 0)
173
                            mapPm.t = content;
174
                        else if (e.compare("pg") == 0)
175
                            mapPm.pg = xml.convertElementToInt(content);
176
                        else if (e.compare("bt") == 0)
177
                            mapPm.bt = xml.convertElementToInt(content);
178
                        else if (e.compare("pn") == 0)
179
                            mapPm.pn = content;
180
                        else if (e.compare("bn") == 0)
181
                            mapPm.bn = content;
182
                    }
183
 
184
                    oldIndex = index;
185
                }
186
 
187
                if (mapIter->compare("cm") == 0)
188
                    mMap.map_cm.push_back(map);
189
                else if (mapIter->compare("am") == 0)
190
                    mMap.map_am.push_back(map);
191
                else if (mapIter->compare("lm") == 0)
192
                    mMap.map_lm.push_back(map);
193
                else if (mapIter->compare("strm") == 0)
194
                    mMap.map_strm.push_back(map);
195
                else if (mapIter->compare("pm") == 0)
196
                    mMap.map_pm.push_back(mapPm);
197
 
198
                if (index == TExpat::npos)
199
                    index = oldIndex + 1;
200
            }
201
 
202
            oldIndex = index;
203
        }
204
    }
205
 
206
    return true;
207
}
208
 
209
vector<TMap::MAP_T> TMap::findButtons(int port, vector<int>& channels, MAP_TYPE mt)
210
{
331 andreas 211
    DECL_TRACER("TMap::findButtons(int port, vector<int>& channels, MAP_TYPE mt)");
193 andreas 212
 
213
    vector<MAP_T> map;
214
    vector<int>::iterator iter;
215
 
216
    if (channels.empty())
217
    {
218
        MSG_WARNING("Got empty channel list!");
219
        return map;
220
    }
221
 
222
    vector<MAP_T> localMap;
223
 
224
    switch (mt)
225
    {
226
        case TYPE_AM:   localMap = mMap.map_am; break;
227
        case TYPE_CM:   localMap = mMap.map_cm; break;
228
        case TYPE_LM:   localMap = mMap.map_lm; break;
229
    }
230
 
231
    if (localMap.empty())
232
    {
233
        MSG_WARNING("The internal list of elements is empty!")
234
        return map;
235
    }
236
 
237
    for (iter = channels.begin(); iter != channels.end(); ++iter)
238
    {
239
        vector<MAP_T>::iterator mapIter;
240
 
241
        for (mapIter = localMap.begin(); mapIter != localMap.end(); ++mapIter)
242
        {
243
            if (mapIter->p == port && mapIter->c == *iter)
244
                map.push_back(*mapIter);
245
        }
246
    }
247
 
248
    MSG_DEBUG("Found " << map.size() << " buttons.");
249
    return map;
250
}
251
 
252
vector<TMap::MAP_T> TMap::findButtonByName(const string& name)
253
{
254
    DECL_TRACER("TAmxCommands::findButtonByName(const string& name)");
255
 
256
    vector<MAP_T> map;
257
 
258
    if (mMap.map_cm.empty())
259
    {
260
        MSG_WARNING("The internal list of elements is empty!")
261
        return map;
262
    }
263
 
264
    vector<MAP_T>::iterator mapIter;
265
 
266
    for (mapIter = mMap.map_cm.begin(); mapIter != mMap.map_cm.end(); ++mapIter)
267
    {
268
        if (mapIter->bn == name)
269
            map.push_back(*mapIter);
270
    }
271
 
272
    MSG_DEBUG("Found " << map.size() << " buttons.");
273
    return map;
274
}
275
 
276
string TMap::findImage(int bt, int page, int instance)
277
{
278
    DECL_TRACER("TAmxCommands::findImage(int bt, int page, int instance)");
279
 
280
    vector<MAP_BM_T> mapBm = mMap.map_bm;
281
    vector<MAP_BM_T>::iterator iter;
282
 
283
    if (mapBm.empty())
284
        return string();
285
 
286
    for (iter = mapBm.begin(); iter != mapBm.end(); ++iter)
287
    {
288
        if (iter->bt == bt && iter->pg == page && !iter->i.empty())
289
        {
290
            if (instance >= 0 && iter->st == (instance + 1))
291
                return iter->i;
292
            else if (instance < 0)
293
                return iter->i;
294
        }
295
    }
296
 
297
    return string();
298
}
299
 
300
string TMap::findImage(const string& name)
301
{
302
    DECL_TRACER("TAmxCommands::findImage(const string& name)");
303
 
304
    vector<MAP_BM_T> mapBm = mMap.map_bm;
305
    vector<MAP_BM_T>::iterator iter;
306
 
307
    if (mapBm.empty() || name.empty())
308
        return string();
309
 
310
    size_t cnt = 0;
311
 
312
    for (iter = mapBm.begin(); iter != mapBm.end(); ++iter)
313
    {
314
        if (!iter->i.empty() && iter->i.find(name) != string::npos)
315
        {
316
            size_t pos = iter->i.find_last_of(".");
317
 
318
            if (pos != string::npos)
319
            {
320
                string left = iter->i.substr(0, pos);
225 andreas 321
 
193 andreas 322
                if (left == name)
323
                    return iter->i;
324
            }
325
        }
326
 
327
        cnt++;
328
    }
329
 
330
    MSG_WARNING("No image with name " << name << " in table found!");
331
    return string();
332
}
333
 
334
vector<TMap::MAP_T> TMap::findBargraphs(int port, vector<int>& channels)
335
{
336
    DECL_TRACER("TAmxCommands::findBargraphs(int port, vector<int>& channels)");
337
 
338
    vector<MAP_T> map;
339
    vector<int>::iterator iter;
340
 
341
    if (channels.size() == 0)
342
        return map;
343
 
344
    for (iter = channels.begin(); iter != channels.end(); ++iter)
345
    {
346
        vector<MAP_T>::iterator mapIter;
347
 
348
        if (mMap.map_lm.size() > 0)
349
        {
350
            for (mapIter = mMap.map_lm.begin(); mapIter != mMap.map_lm.end(); ++mapIter)
351
            {
352
                if (mapIter->p == port && mapIter->c == *iter)
353
                    map.push_back(*mapIter);
354
            }
355
        }
356
    }
357
 
358
    MSG_DEBUG("Found " << map.size() << " buttons.");
359
    return map;
360
}
361
 
362
vector<string> TMap::findSounds()
363
{
364
    DECL_TRACER("TAmxCommands::findSounds()");
365
 
366
    return mMap.map_sm;
367
}
368
 
369
bool TMap::soundExist(const string& sname)
370
{
371
    DECL_TRACER("TAmxCommands::soundExist(const string sname)");
372
 
373
    if (mMap.map_sm.size() == 0)
374
        return false;
375
 
376
    vector<string>::iterator iter;
377
 
378
    for (iter = mMap.map_sm.begin(); iter != mMap.map_sm.end(); ++iter)
379
    {
380
        if (iter->compare(sname) == 0)
381
            return true;
382
    }
383
 
384
    return false;
385
}