-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.cpp
124 lines (108 loc) · 3.71 KB
/
script.cpp
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
/*
Copyright (C) 2012-2014 Jan Christian Rohde
This file is part of MACE.
MACE is free software; you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.
MACE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MACE; if not, see http://www.gnu.org/licenses.
*/
#include "command.h"
#include "script.h"
static bool checkArg(QString &arg) {
bool valid = true;
killEmptyEnds(arg);
valid = valid && arg.size() > 0;
valid = valid && arg.indexOf(' ') == -1;
valid = valid && arg.indexOf('\n') == -1;
return valid;
}
bool decompose(const QString &source, QString &name, std::vector<std::string> *args) {
QString arg;
int count = 0;
int pos = 0, begin = 0, end;
int brack = 0;
bool valid = true;
args->clear();
while (pos < source.size() && valid) {
if (source.at(pos).toLatin1() == '(') brack++;
if (source.at(pos).toLatin1() == ')') brack--;
if (source.at(pos).toLatin1() == ',' && brack == 0) {
end = pos - begin;
if (count == 0) {
name = source.left(end);
killEmptyEnds(name);
}
else {
arg = source.mid(begin,end);
valid = valid && checkArg(arg);
args->push_back(arg.toUtf8().constData());
}
count++;
begin = pos + 1;
}
pos++;
}
if (count == 0) name = source.left(end);
else {
arg = source.mid(begin,end);
valid = valid && checkArg(arg);
args->push_back(arg.toUtf8().constData());
}
return valid;
}
bool decompose(const QString &source, std::vector<std::string> &args) {
QString midString;
int pos = 0, begin = 0, end;
int brack = 0;
bool valid = true;
args.clear();
while (pos < source.size() && valid) {
if (source.at(pos).toLatin1() == '(') brack++;
if (source.at(pos).toLatin1() == ')') brack--;
if (source.at(pos).toLatin1() == ',' && brack == 0) {
end = pos - begin;
midString = source.mid(begin,end);
valid = valid && checkArg(midString);
args.push_back(midString.toUtf8().constData());
begin = pos + 1;
}
pos++;
}
midString = source.mid(begin,end);
valid = valid && checkArg(midString);
args.push_back(midString.toUtf8().constData());
return valid;
}
bool script::findToken(token &curToken) {
bool result = true;
if (read.getTokenString(";")) curToken = SILENT;
else if (read.getTokenString(":")) curToken = VISIBLE;
else result = false;
return result;
}
script::script()
{
}
script::token script::getCommandLine() {
bool tokenFound = false;
script::token curToken = NOTHING;
commandLine.clear();
while (read.pos < read.text.size() && !tokenFound) {
if (read.text.at(read.pos).toLatin1() == ' ') commandLine.append(' ');
tokenFound = findToken(curToken);
if (!tokenFound) {
if (read.text.at(read.pos).toLatin1() == '%') {
read.runOverComment();
}
else {
commandLine.append(read.text.at(read.pos));
}
read.pos++;
}
}
return curToken;
}