Skip to content

Commit

Permalink
[#2] Generated (unordered) INI file from INI data
Browse files Browse the repository at this point in the history
Signed-off-by: Clovis Durand <cd.clovel19@gmail.com>
  • Loading branch information
Clovel committed Mar 3, 2020
1 parent 45b8b41 commit 2c3af7f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
35 changes: 33 additions & 2 deletions generator/src/INI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,37 @@ int INI::getDouble(const std::string &pKey, double &pValue, const std::string &p
}

int INI::generateINI(const std::string &pDest) const {
std::ostringstream mOutputFile(pDest);


/* Are we overwriting our original INI file ? */
if(mFileName == pDest) {
/* Overwrite detectedn not supported for now */
std::cerr << "[ERROR] <INI::generateINI> Overwrite detectedn not supported for now." << std::endl;
return -1;
}

std::fstream lOutputFileStream(pDest, std::ios::out);
if(!lOutputFileStream.is_open()) {
std::cerr << "[ERROR] <INI::generateINI> Failed to open file " << pDest << " !" << std::endl;
return -1;
}

/* For each section */
for(const auto &lSectionPair : mSections) {
/* Write the section name */
lOutputFileStream << "[" << lSectionPair.first << "]" << std::endl;

/* For each key in the section */
for(const auto &lKeyPair : mSections.at(lSectionPair.first)) {
/* Write the key, the equal sign and the value */
lOutputFileStream << lKeyPair.first << "=" << lKeyPair.second << std::endl;
}

/* Add an empty line between sections.
* This will also add an empty line at EOF.
*/
lOutputFileStream << std::endl;
}

std::cout << "[INFO ] <INI::generateINI> Successfully generated INI file " << pDest << std::endl;
return 0;
}
14 changes: 13 additions & 1 deletion generator/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/* C++ system */
#include <iostream>
#include <string>

/* C system */
#include <cstring>
Expand Down Expand Up @@ -37,15 +38,26 @@ int main(const int argc, const char * const * const argv) {
return EXIT_FAILURE;
}

const std::string lEDSFile = std::string(argv[1U]);

/* Create an EDS instance */
EDS *lEDS = nullptr;
try {
std::cout << "[DEBUG] Opening EDS file " << argv[1U] << std::endl;
EDS lEDS((std::string(argv[1U])));
lEDS = new EDS(lEDSFile);
std::cerr << "[ERROR] Successfully parsed EDS file " << argv[1U] << " !" << std::endl;
} catch (const std::exception &e) {
std::cerr << "[ERROR] Failed to parse EDS file " << argv[1U] << " !" << std::endl;
return EXIT_FAILURE;
}

/* Generating the same INI file */
std::string lCopyEDSFile = lEDSFile.substr(0U, lEDSFile.find_last_of('.')) + ".copy.ini";
std::cout << "[DEBUG] lCopyEDSFile = " << lCopyEDSFile << std::endl;
if(0 > lEDS->generateINI(lCopyEDSFile)) {
std::cerr << "[ERROR] Failed to generate copy if ini file ! " << std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

0 comments on commit 2c3af7f

Please sign in to comment.