diff --git a/src/ini_reader.h b/src/ini_reader.h index 1e735f0..e15c72f 100644 --- a/src/ini_reader.h +++ b/src/ini_reader.h @@ -9,6 +9,15 @@ #include "misc.h" +#define INIREADER_EXCEPTION_NONE 0 +#define INIREADER_EXCEPTION_EMPTY -1 +#define INIREADER_EXCEPTION_DUPLICATE -2 +#define INIREADER_EXCEPTION_OUTOFBOUND -3 +#define INIREADER_EXCEPTION_NOTEXIST -4 +#define INIREADER_EXCEPTION_NOTPARSED -5 + +#define __SAVE_ERROR_AND_RETURN(x) {last_error = x; return last_error;} + typedef std::map> ini_data_struct; typedef std::multimap string_multimap; typedef std::vector string_array; @@ -34,6 +43,10 @@ private: std::string isolated_items_section; + //error flags + int last_error = INIREADER_EXCEPTION_NONE; + unsigned int last_error_index = 0; + bool chkIgnore(std::string section) { bool excluded = false, included = false; @@ -86,6 +99,33 @@ public: ~INIReader() = default; + std::string GetErrorString(int error) + { + switch(error) + { + case INIREADER_EXCEPTION_EMPTY: + return "Empty document"; + case INIREADER_EXCEPTION_DUPLICATE: + return "Duplicate section"; + case INIREADER_EXCEPTION_NOTEXIST: + return "Target does not exist"; + case INIREADER_EXCEPTION_OUTOFBOUND: + return "Item exists outside of any section"; + case INIREADER_EXCEPTION_NOTPARSED: + return "Parse error"; + default: + return "Undefined"; + } + } + + std::string GetLastError() + { + if(parsed) + return GetErrorString(last_error); + else + return "line " + std::__cxx11::to_string(last_error_index) + ": " + GetErrorString(last_error); + } + /** * @brief Exclude a section with the given name. */ @@ -137,7 +177,7 @@ public: int Parse(std::string content) //parse content into mapped data { if(!content.size()) //empty content - return -1; + __SAVE_ERROR_AND_RETURN(INIREADER_EXCEPTION_EMPTY); //remove UTF-8 BOM if(content.compare(0, 3, "\xEF\xBB\xBF") == 0) @@ -157,8 +197,10 @@ public: if(store_isolated_line) curSection = isolated_items_section; //items before any section define will be store in this section strStrm<>(section, mapTemp)); } - return 0; + __SAVE_ERROR_AND_RETURN(INIREADER_EXCEPTION_NONE); } /** @@ -565,7 +607,7 @@ public: int Set(std::string itemName, std::string itemVal) { if(!current_section.size()) - return -1; + __SAVE_ERROR_AND_RETURN(INIREADER_EXCEPTION_NOTEXIST); return Set(current_section, itemName, itemVal); } @@ -640,7 +682,7 @@ public: int RenameSection(std::string oldName, std::string newName) { if(!SectionExist(oldName) || SectionExist(newName)) - return -1; + __SAVE_ERROR_AND_RETURN(INIREADER_EXCEPTION_DUPLICATE); /* auto nodeHandler = ini_content.extract(oldName); nodeHandler.key() = newName; @@ -648,7 +690,7 @@ public: */ ini_content[newName] = std::move(ini_content[oldName]); ini_content.erase(oldName); - return 0; + __SAVE_ERROR_AND_RETURN(INIREADER_EXCEPTION_NONE); } /** @@ -658,7 +700,7 @@ public: { int retVal; if(!SectionExist(section)) - return -1; + __SAVE_ERROR_AND_RETURN(INIREADER_EXCEPTION_NOTEXIST); retVal = ini_content.at(section).erase(itemName); if(retVal && cached_section == section) @@ -690,11 +732,11 @@ public: { cached_section_content = mapTemp; } - return 0; + __SAVE_ERROR_AND_RETURN(INIREADER_EXCEPTION_NONE); } else { - return -1; + __SAVE_ERROR_AND_RETURN(INIREADER_EXCEPTION_NOTEXIST); } } diff --git a/src/main.cpp b/src/main.cpp index a53f901..5705aa4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -134,15 +134,21 @@ void readConf() { guarded_mutex guard(on_configuring); std::cerr<<"Reading preference settings..."<