Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 andreas 1
/*
101 andreas 2
 * Copyright (C) 2020 to 2022 by Andreas Theofilu <andreas@theosys.at>
11 andreas 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 "tamxcommands.h"
20
#include "terror.h"
21
#include "tresources.h"
14 andreas 22
#include "tconfig.h"
78 andreas 23
#include "texpat++.h"
11 andreas 24
 
83 andreas 25
#include <string>
26
#include <vector>
27
 
11 andreas 28
using std::string;
29
using std::vector;
78 andreas 30
using namespace Expat;
11 andreas 31
 
32
typedef struct CMD_DEFINATIONS
33
{
34
    string cmd;
35
    bool hasChannels{false};
36
    bool hasPars{false};
14 andreas 37
    char separator{0};
11 andreas 38
}CMD_DEFINATIONS;
39
 
40
CMD_DEFINATIONS cmdDefinations[] = {
14 andreas 41
    { "@WLD", false, true, ',' },
42
    { "@AFP", false, true, ',' },
43
    { "@GCE", false, true, ',' },
44
    { "@APG", false, true, ';' },
45
    { "@CPG", false, true, ',' },
46
    { "@DPG", false, true, ';' },
47
    { "@PDR", false, true, ';' },
48
    { "@PHE", false, true, ';' },
49
    { "@PHP", false, true, ';' },
50
    { "@PHT", false, true, ';' },
51
    { "@PPA", false, true, ',' },
52
    { "@PPF", false, true, ';' },
53
    { "@PPG", false, true, ';' },
54
    { "@PPK", false, true, ',' },
55
    { "@PPM", false, true, ';' },
56
    { "@PPN", false, true, ';' },
57
    { "@PPT", false, true, ';' },
58
    { "@PPX", false, false, '\0' },
59
    { "@PSE", false, true, ';' },
60
    { "@PSP", false, true, ';' },
61
    { "@PST", false, true, ';' },
62
    { "PAGE", false, true, ',' },
63
    { "PPOF", false, true, ';' },
64
    { "PPOG", false, true, ';' },
65
    { "PPON", false, true, ';' },
66
    { "^ANI", true, true, ',' },
67
    { "^APF", true, true, ',' },
68
    { "^BAT", true, true, ',' },
69
    { "^BAU", true, true, ',' },
70
    { "^BCB", true, true, ',' },
71
    { "?BCB", true, true, ',' },
72
    { "^BCF", true, true, ',' },
73
    { "?BCF", true, true, ',' },
74
    { "^BCT", true, true, ',' },
75
    { "?BCT", true, true, ',' },
76
    { "^BDO", true, true, ',' },
77
    { "^BFB", true, true, ',' },
78
    { "^BIM", true, true, ',' },
79
    { "^BLN", true, true, ',' },
80
    { "^BMC", true, true, ',' },
81
    { "^BMF", true, true, ',' },
82
    { "^BMI", true, true, ',' },
83
    { "^BML", true, true, ',' },
84
    { "^BMP", true, true, ',' },
85
    { "?BMP", true, true, ',' },
86
    { "^BNC", true, true, ',' },
87
    { "^BNN", true, true, ',' },
88
    { "^BNT", true, true, ',' },
89
    { "^BOP", true, true, ',' },
90
    { "?BOP", true, true, ',' },
91
    { "^BOR", true, true, ',' },
92
    { "^BOS", true, true, ',' },
93
    { "^BPP", true, true, ',' },
94
    { "^BRD", true, true, ',' },
95
    { "?BRD", true, true, ',' },
96
    { "^BSF", true, true, ',' },
97
    { "^BSP", true, true, ',' },
98
    { "^BSM", true, false, ',' },
99
    { "^BSO", true, true, ',' },
100
    { "^BVL", true, true, ',' },
101
    { "^BVN", false, true, ',' },
102
    { "^BVP", true, true, ',' },
103
    { "^BVT", true, true, ',' },
104
    { "^BWW", true, true, ',' },
105
    { "?BWW", true, true, ',' },
106
    { "^CPF", true, false, ',' },
107
    { "^DLD", false, false, ',' },
108
    { "^DPF", true, true, ',' },
109
    { "^ENA", true, true, ',' },
110
    { "^FON", true, true, ',' },
111
    { "?FON", true, true, ',' },
112
    { "^GDI", true, true, ',' },
113
    { "^GIV", true, true, ',' },
114
    { "^GLH", true, true, ',' },
115
    { "^GLL", true, true, ',' },
116
    { "^GRD", true, true, ',' },
117
    { "^GRU", true, true, ',' },
118
    { "^GSC", true, true, ',' },
119
    { "^GSN", true, true, ',' },
120
    { "^ICO", true, true, ',' },
121
    { "?ICO", true, true, ',' },
122
    { "^IRM", false, true, ',' },
123
    { "^JSB", true, true, ',' },
124
    { "?JSB", true, true, ',' },
125
    { "^JSI", true, true, ',' },
126
    { "?JSI", true, true, ',' },
127
    { "^JST", true, true, ',' },
128
    { "?JST", true, true, ',' },
129
    { "^MBT", false, false, ',' },
130
    { "^MDC", false, false, ',' },
131
    { "^SHO", true, true, ',' },
132
    { "^TEC", true, true, ',' },
133
    { "?TEC", true, true, ',' },
134
    { "^TEF", true, true, ',' },
135
    { "?TEF", true, true, ',' },
136
    { "^TOP", false, true, ',' },
137
    { "^TXT", true, true, ',' },
138
    { "?TXT", true, true, ',' },
139
    { "^UNI", true, true, ',' },
140
    { "^LPC", false, true, ',' },
141
    { "^LPR", false, true, ',' },
142
    { "^LPS", false, true, ',' },
143
    { "ABEEP", false, false, ',' },
144
    { "ADBEEP", false, false, ',' },
145
    { "@AKB", false, true, ';' },
146
    { "AKEYB", false, true, ',' },
147
    { "AKEYP", false, true, ',' },
148
    { "AKEYR", false, true, ',' },
149
    { "@AKP", false, true, ';' },
150
    { "@AKR", false, false, ',' },
151
    { "BEEP", false, false, ',' },
152
    { "BRIT", false, true, ',' },
153
    { "@BRT", false, true, ',' },
154
    { "DBEEP", false, false, ',' },
155
    { "@EKP", false, true, ';' },
156
    { "PKEYP", false, true, ',' },
63 andreas 157
    { "@PKB", false, true, ';' },
14 andreas 158
    { "@PKP", false, true, ';' },
159
    { "SETUP", false, false, ',' },
160
    { "SHUTDOWN", false, false, ',' },
161
    { "SLEEP", false, false, ',' },
162
    { "@SOU", false, true, ',' },
163
    { "@TKP", false, true, ';' },
164
    { "TPAGEON", false, false, ',' },
165
    { "TPAGEOFF", false, false, ',' },
166
    { "@VKB", false, false, ',' },
167
    { "WAKE", false, false, ',' },
168
    { "^CAL", false, false, ',' },
169
    { "^KPS", false, true, ',' },
170
    { "^VKS", false, true, ',' },
171
    { "@PWD", false, true, ',' },
172
    { "^PWD", false, true, ',' },
173
    { "^BBR", true, true, ',' },
174
    { "^RAF", false, true, ',' },
175
    { "^RFR", false, true, ',' },
176
    { "^RMF", false, true, ',' },
177
    { "^RSR", false, true, ',' },
178
    { "^MODEL?", false, false, ',' },
179
    { "^ICS", false, true, ',' },
180
    { "^ICE", false, false, ',' },
181
    { "^ICM", false, true, ',' },
182
    { "^PHN", false, true, ',' },
183
    { "?PHN", false, true, ',' },
184
    { "LEVON", false, false, ',' },
185
    { "RXON", false, false, ',' },
186
    { "ON", false, true, ',' },
187
    { "OFF", false, true, ',' },
15 andreas 188
    { "LEVEL", false, true, ',' },
189
    { "BLINK", false, true, ',' },
23 andreas 190
    { "#FTR", false, true, ':' },
14 andreas 191
    { "", false, false, '\0' }
11 andreas 192
};
193
 
194
CMD_DEFINATIONS& findCmdDefines(const string& cmd)
195
{
196
    DECL_TRACER("findCmdDefines(const string& cmd)");
197
 
198
    int i = 0;
101 andreas 199
    string uCmd = cmd;
200
    uCmd = toUpper(uCmd);
11 andreas 201
 
202
    while (cmdDefinations[i].cmd.length() > 0)
203
    {
101 andreas 204
        if (cmdDefinations[i].cmd.compare(uCmd) == 0)
11 andreas 205
            return cmdDefinations[i];
206
 
207
        i++;
208
    }
209
 
210
    return cmdDefinations[i];
211
}
212
 
213
TAmxCommands::TAmxCommands()
214
{
215
    DECL_TRACER("TAmxCommands::TAmxCommands()");
14 andreas 216
    readMap();
11 andreas 217
}
218
 
219
TAmxCommands::~TAmxCommands()
220
{
221
    DECL_TRACER("TAmxCommands::~TAmxCommands()");
222
}
223
 
14 andreas 224
bool TAmxCommands::readMap()
225
{
226
    DECL_TRACER("TAmxCommands::readMap()");
227
 
88 andreas 228
    string path = makeFileName(TConfig::getProjectPath(), "map.xma");
14 andreas 229
    vector<string> elements = { "cm", "am", "lm", "bm", "sm", "strm", "pm" };
230
 
88 andreas 231
    if (!isValidFile())
232
    {
233
        MSG_ERROR("File \"" << path << "\" is not a regular readable file!");
234
        return false;
235
    }
14 andreas 236
 
78 andreas 237
    TExpat xml(path);
238
    xml.setEncoding(ENC_CP1250);
14 andreas 239
 
78 andreas 240
    if (!xml.parse())
14 andreas 241
        return false;
242
 
78 andreas 243
    int depth = 0;
244
    size_t index = 0;
14 andreas 245
    vector<string>::iterator mapIter;
78 andreas 246
    size_t oldIndex = 0;
14 andreas 247
 
88 andreas 248
    if (elements.size() == 0)
249
        return false;
250
 
14 andreas 251
    for (mapIter = elements.begin(); mapIter != elements.end(); mapIter++)
252
    {
78 andreas 253
        if ((index = xml.getElementIndex(*mapIter, &depth)) == TExpat::npos)
14 andreas 254
        {
23 andreas 255
            MSG_WARNING("Element \"" << *mapIter << "\" was not found!");
256
            continue;
14 andreas 257
        }
258
 
259
        MAP_T map;
260
        MAP_BM_T mapBm;
261
        MAP_PM_T mapPm;
78 andreas 262
        string name, content;
14 andreas 263
 
78 andreas 264
        while ((index = xml.getNextElementFromIndex(index, &name, nullptr, nullptr)) != TExpat::npos)
14 andreas 265
        {
78 andreas 266
            string el = name;
14 andreas 267
 
268
            if (el.compare("me") == 0)
269
            {
78 andreas 270
                while ((index = xml.getNextElementFromIndex(index, &name, &content, nullptr)) != TExpat::npos)
14 andreas 271
                {
78 andreas 272
                    string e = name;
14 andreas 273
 
274
                    if (mapIter->compare("cm") == 0 || mapIter->compare("am") == 0 ||
275
                        mapIter->compare("lm") == 0 || mapIter->compare("strm") == 0)
276
                    {
277
                        if (e.compare("p") == 0)
78 andreas 278
                            map.p = xml.convertElementToInt(content);
14 andreas 279
                        else if (e.compare("c") == 0)
78 andreas 280
                            map.c = xml.convertElementToInt(content);
14 andreas 281
                        else if (e.compare("ax") == 0)
78 andreas 282
                            map.ax = xml.convertElementToInt(content);
14 andreas 283
                        else if (e.compare("pg") == 0)
78 andreas 284
                            map.pg = xml.convertElementToInt(content);
14 andreas 285
                        else if (e.compare("bt") == 0)
78 andreas 286
                            map.bt = xml.convertElementToInt(content);
14 andreas 287
                        else if (e.compare("pn") == 0)
78 andreas 288
                            map.pn = content;
14 andreas 289
                        else if (e.compare("bn") == 0)
78 andreas 290
                            map.bn = content;
14 andreas 291
                    }
292
                    else if (mapIter->compare("bm") == 0)
293
                    {
78 andreas 294
                        while ((index = xml.getNextElementFromIndex(index, &name, &content, nullptr)) != TExpat::npos)
14 andreas 295
                        {
78 andreas 296
                            string im = name;
14 andreas 297
 
298
                            if (im.compare("i") == 0)
78 andreas 299
                                mapBm.i = content;
14 andreas 300
                            else if (im.compare("id") == 0)
78 andreas 301
                                mapBm.id = xml.convertElementToInt(content);
14 andreas 302
                            else if (im.compare("rt") == 0)
78 andreas 303
                                mapBm.rt = xml.convertElementToInt(content);
14 andreas 304
                            else if (im.compare("pg") == 0)
78 andreas 305
                                mapBm.pg = xml.convertElementToInt(content);
14 andreas 306
                            else if (im.compare("bt") == 0)
78 andreas 307
                                mapBm.bt = xml.convertElementToInt(content);
14 andreas 308
                            else if (im.compare("st") == 0)
78 andreas 309
                                mapBm.st = xml.convertElementToInt(content);
14 andreas 310
                            else if (im.compare("sl") == 0)
78 andreas 311
                                mapBm.sl = xml.convertElementToInt(content);
14 andreas 312
                            else if (im.compare("pn") == 0)
78 andreas 313
                                mapBm.pn = content;
14 andreas 314
                            else if (im.compare("bn") == 0)
78 andreas 315
                                mapBm.bn = content;
14 andreas 316
 
78 andreas 317
                            oldIndex = index;
14 andreas 318
                        }
319
 
320
                        mMap.map_bm.push_back(mapBm);
78 andreas 321
 
322
                        if (index == TExpat::npos)
323
                            index = oldIndex + 1;
14 andreas 324
                    }
325
                    else if (mapIter->compare("sm") == 0)
326
                    {
327
                        if (e.compare("i") == 0)
78 andreas 328
                            mMap.map_sm.push_back(content);
14 andreas 329
                    }
330
                    else if (mapIter->compare("pm") == 0)
331
                    {
332
                        if (e.compare("a") == 0)
78 andreas 333
                            mapPm.a = xml.convertElementToInt(content);
14 andreas 334
                        else if (e.compare("t") == 0)
78 andreas 335
                            mapPm.t = content;
14 andreas 336
                        else if (e.compare("pg") == 0)
78 andreas 337
                            mapPm.pg = xml.convertElementToInt(content);
14 andreas 338
                        else if (e.compare("bt") == 0)
78 andreas 339
                            mapPm.bt = xml.convertElementToInt(content);
14 andreas 340
                        else if (e.compare("pn") == 0)
78 andreas 341
                            mapPm.pn = content;
14 andreas 342
                        else if (e.compare("bn") == 0)
78 andreas 343
                            mapPm.bn = content;
14 andreas 344
                    }
345
 
78 andreas 346
                    oldIndex = index;
14 andreas 347
                }
348
 
349
                if (mapIter->compare("cm") == 0)
350
                    mMap.map_cm.push_back(map);
351
                else if (mapIter->compare("am") == 0)
352
                    mMap.map_am.push_back(map);
353
                else if (mapIter->compare("lm") == 0)
354
                    mMap.map_lm.push_back(map);
355
                else if (mapIter->compare("strm") == 0)
356
                    mMap.map_strm.push_back(map);
357
                else if (mapIter->compare("pm") == 0)
358
                    mMap.map_pm.push_back(mapPm);
78 andreas 359
 
360
                if (index == TExpat::npos)
361
                    index = oldIndex + 1;
14 andreas 362
            }
363
 
78 andreas 364
            oldIndex = index;
14 andreas 365
        }
366
    }
367
 
368
    return true;
369
}
370
 
371
vector<string> TAmxCommands::getFields(string& msg, char sep)
372
{
373
    DECL_TRACER("TAmxCommands::getFields(string& msg, char sep)");
374
 
375
    vector<string> flds;
376
    bool bStr = false;
377
    string part;
378
 
16 andreas 379
    for (size_t i = 0; i < msg.length(); i++)
14 andreas 380
    {
16 andreas 381
        if (msg.at(i) == sep && !bStr)
14 andreas 382
        {
383
            flds.push_back(part);
384
            part.clear();
385
            continue;
386
        }
16 andreas 387
        else if (msg.at(i) == '\'' && !bStr)
14 andreas 388
            bStr = true;
16 andreas 389
        else if (msg.at(i) == '\'' && bStr)
14 andreas 390
            bStr = false;
391
        else
16 andreas 392
            part.append(msg.substr(i, 1));
14 andreas 393
    }
394
 
395
    if (!part.empty())
396
        flds.push_back(part);
397
 
83 andreas 398
    if (flds.size() > 0 && TStreamError::checkFilter(HLOG_DEBUG))
14 andreas 399
    {
400
        MSG_DEBUG("Found fields:");
401
        vector<string>::iterator iter;
402
        int i = 1;
403
 
404
        for (iter = flds.begin(); iter != flds.end(); iter++)
405
        {
406
            MSG_DEBUG("    " << i << ": " << *iter);
407
            i++;
408
        }
409
    }
410
 
411
    return flds;
412
}
413
 
19 andreas 414
vector<MAP_T> TAmxCommands::findButtons(int port, vector<int>& channels, MAP_TYPE mt)
14 andreas 415
{
19 andreas 416
    DECL_TRACER("TAmxCommands::findButtons(int port, vector<int>& channels, MAP_TYPE mt)");
14 andreas 417
 
418
    vector<MAP_T> map;
419
    vector<int>::iterator iter;
420
 
19 andreas 421
    if (channels.empty())
422
    {
423
        MSG_WARNING("Got empty channel list!");
424
        return map;
425
    }
426
 
427
    vector<MAP_T> localMap;
428
 
429
    switch (mt)
430
    {
431
        case TYPE_AM:   localMap = mMap.map_am; break;
432
        case TYPE_CM:   localMap = mMap.map_cm; break;
433
        case TYPE_LM:   localMap = mMap.map_lm; break;
434
    }
435
 
436
    if (localMap.empty())
437
    {
438
        MSG_WARNING("The internal list of elements is empty!")
439
        return map;
440
    }
441
 
14 andreas 442
    for (iter = channels.begin(); iter != channels.end(); iter++)
443
    {
444
        vector<MAP_T>::iterator mapIter;
445
 
19 andreas 446
        for (mapIter = localMap.begin(); mapIter != localMap.end(); mapIter++)
14 andreas 447
        {
448
            if (mapIter->p == port && mapIter->c == *iter)
449
                map.push_back(*mapIter);
450
        }
451
    }
452
 
453
    MSG_DEBUG("Found " << map.size() << " buttons.");
454
    return map;
455
}
456
 
97 andreas 457
vector<MAP_T> TAmxCommands::findButtonByName(const string& name)
458
{
459
    DECL_TRACER("TAmxCommands::findButtonByName(const string& name)");
460
 
461
    vector<MAP_T> map;
462
 
463
    if (mMap.map_cm.empty())
464
    {
465
        MSG_WARNING("The internal list of elements is empty!")
466
        return map;
467
    }
468
 
469
    vector<MAP_T>::iterator mapIter;
470
 
471
    for (mapIter = mMap.map_cm.begin(); mapIter != mMap.map_cm.end(); mapIter++)
472
    {
473
        if (mapIter->bn == name)
474
            map.push_back(*mapIter);
475
    }
476
 
477
    MSG_DEBUG("Found " << map.size() << " buttons.");
478
    return map;
479
}
480
 
15 andreas 481
vector<MAP_T> TAmxCommands::findBargraphs(int port, vector<int>& channels)
482
{
483
    DECL_TRACER("TAmxCommands::findBargraphs(int port, vector<int>& channels)");
484
 
485
    vector<MAP_T> map;
486
    vector<int>::iterator iter;
487
 
83 andreas 488
    if (channels.size() == 0)
489
        return map;
490
 
15 andreas 491
    for (iter = channels.begin(); iter != channels.end(); iter++)
492
    {
493
        vector<MAP_T>::iterator mapIter;
494
 
83 andreas 495
        if (mMap.map_lm.size() > 0)
15 andreas 496
        {
83 andreas 497
            for (mapIter = mMap.map_lm.begin(); mapIter != mMap.map_lm.end(); mapIter++)
498
            {
499
                if (mapIter->p == port && mapIter->c == *iter)
500
                    map.push_back(*mapIter);
501
            }
15 andreas 502
        }
503
    }
504
 
505
    MSG_DEBUG("Found " << map.size() << " buttons.");
506
    return map;
507
}
508
 
11 andreas 509
bool TAmxCommands::parseCommand(int device, int port, const string& cmd)
510
{
511
    DECL_TRACER("TAmxCommands::parseCommand(int device, int port, const string& cmd)");
65 andreas 512
/*
60 andreas 513
    if (device != TConfig::getChannel())    // This happens on a filetransfer only!
14 andreas 514
    {
515
        MSG_WARNING("Command is for device " << device << ", but this device is " << TConfig::getChannel());
516
    }
65 andreas 517
*/
11 andreas 518
    vector<CMD_TABLE>::iterator iter;
