Added gm_string_split
This commit is contained in:
parent
dec19edcdb
commit
eed9b8ad0e
|
@ -1,11 +1,9 @@
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "gm-string.h"
|
||||
#include "gm-debug.h"
|
||||
|
||||
|
||||
gboolean
|
||||
gm_string_to_int(const gchar *str, int *result) {
|
||||
*result = 0;
|
||||
|
@ -183,3 +181,44 @@ gm_string_escape(gchar const *line) {
|
|||
return g_strdup(line);
|
||||
}
|
||||
}
|
||||
|
||||
GList *
|
||||
gm_string_split_add_line(gchar *line, GList *lines, GList **lastptr) {
|
||||
if (!lines) {
|
||||
lines = g_list_append(NULL, line);
|
||||
*lastptr = lines;
|
||||
} else {
|
||||
g_list_append(*lastptr, line);
|
||||
*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;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <glib.h>
|
||||
|
||||
int gm_string_to_int(const gchar *str, int *result);
|
||||
char *gm_string_catn(char *str, char *add, unsigned int n);
|
||||
|
@ -15,5 +16,6 @@ void gm_string_skip_nonspace(gchar const **ptr);
|
|||
void gm_string_skip_till(gchar const **ptr, gchar const *find);
|
||||
|
||||
gchar *gm_string_escape(gchar const *line);
|
||||
GList *gm_string_split(gchar const *text);
|
||||
|
||||
#endif
|
||||
|
|
Reference in New Issue