diff --git a/alib2data/src/Api.cpp b/alib2data/src/Api.cpp index 9935a06342faa47d6ab726506ba78e0609d1732d..11eacfe239374f12d3cce01f13c3fd50e17f9b4c 100644 --- a/alib2data/src/Api.cpp +++ b/alib2data/src/Api.cpp @@ -535,6 +535,14 @@ std::list<sax::Token> api<primitive::String>::compose(const primitive::String& d return ToXMLComposers::primitiveComposer.compose(data); } +std::string api<std::string>::parse(std::list<sax::Token>& input) { + return FromXMLParsers::primitiveParser.parseStringRaw(input); +} + +std::list<sax::Token> api<std::string>::compose(const std::string& data) { + return ToXMLComposers::primitiveComposer.compose(data); +} + primitive::Integer api<primitive::Integer>::parse(std::list<sax::Token>& input) { return FromXMLParsers::primitiveParser.parseInteger(input); } @@ -543,6 +551,14 @@ std::list<sax::Token> api<primitive::Integer>::compose(const primitive::Integer& return ToXMLComposers::primitiveComposer.compose(data); } +int api<int>::parse(std::list<sax::Token>& input) { + return FromXMLParsers::primitiveParser.parseIntegerRaw(input); +} + +std::list<sax::Token> api<int>::compose(int data) { + return ToXMLComposers::primitiveComposer.compose(data); +} + primitive::Character api<primitive::Character>::parse(std::list<sax::Token>& input) { return FromXMLParsers::primitiveParser.parseCharacter(input); } @@ -551,6 +567,29 @@ std::list<sax::Token> api<primitive::Character>::compose(const primitive::Charac return ToXMLComposers::primitiveComposer.compose(data); } +char api<char>::parse(std::list<sax::Token>& input) { + return FromXMLParsers::primitiveParser.parseCharacterRaw(input); +} + +std::list<sax::Token> api<char>::compose(char data) { + return ToXMLComposers::primitiveComposer.compose(data); +} + +unsigned api<unsigned>::parse(std::list<sax::Token>& input) { + return FromXMLParsers::primitiveParser.parseCharacterRaw(input); +} + +std::list<sax::Token> api<unsigned>::compose(unsigned data) { + return ToXMLComposers::primitiveComposer.compose(data); +} + +bool api<bool>::parse(std::list<sax::Token>& input) { + return FromXMLParsers::primitiveParser.parseCharacterRaw(input); +} + +std::list<sax::Token> api<bool>::compose(bool data) { + return ToXMLComposers::primitiveComposer.compose(data); +} void ToXMLComposers::Visit(void* data, const Void& voidObject) const { *((std::list<sax::Token>*) data) = std::move(api<Void>::compose(voidObject)); diff --git a/alib2data/src/Api.hpp b/alib2data/src/Api.hpp index 7d5b11d276bc05346922852a016935afad2e306f..2a0e0acc792a3670a12cb09145788301f359045a 100644 --- a/alib2data/src/Api.hpp +++ b/alib2data/src/Api.hpp @@ -436,18 +436,48 @@ struct api<primitive::String> { static std::list<sax::Token> compose(const primitive::String& data); }; +template<> +struct api<std::string> { + static std::string parse(std::list<sax::Token>& input); + static std::list<sax::Token> compose(const std::string& data); +}; + template<> struct api<primitive::Integer> { static primitive::Integer parse(std::list<sax::Token>& input); static std::list<sax::Token> compose(const primitive::Integer& data); }; +template<> +struct api<int> { + static int parse(std::list<sax::Token>& input); + static std::list<sax::Token> compose(int data); +}; + template<> struct api<primitive::Character> { static primitive::Character parse(std::list<sax::Token>& input); static std::list<sax::Token> compose(const primitive::Character& data); }; +template<> +struct api<char> { + static char parse(std::list<sax::Token>& input); + static std::list<sax::Token> compose(char data); +}; + +template<> +struct api<unsigned> { + static unsigned parse(std::list<sax::Token>& input); + static std::list<sax::Token> compose(unsigned data); +}; + +template<> +struct api<bool> { + static bool parse(std::list<sax::Token>& input); + static std::list<sax::Token> compose(bool data); +}; + } /* namespace alib */ #include "container/ContainerFromXMLParser.hpp" diff --git a/alib2data/src/primitive/PrimitiveFromXMLParser.cpp b/alib2data/src/primitive/PrimitiveFromXMLParser.cpp index 43705c7ed92769c0d6a2e890f5c6e5330d6b6d97..329fa9f96f37c298e3a71557d9b13c64a7e068ce 100644 --- a/alib2data/src/primitive/PrimitiveFromXMLParser.cpp +++ b/alib2data/src/primitive/PrimitiveFromXMLParser.cpp @@ -8,6 +8,7 @@ #include "PrimitiveFromXMLParser.h" #include "../sax/ParserException.h" #include <algorithm> +#include "../std/stou.h" namespace primitive { @@ -59,4 +60,46 @@ String PrimitiveFromXMLParser::parseString(std::list<sax::Token>& input) const { return data; } +int PrimitiveFromXMLParser::parseIntegerRaw(std::list<sax::Token>& input) const { + popToken(input, sax::Token::TokenType::START_ELEMENT, "Integer"); + int data = std::stoi(popTokenData(input, sax::Token::TokenType::CHARACTER)); + popToken(input, sax::Token::TokenType::END_ELEMENT, "Integer"); + return data; +} + +char PrimitiveFromXMLParser::parseCharacterRaw(std::list<sax::Token>& input) const { + popToken(input, sax::Token::TokenType::START_ELEMENT, "Character"); + char data = popTokenData(input, sax::Token::TokenType::CHARACTER)[0]; + popToken(input, sax::Token::TokenType::END_ELEMENT, "Character"); + return data; +} + +std::string PrimitiveFromXMLParser::parseStringRaw(std::list<sax::Token>& input) const { + popToken(input, sax::Token::TokenType::START_ELEMENT, "String"); + std::string data = popTokenData(input, sax::Token::TokenType::CHARACTER); + popToken(input, sax::Token::TokenType::END_ELEMENT, "String"); + return data; +} + +unsigned PrimitiveFromXMLParser::parseUnsignedRaw(std::list<sax::Token>& input) const { + popToken(input, sax::Token::TokenType::START_ELEMENT, "Unsigned"); + unsigned data = std::stou(popTokenData(input, sax::Token::TokenType::CHARACTER)); + popToken(input, sax::Token::TokenType::END_ELEMENT, "Unsigned"); + return data; +} + +bool PrimitiveFromXMLParser::parseBoolRaw(std::list<sax::Token>& input) const { + popToken(input, sax::Token::TokenType::START_ELEMENT, "Bool"); + std::string tmp = popTokenData(input, sax::Token::TokenType::CHARACTER); + bool data; + if(tmp == "true") + data = true; + else if(tmp == "false") + data = false; + else + throw exception::AlibException("Invalid boolean value"); + popToken(input, sax::Token::TokenType::END_ELEMENT, "Bool"); + return data; +} + } /* namespace primitive */ diff --git a/alib2data/src/primitive/PrimitiveFromXMLParser.h b/alib2data/src/primitive/PrimitiveFromXMLParser.h index 9f8459e041953eef270ad2444452a759504d490e..7c48fa8e1dec28a01dd003af1cdd8554c1020db9 100644 --- a/alib2data/src/primitive/PrimitiveFromXMLParser.h +++ b/alib2data/src/primitive/PrimitiveFromXMLParser.h @@ -37,6 +37,12 @@ class PrimitiveFromXMLParser : public sax::FromXMLParserHelper { Character parseCharacter(std::list<sax::Token>& input) const; String parseString(std::list<sax::Token>& input) const; + int parseIntegerRaw(std::list<sax::Token>& input) const; + char parseCharacterRaw(std::list<sax::Token>& input) const; + std::string parseStringRaw(std::list<sax::Token>& input) const; + unsigned parseUnsignedRaw(std::list<sax::Token>& input) const; + bool parseBoolRaw(std::list<sax::Token>& input) const; + template<typename T> friend class alib::api; public: bool first(std::list<sax::Token>& input) const; diff --git a/alib2data/src/primitive/PrimitiveToXMLComposer.cpp b/alib2data/src/primitive/PrimitiveToXMLComposer.cpp index 8a650c5c47738f47991462f91e64a7f1eee8c15a..21710da13234f92951af13e6f358fb28b295ae4f 100644 --- a/alib2data/src/primitive/PrimitiveToXMLComposer.cpp +++ b/alib2data/src/primitive/PrimitiveToXMLComposer.cpp @@ -10,6 +10,7 @@ #include "String.h" #include "Character.h" #include "../std/itos.h" +#include "../std/utos.h" #include "../Api.hpp" @@ -51,4 +52,47 @@ std::list<sax::Token> PrimitiveToXMLComposer::compose(const String& primitive) c return out; } +std::list<sax::Token> PrimitiveToXMLComposer::compose(int primitive) const { + std::list<sax::Token> out; + out.push_back(sax::Token("Integer", sax::Token::TokenType::START_ELEMENT)); + out.push_back(sax::Token(std::itos(primitive), sax::Token::TokenType::CHARACTER)); + out.push_back(sax::Token("Integer", sax::Token::TokenType::END_ELEMENT)); + return out; +} + +std::list<sax::Token> PrimitiveToXMLComposer::compose(char primitive) const { + std::list<sax::Token> out; + out.push_back(sax::Token("Character", sax::Token::TokenType::START_ELEMENT)); + out.push_back(sax::Token(std::string(1, primitive), sax::Token::TokenType::CHARACTER)); + out.push_back(sax::Token("Character", sax::Token::TokenType::END_ELEMENT)); + return out; +} + +std::list<sax::Token> PrimitiveToXMLComposer::compose(const std::string& primitive) const { + std::list<sax::Token> out; + out.push_back(sax::Token("String", sax::Token::TokenType::START_ELEMENT)); + out.push_back(sax::Token(primitive, sax::Token::TokenType::CHARACTER)); + out.push_back(sax::Token("String", sax::Token::TokenType::END_ELEMENT)); + return out; +} + +std::list<sax::Token> PrimitiveToXMLComposer::compose(unsigned primitive) const { + std::list<sax::Token> out; + out.push_back(sax::Token("Unsigned", sax::Token::TokenType::START_ELEMENT)); + out.push_back(sax::Token(std::utos(primitive), sax::Token::TokenType::CHARACTER)); + out.push_back(sax::Token("Unsigned", sax::Token::TokenType::END_ELEMENT)); + return out; +} + +std::list<sax::Token> PrimitiveToXMLComposer::compose(bool primitive) const { + std::list<sax::Token> out; + out.push_back(sax::Token("Bool", sax::Token::TokenType::START_ELEMENT)); + if(primitive) + out.push_back(sax::Token("true", sax::Token::TokenType::END_ELEMENT)); + else + out.push_back(sax::Token("false", sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("Bool", sax::Token::TokenType::END_ELEMENT)); + return out; +} + } /* namespace primitive */ diff --git a/alib2data/src/primitive/PrimitiveToXMLComposer.h b/alib2data/src/primitive/PrimitiveToXMLComposer.h index 1a541349f98bf62684cb96d7e5a0d9cff6dda54e..9c26fefd025798cb604c6374c48b0f4813328afc 100644 --- a/alib2data/src/primitive/PrimitiveToXMLComposer.h +++ b/alib2data/src/primitive/PrimitiveToXMLComposer.h @@ -9,6 +9,7 @@ #define PRIMITIVE_TO_XML_COMPOSER_H_ #include <list> +#include <string> #include "Primitive.h" #include "../sax/Token.h" @@ -44,6 +45,12 @@ class PrimitiveToXMLComposer { std::list<sax::Token> compose(const Integer& primitive) const; std::list<sax::Token> compose(const Character& primitive) const; + std::list<sax::Token> compose(const std::string& primitive) const; + std::list<sax::Token> compose(int primitive) const; + std::list<sax::Token> compose(char primitive) const; + std::list<sax::Token> compose(unsigned primitive) const; + std::list<sax::Token> compose(bool primitive) const; + template<typename T> friend class alib::api; }; diff --git a/alib2data/src/std/stou.cpp b/alib2data/src/std/stou.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d1de978dc72e1d530be6537e5b73c7590603c3de --- /dev/null +++ b/alib2data/src/std/stou.cpp @@ -0,0 +1,12 @@ +#include "stou.h" + +namespace std { + +unsigned stou(const std::string& string, int) { + stringstream ss(string); + unsigned integer; + ss >> integer; + return integer; +} + +} diff --git a/alib2data/src/std/stou.h b/alib2data/src/std/stou.h new file mode 100644 index 0000000000000000000000000000000000000000..c6a04fe3fe40ffc59e5ea80d8474456da2963a65 --- /dev/null +++ b/alib2data/src/std/stou.h @@ -0,0 +1,13 @@ +#ifndef STOU_H_ +#define STOU_H_ + +#include <sstream> +#include <string> + +namespace std { + +unsigned stou(const std::string&, int base = 10); + +} /* namespace std */ + +#endif /* STOU_H_ */ diff --git a/alib2data/src/std/utos.cpp b/alib2data/src/std/utos.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fbec7d462b65d7e450d5b530f6dd02777d3d9197 --- /dev/null +++ b/alib2data/src/std/utos.cpp @@ -0,0 +1,11 @@ +#include "utos.h" + +namespace std { + +string utos(unsigned integer, int) { + stringstream ss; + ss << integer; + return ss.str(); +} + +} diff --git a/alib2data/src/std/utos.h b/alib2data/src/std/utos.h new file mode 100644 index 0000000000000000000000000000000000000000..726d5e0430e434ed4359a348e17f27358414b84c --- /dev/null +++ b/alib2data/src/std/utos.h @@ -0,0 +1,13 @@ +#ifndef UTOS_H_ +#define UTOS_H_ + +#include <sstream> +#include <string> + +namespace std { + +string utos(unsigned integer, int base = 10); + +} /* namespace std */ + +#endif /* UTOS_H_ */ diff --git a/arun2/src/arun.cpp b/arun2/src/arun.cpp index 6982d2a0ee74d99053fafdf81c2265d707e834f5..76d412735200c7f78ad8198d943adf2f4d052185 100644 --- a/arun2/src/arun.cpp +++ b/arun2/src/arun.cpp @@ -50,21 +50,13 @@ int main(int argc, char* argv[]) { string::LinearString input = alib::DataFactory::fromTokens<string::LinearString>(inputTokens); automaton::Automaton automaton = alib::DataFactory::fromTokens<automaton::Automaton>(automatonTokens); std::set<unsigned> res = automaton::run::Occurrences::occurrences(automaton, input); - for(unsigned number : res ) { - std::cout << number << std::endl; - } + alib::DataFactory::toStdout( res ); return 0; } else if( type.getValue() == "accept") { string::LinearString input = alib::DataFactory::fromTokens<string::LinearString>(inputTokens); automaton::Automaton automaton = alib::DataFactory::fromTokens<automaton::Automaton>(automatonTokens); bool res = automaton::run::Accept::accept(automaton, input); - if(res) { - std::cout << "Input matches." << std::endl; - return 0; - } else { - std::cout << "Input don't matches." << std::endl; - return 1; - } + alib::DataFactory::toStdout( res ); } else { throw exception::AlibException( "Invalid run type" ); return 1; diff --git a/astringology2/src/astringology.cpp b/astringology2/src/astringology.cpp index b430d27fc513f16e0ea3f9a1eff1ef3e8c6fd5fc..03de19343ebd6a7963140cf8d3bc14cf51705437 100644 --- a/astringology2/src/astringology.cpp +++ b/astringology2/src/astringology.cpp @@ -50,9 +50,7 @@ int main(int argc, char* argv[]) { string::String subject = alib::DataFactory::fromTokens<string::String>(subjectTokens); string::String pattern = alib::DataFactory::fromTokens<string::String>(patternTokens); std::set<unsigned> res = string::naive::ExactMatch::match(subject, pattern); - for(unsigned number : res ) { - std::cout << number << std::endl; - } + alib::DataFactory::toStdout(res); return 0; } else if( algorithm.getValue() == "exactMatchingAutomaton") { string::String pattern = alib::DataFactory::fromTokens<string::String>(patternTokens);