#include #include #include #include "gm-string.h" #include "gm-debug.h" /* void stringlist_add(stringlist * strl, char *data) { stringlist_item *newStringlistItem = (stringlist_item *) malloc(sizeof(stringlist_item)); newStringlistItem->data = strdup(data); if (strl->firstItem == NULL) { strl->firstItem = newStringlistItem; } else { newStringlistItem->prev = strl->lastItem; if (strl->lastItem != NULL) strl->lastItem->next = newStringlistItem; } strl->lastItem = newStringlistItem; newStringlistItem->next = NULL; strl->count++; } void stringlist_remove(stringlist * strl, stringlist_item * removed) { if (removed == strl->firstItem) { strl->firstItem = removed->next; } else { removed->prev->next = removed->next; } if (removed == strl->lastItem) { strl->lastItem = removed->prev; } else { removed->next->prev = removed->next; } free(removed->data); free(removed); strl->count--; } stringlist * stringlist_create(char *argstr, char *delim) { stringlist *newStringlist = (stringlist *) malloc(sizeof(stringlist)); char *data, *work, *working, *strPos; newStringlist->firstItem = NULL; newStringlist->lastItem = NULL; newStringlist->count = 0; if (delim == NULL) { stringlist_add(newStringlist, argstr); return newStringlist; } working = strdup(argstr); working = mystring_cat(working, delim); work = working; while (strlen(work) != 0) { if ((strPos = strstr(work, delim)) != NULL && strPos > work) { data = NULL; data = mystring_catn(data, work, (strPos - work)); stringlist_add(newStringlist, data); free(data); } work = strPos + strlen(delim); } free(working); return newStringlist; } void stringlist_destroy(stringlist * strl) { stringlist_item *curItem = strl->firstItem; while (curItem != NULL) { stringlist_remove(strl, curItem); curItem = strl->firstItem; } free(strl); } char * stringlist_glue_it(stringlist * strl, char *glue) { char *result = NULL; stringlist_item *curItem = strl->firstItem; while (curItem != NULL) { if (curItem != strl->firstItem) { result = mystring_cat(result, glue); result = mystring_cat(result, curItem->data); } else { result = strdup(curItem->data); } } return result; } */ gboolean gm_string_to_int(const gchar *str, int *result) { *result = 0; if (str == NULL || *str == '\0') { return FALSE; } *result = atoi(str); return TRUE; } gchar * gm_string_catn(gchar *str, gchar *add, guint n) { guint relen; gchar *newstr; if (str == NULL) { relen = 0; } else { relen = strlen(str); } if ((newstr = (gchar *) realloc(str, (relen + n + 1) * sizeof(gchar))) == NULL) { gm_debug_msg(DEBUG_DEFAULT, "mystring_catn: REALLOC FAILED!"); return str; } else { if (relen == 0) { newstr[0] = '\0'; } strncat(newstr, add, n); return newstr; } } gchar * gm_string_cat(gchar *str, gchar *add) { return gm_string_catn(str, add, strlen(add)); } gchar * gm_string_trim(const gchar *str) { gchar *newstr = NULL; const gchar *r, *l; if (str == NULL) { return NULL; } r = str + strlen(str); l = str; while (*l == ' ' || *r == ' ') { if (l == r) { break; } if (*l == ' ') { l++; } if (l == r) { break; } if (*r == ' ') { r--; } } if (l == r) { return g_strdup(""); } else { newstr = g_strndup(l, r - l); return newstr; } } gchar * gm_string_remove_char(gchar *str, gchar rem) { gchar *ptr = str; gboolean changed = FALSE; gunichar c; while (*str != '\0') { c = g_utf8_get_char(str); if (c != (gunichar)rem) { if (changed) { ptr += g_unichar_to_utf8(c, ptr); ++ptr; } else { ptr = g_utf8_next_char(ptr); } } else { changed = TRUE; } str = g_utf8_next_char(str); } *ptr = '\0'; return str; } void gm_string_skip_space(gchar const **ptr) { while (**ptr != '\0' && g_unichar_isspace(g_utf8_get_char(*ptr))) { *ptr = g_utf8_next_char(*ptr); } } void gm_string_skip_nonspace(gchar const **ptr) { while (**ptr != '\0' && !g_unichar_isspace(g_utf8_get_char(*ptr))) { *ptr = g_utf8_next_char(*ptr); } } void gm_string_skip_till(gchar const **ptr, gchar const *find) { gchar const *fptr; gunichar check; while (**ptr != '\0') { check = g_utf8_get_char(*ptr); for (fptr = find; *fptr; ++fptr) { // CHECK: find should also be treated as utf8! if (check == (gunichar)(*fptr)) { return; } } *ptr = g_utf8_next_char(*ptr); } } gchar * gm_string_escape(gchar const *line) { GString *new_line; gchar *ptr; gunichar c; if (*line == '\0') { return g_strdup(""); } if (g_utf8_strchr(line, '"', -1) || g_utf8_strchr(line, '\\', -1)) { // We need at least strlen(line) new_line = g_string_sized_new(strlen(line)); while (*line != '\0') { c = g_utf8_get_char(line); if (c == '"' || c == '\\') { g_string_append_c(new_line, '\\'); } g_string_append_unichar(new_line, c); line = g_utf8_next_char(line); } ptr = new_line->str; g_string_free(new_line, FALSE); return ptr; } else { return g_strdup(line); } }