2005-10-09 10:53:36 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "gm-string.h"
|
2005-11-06 17:24:28 +01:00
|
|
|
#include "gm-debug.h"
|
2005-10-09 10:53:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
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) {
|
2005-11-06 17:38:51 +01:00
|
|
|
gm_debug_msg(DEBUG_DEFAULT, "mystring_catn: REALLOC FAILED!");
|
2005-10-09 10:53:36 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gm_string_remove_char(char *str, char rem) {
|
|
|
|
int i, j = 0;
|
|
|
|
|
|
|
|
for (i = 0; str[i] != '\0'; i++) {
|
|
|
|
if (str[i] != rem) {
|
|
|
|
str[j] = str[i];
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str[j] = '\0';
|
|
|
|
}
|