api.cpp
author insilmaril
Mon, 16 Oct 2006 12:42:54 +0000
changeset 392 18f824bd3070
parent 377 5391ab620c95
child 394 67cfa6e6b863
permissions -rw-r--r--
Code simplifications
insilmaril@105
     1
#include "api.h"
insilmaril@105
     2
insilmaril@105
     3
#include <qregexp.h>
insilmaril@105
     4
insilmaril@132
     5
API::API()
insilmaril@105
     6
{
insilmaril@132
     7
	initCommand();
insilmaril@132
     8
}
insilmaril@132
     9
insilmaril@132
    10
void API::initCommand()
insilmaril@132
    11
{
insilmaril@132
    12
	com="";
insilmaril@132
    13
	paramList.clear();
insilmaril@132
    14
	errorString="";
insilmaril@132
    15
	noErr=true;
insilmaril@132
    16
}
insilmaril@132
    17
insilmaril@132
    18
void API::parseCommand (const QString &s)
insilmaril@132
    19
{
insilmaril@132
    20
	initCommand();
insilmaril@105
    21
	QRegExp re;
insilmaril@105
    22
	int pos;
insilmaril@105
    23
insilmaril@105
    24
	// Get command
insilmaril@105
    25
	re.setPattern ("(.*)\\s");
insilmaril@106
    26
	re.setMinimal (true);
insilmaril@105
    27
	pos=re.search (s);
insilmaril@105
    28
	if (pos>=0)
insilmaril@132
    29
		com=re.cap(1);
insilmaril@105
    30
insilmaril@105
    31
	// Get parameters
insilmaril@138
    32
	paramList.clear();
insilmaril@105
    33
	re.setPattern ("\\((.*)\\)");
insilmaril@105
    34
	pos=re.search (s);
insilmaril@105
    35
	if (pos>=0)
insilmaril@132
    36
	{
insilmaril@132
    37
		QString s=re.cap(1);
insilmaril@138
    38
		QString a;
insilmaril@138
    39
		bool inquote=false;
insilmaril@138
    40
		pos=0;
insilmaril@138
    41
		if (!s.isEmpty())
insilmaril@138
    42
		{
insilmaril@138
    43
			while (pos<s.length())
insilmaril@138
    44
			{
insilmaril@138
    45
				if (s.at(pos)=='\"') 
insilmaril@138
    46
				{
insilmaril@138
    47
					if (inquote)
insilmaril@138
    48
						inquote=false;
insilmaril@138
    49
					else	
insilmaril@138
    50
						inquote=true;
insilmaril@138
    51
				}
insilmaril@138
    52
insilmaril@138
    53
				if (s.at(pos)==',' && !inquote)
insilmaril@138
    54
				{
insilmaril@138
    55
					a=s.left(pos);
insilmaril@138
    56
					paramList.append(a);
insilmaril@138
    57
					s=s.right(s.length()-pos-1);
insilmaril@138
    58
					pos=0;
insilmaril@138
    59
				} else
insilmaril@138
    60
					pos++;
insilmaril@138
    61
				
insilmaril@138
    62
			}
insilmaril@138
    63
			paramList.append (s);
insilmaril@138
    64
		}	
insilmaril@132
    65
	}	
insilmaril@105
    66
}
insilmaril@105
    67
insilmaril@132
    68
QString API::command()
insilmaril@105
    69
{
insilmaril@132
    70
	return com;
insilmaril@132
    71
}
insilmaril@132
    72
insilmaril@132
    73
QStringList API::parameters()
insilmaril@132
    74
{
insilmaril@132
    75
	return paramList;
insilmaril@132
    76
}
insilmaril@132
    77
insilmaril@132
    78
QString API::errorDesc()
insilmaril@132
    79
{
insilmaril@132
    80
	return errorString;
insilmaril@132
    81
}
insilmaril@132
    82
insilmaril@132
    83
bool API::error()
insilmaril@132
    84
{
insilmaril@132
    85
	// invert noErr
insilmaril@132
    86
	return (noErr) ?false:true;
insilmaril@132
    87
}
insilmaril@132
    88
insilmaril@132
    89
void API::setError(const QString &e)
insilmaril@132
    90
{
insilmaril@132
    91
	noErr=false;
insilmaril@132
    92
	errorString=e;
insilmaril@132
    93
}
insilmaril@132
    94
insilmaril@377
    95
bool API::checkParamCount (const int &expected)
insilmaril@132
    96
{
insilmaril@132
    97
	if (paramList.count()!=expected)
insilmaril@132
    98
	{
insilmaril@132
    99
		errorString=QString("expected %1 parameters, but got %2").arg(expected).arg(paramList.count());
insilmaril@138
   100
		noErr=false;
insilmaril@138
   101
	} else 
insilmaril@138
   102
		noErr=true;
insilmaril@138
   103
	return noErr;	
insilmaril@132
   104
}
insilmaril@132
   105
insilmaril@377
   106
bool API::checkParamIsInt(const int &index)
insilmaril@132
   107
{
insilmaril@132
   108
	bool ok;
insilmaril@132
   109
	if (index > paramList.count())
insilmaril@132
   110
	{
insilmaril@132
   111
		errorString =QString("Parameter index %1 is outside of parameter list").arg(index);
insilmaril@138
   112
		noErr=false;
insilmaril@132
   113
	} else
insilmaril@132
   114
	{
insilmaril@132
   115
		paramList[index].toInt (&ok, 10);
insilmaril@132
   116
		if (!ok)
insilmaril@132
   117
		{
insilmaril@132
   118
			errorString=QString("Parameter %1 is not an integer").arg(index);
insilmaril@138
   119
			noErr=false;
insilmaril@138
   120
		} else
insilmaril@138
   121
			noErr=true;
insilmaril@132
   122
	}	
insilmaril@138
   123
	return noErr;
insilmaril@132
   124
}
insilmaril@132
   125
insilmaril@132
   126
int API::parInt (bool &ok,const uint &index)
insilmaril@132
   127
{
insilmaril@132
   128
	if (checkParamIsInt (index))
insilmaril@132
   129
		return paramList[index].toInt (&ok, 10);
insilmaril@132
   130
	ok=false;
insilmaril@132
   131
	return 0;
insilmaril@132
   132
}
insilmaril@132
   133
insilmaril@377
   134
QString API::parString (bool &ok,const int &index)
insilmaril@132
   135
{
insilmaril@132
   136
	// return the string at index, this could be also stored in
insilmaril@105
   137
	// a variable later
insilmaril@132
   138
	QString r;
insilmaril@105
   139
	QRegExp re("\"(.*)\"");
insilmaril@132
   140
	int pos=re.search (paramList[index]);
insilmaril@105
   141
	if (pos>=0)
insilmaril@132
   142
		r=re.cap (1);
insilmaril@105
   143
	else	
insilmaril@132
   144
		r="";
insilmaril@132
   145
	ok=true;
insilmaril@132
   146
	return r;
insilmaril@132
   147
}