16 andreas 519
    size_t pos = cmd.find_first_of("-");
11 andreas 520
 
16 andreas 521
    if (pos != string::npos)
522
    {
523
        MSG_TRACE("Parsing for device <" << device << ":" << port << ":" << TConfig::getSystem() << "> the command: " << cmd.substr(0, pos));
524
    }
525
    else
526
    {
527
        MSG_TRACE("Parsing for device <" << device << ":" << port << ":" << TConfig::getSystem() << "> the command: " << cmd);
528
    }
13 andreas 529
 
11 andreas 530
    if (pos != string::npos)    // Command with parameters
531
    {
532
        string bef = cmd.substr(0, pos);
533
        string rest = cmd.substr(pos + 1);
534
 
535
        for (iter = mCmdTable.begin(); iter != mCmdTable.end(); iter++)
536
        {
13 andreas 537
            iter->channels.clear();
538
            iter->pars.clear();
539
 
11 andreas 540
            if (iter->cmd.compare(bef) == 0 && iter->command)
541
            {
542
                CMD_DEFINATIONS cdef = findCmdDefines(bef);
543
 
544
                if (cdef.cmd.empty())
545
                {
546
                    MSG_WARNING("Command \"" << bef << "\" not found in command table! Ignoring it.");
547
                    continue;
548
                }
549
 
550
                if (cdef.hasChannels || cdef.hasPars)
551
                {
14 andreas 552
                    vector<string> parts = getFields(rest, cdef.separator);
11 andreas 553
 
554
                    if (cdef.hasChannels)
13 andreas 555
                        extractChannels(parts[0], &iter->channels);
11 andreas 556
 
557
                    if (cdef.hasPars)
558
                    {
13 andreas 559
                        MSG_DEBUG("Command may have parameters. Found " << parts.size() << " parameters.");
11 andreas 560
 
13 andreas 561
                        if (parts.size() > 0)
11 andreas 562
                        {
13 andreas 563
                            vector<string>::iterator piter;
564
                            int cnt = 0;
565
 
566
                            for (piter = parts.begin(); piter != parts.end(); piter++)
11 andreas 567
                            {
13 andreas 568
                                if (cdef.hasChannels && !cnt)
569
                                {
570
                                    cnt++;
571
                                    continue;
572
                                }
573
 
574
                                iter->pars.push_back(*piter);
11 andreas 575
                                cnt++;
576
                            }
577
                        }
13 andreas 578
                        else
579
                            iter->pars.push_back(rest);
11 andreas 580
                    }
581
                }
582
 
583
                iter->command(port, iter->channels, iter->pars);
584
                return true;
585
            }
586
        }
587
    }
