api.cpp
author jhilmer
Sat, 23 Jul 2005 15:13:34 +0000
changeset 135 e7f59f62bbe3
parent 132 fd7f08a85971
child 138 9079931da6c3
permissions -rw-r--r--
Fixed possible use of uninitialized variable (state)
     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 	errorString="";
    15 	noErr=true;
    16 }
    17 
    18 void API::parseCommand (const QString &s)
    19 {
    20 	initCommand();
    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 	re.setPattern ("\\((.*)\\)");
    33 	pos=re.search (s);
    34 	if (pos>=0)
    35 	{
    36 		QString s=re.cap(1);
    37 		paramList=QStringList::split(",",s);
    38 	}	
    39 }
    40 
    41 QString API::command()
    42 {
    43 	return com;
    44 }
    45 
    46 QStringList API::parameters()
    47 {
    48 	return paramList;
    49 }
    50 
    51 QString API::errorDesc()
    52 {
    53 	return errorString;
    54 }
    55 
    56 bool API::error()
    57 {
    58 	// invert noErr
    59 	return (noErr) ?false:true;
    60 }
    61 
    62 void API::setError(const QString &e)
    63 {
    64 	noErr=false;
    65 	errorString=e;
    66 }
    67 
    68 bool API::checkParamCount (const uint &expected)
    69 {
    70 	if (paramList.count()!=expected)
    71 	{
    72 		errorString=QString("expected %1 parameters, but got %2").arg(expected).arg(paramList.count());
    73 		return false;
    74 	} 
    75 	return true;
    76 }
    77 
    78 bool API::checkParamIsInt(const uint &index)
    79 {
    80 	bool ok;
    81 	if (index > paramList.count())
    82 	{
    83 		errorString =QString("Parameter index %1 is outside of parameter list").arg(index);
    84 		return false;
    85 	} else
    86 	{
    87 		paramList[index].toInt (&ok, 10);
    88 		if (!ok)
    89 		{
    90 			errorString=QString("Parameter %1 is not an integer").arg(index);
    91 			return false;
    92 		}
    93 		return true;
    94 	}	
    95 }
    96 
    97 int API::parInt (bool &ok,const uint &index)
    98 {
    99 	if (checkParamIsInt (index))
   100 	{
   101 		return paramList[index].toInt (&ok, 10);
   102 	}
   103 	ok=false;
   104 	return 0;
   105 }
   106 
   107 QString API::parString (bool &ok,const uint &index)
   108 {
   109 	// return the string at index, this could be also stored in
   110 	// a variable later
   111 	QString r;
   112 	QRegExp re("\"(.*)\"");
   113 	int pos=re.search (paramList[index]);
   114 	if (pos>=0)
   115 		r=re.cap (1);
   116 	else	
   117 		r="";
   118 	ok=true;
   119 	return r;
   120 }