summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Minar <miminar@redhat.com>2014-05-05 15:38:19 +0200
committerMichal Minar <miminar@redhat.com>2014-06-17 08:38:56 +0200
commitd53ca0303a56a1811f73806f5f2ea38ae676049f (patch)
tree3adbeeb840a2fe65a2da45f37866b5fd78e8174c
parent19c55362408c5fa2cf857f67c18fa2a54014d82d (diff)
downloadopenlmi-providers-d53ca0303a56a1811f73806f5f2ea38ae676049f.tar.gz
openlmi-providers-d53ca0303a56a1811f73806f5f2ea38ae676049f.tar.xz
openlmi-providers-d53ca0303a56a1811f73806f5f2ea38ae676049f.zip
added openlmi-utils to openlmicommon library
-rw-r--r--src/libs/libopenlmi/CMakeLists.txt3
-rw-r--r--src/libs/libopenlmi/openlmi-utils.c68
-rw-r--r--src/libs/libopenlmi/openlmi-utils.h46
3 files changed, 116 insertions, 1 deletions
diff --git a/src/libs/libopenlmi/CMakeLists.txt b/src/libs/libopenlmi/CMakeLists.txt
index 867260e..de2f607 100644
--- a/src/libs/libopenlmi/CMakeLists.txt
+++ b/src/libs/libopenlmi/CMakeLists.txt
@@ -9,6 +9,7 @@ install(FILES openlmi.conf DESTINATION ${SYSCONF_INSTALL_DIR}/openlmi)
add_library(openlmicommon SHARED
openlmi.c
+ openlmi-utils.c
)
target_link_libraries(openlmicommon ${GLIB_LIBRARIES} dl)
@@ -17,4 +18,4 @@ set_target_properties(openlmicommon PROPERTIES VERSION ${OPENLMICOMMON_VERSION})
set_target_properties(openlmicommon PROPERTIES SOVERSION ${OPENLMICOMMON_VERSION_MAJOR})
install(TARGETS openlmicommon DESTINATION lib${LIB_SUFFIX})
-install(FILES openlmi.h DESTINATION include/openlmi)
+install(FILES openlmi.h openlmi-utils.h DESTINATION include/openlmi)
diff --git a/src/libs/libopenlmi/openlmi-utils.c b/src/libs/libopenlmi/openlmi-utils.c
new file mode 100644
index 0000000..0e5b687
--- /dev/null
+++ b/src/libs/libopenlmi/openlmi-utils.c
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2014 Red Hat, Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Michal Minar <miminar@redhat.com>
+ */
+
+#include <ctype.h>
+#include <string.h>
+#include "openlmi.h"
+#include "openlmi-utils.h"
+
+struct _TreeKeyValueContainer {
+ gpointer key;
+ gpointer value;
+};
+
+static gboolean _do_tree_peek_first_item(gpointer key,
+ gpointer value,
+ gpointer data)
+{
+ struct _TreeKeyValueContainer *cnt = data;
+
+ cnt->key = key;
+ cnt->value = value;
+ return true;
+}
+
+guint lmi_str_lcase_hash_func(gconstpointer key) {
+ const gchar *strkey = key;
+ int len = strlen(strkey);
+ gchar tmp[BUFLEN];
+
+ for (int i=0; i < len; ++i) {
+ tmp[i] = tolower(strkey[i]);
+ }
+ tmp[len] = '\0';
+ return g_str_hash(tmp);
+}
+
+gboolean lmi_str_icase_equal(gconstpointer a, gconstpointer b)
+{
+ return !g_ascii_strcasecmp((gchar const *) a, (gchar const *) b);
+}
+
+gpointer lmi_tree_peek_first(GTree *tree, gpointer *value)
+{
+ struct _TreeKeyValueContainer data = {NULL, NULL};
+
+ g_tree_foreach(tree, _do_tree_peek_first_item, &data);
+ if (value)
+ *value = data.value;
+ return data.key;
+}
+
diff --git a/src/libs/libopenlmi/openlmi-utils.h b/src/libs/libopenlmi/openlmi-utils.h
new file mode 100644
index 0000000..9af64e5
--- /dev/null
+++ b/src/libs/libopenlmi/openlmi-utils.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2014 Red Hat, Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Michal Minar <miminar@redhat.com>
+ */
+
+#ifndef OPENLMI_UTILS_H
+#define OPENLMI_UTILS_H
+
+#include <glib.h>
+
+/**
+ * Hash function used in hash table treating its keys in case-insensitive way.
+ */
+guint lmi_str_lcase_hash_func(gconstpointer key);
+
+/**
+ * Comparison function for use within hash table treating its keys in
+ * case-insensitive way.
+ */
+gboolean lmi_str_icase_equal(gconstpointer a, gconstpointer b);
+
+/**
+ * Get the first key/value pair from balanced binary tree.
+ *
+ * @param value If supplied, it will be filled with a pointer
+ * to associated value.
+ * @return Pointer to key.
+ */
+gpointer lmi_tree_peek_first(GTree* tree, gpointer *value);
+
+#endif /* end of include guard: OPENLMI_UTILS_H */