Skip to content

Commit cba3a85

Browse files
authored
Allow optionally overwriting to an ini file (#24)
* Allow optionally overwriting to an ini file
1 parent 1c2bd14 commit cba3a85

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

ini/ini.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ inline int ini_parse(const char* filename, ini_handler handler, void* user) {
200200
class INIReader {
201201
public:
202202
// Empty Constructor
203-
INIReader(){};
203+
INIReader() {};
204204

205205
// Construct INIReader and parse given filename. See ini.h for more info
206206
// about the parsing.
@@ -593,18 +593,25 @@ inline int INIReader::ValueHandler(void* user, const char* section,
593593

594594
class INIWriter {
595595
public:
596-
INIWriter(){};
596+
INIWriter() {};
597597
/**
598598
* @brief Write the contents of an INI file to a new file
599599
* @param filepath The path of the output file
600600
* @param reader The INIReader object to write to the file
601+
* @param overwrite Whether to just overwrite an existing file
601602
* @throws std::runtime_error if the output file already exists or cannot be
602603
* opened
603604
*/
604605
inline static void write(const std::string& filepath,
605-
const INIReader& reader) {
606-
if (struct stat buf; stat(filepath.c_str(), &buf) == 0) {
607-
throw std::runtime_error("file: " + filepath + " already exist.");
606+
const INIReader& reader,
607+
const bool& overwrite = false) {
608+
if (overwrite) {
609+
std::remove(filepath.c_str());
610+
} else {
611+
if (struct stat buf; stat(filepath.c_str(), &buf) == 0) {
612+
throw std::runtime_error("file: " + filepath +
613+
" already exists.");
614+
}
608615
}
609616
std::ofstream out;
610617
out.open(filepath);

0 commit comments

Comments
 (0)