api.cpp
branchqt4-port
changeset 18 70c41284cb48
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/api.cpp	Thu Aug 31 11:55:33 2006 +0000
     1.3 @@ -0,0 +1,147 @@
     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 +	errorString="";
    1.18 +	noErr=true;
    1.19 +}
    1.20 +
    1.21 +void API::parseCommand (const QString &s)
    1.22 +{
    1.23 +	initCommand();
    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 +QString API::errorDesc()
    1.82 +{
    1.83 +	return errorString;
    1.84 +}
    1.85 +
    1.86 +bool API::error()
    1.87 +{
    1.88 +	// invert noErr
    1.89 +	return (noErr) ?false:true;
    1.90 +}
    1.91 +
    1.92 +void API::setError(const QString &e)
    1.93 +{
    1.94 +	noErr=false;
    1.95 +	errorString=e;
    1.96 +}
    1.97 +
    1.98 +bool API::checkParamCount (const uint &expected)
    1.99 +{
   1.100 +	if (paramList.count()!=expected)
   1.101 +	{
   1.102 +		errorString=QString("expected %1 parameters, but got %2").arg(expected).arg(paramList.count());
   1.103 +		noErr=false;
   1.104 +	} else 
   1.105 +		noErr=true;
   1.106 +	return noErr;	
   1.107 +}
   1.108 +
   1.109 +bool API::checkParamIsInt(const uint &index)
   1.110 +{
   1.111 +	bool ok;
   1.112 +	if (index > paramList.count())
   1.113 +	{
   1.114 +		errorString =QString("Parameter index %1 is outside of parameter list").arg(index);
   1.115 +		noErr=false;
   1.116 +	} else
   1.117 +	{
   1.118 +		paramList[index].toInt (&ok, 10);
   1.119 +		if (!ok)
   1.120 +		{
   1.121 +			errorString=QString("Parameter %1 is not an integer").arg(index);
   1.122 +			noErr=false;
   1.123 +		} else
   1.124 +			noErr=true;
   1.125 +	}	
   1.126 +	return noErr;
   1.127 +}
   1.128 +
   1.129 +int API::parInt (bool &ok,const uint &index)
   1.130 +{
   1.131 +	if (checkParamIsInt (index))
   1.132 +		return paramList[index].toInt (&ok, 10);
   1.133 +	ok=false;
   1.134 +	return 0;
   1.135 +}
   1.136 +
   1.137 +QString API::parString (bool &ok,const uint &index)
   1.138 +{
   1.139 +	// return the string at index, this could be also stored in
   1.140 +	// a variable later
   1.141 +	QString r;
   1.142 +	QRegExp re("\"(.*)\"");
   1.143 +	int pos=re.search (paramList[index]);
   1.144 +	if (pos>=0)
   1.145 +		r=re.cap (1);
   1.146 +	else	
   1.147 +		r="";
   1.148 +	ok=true;
   1.149 +	return r;
   1.150 +}