Permalink
Browse files

1 new feature + 1 refactor

    - Handle multiple strings in input (that is, passed by the "-s"
flag)
    - Better algorithm to handle the new structure
  • Loading branch information...
1 parent 13bccfb commit 6d9a7b4441b69ffadcc191bb4e796c9afaa0de54 @massix committed May 12, 2012
Showing with 50 additions and 62 deletions.
  1. +4 −5 include/PMBinaryValidatorApplication.h
  2. +1 −1 src/CMakeLists.txt
  3. +45 −56 src/PMBinaryValidatorApplication.cpp
View
9 include/PMBinaryValidatorApplication.h
@@ -61,20 +61,19 @@ class Application
private:
std::string aFileName;
- std::string anInputString;
+ std::vector<std::string> aVectorString;
std::string anOutputString;
std::string aDirectory;
bool isFileNameSet;
bool isInputStringSet;
bool isDirectorySet;
- void generateOutputString(PhantomMenace::ParsingEnvironment iParsingEnv);
-
- PhantomMenace::ParsingEnvironment parsingEnvironment;
+ void generateOutputString(
+ const std::string& iString,
+ const PhantomMenace::ParsingEnvironment& iParsingEnv);
std::vector<boost::shared_ptr<PhantomMenace::ParsingEnvironment> > aParsingEnvironmentVector;
-// std::vector<PhantomMenace::ParsingEnvironment*> aParsingEnvironmentVector;
protected:
Application();
View
2 src/CMakeLists.txt
@@ -6,7 +6,7 @@ file(GLOB SOURCES *.cpp)
file(GLOB HEADERS ../include/*.h)
set(PhantomMenaceVersionMajor 1)
-set(PhantomMenaceVersionBinComp 1)
+set(PhantomMenaceVersionBinComp 2)
set(PhantomMenaceVersionMinor 0)
set(PhantomMenaceVersion
${PhantomMenaceVersionMajor}.${PhantomMenaceVersionBinComp}.${PhantomMenaceVersionMinor})
View
101 src/PMBinaryValidatorApplication.cpp
@@ -93,7 +93,7 @@ void Application::setFileName(const std::string& iFileName)
void Application::setString(const std::string& iString)
{
- anInputString = iString;
+ aVectorString.push_back(iString);
isInputStringSet = true;
}
@@ -105,6 +105,9 @@ void Application::setDirectory(const std::string& iDirectory)
bool Application::validateString() throw (std::runtime_error)
{
+ bool return_value = true;
+
+ /* Check consistency of the request */
if (!isFileNameSet && !isDirectorySet)
{
throw std::runtime_error("filename or directory not set");
@@ -120,104 +123,90 @@ bool Application::validateString() throw (std::runtime_error)
throw std::runtime_error("string not set");
}
+ /* Get the parsing environments */
if (isFileNameSet)
{
- if (!parsingEnvironment.parseFromFile(aFileName))
+ boost::shared_ptr<PhantomMenace::ParsingEnvironment> aParsingEnvironment(
+ new PhantomMenace::ParsingEnvironment());
+
+ if (!aParsingEnvironment->parseFromFile(aFileName))
{
throw std::runtime_error("couldn't find filename!");
}
+
+ aParsingEnvironmentVector.push_back(aParsingEnvironment);
}
- if (isDirectorySet)
+ else
{
// Parse the whole directory and build the vector of PE
- DIR* aDirectory;
+ DIR* aDirectoryPtr = opendir(aDirectory.c_str());
struct dirent* aDirent;
- if ((aDirectory = opendir(this->aDirectory.c_str())) == 0)
+ if ((aDirectoryPtr = opendir(this->aDirectory.c_str())) == 0)
{
throw std::runtime_error("couldn't parse directory");
}
- while ((aDirent = readdir(aDirectory)) != 0)
+ while ((aDirent = readdir(aDirectoryPtr)) != 0)
{
- if (aDirent->d_type == DT_DIR)
- continue;
-
- std::string aFileName = this->aDirectory + "/" + aDirent->d_name;
- if (aFileName.find(".ini") != std::string::npos)
+ if (aDirent->d_type == DT_REG)
{
- boost::shared_ptr<PhantomMenace::ParsingEnvironment> aPE(
- new PhantomMenace::ParsingEnvironment());
- try
+ std::string aFileName(aDirectory + aDirent->d_name);
+ if (aFileName.find(".ini") != std::string::npos)
{
- if (aPE->parseFromFile(aFileName))
+ boost::shared_ptr<PhantomMenace::ParsingEnvironment> aPE(
+ new PhantomMenace::ParsingEnvironment());
+ try
{
- if (aPE->isEnvironmentValid())
+ if (aPE->parseFromFile(aFileName))
{
- aParsingEnvironmentVector.push_back(aPE);
+ if (aPE->isEnvironmentValid())
+ {
+ aParsingEnvironmentVector.push_back(aPE);
+ }
}
}
- }
- catch (...)
- {
- continue;
+
+ catch (...)
+ {
+ continue;
+ }
}
}
}
}
- try
- {
- if (isFileNameSet)
- {
- PhantomMenace::Validator validator(parsingEnvironment);
- if (validator.validateString(anInputString))
- {
- generateOutputString(parsingEnvironment);
- return true;
- }
- else return false;
- }
+ /* Validate strings! */
+ std::vector<std::string>::const_iterator string_iterator;
+ std::vector<boost::shared_ptr<PhantomMenace::ParsingEnvironment> >::iterator parsing_iterator;
- else if (isDirectorySet)
+ for (string_iterator = aVectorString.begin(); string_iterator != aVectorString.end(); ++string_iterator)
+ {
+ for (parsing_iterator = aParsingEnvironmentVector.begin(); parsing_iterator != aParsingEnvironmentVector.end(); ++parsing_iterator)
{
- // Try to validate against every grammar in the vector
- std::vector<boost::shared_ptr<PhantomMenace::ParsingEnvironment> >::iterator ite;
- for (ite = aParsingEnvironmentVector.begin();
- ite != aParsingEnvironmentVector.end();
- ++ite)
+ PhantomMenace::Validator aValidator(**parsing_iterator);
+ if (aValidator.validateString(*string_iterator))
{
- PhantomMenace::Validator aValidator(**ite);
- if (aValidator.validateString(anInputString))
- {
- generateOutputString(**ite);
- return true;
- }
+ generateOutputString(*string_iterator, **parsing_iterator);
}
-
- return false;
}
-
- else return false;
- }
- catch (...)
- {
- return false;
}
- /* We should never arrive here! */
- return false;
+ return return_value;
}
void Application::printLog() const
{
std::cout << anOutputString << std::endl;
}
-void Application::generateOutputString(PhantomMenace::ParsingEnvironment iParsingEnv)
+void Application::generateOutputString(
+ const std::string& iString,
+ const PhantomMenace::ParsingEnvironment& iParsingEnv)
{
+ anOutputString += "\n\nString \"" + iString + "\" validated\n";
anOutputString += "Grammar name : ";
anOutputString += iParsingEnv.getGrammar().getElementName();
anOutputString += "\n";

0 comments on commit 6d9a7b4

Please sign in to comment.