-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
189 lines (160 loc) · 5.43 KB
/
util.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
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
#include <QDebug>
#include "util.h"
char * q_get_text_between(char *source_string, char A, char B, int length=-1)
{
QString txt=QString::fromUtf8(source_string);
if(length!=-1){txt.truncate(length);} //only in the specified range
int A_pos=-2,B_pos;
//find first use of A - indexOf
if(A!=0){A_pos=txt.indexOf(QChar(A));}
//trim - remove
if( (A_pos!=-1)&&(A_pos!=-2) ){txt.remove(0,A_pos+1);} //remove preceding string and the delimiting char
if (A_pos==-1)return nullptr;//if not found (if ==-2 => A==nullptr =>dont trim)
//find first use of B indexOf
if(B!=0){B_pos=txt.indexOf(QChar(B));}
else { return strdup(txt.toUtf8().data()); }
//trim - truncate
if(B_pos!=-1){txt.truncate(B_pos);}
else return nullptr; //if not found
return strdup(txt.toUtf8().data());
}
char * q_get_text_between(const char *source_string, char A, char B, int length=-1)
{
QString txt=QString::fromUtf8(source_string);
if(length!=-1){txt.truncate(length);} //only in the specified range
int A_pos=-2,B_pos;
//find first use of A - indexOf
if(A!=0){A_pos=txt.indexOf(QChar(A));}
//trim - remove
if( (A_pos!=-1)&&(A_pos!=-2) ){txt.remove(0,A_pos+1);} //remove preceding string and the delimiting char
if (A_pos==-1)return nullptr;//if not found (if ==-2 => A==nullptr =>dont trim)
//find first use of B indexOf
if(B!=0){B_pos=txt.indexOf(QChar(B));}
else { return strdup(txt.toUtf8().data()); }
//trim - truncate
if(B_pos!=-1){txt.truncate(B_pos);}
else return nullptr; //if not found
return strdup(txt.toUtf8().data());
}
QString q_get_text_between(QString txt, char A, char B, int length)
{
if(length!=-1){txt.truncate(length);} //only in the specified range
int A_pos=-2,B_pos;
//find first use of A - indexOf
if(A!=0){A_pos=txt.indexOf(QChar(A));}
//trim - remove
if( (A_pos!=-1)&&(A_pos!=-2) ){txt.remove(0,A_pos+1);} //remove preceding string and the delimiting char
if (A_pos==-1)return nullptr;//if not found (if ==-2 => A==nullptr =>dont trim)
//find first use of B indexOf
if(B!=0){B_pos=txt.indexOf(QChar(B));}
else { return txt; }
//trim - truncate
if(B_pos!=-1){txt.truncate(B_pos);}
else return QString(); //if not found
return txt;
}
int q_get_value_for_key(QString string, QString key, QString& result) //string should be a list of INI type key-value pairs
{
QStringList line;
key+="="; //to fit the startsWith test more acurately
line = string.split("\n",QString::SkipEmptyParts);
for(int i=0;i<line.size();i++){
if(line[i].startsWith(key)){
result=q_get_text_between(line[i],'=',0);
return 0;
}
}
return -1;
}
int q_get_value_for_key(QString string, QString key, float& result)
{
QString txt;
int err = q_get_value_for_key(string,key,txt);
if (err<0) return err;
result = txt.toFloat();
return 0;
}
int q_get_value_for_key(QString string, QString key, double& result)
{
QString txt;
int err = q_get_value_for_key(string,key,txt);
if (err<0) return err;
result = txt.toDouble();
return 0;
}
int q_get_value_for_key(QString string, QString key, int& result)
{
QString txt;
int err = q_get_value_for_key(string,key,txt);
if (err<0) return err;
result = txt.toInt();
return 0;
}
int q_get_value_for_key(QString string, QString key, unsigned int& result)
{
QString txt;
int err = q_get_value_for_key(string,key,txt);
if (err<0) return err;
result = txt.toUInt();
return 0;
}
int q_get_value_for_key(QString string, QString key, bool& result)
{
QString txt;
int err = q_get_value_for_key(string,key,txt);
if (err<0) return err;
int res = txt.toInt();
if( (res==1) | (res==0) ){result = res;}
else return -1;
return 0;
}
int q_get_value_for_key(QString string, QString key, QStringList& results)
{
QString txt;
int err = q_get_value_for_key(string,key,txt);
if (err<0) return err;
results = txt.split(";",QString::SkipEmptyParts);
//Trim the names
for(QString &result :results){
result = result.trimmed();
}
return 0;
}
int q_get_groups(QString string, QStringList &names, QStringList &groups)
{
QStringList line=string.split("\n",QString::SkipEmptyParts);
QString group; //buffer
names.clear();
groups.clear();
for(int i=0;i<line.size();i++){
if(line[i].startsWith("[")){
if(names.size()!=0){ //if there's a group started - complete it
if(group.size()!=0) groups.push_back(group);
group.clear();
}
names.push_back(q_get_text_between(line[i],'[',']'));//and start the new group
}else {
group+=line[i];
group+="\n";
}
}
if((group.size()!=0) && names.size()!=0) groups.push_back(group); //complete the last group if there's some text in the buffer and there's a group started
if(groups.size()!=names.size()){
return -1;
}else {
return 1;
}
}
int q_version_string_to_number(QString version)
{//Implies 3 version numbers and max 999 on each
int result;
QStringList numbers = version.split(".");
if(numbers.size()<3){
qDebug()<<"[q_version_string_to_number]Bad version number. Returning 0. Number:" + version;
return 0;
}
result = numbers[0].toInt()*1000000;
result += numbers[1].toInt()*1000;
result += numbers[2].toInt();
return result;
}