api.cpp
author insilmaril
Thu, 23 Nov 2006 13:53:08 +0000
changeset 406 1c8ff1928b97
parent 395 7ced3733ba60
permissions -rw-r--r--
Removed more QT3 stuff. Drag & Drop not 100% functional at the moment
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@394
    14
	resetError();
insilmaril@132
    15
}
insilmaril@132
    16
insilmaril@394
    17
void API::parseInput (const QString &s)
insilmaril@132
    18
{
insilmaril@132
    19
	initCommand();
insilmaril@394
    20
	input=s;
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@394
    78
int API::paramCount()
insilmaril@132
    79
{
insilmaril@394
    80
	return paramList.count();
insilmaril@132
    81
}
insilmaril@132
    82
insilmaril@394
    83
insilmaril@394
    84
QString API::errorMessage()
insilmaril@132
    85
{
insilmaril@394
    86
	QString l;
insilmaril@394
    87
	switch (errLevel)
insilmaril@394
    88
	{
insilmaril@394
    89
		case NoError: l="No Error";
insilmaril@394
    90
		case Warning: l="Warning";
insilmaril@394
    91
		case Aborted: l="Aborted";
insilmaril@394
    92
	}
insilmaril@394
    93
	return QString ("Error Level: %1\n    Command: %2\nDescription: %3")
insilmaril@394
    94
		.arg(l).arg(com).arg(errDescription);
insilmaril@132
    95
}
insilmaril@132
    96
insilmaril@394
    97
QString API::errorDescription()
insilmaril@132
    98
{
insilmaril@394
    99
	return errDescription;
insilmaril@394
   100
}
insilmaril@394
   101
insilmaril@394
   102
ErrorLevel API::errorLevel()
insilmaril@394
   103
{
insilmaril@394
   104
	return errLevel;
insilmaril@394
   105
}
insilmaril@394
   106
insilmaril@394
   107
void API::setError(ErrorLevel level, const QString &description)
insilmaril@394
   108
{
insilmaril@394
   109
	errDescription=description;
insilmaril@394
   110
	errLevel=level;
insilmaril@394
   111
}
insilmaril@394
   112
insilmaril@394
   113
void API::resetError ()
insilmaril@394
   114
{
insilmaril@394
   115
	errMessage="";
insilmaril@394
   116
	errDescription="";
insilmaril@394
   117
	errLevel=NoError;
insilmaril@394
   118
}
insilmaril@394
   119
insilmaril@394
   120
insilmaril@394
   121
bool API::checkParamCount (QList <int> plist)
insilmaril@394
   122
{
insilmaril@394
   123
	QStringList expList;
insilmaril@394
   124
	QString expected;
insilmaril@394
   125
	for (int i=0; i<plist.count();i++)
insilmaril@394
   126
	{
insilmaril@394
   127
		if (checkParamCount (plist[i])) 
insilmaril@394
   128
		{
insilmaril@394
   129
			resetError();
insilmaril@394
   130
			return true;
insilmaril@394
   131
		}
insilmaril@394
   132
		expList.append(QString().setNum(plist[i]));
insilmaril@394
   133
	}	
insilmaril@394
   134
	expected=expList.join(",");	
insilmaril@394
   135
	errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
insilmaril@394
   136
	return false;
insilmaril@132
   137
}
insilmaril@132
   138
insilmaril@377
   139
bool API::checkParamCount (const int &expected)
insilmaril@132
   140
{
insilmaril@132
   141
	if (paramList.count()!=expected)
insilmaril@132
   142
	{
insilmaril@394
   143
		errLevel=Aborted;
insilmaril@394
   144
		errDescription=QString("Wrong number of parameters: Expected %1, but found %2").arg(expected).arg(paramList.count());
insilmaril@394
   145
		return false;
insilmaril@394
   146
	} 
insilmaril@394
   147
	return true;	
insilmaril@132
   148
}
insilmaril@132
   149
insilmaril@377
   150
bool API::checkParamIsInt(const int &index)
insilmaril@132
   151
{
insilmaril@132
   152
	bool ok;
insilmaril@132
   153
	if (index > paramList.count())
insilmaril@132
   154
	{
insilmaril@394
   155
		errLevel=Aborted;
insilmaril@394
   156
		errDescription=QString("Parameter index %1 is outside of parameter list").arg(index);
insilmaril@394
   157
		return false;
insilmaril@132
   158
	} else
insilmaril@132
   159
	{
insilmaril@132
   160
		paramList[index].toInt (&ok, 10);
insilmaril@132
   161
		if (!ok)
insilmaril@132
   162
		{
insilmaril@394
   163
			errLevel=Aborted;
insilmaril@394
   164
			errDescription=QString("Parameter %1 is not an integer").arg(index);
insilmaril@394
   165
			return false;
insilmaril@394
   166
		} 
insilmaril@132
   167
	}	
insilmaril@394
   168
	return true;
insilmaril@132
   169
}
insilmaril@132
   170
insilmaril@132
   171
int API::parInt (bool &ok,const uint &index)
insilmaril@132
   172
{
insilmaril@132
   173
	if (checkParamIsInt (index))
insilmaril@132
   174
		return paramList[index].toInt (&ok, 10);
insilmaril@132
   175
	ok=false;
insilmaril@132
   176
	return 0;
insilmaril@132
   177
}
insilmaril@132
   178
insilmaril@377
   179
QString API::parString (bool &ok,const int &index)
insilmaril@132
   180
{
insilmaril@132
   181
	// return the string at index, this could be also stored in
insilmaril@105
   182
	// a variable later
insilmaril@132
   183
	QString r;
insilmaril@105
   184
	QRegExp re("\"(.*)\"");
insilmaril@132
   185
	int pos=re.search (paramList[index]);
insilmaril@105
   186
	if (pos>=0)
insilmaril@132
   187
		r=re.cap (1);
insilmaril@105
   188
	else	
insilmaril@132
   189
		r="";
insilmaril@132
   190
	ok=true;
insilmaril@132
   191
	return r;
insilmaril@132
   192
}
insilmaril@394
   193
insilmaril@394
   194
bool API::parBool (bool &ok,const int &index)
insilmaril@394
   195
{
insilmaril@394
   196
	// return the bool at index, this could be also stored in
insilmaril@394
   197
	// a variable later
insilmaril@394
   198
	QString r;
insilmaril@394
   199
	ok=true;
insilmaril@395
   200
	QString p=paramList[index];
insilmaril@395
   201
	if (p=="true" || p=="1")
insilmaril@394
   202
		return true;
insilmaril@395
   203
	else if	(p=="false" || p=="0")
insilmaril@394
   204
		return false;
insilmaril@394
   205
	ok=false;
insilmaril@394
   206
	return ok;
insilmaril@394
   207
}
insilmaril@394
   208
insilmaril@395
   209
QColor API::parColor(bool &ok,const int &index)
insilmaril@395
   210
{
insilmaril@395
   211
	// return the QColor at index
insilmaril@395
   212
	ok=true;
insilmaril@395
   213
	return QColor (paramList[index]);
insilmaril@395
   214
}
insilmaril@395
   215