summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKarel Klic <kklic@redhat.com>2010-02-12 16:08:30 +0100
committerKarel Klic <kklic@redhat.com>2010-02-12 16:08:30 +0100
commita4ff7719867332304e5b44d7e24e3333dab256b6 (patch)
tree2fb6677121129800442995a903d8a0b754616381 /lib
parente08c1d7c3c56c99cafde076529b17c956ef3aeb0 (diff)
downloadabrt-a4ff7719867332304e5b44d7e24e3333dab256b6.tar.gz
abrt-a4ff7719867332304e5b44d7e24e3333dab256b6.tar.xz
abrt-a4ff7719867332304e5b44d7e24e3333dab256b6.zip
Move LoadPluginSettings to the shared utils library. Add parameter skipKeysWithoutValue.
Diffstat (limited to 'lib')
-rw-r--r--lib/Utils/Plugin.cpp69
-rw-r--r--lib/Utils/Plugin.h16
2 files changed, 84 insertions, 1 deletions
diff --git a/lib/Utils/Plugin.cpp b/lib/Utils/Plugin.cpp
index 2865027..40cd2a0 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 c699eb3..a2d3a7e 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