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