author | insilmaril |
Wed Mar 01 14:39:04 2006 +0000 (2006-03-01) | |
changeset 225 | d5c70aaca22d |
parent 132 | fd7f08a85971 |
child 364 | 7b74fa3772bf |
permissions | -rw-r--r-- |
1 #include "api.h"
3 #include <qregexp.h>
5 API::API()
6 {
7 initCommand();
8 }
10 void API::initCommand()
11 {
12 com="";
13 paramList.clear();
14 errorString="";
15 noErr=true;
16 }
18 void API::parseCommand (const QString &s)
19 {
20 initCommand();
21 QRegExp re;
22 int pos;
24 // Get command
25 re.setPattern ("(.*)\\s");
26 re.setMinimal (true);
27 pos=re.search (s);
28 if (pos>=0)
29 com=re.cap(1);
31 // Get parameters
32 paramList.clear();
33 re.setPattern ("\\((.*)\\)");
34 pos=re.search (s);
35 if (pos>=0)
36 {
37 QString s=re.cap(1);
38 QString a;
39 bool inquote=false;
40 pos=0;
41 if (!s.isEmpty())
42 {
43 while (pos<s.length())
44 {
45 if (s.at(pos)=='\"')
46 {
47 if (inquote)
48 inquote=false;
49 else
50 inquote=true;
51 }
53 if (s.at(pos)==',' && !inquote)
54 {
55 a=s.left(pos);
56 paramList.append(a);
57 s=s.right(s.length()-pos-1);
58 pos=0;
59 } else
60 pos++;
62 }
63 paramList.append (s);
64 }
65 }
66 }
68 QString API::command()
69 {
70 return com;
71 }
73 QStringList API::parameters()
74 {
75 return paramList;
76 }
78 QString API::errorDesc()
79 {
80 return errorString;
81 }
83 bool API::error()
84 {
85 // invert noErr
86 return (noErr) ?false:true;
87 }
89 void API::setError(const QString &e)
90 {
91 noErr=false;
92 errorString=e;
93 }
95 bool API::checkParamCount (const uint &expected)
96 {
97 if (paramList.count()!=expected)
98 {
99 errorString=QString("expected %1 parameters, but got %2").arg(expected).arg(paramList.count());
100 noErr=false;
101 } else
102 noErr=true;
103 return noErr;
104 }
106 bool API::checkParamIsInt(const uint &index)
107 {
108 bool ok;
109 if (index > paramList.count())
110 {
111 errorString =QString("Parameter index %1 is outside of parameter list").arg(index);
112 noErr=false;
113 } else
114 {
115 paramList[index].toInt (&ok, 10);
116 if (!ok)
117 {
118 errorString=QString("Parameter %1 is not an integer").arg(index);
119 noErr=false;
120 } else
121 noErr=true;
122 }
123 return noErr;
124 }
126 int API::parInt (bool &ok,const uint &index)
127 {
128 if (checkParamIsInt (index))
129 {
130 return paramList[index].toInt (&ok, 10);
131 }
132 ok=false;
133 return 0;
134 }
136 QString API::parString (bool &ok,const uint &index)
137 {
138 // return the string at index, this could be also stored in
139 // a variable later
140 QString r;
141 QRegExp re("\"(.*)\"");
142 int pos=re.search (paramList[index]);
143 if (pos>=0)
144 r=re.cap (1);
145 else
146 r="";
147 ok=true;
148 return r;
149 }