insilmaril@105: #include "api.h" insilmaril@105: insilmaril@105: #include insilmaril@105: insilmaril@132: API::API() insilmaril@105: { insilmaril@132: initCommand(); insilmaril@132: } insilmaril@132: insilmaril@132: void API::initCommand() insilmaril@132: { insilmaril@132: com=""; insilmaril@132: paramList.clear(); insilmaril@132: errorString=""; insilmaril@132: noErr=true; insilmaril@132: } insilmaril@132: insilmaril@132: void API::parseCommand (const QString &s) insilmaril@132: { insilmaril@132: initCommand(); insilmaril@105: QRegExp re; insilmaril@105: int pos; insilmaril@105: insilmaril@105: // Get command insilmaril@105: re.setPattern ("(.*)\\s"); insilmaril@106: re.setMinimal (true); insilmaril@105: pos=re.search (s); insilmaril@105: if (pos>=0) insilmaril@132: com=re.cap(1); insilmaril@105: insilmaril@105: // Get parameters insilmaril@105: re.setPattern ("\\((.*)\\)"); insilmaril@105: pos=re.search (s); insilmaril@105: if (pos>=0) insilmaril@132: { insilmaril@132: QString s=re.cap(1); insilmaril@132: paramList=QStringList::split(",",s); insilmaril@132: } insilmaril@105: } insilmaril@105: insilmaril@132: QString API::command() insilmaril@105: { insilmaril@132: return com; insilmaril@132: } insilmaril@132: insilmaril@132: QStringList API::parameters() insilmaril@132: { insilmaril@132: return paramList; insilmaril@132: } insilmaril@132: insilmaril@132: QString API::errorDesc() insilmaril@132: { insilmaril@132: return errorString; insilmaril@132: } insilmaril@132: insilmaril@132: bool API::error() insilmaril@132: { insilmaril@132: // invert noErr insilmaril@132: return (noErr) ?false:true; insilmaril@132: } insilmaril@132: insilmaril@132: void API::setError(const QString &e) insilmaril@132: { insilmaril@132: noErr=false; insilmaril@132: errorString=e; insilmaril@132: } insilmaril@132: insilmaril@132: bool API::checkParamCount (const uint &expected) insilmaril@132: { insilmaril@132: if (paramList.count()!=expected) insilmaril@132: { insilmaril@132: errorString=QString("expected %1 parameters, but got %2").arg(expected).arg(paramList.count()); insilmaril@132: return false; insilmaril@132: } insilmaril@132: return true; insilmaril@132: } insilmaril@132: insilmaril@132: bool API::checkParamIsInt(const uint &index) insilmaril@132: { insilmaril@132: bool ok; insilmaril@132: if (index > paramList.count()) insilmaril@132: { insilmaril@132: errorString =QString("Parameter index %1 is outside of parameter list").arg(index); insilmaril@132: return false; insilmaril@132: } else insilmaril@132: { insilmaril@132: paramList[index].toInt (&ok, 10); insilmaril@132: if (!ok) insilmaril@132: { insilmaril@132: errorString=QString("Parameter %1 is not an integer").arg(index); insilmaril@132: return false; insilmaril@132: } insilmaril@132: return true; insilmaril@132: } insilmaril@132: } insilmaril@132: insilmaril@132: int API::parInt (bool &ok,const uint &index) insilmaril@132: { insilmaril@132: if (checkParamIsInt (index)) insilmaril@132: { insilmaril@132: return paramList[index].toInt (&ok, 10); insilmaril@132: } insilmaril@132: ok=false; insilmaril@132: return 0; insilmaril@132: } insilmaril@132: insilmaril@132: QString API::parString (bool &ok,const uint &index) insilmaril@132: { insilmaril@132: // return the string at index, this could be also stored in insilmaril@105: // a variable later insilmaril@132: QString r; insilmaril@105: QRegExp re("\"(.*)\""); insilmaril@132: int pos=re.search (paramList[index]); insilmaril@105: if (pos>=0) insilmaril@132: r=re.cap (1); insilmaril@105: else insilmaril@132: r=""; insilmaril@132: ok=true; insilmaril@132: return r; insilmaril@132: }