588
    else        // Command without parameter
589
    {
590
        for (iter = mCmdTable.begin(); iter != mCmdTable.end(); iter++)
591
        {
592
            if (iter->cmd.compare(cmd) == 0 && iter->command)
593
            {
594
                iter->command(port, iter->channels, iter->pars);
595
                return true;
596
            }
597
        }
598
    }
599
 
600
    MSG_WARNING("Command \"" << cmd << "\" currently not supported!");
601
    return false;
602
}
603
 
604
bool TAmxCommands::extractChannels(const string& schan, vector<int>* ch)
605
{
606
    DECL_TRACER("TAmxCommands::extractChannels(const string& schan, vector<int>* ch)");
607
 
608
    if (!ch || schan.empty())
609
        return false;
610
 
16 andreas 611
    if (schan.find("&") == string::npos && schan.find(".") == string::npos)
11 andreas 612
    {
613
        int c = atoi(schan.c_str());
614
        ch->push_back(c);
615
        return true;
616
    }
617
 
618
    if (schan.find("&") != string::npos)
619
    {
620
        vector<string> parts = StrSplit(schan, "&");
621
        vector<string>::iterator iter;
622
 
83 andreas 623
        if (parts.size() > 0)
11 andreas 624
        {
83 andreas 625
            for (iter = parts.begin(); iter != parts.end(); iter++)
11 andreas 626
            {
83 andreas 627
                if (iter->find(".") != string::npos)
628
                {
629
                    vector<string> p2 = StrSplit(*iter, ".");
11 andreas 630
 
83 andreas 631
                    if (p2.size() >= 2)
632
                    {
633
                        for (int i = atoi(p2[0].c_str()); i <= atoi(p2[1].c_str()); i++)
634
                            ch->push_back(i);
635
                    }
636
                    else if (p2.size() > 0)
637
                        ch->push_back(atoi(p2[0].c_str()));
38 andreas 638
                }
83 andreas 639
                else
640
                    ch->push_back(atoi(iter->c_str()));
11 andreas 641
            }
642
        }
643
    }
644
    else
645
    {
646
        vector<string> parts = StrSplit(schan, ".");
647
 
38 andreas 648
        if (parts.size() >= 2)
649
        {
60 andreas 650
            for (int i = atoi(parts[0].c_str()); i <= atoi(parts[1].c_str()); i++)
38 andreas 651
                ch->push_back(i);
652
        }
653
        else if (parts.size() > 0)
654
            ch->push_back(atoi(parts[0].c_str()));
11 andreas 655
    }
656
 
657
    return true;
658
}
659
 
660
void TAmxCommands::registerCommand(std::function<void (int port, vector<int>& channels, vector<string>& pars)> command, const string& name)
661
{
662
    DECL_TRACER("TAmxCommands::registerCommand(std::function<void (vector<int>& channels, vector<string>& pars)> command, const string& name)");
663
 
664
    vector<CMD_TABLE>::iterator iter;
665
 
666
    for (iter = mCmdTable.begin(); iter != mCmdTable.end(); iter++)
667
    {
668
        if (iter->cmd.compare(name) == 0)
669
        {
670
            iter->command = command;
671
            iter->channels.clear();
672
            iter->pars.clear();
673
            return;
674
        }
675
    }
676
 
677
    CMD_TABLE ctbl;
678
    ctbl.cmd = name;
679
    ctbl.command = command;
680
    mCmdTable.push_back(ctbl);
681
}