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