Skip to content

Commit

Permalink
implemented node and derivates serialize, fixed some compiler warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
gittiver committed Oct 3, 2024
1 parent 40aca2f commit 983a57a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/delegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ XML_Error Error::errorcode() const
return error;
}

abstract_delegate::abstract_delegate() {}

void abstract_delegate::onStartElement(const XML_Char */* fullname */,
const XML_Char ** /* atts */)
{}
Expand Down
4 changes: 2 additions & 2 deletions src/delegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace xmlpp {
/** wrapper class type for expats XML_Error */
class Error {
public:
Error (XML_Error error) noexcept;
explicit Error (XML_Error error) noexcept;
std::string to_string() const;
XML_Error errorcode() const;
private:
Expand Down Expand Up @@ -216,7 +216,7 @@ class delegate {
only needed methods needs to be overridden, all interface maethods have an empt default implementation */
class abstract_delegate : public delegate {
public:
abstract_delegate();
abstract_delegate()=default;

void onStartElement(const XML_Char *fullname, const XML_Char **atts) override;
void onEndElement( const XML_Char *fullname) override;
Expand Down
35 changes: 35 additions & 0 deletions src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,38 @@
* See LICENSE for copyright information.
*/
#include "generator.hpp"

void xmlpp::generator::element::serialize(std::ostream &os) {
os << '<' << name;
if (!attributes.empty()) {
os << ' ';
for (const auto& a : attributes) {
os << a.name << '=' << a.value << '"';
}
}
os << "/>";
}

void xmlpp::generator::composite_element::serialize(std::ostream &os) {
os << '<' << name;
if (!attributes.empty()) {
os << ' ';
for (const auto& a : attributes) {
os << a.name << '=' << a.value << '"';
}
}

if (children.empty()) {
os << " />";
}
else {
for (const auto& e : children) {
e->serialize(os);
}
os << "</" << name << '>';
}
}

void xmlpp::generator::text::serialize(std::ostream &os) {
os << value;
}
6 changes: 4 additions & 2 deletions src/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <list>
#include <memory>
#include <iostream>

namespace xmlpp {
/** generating xml elements */
Expand All @@ -19,7 +20,6 @@ struct attribute {
};

struct node {
protected:
virtual void serialize(std::ostream& os) = 0;
};

Expand All @@ -32,6 +32,8 @@ struct element: public node {

struct composite_element: public element {
std::list<std::shared_ptr<node>> children;
protected:
void serialize(std::ostream& os) override;
};

struct text: public node {
Expand All @@ -41,4 +43,4 @@ struct text: public node {
};
} // end namespace generator
} // end: namespace xmlpp
#endif // #ifndef xlmpp_generator_hpp
#endif // #ifndef xmlpp_generator_hpp
2 changes: 1 addition & 1 deletion src/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void StatefulDelegate::onStartElement( const XML_Char *fullname,
processed = true;
break;
}
};
}
if (!processed)
{
cerr << "unexpected Element: " << fullname << "@" << parseStates.top()->tag << endl;
Expand Down
8 changes: 4 additions & 4 deletions src/state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
namespace xmlpp {

struct State {
State(const std::string& _tag,
explicit State(const std::string& _tag,
std::function<void (const XML_Char **atts)> _pfStart = nullptr,
std::function<void ()> _pfEnd = nullptr,
std::function<void (const char *pBuf, int len)> _pfText = nullptr
Expand All @@ -29,12 +29,12 @@ struct State {
tag{_tag}
{}

virtual ~State(){}
virtual ~State()=default;

void addState(State* s) { substates_.push_back(s); }
State* addState(const std::string& tagname)
{
State* s = new State(tagname);
auto s = new State(tagname);
substates_.push_back(s);
return s;
}
Expand All @@ -43,7 +43,7 @@ struct State {
std::function<void ()> pfEnd{nullptr};
std::function<void (const char *pBuf, int len)> pfText{nullptr};

std::string tag;
std::string tag{};
private:
std::list<State*> substates_;
};
Expand Down
15 changes: 8 additions & 7 deletions src/xmlparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
* See LICENSE for copyright information.
*/

#include <string.h>
#include "xmlparser.hpp"
#include <cstring>
#include <expat.h>

#include "xmlparser.hpp"

using std::string;

using xmlpp::parser;
Expand Down Expand Up @@ -302,25 +303,25 @@ xmlpp::parser::result parser::parseFile(const std::string& filename,
parser::error_t parser::errorcode() const
{ return (error_t)XML_GetErrorCode(m_parser); }

int parser::current_line_number() const
size_t parser::current_line_number() const
{ return XML_GetCurrentLineNumber(m_parser); }
int parser::current_column_number() const
size_t parser::current_column_number() const
{ return XML_GetCurrentColumnNumber(m_parser); }

const XML_Char* parser::xmlGetAttrValue(const XML_Char** attrs,
const XML_Char* key)
{
if (attrs!=NULL)
if (attrs!= nullptr)
{
for (size_t i = 0; attrs[i]!=NULL;i+=2)
for (size_t i = 0; attrs[i]!=nullptr;i+=2)
{
if (!strcmp(attrs[i],key))
{
return attrs[i+1];
}
}
}
return NULL;
return nullptr;
}

std::string Attr::getValue(const char* key)
Expand Down
10 changes: 5 additions & 5 deletions src/xmlparser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace xmlpp {
/** namespace for SAX2 xml Parser based on expat */
class parser {
public:
enum class result : uint8_t {
enum result : uint8_t {
OK,
NO_DELEGATE,
ERROR_OPEN_FILE,
Expand Down Expand Up @@ -74,7 +74,7 @@ class parser {
INVALID_ARGUMENT
};

parser(delegate& delegate,char namespaceSeparator = ':');
explicit parser(delegate& delegate,char namespaceSeparator = ':');
virtual ~parser();

static result parseString(const char*pszString, delegate& delegate);
Expand All @@ -91,8 +91,8 @@ class parser {

status_t parse(const char* buffer, int len, bool isFinal);
error_t errorcode() const;
int current_line_number() const ;
int current_column_number() const ;
size_t current_line_number() const ;
size_t current_column_number() const ;
private:
XML_Parser m_parser;
};
Expand All @@ -102,7 +102,7 @@ class parser {

class Attr {
public:
Attr(const XML_Char** attrs) : attrs_(attrs){};
explicit Attr(const XML_Char** attrs) : attrs_(attrs){};
std::string getValue(const char* key);
private:
const XML_Char** attrs_{nullptr};
Expand Down
2 changes: 1 addition & 1 deletion src/xsdgen/xsdgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void generate(const xsd::schema_t::simpleType_t& s) {
} else
cout << ", " << endl << v.value;
// TODO:uppercase
};
}
cout << endl << "};" << endl << endl;

cout << "std::string to_string(" << s.name <<" e) {" << endl
Expand Down
2 changes: 1 addition & 1 deletion test/test_asam_generation_problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct empty_delegate : public xmlpp::abstract_delegate {
const XML_Char ** /* atts*/ ) override
{
// printf("|%s|\n",fullname);
elementnames.push_back(fullname);
elementnames.emplace_back(fullname);
}

void onEndElement( const XML_Char * /* fullname */ ) override
Expand Down

0 comments on commit 983a57a

Please sign in to comment.