1.1 --- a/api.cpp Sat Feb 24 12:32:53 2007 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,215 +0,0 @@
1.4 -#include "api.h"
1.5 -
1.6 -#include <qregexp.h>
1.7 -
1.8 -API::API()
1.9 -{
1.10 - initCommand();
1.11 -}
1.12 -
1.13 -void API::initCommand()
1.14 -{
1.15 - com="";
1.16 - paramList.clear();
1.17 - resetError();
1.18 -}
1.19 -
1.20 -void API::parseInput (const QString &s)
1.21 -{
1.22 - initCommand();
1.23 - input=s;
1.24 - QRegExp re;
1.25 - int pos;
1.26 -
1.27 - // Get command
1.28 - re.setPattern ("(.*)\\s");
1.29 - re.setMinimal (true);
1.30 - pos=re.search (s);
1.31 - if (pos>=0)
1.32 - com=re.cap(1);
1.33 -
1.34 - // Get parameters
1.35 - paramList.clear();
1.36 - re.setPattern ("\\((.*)\\)");
1.37 - pos=re.search (s);
1.38 - if (pos>=0)
1.39 - {
1.40 - QString s=re.cap(1);
1.41 - QString a;
1.42 - bool inquote=false;
1.43 - pos=0;
1.44 - if (!s.isEmpty())
1.45 - {
1.46 - while (pos<s.length())
1.47 - {
1.48 - if (s.at(pos)=='\"')
1.49 - {
1.50 - if (inquote)
1.51 - inquote=false;
1.52 - else
1.53 - inquote=true;
1.54 - }
1.55 -
1.56 - if (s.at(pos)==',' && !inquote)
1.57 - {
1.58 - a=s.left(pos);
1.59 - paramList.append(a);
1.60 - s=s.right(s.length()-pos-1);
1.61 - pos=0;
1.62 - } else
1.63 - pos++;
1.64 -
1.65 - }
1.66 - paramList.append (s);
1.67 - }
1.68 - }
1.69 -}
1.70 -
1.71 -QString API::command()
1.72 -{
1.73 - return com;
1.74 -}
1.75 -
1.76 -QStringList API::parameters()
1.77 -{
1.78 - return paramList;
1.79 -}
1.80 -
1.81 -int API::paramCount()
1.82 -{
1.83 - return paramList.count();
1.84 -}
1.85 -
1.86 -
1.87 -QString API::errorMessage()
1.88 -{
1.89 - QString l;
1.90 - switch (errLevel)
1.91 - {
1.92 - case NoError: l="No Error";
1.93 - case Warning: l="Warning";
1.94 - case Aborted: l="Aborted";
1.95 - }
1.96 - return QString ("Error Level: %1\n Command: %2\nDescription: %3")
1.97 - .arg(l).arg(com).arg(errDescription);
1.98 -}
1.99 -
1.100 -QString API::errorDescription()
1.101 -{
1.102 - return errDescription;
1.103 -}
1.104 -
1.105 -ErrorLevel API::errorLevel()
1.106 -{
1.107 - return errLevel;
1.108 -}
1.109 -
1.110 -void API::setError(ErrorLevel level, const QString &description)
1.111 -{
1.112 - errDescription=description;
1.113 - errLevel=level;
1.114 -}
1.115 -
1.116 -void API::resetError ()
1.117 -{
1.118 - errMessage="";
1.119 - errDescription="";
1.120 - errLevel=NoError;
1.121 -}
1.122 -
1.123 -
1.124 -bool API::checkParamCount (QList <int> plist)
1.125 -{
1.126 - QStringList expList;
1.127 - QString expected;
1.128 - for (int i=0; i<plist.count();i++)
1.129 - {
1.130 - if (checkParamCount (plist[i]))
1.131 - {
1.132 - resetError();
1.133 - return true;
1.134 - }
1.135 - expList.append(QString().setNum(plist[i]));
1.136 - }
1.137 - expected=expList.join(",");
1.138 - errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
1.139 - return false;
1.140 -}
1.141 -
1.142 -bool API::checkParamCount (const int &expected)
1.143 -{
1.144 - if (paramList.count()!=expected)
1.145 - {
1.146 - errLevel=Aborted;
1.147 - errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
1.148 - return false;
1.149 - }
1.150 - return true;
1.151 -}
1.152 -
1.153 -bool API::checkParamIsInt(const int &index)
1.154 -{
1.155 - bool ok;
1.156 - if (index > paramList.count())
1.157 - {
1.158 - errLevel=Aborted;
1.159 - errDescription=QString("Parameter index %1 is outside of parameter list").arg(index);
1.160 - return false;
1.161 - } else
1.162 - {
1.163 - paramList[index].toInt (&ok, 10);
1.164 - if (!ok)
1.165 - {
1.166 - errLevel=Aborted;
1.167 - errDescription=QString("Parameter %1 is not an integer").arg(index);
1.168 - return false;
1.169 - }
1.170 - }
1.171 - return true;
1.172 -}
1.173 -
1.174 -int API::parInt (bool &ok,const uint &index)
1.175 -{
1.176 - if (checkParamIsInt (index))
1.177 - return paramList[index].toInt (&ok, 10);
1.178 - ok=false;
1.179 - return 0;
1.180 -}
1.181 -
1.182 -QString API::parString (bool &ok,const int &index)
1.183 -{
1.184 - // return the string at index, this could be also stored in
1.185 - // a variable later
1.186 - QString r;
1.187 - QRegExp re("\"(.*)\"");
1.188 - int pos=re.search (paramList[index]);
1.189 - if (pos>=0)
1.190 - r=re.cap (1);
1.191 - else
1.192 - r="";
1.193 - ok=true;
1.194 - return r;
1.195 -}
1.196 -
1.197 -bool API::parBool (bool &ok,const int &index)
1.198 -{
1.199 - // return the bool at index, this could be also stored in
1.200 - // a variable later
1.201 - QString r;
1.202 - ok=true;
1.203 - QString p=paramList[index];
1.204 - if (p=="true" || p=="1")
1.205 - return true;
1.206 - else if (p=="false" || p=="0")
1.207 - return false;
1.208 - ok=false;
1.209 - return ok;
1.210 -}
1.211 -
1.212 -QColor API::parColor(bool &ok,const int &index)
1.213 -{
1.214 - // return the QColor at index
1.215 - ok=true;
1.216 - return QColor (paramList[index]);
1.217 -}
1.218 -