diff --git a/alib2data/src/XmlApi.cpp b/alib2data/src/XmlApi.cpp index 30ee8856dee554b20e226e4b2f8d82ff0451c69e..ce6c606d9474e9b0edbbfb68709f07f9beaed2aa 100644 --- a/alib2data/src/XmlApi.cpp +++ b/alib2data/src/XmlApi.cpp @@ -93,7 +93,6 @@ const std::string Names::REGEXP_FORMAL_REGEXP = "FormalRegExp"; const std::string Names::STRING_LINEAR_STRING = "LinearString"; const std::string Names::STRING_CYCLIC_STRING = "CyclicString"; const std::string Names::STRING_EPSILON = "Epsilon"; -const std::string Names::PRIMITIVE_PRIMITIVE = "Primitive"; const std::string Names::PRIMITIVE_STRING = "String"; const std::string Names::PRIMITIVE_INTEGER = "Integer"; const std::string Names::PRIMITIVE_CHARACTER = "Character"; diff --git a/alib2data/src/XmlApi.hpp b/alib2data/src/XmlApi.hpp index 65e8d82ab77e166ff85205d8eec0d634c7cdb6b4..1c4b5f54fd71fb5561dfb9a1d859a4aeb11095bc 100644 --- a/alib2data/src/XmlApi.hpp +++ b/alib2data/src/XmlApi.hpp @@ -94,7 +94,6 @@ public: const static std::string STRING_LINEAR_STRING; const static std::string STRING_CYCLIC_STRING; const static std::string STRING_EPSILON; - const static std::string PRIMITIVE_PRIMITIVE; const static std::string PRIMITIVE_STRING; const static std::string PRIMITIVE_INTEGER; const static std::string PRIMITIVE_CHARACTER; diff --git a/alib2data/src/alphabet/Symbol.cpp b/alib2data/src/alphabet/Symbol.cpp index 4d8dae2132394bb6f5cc39f24c4ac9ba3a6b6190..9fd53443cc3609d7fcbf9f25418c1541517c9692 100644 --- a/alib2data/src/alphabet/Symbol.cpp +++ b/alib2data/src/alphabet/Symbol.cpp @@ -36,9 +36,13 @@ alphabet::Symbol symbolFrom( char character ) { return alphabet::Symbol { alphabet::LabeledSymbol { label::labelFrom ( character ) } }; } -alphabet::Symbol symbolFrom( std::string string ) { +alphabet::Symbol symbolFrom( const std::string& string ) { return alphabet::Symbol { alphabet::LabeledSymbol { label::labelFrom ( string ) } }; } +alphabet::Symbol symbolFrom( const char* string ) { + return symbolFrom( (std::string) string ); +} + } /* namespace alphabet */ diff --git a/alib2data/src/alphabet/Symbol.h b/alib2data/src/alphabet/Symbol.h index 6b7883c14ba2f92a477cd0c5fadbce7c389f4577..ec8fddcfd9fb448b7a5249275168be01538cd39c 100644 --- a/alib2data/src/alphabet/Symbol.h +++ b/alib2data/src/alphabet/Symbol.h @@ -34,7 +34,8 @@ alphabet::Symbol createUniqueSymbol(const alphabet::Symbol& base, const std::set alphabet::Symbol symbolFrom(int number); alphabet::Symbol symbolFrom(char character); -alphabet::Symbol symbolFrom(std::string string); +alphabet::Symbol symbolFrom(const std::string& string); +alphabet::Symbol symbolFrom(const char* string); } /* namespace alphabet */ diff --git a/alib2data/src/automaton/common/State.cpp b/alib2data/src/automaton/common/State.cpp index 6f5319aa420e1693dfa7161200dcb078dae3dfd5..a1ae066da0a990cc4f42ec9acfc097597ac9e1d8 100644 --- a/alib2data/src/automaton/common/State.cpp +++ b/alib2data/src/automaton/common/State.cpp @@ -25,6 +25,9 @@ State::State(char character) : name(label::labelFrom(character)) { State::State(const std::string& name) : name(label::labelFrom(name)) { } +State::State(const char* name) : State((std::string) name) { +} + const label::Label& State::getName() const { return name; } diff --git a/alib2data/src/automaton/common/State.h b/alib2data/src/automaton/common/State.h index dfa4b9bd8aaa83df08ea331b5b606ec8f622355a..f3a1512a84371786bc0dc4612e91f3038bc29370 100644 --- a/alib2data/src/automaton/common/State.h +++ b/alib2data/src/automaton/common/State.h @@ -27,6 +27,7 @@ public: explicit State(int number); explicit State(char character); explicit State(const std::string& name); + explicit State(const char* name); const label::Label& getName() const; diff --git a/alib2data/src/label/Label.cpp b/alib2data/src/label/Label.cpp index 05af029c78bed654981a4c97ec31570fd9baebcd..c5500fd1b4f466730e7b6cda415a4aa9ecc6ff52 100644 --- a/alib2data/src/label/Label.cpp +++ b/alib2data/src/label/Label.cpp @@ -19,9 +19,13 @@ label::Label labelFrom(char character) { return label::Label { label::PrimitiveLabel { primitive::primitiveFrom ( character ) } }; } -label::Label labelFrom(std::string string) { +label::Label labelFrom(const std::string& string) { return label::Label { label::PrimitiveLabel { primitive::primitiveFrom ( string ) } }; } +label::Label labelFrom(const char* string) { + return labelFrom((std::string) string); +} + } /* namespace label */ diff --git a/alib2data/src/label/Label.h b/alib2data/src/label/Label.h index feddac9c511789c271ca65338452afa9560029df..3201b4419d5d8e454fb0e6f684f641b1881d6f41 100644 --- a/alib2data/src/label/Label.h +++ b/alib2data/src/label/Label.h @@ -22,7 +22,8 @@ typedef alib::wrapper<LabelBase> Label; label::Label labelFrom(int number); label::Label labelFrom(char character); -label::Label labelFrom(std::string string); +label::Label labelFrom(const std::string& string); +label::Label labelFrom(const char* string); } /* namespace label */ diff --git a/alib2data/src/primitive/Primitive.cpp b/alib2data/src/primitive/Primitive.cpp index 24b9f9cf2eb1d00de2d10c7bd3c583919bf1291c..b6695aecbc977aad7ca98ca12f95006228a25eee 100644 --- a/alib2data/src/primitive/Primitive.cpp +++ b/alib2data/src/primitive/Primitive.cpp @@ -22,7 +22,11 @@ Primitive primitiveFrom(char character) { return Primitive { Character { character } }; } -Primitive primitiveFrom(std::string string) { +Primitive primitiveFrom(const char* string) { + return primitiveFrom((std::string) string); +} + +Primitive primitiveFrom(const std::string& string) { return Primitive { String { string } }; } diff --git a/alib2data/src/primitive/Primitive.h b/alib2data/src/primitive/Primitive.h index 5deca51cede323e818947b0dbc3ec66e707d3af7..731b106825933049087b82917069cdea227f72fc 100644 --- a/alib2data/src/primitive/Primitive.h +++ b/alib2data/src/primitive/Primitive.h @@ -22,7 +22,8 @@ typedef alib::wrapper<PrimitiveBase> Primitive; Primitive primitiveFrom(int number); Primitive primitiveFrom(char character); -Primitive primitiveFrom(std::string string); +Primitive primitiveFrom(const std::string& string); +Primitive primitiveFrom(const char* string); Primitive primitiveFrom(unsigned number); Primitive primitiveFrom(bool value); diff --git a/alib2data/src/primitive/PrimitiveFromXMLParser.cpp b/alib2data/src/primitive/PrimitiveFromXMLParser.cpp index 7c687adcec21032d59459bd9a2bac60780db8c8d..04c9533882e7f79358e256e5990c318644ce103f 100644 --- a/alib2data/src/primitive/PrimitiveFromXMLParser.cpp +++ b/alib2data/src/primitive/PrimitiveFromXMLParser.cpp @@ -63,9 +63,11 @@ Character PrimitiveFromXMLParser::parseCharacter(std::list<sax::Token>& input) c String PrimitiveFromXMLParser::parseString(std::list<sax::Token>& input) const { popToken(input, sax::Token::TokenType::START_ELEMENT, alib::Names::PRIMITIVE_STRING); - String data(popTokenData(input, sax::Token::TokenType::CHARACTER)); + std::string data = ""; + if( isTokenType(input, sax::Token::TokenType::CHARACTER)) + data = popTokenData(input, sax::Token::TokenType::CHARACTER); popToken(input, sax::Token::TokenType::END_ELEMENT, alib::Names::PRIMITIVE_STRING); - return data; + return String(data); } Unsigned PrimitiveFromXMLParser::parseUnsigned(std::list<sax::Token>& input) const { diff --git a/alib2data/src/primitive/PrimitiveToXMLComposer.cpp b/alib2data/src/primitive/PrimitiveToXMLComposer.cpp index 948d0c50882dc87eb593043de8f254cefa10adcd..83a3361c694fa978b2cec3602e19ba68f9f49375 100644 --- a/alib2data/src/primitive/PrimitiveToXMLComposer.cpp +++ b/alib2data/src/primitive/PrimitiveToXMLComposer.cpp @@ -45,9 +45,9 @@ void PrimitiveToXMLComposer::compose(std::list<sax::Token>& out, const Unsigned& void PrimitiveToXMLComposer::compose(std::list<sax::Token>& out, const Bool& primitive) const { out.push_back(sax::Token(alib::Names::PRIMITIVE_BOOL, sax::Token::TokenType::START_ELEMENT)); if(primitive.getData()) - out.push_back(sax::Token("true", sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("true", sax::Token::TokenType::CHARACTER)); else - out.push_back(sax::Token("false", sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("false", sax::Token::TokenType::CHARACTER)); out.push_back(sax::Token(alib::Names::PRIMITIVE_BOOL, sax::Token::TokenType::END_ELEMENT)); } @@ -84,9 +84,9 @@ void PrimitiveToXMLComposer::compose(std::list<sax::Token>& out, unsigned primit void PrimitiveToXMLComposer::compose(std::list<sax::Token>& out, bool primitive) const { out.push_back(sax::Token(alib::Names::PRIMITIVE_BOOL, sax::Token::TokenType::START_ELEMENT)); if(primitive) - out.push_back(sax::Token("true", sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("true", sax::Token::TokenType::CHARACTER)); else - out.push_back(sax::Token("false", sax::Token::TokenType::END_ELEMENT)); + out.push_back(sax::Token("false", sax::Token::TokenType::CHARACTER)); out.push_back(sax::Token(alib::Names::PRIMITIVE_BOOL, sax::Token::TokenType::END_ELEMENT)); } diff --git a/alib2data/src/primitive/String.cpp b/alib2data/src/primitive/String.cpp index 5fcc6edb63dda0289fc75aa53f3813049394cd77..b9bbb6f58fc4c823aa8209e24073950b9ac4baa7 100644 --- a/alib2data/src/primitive/String.cpp +++ b/alib2data/src/primitive/String.cpp @@ -17,6 +17,10 @@ String::String(std::string&& data) : data(std::move(data)) { } +String::String(const char* data) : String((std::string) data) { + +} + PrimitiveBase* String::clone() const { return new String(*this); } diff --git a/alib2data/src/primitive/String.h b/alib2data/src/primitive/String.h index 83bde177e842fa7e156b473f9866bfbfbb5e396a..0ceb083f34a3edd385b45ab1e8d9b21d4cb6c1f0 100644 --- a/alib2data/src/primitive/String.h +++ b/alib2data/src/primitive/String.h @@ -31,6 +31,8 @@ public: explicit String(std::string&& data); + explicit String(const char* data); + virtual PrimitiveBase* clone() const; virtual PrimitiveBase* plunder() &&; diff --git a/alib2data/src/regexp/RegExp.cpp b/alib2data/src/regexp/RegExp.cpp index fde75f2fa71d6fa74b43bea57acdb3689f82ff0e..b0081b93d851eb1a24ae952c799ea6ab73eb0fe5 100644 --- a/alib2data/src/regexp/RegExp.cpp +++ b/alib2data/src/regexp/RegExp.cpp @@ -21,6 +21,10 @@ regexp::RegExp regexpFrom( const std::string& string ) { return regexp::RegExp { regexp::UnboundedRegExp ( con ) }; } +regexp::RegExp regexpFrom( const char* string ) { + return regexpFrom( (std::string) string ); +} + regexp::RegExp regexpFrom( const string::LinearString& string ) { regexp::UnboundedRegExpConcatenation con; for(const auto& symbol : string.getContent()) { diff --git a/alib2data/src/regexp/RegExp.h b/alib2data/src/regexp/RegExp.h index 828ceb1b6f5904a2863f825f18d63c900cd8d60e..7f031301219f0e476836dea140cdcd166a0e115b 100644 --- a/alib2data/src/regexp/RegExp.h +++ b/alib2data/src/regexp/RegExp.h @@ -25,6 +25,8 @@ typedef alib::wrapper<RegExpBase> RegExp; regexp::RegExp regexpFrom( const std::string& string ); +regexp::RegExp regexpFrom( const char* string ); + regexp::RegExp regexpFrom( const string::LinearString& string ); regexp::RegExp regexpFrom( const alphabet::Symbol& symbol ); diff --git a/alib2data/src/sax/SaxComposeInterface.cpp b/alib2data/src/sax/SaxComposeInterface.cpp index 7bff5bb5b23774d1035bebfa23e78d6f6d7e64a1..353d478cc2eb3b9c7e5857eb2b53687eb628fc15 100644 --- a/alib2data/src/sax/SaxComposeInterface.cpp +++ b/alib2data/src/sax/SaxComposeInterface.cpp @@ -1,8 +1,8 @@ /* * SaxComposeInterface.cpp * - * Created on: 8.8.2012 - * Author: Martin Zak + * Created on: 8.8.2012 + * Author: Martin Zak */ #include "SaxComposeInterface.h" @@ -19,9 +19,9 @@ namespace sax { void SaxComposeInterface::printMemory(std::string& xmlOut, const std::list<Token>& in) { xmlBufferPtr buf = xmlBufferCreate(); xmlTextWriterPtr writer = xmlNewTextWriterMemory(buf, 0); - + SaxComposeInterface::xmlSAXUserPrint(writer, in); - + xmlFreeTextWriter(writer); xmlOut = (const char*) buf->content; xmlBufferFree(buf); @@ -31,7 +31,7 @@ void SaxComposeInterface::printFile(const std::string& filename, const std::list xmlTextWriterPtr writer = xmlNewTextWriterFilename(filename.c_str(), 0); SaxComposeInterface::xmlSAXUserPrint(writer, in); - + xmlFreeTextWriter(writer); } @@ -42,9 +42,9 @@ void SaxComposeInterface::printStdout(const std::list<Token>& in) { void SaxComposeInterface::printStream(std::ostream& out, const std::list<Token>& in) { xmlBufferPtr buf = xmlBufferCreate(); xmlTextWriterPtr writer = xmlNewTextWriterMemory(buf, 0); - + SaxComposeInterface::xmlSAXUserPrint(writer, in); - + xmlFreeTextWriter(writer); out << (const char*) buf->content; xmlBufferFree(buf); @@ -54,33 +54,33 @@ void SaxComposeInterface::xmlSAXUserPrint(xmlTextWriterPtr writer, const std::li xmlTextWriterStartDocument(writer, NULL, NULL, NULL); std::deque<Token> stack; for(const Token& token : in) { - switch(token.getType()) { + switch(token.getType()) { case Token::TokenType::START_ELEMENT: - xmlTextWriterStartElement(writer, (const xmlChar*) token.getData().c_str()); - stack.push_back(token); - break; + xmlTextWriterStartElement(writer, (const xmlChar*) token.getData().c_str()); + stack.push_back(token); + break; case Token::TokenType::END_ELEMENT: - if(stack.empty() || stack.back().getData() != token.getData() || stack.back().getType() != Token::TokenType::START_ELEMENT) { - throw ComposerException(Token(stack.back().getData(), Token::TokenType::END_ELEMENT), token); - } - stack.pop_back(); - xmlTextWriterEndElement(writer); - break; + if(stack.empty() || stack.back().getData() != token.getData() || stack.back().getType() != Token::TokenType::START_ELEMENT) { + throw ComposerException(Token(stack.back().getData(), Token::TokenType::END_ELEMENT), token); + } + stack.pop_back(); + xmlTextWriterEndElement(writer); + break; case Token::TokenType::CHARACTER: - xmlTextWriterWriteString(writer, (const xmlChar*) token.getData().c_str()); - break; + xmlTextWriterWriteString(writer, (const xmlChar*) token.getData().c_str()); + break; case Token::TokenType::START_ATTRIBUTE: - xmlTextWriterStartAttribute(writer, (const xmlChar*) token.getData().c_str()); - stack.push_back(token); - break; + xmlTextWriterStartAttribute(writer, (const xmlChar*) token.getData().c_str()); + stack.push_back(token); + break; case Token::TokenType::END_ATTRIBUTE: - if(stack.empty() || stack.back().getData() != token.getData() || stack.back().getType() != Token::TokenType::START_ATTRIBUTE) { - throw ComposerException(Token(stack.back().getData(), Token::TokenType::END_ATTRIBUTE), token); - } - stack.pop_back(); - xmlTextWriterEndAttribute(writer); - break; - } + if(stack.empty() || stack.back().getData() != token.getData() || stack.back().getType() != Token::TokenType::START_ATTRIBUTE) { + throw ComposerException(Token(stack.back().getData(), Token::TokenType::END_ATTRIBUTE), token); + } + stack.pop_back(); + xmlTextWriterEndAttribute(writer); + break; + } } xmlTextWriterEndDocument(writer); } diff --git a/alib2data/src/string/CyclicString.cpp b/alib2data/src/string/CyclicString.cpp index 640a5c77a9472f435b953633b1ff63ee367a7c79..443be9b159f095d44cfe4990093567b78cc70d08 100644 --- a/alib2data/src/string/CyclicString.cpp +++ b/alib2data/src/string/CyclicString.cpp @@ -50,6 +50,9 @@ CyclicString::CyclicString(const std::string& str) { alphabet = std::set<alphabet::Symbol>(m_Data.begin(), m_Data.end()); } +CyclicString::CyclicString(const char* str) : CyclicString((std::string) str) { +} + CyclicString::CyclicString(const Epsilon& epsilon) { alphabet = epsilon.getAlphabet(); } diff --git a/alib2data/src/string/CyclicString.h b/alib2data/src/string/CyclicString.h index 2026c6f1d523190bcc8ac879c001a6cffb734a23..b45a41ba9ab7eea5e6d8edc62b09b402cfa7d477 100644 --- a/alib2data/src/string/CyclicString.h +++ b/alib2data/src/string/CyclicString.h @@ -33,6 +33,7 @@ public: explicit CyclicString(const std::vector<alphabet::Symbol>& data); explicit CyclicString(std::vector<alphabet::Symbol>&& data); explicit CyclicString(const std::string& str); + explicit CyclicString(const char* str); explicit CyclicString(const Epsilon& epsilon); virtual StringBase* clone() const; diff --git a/alib2data/src/string/LinearString.cpp b/alib2data/src/string/LinearString.cpp index c02fde9bb282d9559ac392f946831cf338939975..856cfd2a7afda8623125d6f5b9d5ac6181397bed 100644 --- a/alib2data/src/string/LinearString.cpp +++ b/alib2data/src/string/LinearString.cpp @@ -50,6 +50,9 @@ LinearString::LinearString(const std::string& str) { alphabet = std::set<alphabet::Symbol>(m_Data.begin(), m_Data.end()); } +LinearString::LinearString(const char* str) : LinearString((std::string) str) { +} + LinearString::LinearString(const Epsilon& epsilon) { alphabet = epsilon.getAlphabet(); } diff --git a/alib2data/src/string/LinearString.h b/alib2data/src/string/LinearString.h index ee463d1ea400124df4c488ccf7653bed3efe647e..05da8ee3d550f4f511f41c828529dc8146c76d53 100644 --- a/alib2data/src/string/LinearString.h +++ b/alib2data/src/string/LinearString.h @@ -34,6 +34,7 @@ public: explicit LinearString(const std::vector<alphabet::Symbol>& data); explicit LinearString(std::vector<alphabet::Symbol>&& data); explicit LinearString(const std::string& str); + explicit LinearString(const char* str); explicit LinearString(const Epsilon& epsilon); virtual StringBase* clone() const; diff --git a/alib2data/src/string/String.cpp b/alib2data/src/string/String.cpp index 8d08af43095ff7f05ea0a8bf35ff8bbe0209ac85..05368df2501a9ff35ec8ad49391311a17cc8955f 100644 --- a/alib2data/src/string/String.cpp +++ b/alib2data/src/string/String.cpp @@ -19,5 +19,9 @@ string::String stringFrom( const std::string& string ) { return string::String { string::LinearString { string } }; } +string::String stringFrom( const char* string ) { + return stringFrom( (std::string) string); +} + } /* namespace string */ diff --git a/alib2data/src/string/String.h b/alib2data/src/string/String.h index 5d87b2f9bba7240937361e832a291f5713f1d1bf..2ef1e1800132b44bafae156f7c8331348cd60810 100644 --- a/alib2data/src/string/String.h +++ b/alib2data/src/string/String.h @@ -23,6 +23,7 @@ typedef alib::wrapper<StringBase> String; string::String stringFrom(const alphabet::Symbol& symbol); string::String stringFrom(const std::string& str); +string::String stringFrom(const char* str); } /* namespace string */ diff --git a/alib2data/test-src/primitive/PrimitiveTest.cpp b/alib2data/test-src/primitive/PrimitiveTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e64cf8f66aa384a2bf2f6cc1ae3acf1aba3454f1 --- /dev/null +++ b/alib2data/test-src/primitive/PrimitiveTest.cpp @@ -0,0 +1,47 @@ +#include <list> +#include "PrimitiveTest.h" + +#include "sax/SaxParseInterface.h" +#include "sax/SaxComposeInterface.h" + +#include "primitive/Primitive.h" + +#include "factory/XmlDataFactory.hpp" +#include "factory/StringDataFactory.hpp" +#include <algorithm> + +#define CPPUNIT_EXCLUSIVE_OR(x, y) CPPUNIT_ASSERT((!(x) && (y)) || ((x) && !(y))) + +CPPUNIT_TEST_SUITE_REGISTRATION( PrimitiveTest ); + +void PrimitiveTest::setUp() { +} + +void PrimitiveTest::tearDown() { +} + +void PrimitiveTest::testXMLParser() { + + primitive::Primitive primitive = primitive::primitiveFrom("aaa"); + std::cout << primitive << std::endl; + + { + std::list<sax::Token> tokens = alib::XmlDataFactory::toTokens(primitive); + std::cout << tokens << std::endl; + std::string tmp; + sax::SaxComposeInterface::printMemory(tmp, tokens); + + std::list<sax::Token> tokens2; + sax::SaxParseInterface::parseMemory(tmp, tokens2); + primitive::Primitive primitive2 = alib::XmlDataFactory::fromTokens<primitive::Primitive>(tokens2); + + CPPUNIT_ASSERT( primitive == primitive2 ); + } + { + std::string tmp = alib::XmlDataFactory::toString(primitive); + primitive::Primitive primitive2 = alib::XmlDataFactory::fromString<primitive::Primitive>(tmp); + + CPPUNIT_ASSERT( primitive == primitive2 ); + } +} + diff --git a/alib2data/test-src/primitive/PrimitiveTest.h b/alib2data/test-src/primitive/PrimitiveTest.h new file mode 100644 index 0000000000000000000000000000000000000000..e24b6ff857321af473e8b957a249196e6e139aba --- /dev/null +++ b/alib2data/test-src/primitive/PrimitiveTest.h @@ -0,0 +1,19 @@ +#ifndef PRIMITIVE_TEST_H_ +#define PRIMITIVE_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class PrimitiveTest : public CppUnit::TestFixture +{ + CPPUNIT_TEST_SUITE( PrimitiveTest ); + CPPUNIT_TEST( testXMLParser ); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + void tearDown(); + + void testXMLParser(); +}; + +#endif // PRIMITIVE_TEST_H_