api.cpp
author insilmaril
Thu, 23 Mar 2006 12:38:54 +0000
changeset 258 42c8cf6dd1c3
parent 138 9079931da6c3
child 364 7b74fa3772bf
permissions -rw-r--r--
Version 1.7.12
     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 	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 QString API::errorDesc()
    79 {
    80 	return errorString;
    81 }
    82 
    83 bool API::error()
    84 {
    85 	// invert noErr
    86 	return (noErr) ?false:true;
    87 }
    88 
    89 void API::setError(const QString &e)
    90 {
    91 	noErr=false;
    92 	errorString=e;
    93 }
    94 
    95 bool API::checkParamCount (const uint &expected)
    96 {
    97 	if (paramList.count()!=expected)
    98 	{
    99 		errorString=QString("expected %1 parameters, but got %2").arg(expected).arg(paramList.count());
   100 		noErr=false;
   101 	} else 
   102 		noErr=true;
   103 	return noErr;	
   104 }
   105 
   106 bool API::checkParamIsInt(const uint &index)
   107 {
   108 	bool ok;
   109 	if (index > paramList.count())
   110 	{
   111 		errorString =QString("Parameter index %1 is outside of parameter list").arg(index);
   112 		noErr=false;
   113 	} else
   114 	{
   115 		paramList[index].toInt (&ok, 10);
   116 		if (!ok)
   117 		{
   118 			errorString=QString("Parameter %1 is not an integer").arg(index);
   119 			noErr=false;
   120 		} else
   121 			noErr=true;
   122 	}	
   123 	return noErr;
   124 }
   125 
   126 int API::parInt (bool &ok,const uint &index)
   127 {
   128 	if (checkParamIsInt (index))
   129 	{
   130 		return paramList[index].toInt (&ok, 10);
   131 	}
   132 	ok=false;
   133 	return 0;
   134 }
   135 
   136 QString API::parString (bool &ok,const uint &index)
   137 {
   138 	// return the string at index, this could be also stored in
   139 	// a variable later
   140 	QString r;
   141 	QRegExp re("\"(.*)\"");
   142 	int pos=re.search (paramList[index]);
   143 	if (pos>=0)
   144 		r=re.cap (1);
   145 	else	
   146 		r="";
   147 	ok=true;
   148 	return r;
   149 }