Added error checking in XmlParser, added sample file

This commit is contained in:
Łukasz Czyż 2019-11-14 13:38:04 +01:00
parent a00ced6aa1
commit e5605248ea
6 changed files with 24950 additions and 5 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
*.o *.o
*.a

View file

@ -3,7 +3,7 @@ SOURCE = Src/main.cpp tinyxml2/tinyxml2.cpp
INCLUDE = -I tinyxml2 -I cxxopts/include -I Src INCLUDE = -I tinyxml2 -I cxxopts/include -I Src
OUT = svd2cpp OUT = svd2cpp
CC = g++ CC = g++
FLAGS = -g -c -Wall FLAGS = -g -c -Wall -std=c++17
all: $(OBJS) all: $(OBJS)
$(CC) -g $(OBJS) -o $(OUT) $(LFLAGS) $(CC) -g $(OBJS) -o $(OUT) $(LFLAGS)

24923
Sample/STM32F103xx.svd Normal file

File diff suppressed because it is too large Load diff

View file

@ -2,5 +2,15 @@
#include <iostream> #include <iostream>
XmlParser::XmlParser(const std::string& inputFile){ XmlParser::XmlParser(const std::string& inputFile){
std::cout << inputFile; xmlDocument.LoadFile(inputFile.c_str());
}
std::optional<std::string> XmlParser::isError() const{
return xmlDocument.Error() ? std::optional<std::string>(xmlDocument.ErrorStr()) : std::nullopt;
}
void XmlParser::parseXml(){
root = xmlDocument.FirstChildElement();
if(root == nullptr)
return;
std::cout << root->GetText();
} }

View file

@ -3,12 +3,17 @@
#include <tinyxml2.h> #include <tinyxml2.h>
#include <string> #include <string>
#include <optional>
#include <memory>
struct XmlParser{ struct XmlParser{
XmlParser(const std::string& inputFile); XmlParser(const std::string& inputFile);
std::optional<std::string> isError() const;
void parseXml();
private: private:
tinyxml2::XMLDocument xmlDocument;
tinyxml2::XMLElement* root;
}; };
#endif #endif

View file

@ -26,7 +26,13 @@ int main(int argc, char** argv){
} }
catch(cxxopts::OptionException& ex){ catch(cxxopts::OptionException& ex){
std::cout << ex.what() << std::endl; std::cout << ex.what() << std::endl;
return -1; return 2;
} }
//Try to parse the file
XmlParser xmlParser(inputFile); XmlParser xmlParser(inputFile);
if (auto err = xmlParser.isError()){
std::cout << "There was an error while reading " << inputFile << ":" << std::endl << *err << std::endl;
return 3;
}
xmlParser.parseXml();
} }