diff --git a/CMakeLists.txt b/CMakeLists.txt index 8be0593..3ff5da5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,8 +9,9 @@ include_directories(tinyxml2) add_library(ClassBuilder Src/ClassBuilder.cpp) add_library(Builders Src/Builders.cpp) +add_library(OutputFile Src/OutputFile.cpp) add_library(XmlParser Src/XmlParser.cpp) add_library(tinyxml2 tinyxml2/tinyxml2.cpp) add_executable(svd2cpp Src/main.cpp) -target_link_libraries(svd2cpp ClassBuilder Builders XmlParser tinyxml2) \ No newline at end of file +target_link_libraries(svd2cpp ClassBuilder Builders OutputFile XmlParser tinyxml2) \ No newline at end of file diff --git a/Src/Builders.cpp b/Src/Builders.cpp index 659d86d..5203659 100644 --- a/Src/Builders.cpp +++ b/Src/Builders.cpp @@ -22,7 +22,7 @@ void PeripheralBuilder::build(std::stringstream& ss) const{ for(auto& registe : peripheral.registers){ RegisterBuilder(registe, peripheral.baseAddress).build(ss); } - ss << "}\n"; + ss << "}\n\n"; } void RegisterBuilder::build(std::stringstream& ss) const{ diff --git a/Src/ClassBuilder.cpp b/Src/ClassBuilder.cpp index 6d9ed65..d7f12b8 100644 --- a/Src/ClassBuilder.cpp +++ b/Src/ClassBuilder.cpp @@ -22,5 +22,8 @@ void ClassBuilder::build(){ for(auto& builder : builders){ builder->build(outputStream); } - std::cout << outputStream.str(); +} + +const std::stringstream& ClassBuilder::getStream() const{ + return outputStream; } diff --git a/Src/ClassBuilder.hpp b/Src/ClassBuilder.hpp index b443842..a644d00 100644 --- a/Src/ClassBuilder.hpp +++ b/Src/ClassBuilder.hpp @@ -17,6 +17,7 @@ struct ClassBuilder const std::vector& peripherals_); void setupBuilders(); void build(); + const std::stringstream& getStream() const; private: const cxxopts::ParseResult& results; diff --git a/Src/XmlParser.cpp b/Src/XmlParser.cpp index 47e9c46..ae437d3 100644 --- a/Src/XmlParser.cpp +++ b/Src/XmlParser.cpp @@ -41,12 +41,12 @@ void XmlParser::parseXml(){ void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, std::string &field) const{ tinyxml2::XMLElement* deviceEntry = deviceRoot->FirstChildElement(name); - field = deviceEntry != nullptr ? std::string(deviceEntry->GetText()) : noValue; + field = deviceEntry ? std::string(deviceEntry->GetText()) : noValue; } void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, unsigned int &field) const{ tinyxml2::XMLElement* deviceEntry = deviceRoot->FirstChildElement(name); - field = deviceEntry != nullptr ? std::stol(deviceEntry->GetText(), 0, 16) : 0; + field = deviceEntry ? (std::stol(deviceEntry->GetText()) ?: std::stol(deviceEntry->GetText(), 0, 16)) : 0; } void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, EAccess &field) const{ tinyxml2::XMLElement* deviceEntry = deviceRoot->FirstChildElement(name); diff --git a/Src/main.cpp b/Src/main.cpp index 76cdd0b..f10e56e 100644 --- a/Src/main.cpp +++ b/Src/main.cpp @@ -2,18 +2,24 @@ #include #include #include - +#include int main(int argc, char** argv){ // Create and configure options for the program cxxopts::Options options("svd2cpp", "Parser from svd files to C++ header"); options.add_options() - ("i, input", "File to be parsed", cxxopts::value()) - ("o, output", "OutputFile", cxxopts::value()); + ("i, input", "File with .svd extention to be parsed", cxxopts::value()) + ("o, output", "Output file", cxxopts::value()) + ("h, help", "Print help"); + std::string inputFile, outputFile; auto result = options.parse(argc, argv); try { + if(result.count("help")){ + std::cout << options.help() << std::endl; + return 0; + } if(result.count("input") != 1){ std::cout << "Missing input file!" << std::endl; return 1; @@ -40,4 +46,7 @@ int main(int argc, char** argv){ ClassBuilder classBuilder(result, xmlParser.getDeviceInfo(), xmlParser.getPeripherals()); classBuilder.setupBuilders(); classBuilder.build(); + + OutputFile oFile(outputFile); + std::cout << (oFile.save(classBuilder.getStream()) ? "Successfuly created " : "Failed to create ") << outputFile << std::endl; } \ No newline at end of file