2005-11-07 10:56:25 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "gm-string.h"
|
|
|
|
#include "gm-debug.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-15 12:59:11 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-11-07 10:56:25 +01:00
|
|
|
void
|
2005-11-15 12:59:11 +01:00
|
|
|
gm_string_skip_space(gchar const **ptr) {
|
|
|
|
while (**ptr != '\0' && g_unichar_isspace(g_utf8_get_char(*ptr))) {
|
|
|
|
*ptr = g_utf8_next_char(*ptr);
|
|
|
|
}
|
|
|
|
}
|
2005-11-07 10:56:25 +01:00
|
|
|
|
2005-11-15 12:59:11 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2005-11-07 10:56:25 +01:00
|
|
|
|
2005-11-15 12:59:11 +01:00
|
|
|
void
|
|
|
|
gm_string_skip_till(gchar const **ptr, gchar const *find) {
|
|
|
|
gchar const *fptr;
|
2005-11-19 14:09:43 +01:00
|
|
|
gunichar check, c;
|
2005-11-15 12:59:11 +01:00
|
|
|
|
|
|
|
while (**ptr != '\0') {
|
|
|
|
check = g_utf8_get_char(*ptr);
|
2005-11-19 14:09:43 +01:00
|
|
|
fptr = find;
|
2005-11-15 12:59:11 +01:00
|
|
|
|
2005-11-19 14:09:43 +01:00
|
|
|
while (*fptr != '\0') {
|
|
|
|
c = g_utf8_get_char(fptr);
|
|
|
|
|
|
|
|
if (check == c) {
|
2005-11-15 12:59:11 +01:00
|
|
|
return;
|
|
|
|
}
|
2005-11-19 14:09:43 +01:00
|
|
|
|
|
|
|
fptr = g_utf8_next_char(fptr);
|
2005-11-15 12:59:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*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);
|
|
|
|
}
|
2005-11-07 10:56:25 +01:00
|
|
|
}
|
2005-12-23 17:08:39 +01:00
|
|
|
|
|
|
|
GList *
|
|
|
|
gm_string_split_add_line(gchar *line, GList *lines, GList **lastptr) {
|
|
|
|
if (!lines) {
|
|
|
|
lines = g_list_append(NULL, line);
|
|
|
|
*lastptr = lines;
|
|
|
|
} else {
|
2006-02-06 19:41:30 +01:00
|
|
|
*lastptr = g_list_append(*lastptr, line);
|
2005-12-23 17:08:39 +01:00
|
|
|
*lastptr = (*lastptr)->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
GList *
|
|
|
|
gm_string_split(gchar const *text) {
|
|
|
|
gchar *fptr, *line;
|
|
|
|
GList *lastptr;
|
|
|
|
GList *lines = NULL;
|
|
|
|
|
|
|
|
if (!text) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((fptr = g_utf8_strchr(text, -1, '\n'))) {
|
|
|
|
line = g_strndup(text, fptr - text);
|
|
|
|
gm_string_remove_char(line, '\r');
|
|
|
|
|
|
|
|
lines = gm_string_split_add_line(line, lines, &lastptr);
|
|
|
|
text = g_utf8_next_char(fptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*text != '\0') {
|
|
|
|
line = g_strdup(text);
|
|
|
|
gm_string_remove_char(line, '\r');
|
|
|
|
|
|
|
|
lines = gm_string_split_add_line(line, lines, &lastptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|