From be03d4718ac3c6832d5693e2598d6f51da3beeed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 22 Dec 2013 19:10:14 +0100 Subject: [PATCH] Library: Added element that is used to define valid input values for functions --- lib/library.cpp | 25 +++++++++++++++++++++++-- lib/library.h | 9 +++++---- test/testlibrary.cpp | 4 ++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/lib/library.cpp b/lib/library.cpp index db24e35c8..78a3835cc 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -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 diff --git a/lib/library.h b/lib/library.h index 14024628c..b325c6335 100644 --- a/lib/library.h +++ b/lib/library.h @@ -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 diff --git a/test/testlibrary.cpp b/test/testlibrary.cpp index 06537cf85..2b27a8690 100644 --- a/test/testlibrary.cpp +++ b/test/testlibrary.cpp @@ -79,6 +79,9 @@ private: " \n" " \n" " \n" + " \n" + " 1-\n" + " \n" " \n" ""; 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() {