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