api.cpp
author insilmaril
Fri, 08 Dec 2006 20:18:58 +0000
changeset 410 ceb4532eae34
parent 395 7ced3733ba60
permissions -rw-r--r--
1.6.1 Port to QGraphics
     1 #include "api.h"
     2 
     3 #include <qregexp.h>
     4 
     5 API::API()
     6 {
     7 	initCommand();
     8 }
     9 
    10 void API::initCommand()
    11 {
    12 	com="";
    13 	paramList.clear();
    14 	resetError();
    15 }
    16 
    17 void API::parseInput (const QString &s)
    18 {
    19 	initCommand();
    20 	input=s;
    21 	QRegExp re;
    22 	int pos;
    23 
    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);
    30 
    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 				}
    52 
    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++;
    61 				
    62 			}
    63 			paramList.append (s);
    64 		}	
    65 	}	
    66 }
    67 
    68 QString API::command()
    69 {
    70 	return com;
    71 }
    72 
    73 QStringList API::parameters()
    74 {
    75 	return paramList;
    76 }
    77 
    78 int API::paramCount()
    79 {
    80 	return paramList.count();
    81 }
    82 
    83 
    84 QString API::errorMessage()
    85 {
    86 	QString l;
    87 	switch (errLevel)
    88 	{
    89 		case NoError: l="No Error";
    90 		case Warning: l="Warning";
    91 		case Aborted: l="Aborted";
    92 	}
    93 	return QString ("Error Level: %1\n    Command: %2\nDescription: %3")
    94 		.arg(l).arg(com).arg(errDescription);
    95 }
    96 
    97 QString API::errorDescription()
    98 {
    99 	return errDescription;
   100 }
   101 
   102 ErrorLevel API::errorLevel()
   103 {
   104 	return errLevel;
   105 }
   106 
   107 void API::setError(ErrorLevel level, const QString &description)
   108 {
   109 	errDescription=description;
   110 	errLevel=level;
   111 }
   112 
   113 void API::resetError ()
   114 {
   115 	errMessage="";
   116 	errDescription="";
   117 	errLevel=NoError;
   118 }
   119 
   120 
   121 bool API::checkParamCount (QList <int> plist)
   122 {
   123 	QStringList expList;
   124 	QString expected;
   125 	for (int i=0; i<plist.count();i++)
   126 	{
   127 		if (checkParamCount (plist[i])) 
   128 		{
   129 			resetError();
   130 			return true;
   131 		}
   132 		expList.append(QString().setNum(plist[i]));
   133 	}	
   134 	expected=expList.join(",");	
   135 	errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
   136 	return false;
   137 }
   138 
   139 bool API::checkParamCount (const int &expected)
   140 {
   141 	if (paramList.count()!=expected)
   142 	{
   143 		errLevel=Aborted;
   144 		errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
   145 		return false;
   146 	} 
   147 	return true;	
   148 }
   149 
   150 bool API::checkParamIsInt(const int &index)
   151 {
   152 	bool ok;
   153 	if (index > paramList.count())
   154 	{
   155 		errLevel=Aborted;
   156 		errDescription=QString("Parameter index %1 is outside of parameter list").arg(index);
   157 		return false;
   158 	} else
   159 	{
   160 		paramList[index].toInt (&ok, 10);
   161 		if (!ok)
   162 		{
   163 			errLevel=Aborted;
   164 			errDescription=QString("Parameter %1 is not an integer").arg(index);
   165 			return false;
   166 		} 
   167 	}	
   168 	return true;
   169 }
   170 
   171 int API::parInt (bool &ok,const uint &index)
   172 {
   173 	if (checkParamIsInt (index))
   174 		return paramList[index].toInt (&ok, 10);
   175 	ok=false;
   176 	return 0;
   177 }
   178 
   179 QString API::parString (bool &ok,const int &index)
   180 {
   181 	// return the string at index, this could be also stored in
   182 	// a variable later
   183 	QString r;
   184 	QRegExp re("\"(.*)\"");
   185 	int pos=re.search (paramList[index]);
   186 	if (pos>=0)
   187 		r=re.cap (1);
   188 	else	
   189 		r="";
   190 	ok=true;
   191 	return r;
   192 }
   193 
   194 bool API::parBool (bool &ok,const int &index)
   195 {
   196 	// return the bool at index, this could be also stored in
   197 	// a variable later
   198 	QString r;
   199 	ok=true;
   200 	QString p=paramList[index];
   201 	if (p=="true" || p=="1")
   202 		return true;
   203 	else if	(p=="false" || p=="0")
   204 		return false;
   205 	ok=false;
   206 	return ok;
   207 }
   208 
   209 QColor API::parColor(bool &ok,const int &index)
   210 {
   211 	// return the QColor at index
   212 	ok=true;
   213 	return QColor (paramList[index]);
   214 }
   215