api.cpp
changeset 394 67cfa6e6b863
parent 377 5391ab620c95
child 395 7ced3733ba60
     1.1 --- a/api.cpp	Wed Oct 18 10:45:00 2006 +0000
     1.2 +++ b/api.cpp	Tue Oct 24 15:36:38 2006 +0000
     1.3 @@ -11,13 +11,13 @@
     1.4  {
     1.5  	com="";
     1.6  	paramList.clear();
     1.7 -	errorString="";
     1.8 -	noErr=true;
     1.9 +	resetError();
    1.10  }
    1.11  
    1.12 -void API::parseCommand (const QString &s)
    1.13 +void API::parseInput (const QString &s)
    1.14  {
    1.15  	initCommand();
    1.16 +	input=s;
    1.17  	QRegExp re;
    1.18  	int pos;
    1.19  
    1.20 @@ -75,32 +75,76 @@
    1.21  	return paramList;
    1.22  }
    1.23  
    1.24 -QString API::errorDesc()
    1.25 +int API::paramCount()
    1.26  {
    1.27 -	return errorString;
    1.28 +	return paramList.count();
    1.29  }
    1.30  
    1.31 -bool API::error()
    1.32 +
    1.33 +QString API::errorMessage()
    1.34  {
    1.35 -	// invert noErr
    1.36 -	return (noErr) ?false:true;
    1.37 +	QString l;
    1.38 +	switch (errLevel)
    1.39 +	{
    1.40 +		case NoError: l="No Error";
    1.41 +		case Warning: l="Warning";
    1.42 +		case Aborted: l="Aborted";
    1.43 +	}
    1.44 +	return QString ("Error Level: %1\n    Command: %2\nDescription: %3")
    1.45 +		.arg(l).arg(com).arg(errDescription);
    1.46  }
    1.47  
    1.48 -void API::setError(const QString &e)
    1.49 +QString API::errorDescription()
    1.50  {
    1.51 -	noErr=false;
    1.52 -	errorString=e;
    1.53 +	return errDescription;
    1.54 +}
    1.55 +
    1.56 +ErrorLevel API::errorLevel()
    1.57 +{
    1.58 +	return errLevel;
    1.59 +}
    1.60 +
    1.61 +void API::setError(ErrorLevel level, const QString &description)
    1.62 +{
    1.63 +	errDescription=description;
    1.64 +	errLevel=level;
    1.65 +}
    1.66 +
    1.67 +void API::resetError ()
    1.68 +{
    1.69 +	errMessage="";
    1.70 +	errDescription="";
    1.71 +	errLevel=NoError;
    1.72 +}
    1.73 +
    1.74 +
    1.75 +bool API::checkParamCount (QList <int> plist)
    1.76 +{
    1.77 +	QStringList expList;
    1.78 +	QString expected;
    1.79 +	for (int i=0; i<plist.count();i++)
    1.80 +	{
    1.81 +		if (checkParamCount (plist[i])) 
    1.82 +		{
    1.83 +			resetError();
    1.84 +			return true;
    1.85 +		}
    1.86 +		expList.append(QString().setNum(plist[i]));
    1.87 +	}	
    1.88 +	expected=expList.join(",");	
    1.89 +	errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
    1.90 +	return false;
    1.91  }
    1.92  
    1.93  bool API::checkParamCount (const int &expected)
    1.94  {
    1.95  	if (paramList.count()!=expected)
    1.96  	{
    1.97 -		errorString=QString("expected %1 parameters, but got %2").arg(expected).arg(paramList.count());
    1.98 -		noErr=false;
    1.99 -	} else 
   1.100 -		noErr=true;
   1.101 -	return noErr;	
   1.102 +		errLevel=Aborted;
   1.103 +		errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
   1.104 +		return false;
   1.105 +	} 
   1.106 +	return true;	
   1.107  }
   1.108  
   1.109  bool API::checkParamIsInt(const int &index)
   1.110 @@ -108,19 +152,20 @@
   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 +		errLevel=Aborted;
   1.117 +		errDescription=QString("Parameter index %1 is outside of parameter list").arg(index);
   1.118 +		return false;
   1.119  	} else
   1.120  	{
   1.121  		paramList[index].toInt (&ok, 10);
   1.122  		if (!ok)
   1.123  		{
   1.124 -			errorString=QString("Parameter %1 is not an integer").arg(index);
   1.125 -			noErr=false;
   1.126 -		} else
   1.127 -			noErr=true;
   1.128 +			errLevel=Aborted;
   1.129 +			errDescription=QString("Parameter %1 is not an integer").arg(index);
   1.130 +			return false;
   1.131 +		} 
   1.132  	}	
   1.133 -	return noErr;
   1.134 +	return true;
   1.135  }
   1.136  
   1.137  int API::parInt (bool &ok,const uint &index)
   1.138 @@ -145,3 +190,26 @@
   1.139  	ok=true;
   1.140  	return r;
   1.141  }
   1.142 +
   1.143 +bool API::parBool (bool &ok,const int &index)
   1.144 +{
   1.145 +	// return the bool at index, this could be also stored in
   1.146 +	// a variable later
   1.147 +	QString r;
   1.148 +	ok=true;
   1.149 +	/*
   1.150 +	QRegExp re("\"(.*)\"");
   1.151 +	int pos=re.search (paramList[index]);
   1.152 +	if (pos>=0)
   1.153 +		r=re.cap (1);
   1.154 +	else	
   1.155 +		r="";
   1.156 +	*/	
   1.157 +	if (paramList[index]=="true")
   1.158 +		return true;
   1.159 +	else if	(paramList[index]=="false")
   1.160 +		return false;
   1.161 +	ok=false;
   1.162 +	return ok;
   1.163 +}
   1.164 +