summaryrefslogtreecommitdiff
path: root/src/core/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/misc.c')
-rw-r--r--src/core/misc.c230
1 files changed, 98 insertions, 132 deletions
diff --git a/src/core/misc.c b/src/core/misc.c
index 5e6087cb..1cfa15b6 100644
--- a/src/core/misc.c
+++ b/src/core/misc.c
@@ -20,8 +20,9 @@
#include "module.h"
#include "misc.h"
+#include "commands.h"
-#ifdef HAVE_REGEX_H
+#ifndef USE_GREGEX
# include <regex.h>
#endif
@@ -150,27 +151,13 @@ int find_substr(const char *list, const char *item)
return FALSE;
}
-int strarray_length(char **array)
-{
- int len;
-
- g_return_val_if_fail(array != NULL, 0);
-
- len = 0;
- while (*array) {
- len++;
- array++;
- }
- return len;
-}
-
int strarray_find(char **array, const char *item)
{
char **tmp;
int index;
- g_return_val_if_fail(array != NULL, 0);
- g_return_val_if_fail(item != NULL, 0);
+ g_return_val_if_fail(array != NULL, -1);
+ g_return_val_if_fail(item != NULL, -1);
index = 0;
for (tmp = array; *tmp != NULL; tmp++, index++) {
@@ -184,7 +171,7 @@ int strarray_find(char **array, const char *item)
GSList *gslist_find_string(GSList *list, const char *key)
{
for (; list != NULL; list = list->next)
- if (strcmp(list->data, key) == 0) return list;
+ if (g_strcmp0(list->data, key) == 0) return list;
return NULL;
}
@@ -211,6 +198,30 @@ void *gslist_foreach_find(GSList *list, FOREACH_FIND_FUNC func, const void *data
return NULL;
}
+void gslist_free_full (GSList *list, GDestroyNotify free_func)
+{
+ GSList *tmp;
+
+ if (list == NULL)
+ return;
+
+ for (tmp = list; tmp != NULL; tmp = tmp->next)
+ free_func(tmp->data);
+
+ g_slist_free(list);
+}
+
+GSList *gslist_remove_string (GSList *list, const char *str)
+{
+ GSList *l;
+
+ l = g_slist_find_custom(list, str, (GCompareFunc) g_strcmp0);
+ if (l != NULL)
+ return g_slist_remove_link(list, l);
+
+ return list;
+}
+
/* `list' contains pointer to structure with a char* to string. */
char *gslistptr_to_string(GSList *list, int offset, const char *delimiter)
{
@@ -255,21 +266,30 @@ void hash_save_key(char *key, void *value, GSList **list)
*list = g_slist_append(*list, key);
}
-/* save all keys in hash table to linked list - you shouldn't remove any
- items while using this list, use g_slist_free() after you're done with it */
-GSList *hashtable_get_keys(GHashTable *hash)
+/* remove all the options from the optlist hash table that are valid for the
+ * command cmd */
+GList *optlist_remove_known(const char *cmd, GHashTable *optlist)
{
- GSList *list;
+ GList *list, *tmp, *next;
+
+ list = g_hash_table_get_keys(optlist);
+ if (cmd != NULL && list != NULL) {
+ for (tmp = list; tmp != NULL; tmp = next) {
+ char *option = tmp->data;
+ next = tmp->next;
+
+ if (command_have_option(cmd, option))
+ list = g_list_remove(list, option);
+ }
+ }
- list = NULL;
- g_hash_table_foreach(hash, (GHFunc) hash_save_key, &list);
return list;
}
GList *glist_find_string(GList *list, const char *key)
{
for (; list != NULL; list = list->next)
- if (strcmp(list->data, key) == 0) return list;
+ if (g_strcmp0(list->data, key) == 0) return list;
return NULL;
}
@@ -365,66 +385,6 @@ char *stristr_full(const char *data, const char *key)
return strstr_full_case(data, key, TRUE);
}
-int regexp_match(const char *str, const char *regexp)
-{
-#ifdef HAVE_REGEX_H
- regex_t preg;
- int ret;
-
- if (regcomp(&preg, regexp, REG_EXTENDED|REG_ICASE|REG_NOSUB) != 0)
- return 0;
-
- ret = regexec(&preg, str, 0, NULL, 0);
- regfree(&preg);
-
- return ret == 0;
-#else
- return FALSE;
-#endif
-}
-
-/* Create the directory and all it's parent directories */
-int mkpath(const char *path, int mode)
-{
- struct stat statbuf;
- const char *p;
- char *dir;
-
- g_return_val_if_fail(path != NULL, -1);
-
- p = g_path_skip_root((char *) path);
- if (p == NULL) {
- /* not a full path, maybe not what we wanted
- but continue anyway.. */
- p = path;
- }
- for (;;) {
- if (*p != G_DIR_SEPARATOR && *p != '\0') {
- p++;
- continue;
- }
-
- dir = g_strndup(path, (int) (p-path));
- if (stat(dir, &statbuf) != 0) {
-#ifndef WIN32
- if (mkdir(dir, mode) == -1)
-#else
- if (_mkdir(dir) == -1)
-#endif
- {
- g_free(dir);
- return -1;
- }
- }
- g_free(dir);
-
- if (*p++ == '\0')
- break;
- }
-
- return 0;
-}
-
/* convert ~/ to $HOME */
char *convert_home(const char *path)
{
@@ -451,22 +411,15 @@ int g_istr_cmp(gconstpointer v, gconstpointer v2)
return g_ascii_strcasecmp((const char *) v, (const char *) v2);
}
-/* a char* hash function from ASU */
-unsigned int g_istr_hash(gconstpointer v)
+guint g_istr_hash(gconstpointer v)
{
- const char *s = (const char *) v;
- unsigned int h = 0, g;
+ const signed char *p;
+ guint32 h = 5381;
- while (*s != '\0') {
- h = (h << 4) + i_toupper(*s);
- if ((g = h & 0xf0000000UL)) {
- h = h ^ (g >> 24);
- h = h ^ g;
- }
- s++;
- }
+ for (p = v; *p != '\0'; p++)
+ h = (h << 5) + h + g_ascii_toupper(*p);
- return h /* % M */;
+ return h;
}
/* Find `mask' from `data', you can use * and ? wildcards. */
@@ -572,15 +525,11 @@ int dec2octal(int decimal)
/* string -> uoff_t */
uoff_t str_to_uofft(const char *str)
{
- uoff_t ret;
-
- ret = 0;
- while (*str != '\0') {
- ret = ret*10 + (*str - '0');
- str++;
- }
-
- return ret;
+#ifdef UOFF_T_LONG_LONG
+ return (uoff_t)strtoull(str, NULL, 10);
+#else
+ return (uoff_t)strtoul(str, NULL, 10);
+#endif
}
/* convert all low-ascii (<32) to ^<A..> combinations */
@@ -791,20 +740,6 @@ char *escape_string(const char *str)
return ret;
}
-int strocpy(char *dest, const char *src, size_t dstsize)
-{
- if (dstsize == 0)
- return -1;
-
- while (*src != '\0' && dstsize > 1) {
- *dest++ = *src++;
- dstsize--;
- }
-
- *dest++ = '\0';
- return *src == '\0' ? 0 : -1;
-}
-
int nearest_power(int num)
{
int n = 1;
@@ -967,20 +902,51 @@ char *ascii_strdown(char *str)
return str;
}
-char **strsplit_len(const char *str, int len)
+char **strsplit_len(const char *str, int len, gboolean onspace)
+{
+ char **ret = g_new(char *, 1);
+ int n;
+ int offset;
+
+ for (n = 0; *str != '\0'; n++, str += offset) {
+ offset = MIN(len, strlen(str));
+ if (onspace && strlen(str) > len) {
+ /*
+ * Try to find a space to split on and leave
+ * the space on the previous line.
+ */
+ int i;
+ for (i = len - 1; i > 0; i--) {
+ if (str[i] == ' ') {
+ offset = i;
+ break;
+ }
+ }
+ }
+ ret[n] = g_strndup(str, offset);
+ ret = g_renew(char *, ret, n + 2);
+ }
+ ret[n] = NULL;
+
+ return ret;
+}
+
+char *binary_to_hex(unsigned char *buffer, size_t size)
{
- char **ret;
- size_t total_len = strlen(str);
- int n = total_len / len;
+ static const char hex[] = "0123456789ABCDEF";
+ char *result = NULL;
int i;
- if (total_len % len)
- n++;
+ if (buffer == NULL || size == 0)
+ return NULL;
+
+ result = g_malloc(3 * size);
- ret = g_new(char *, n + 1);
- for (i = 0; i < n; i++, str += len)
- ret[i] = g_strndup(str, len);
- ret[n] = NULL;
+ for (i = 0; i < size; i++) {
+ result[i * 3 + 0] = hex[(buffer[i] >> 4) & 0xf];
+ result[i * 3 + 1] = hex[(buffer[i] >> 0) & 0xf];
+ result[i * 3 + 2] = i == size - 1 ? '\0' : ':';
+ }
- return ret;
+ return result;
}