diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Utils/Plugin.cpp | 69 | ||||
-rw-r--r-- | lib/Utils/Plugin.h | 16 | ||||
-rw-r--r-- | lib/Utils/xfuncs.cpp | 2 |
3 files changed, 85 insertions, 2 deletions
diff --git a/lib/Utils/Plugin.cpp b/lib/Utils/Plugin.cpp index 2865027f..40cd2a02 100644 --- a/lib/Utils/Plugin.cpp +++ b/lib/Utils/Plugin.cpp @@ -17,6 +17,7 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "Plugin.h" +#include "abrtlib.h" CPlugin::CPlugin() {} @@ -33,3 +34,71 @@ const map_plugin_settings_t& CPlugin::GetSettings() { return m_pSettings; } + +bool LoadPluginSettings(const char *pPath, map_plugin_settings_t& pSettings, + bool skipKeysWithoutValue /*= true*/) +{ + FILE *fp = fopen(pPath, "r"); + if (!fp) + return false; + + char line[512]; + while (fgets(line, sizeof(line), fp)) + { + strchrnul(line, '\n')[0] = '\0'; + unsigned ii; + bool is_value = false; + bool valid = false; + bool in_quote = false; + std::string key; + std::string value; + for (ii = 0; line[ii] != '\0'; ii++) + { + if (line[ii] == '"') + { + in_quote = !in_quote; + } + if (isspace(line[ii]) && !in_quote) + { + continue; + } + if (line[ii] == '#' && !in_quote && key == "") + { + break; + } + if (line[ii] == '=' && !in_quote) + { + is_value = true; + valid = true; + continue; + } + if (!is_value) + { + key += line[ii]; + } + else + { + value += line[ii]; + } + } + + /* Skip broken or empty lines. */ + if (!valid) + continue; + + /* Skip lines with empty key. */ + if (key.length() == 0) + continue; + + if (skipKeysWithoutValue && value.length() == 0) + continue; + + /* Skip lines with unclosed quotes. */ + if (in_quote) + continue; + + pSettings[key] = value; + } + fclose(fp); + return true; +} diff --git a/lib/Utils/Plugin.h b/lib/Utils/Plugin.h index c699eb36..a2d3a7e9 100644 --- a/lib/Utils/Plugin.h +++ b/lib/Utils/Plugin.h @@ -118,9 +118,23 @@ typedef struct SPluginInfo PLUGINS_MAGIC_NUMBER,\ }; -/* helper finctions */ +/* helper functions */ std::string make_description_bz(const map_crash_data_t& pCrashData); std::string make_description_logger(const map_crash_data_t& pCrashData); std::string make_description_catcut(const map_crash_data_t& pCrashData); +/** + * Loads settings and stores it in second parameter. On success it + * returns true, otherwise returns false. + * + * @param path A path of config file. + * Config file consists of "key=value" lines. + * Lines in format "key=" (without value) are skipped. + * @param settings A readed plugin's settings. + * @return if it success it returns true, otherwise it returns false. + */ +extern bool LoadPluginSettings(const char *pPath, + map_plugin_settings_t& pSettings, + bool skipKeysWithoutValue = true); + #endif diff --git a/lib/Utils/xfuncs.cpp b/lib/Utils/xfuncs.cpp index 16f4cb0e..7301d7f0 100644 --- a/lib/Utils/xfuncs.cpp +++ b/lib/Utils/xfuncs.cpp @@ -252,7 +252,7 @@ void xstat(const char *name, struct stat *stat_buf) perror_msg_and_die("can't stat '%s'", name); } -std::string get_home_dir(int uid) +std::string get_home_dir(uid_t uid) { struct passwd* pw = getpwuid(uid); return pw ? pw->pw_dir : ""; |