Library: Added <valid> element that is used to define valid input values for functions

This commit is contained in:
Daniel Marjamäki 2013-12-22 19:10:14 +01:00
parent 85c62f98d7
commit be03d4718a
3 changed files with 32 additions and 6 deletions

View File

@ -112,6 +112,7 @@ bool Library::load(const tinyxml2::XMLDocument &doc)
bool notuninit = false;
bool formatstr = false;
bool strz = false;
std::string valid;
for (const tinyxml2::XMLElement *argnode = functionnode->FirstChildElement(); argnode; argnode = argnode->NextSiblingElement()) {
if (strcmp(argnode->Name(), "not-null") == 0)
notnull = true;
@ -121,13 +122,33 @@ bool Library::load(const tinyxml2::XMLDocument &doc)
formatstr = true;
else if (strcmp(argnode->Name(), "strz") == 0)
strz = true;
else if (strcmp(argnode->Name(), "valid") == 0) {
// Validate the validation expression
const char *p = argnode->GetText();
if (!std::isdigit(*p))
return false;
for (; *p; p++) {
if (std::isdigit(*p))
continue;
if (*p == '-' && std::isdigit(*(p-1)))
continue;
if (*p == ',' && *(p+1) != ',')
continue;
return false;
}
// Set validation expression
valid = argnode->GetText();
}
else
return false;
}
argumentChecks[name][nr].notnull = notnull;
argumentChecks[name][nr].notnull = notnull;
argumentChecks[name][nr].notuninit = notuninit;
argumentChecks[name][nr].formatstr = formatstr;
argumentChecks[name][nr].strz = strz;
argumentChecks[name][nr].strz = strz;
argumentChecks[name][nr].valid = valid;
} else if (strcmp(functionnode->Name(), "ignorefunction") == 0) {
_ignorefunction[name] = (strcmp(functionnode->GetText(), "true") == 0);
} else

View File

@ -96,10 +96,11 @@ public:
notnull = notuninit = formatstr = strz = false;
}
bool notnull;
bool notuninit;
bool formatstr;
bool strz;
bool notnull;
bool notuninit;
bool formatstr;
bool strz;
std::string valid;
};
// function name, argument nr => argument data

View File

@ -79,6 +79,9 @@ private:
" <arg nr=\"4\">\n"
" <strz/>\n"
" </arg>\n"
" <arg nr=\"5\">\n"
" <valid>1-</valid>\n"
" </arg>\n"
" </function>\n"
"</def>";
tinyxml2::XMLDocument doc;
@ -90,6 +93,7 @@ private:
ASSERT_EQUALS(true, library.argumentChecks["foo"][2].notnull);
ASSERT_EQUALS(true, library.argumentChecks["foo"][3].formatstr);
ASSERT_EQUALS(true, library.argumentChecks["foo"][4].strz);
ASSERT_EQUALS("1-", library.argumentChecks["foo"][5].valid);
}
void memory() {