Added support for saving to file, fixed bug with parsing addresses

This commit is contained in:
Łukasz Czyż 2019-12-02 10:47:01 +01:00
parent c05c8fa341
commit 19cd372ec0
6 changed files with 22 additions and 8 deletions

View file

@ -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)
target_link_libraries(svd2cpp ClassBuilder Builders OutputFile XmlParser tinyxml2)

View file

@ -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{

View file

@ -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;
}

View file

@ -17,6 +17,7 @@ struct ClassBuilder
const std::vector<Peripheral>& peripherals_);
void setupBuilders();
void build();
const std::stringstream& getStream() const;
private:
const cxxopts::ParseResult& results;

View file

@ -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);

View file

@ -2,18 +2,24 @@
#include <cxxopts.hpp>
#include <XmlParser.hpp>
#include <ClassBuilder.hpp>
#include <OutputFile.hpp>
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<std::string>())
("o, output", "OutputFile", cxxopts::value<std::string>());
("i, input", "File with .svd extention to be parsed", cxxopts::value<std::string>())
("o, output", "Output file", cxxopts::value<std::string>())
("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;
}