From 039de051ef7fc237b22d94ac5e4e3b4a90d52842 Mon Sep 17 00:00:00 2001 From: Elliot Lee Date: Tue, 20 Oct 1998 21:41:55 +0000 Subject: [PATCH] Move string join/split/free routines from libgnome/gnome-string, rename, * glib.h, gstrfuncs.c: Move string join/split/free routines from libgnome/gnome-string, rename, and add g_str_chug. --- ChangeLog | 5 ++ ChangeLog.pre-2-0 | 5 ++ ChangeLog.pre-2-10 | 5 ++ ChangeLog.pre-2-12 | 5 ++ ChangeLog.pre-2-2 | 5 ++ ChangeLog.pre-2-4 | 5 ++ ChangeLog.pre-2-6 | 5 ++ ChangeLog.pre-2-8 | 5 ++ glib.h | 21 +++++++++ glib/glib.h | 21 +++++++++ glib/gstrfuncs.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gstrfuncs.c | 131 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 344 insertions(+) diff --git a/ChangeLog b/ChangeLog index f751e60..2223719 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +1998-10-20 Elliot Lee + + * glib.h, gstrfuncs.c: Move string join/split/free routines from + libgnome/gnome-string, rename, and add g_str_chug. + 1998-10-20 Sebastian Wilhelmi * configure.in: made the check for needed _ in module's func-names diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index f751e60..2223719 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,8 @@ +1998-10-20 Elliot Lee + + * glib.h, gstrfuncs.c: Move string join/split/free routines from + libgnome/gnome-string, rename, and add g_str_chug. + 1998-10-20 Sebastian Wilhelmi * configure.in: made the check for needed _ in module's func-names diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index f751e60..2223719 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,8 @@ +1998-10-20 Elliot Lee + + * glib.h, gstrfuncs.c: Move string join/split/free routines from + libgnome/gnome-string, rename, and add g_str_chug. + 1998-10-20 Sebastian Wilhelmi * configure.in: made the check for needed _ in module's func-names diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index f751e60..2223719 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,8 @@ +1998-10-20 Elliot Lee + + * glib.h, gstrfuncs.c: Move string join/split/free routines from + libgnome/gnome-string, rename, and add g_str_chug. + 1998-10-20 Sebastian Wilhelmi * configure.in: made the check for needed _ in module's func-names diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index f751e60..2223719 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,8 @@ +1998-10-20 Elliot Lee + + * glib.h, gstrfuncs.c: Move string join/split/free routines from + libgnome/gnome-string, rename, and add g_str_chug. + 1998-10-20 Sebastian Wilhelmi * configure.in: made the check for needed _ in module's func-names diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index f751e60..2223719 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,8 @@ +1998-10-20 Elliot Lee + + * glib.h, gstrfuncs.c: Move string join/split/free routines from + libgnome/gnome-string, rename, and add g_str_chug. + 1998-10-20 Sebastian Wilhelmi * configure.in: made the check for needed _ in module's func-names diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index f751e60..2223719 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,8 @@ +1998-10-20 Elliot Lee + + * glib.h, gstrfuncs.c: Move string join/split/free routines from + libgnome/gnome-string, rename, and add g_str_chug. + 1998-10-20 Sebastian Wilhelmi * configure.in: made the check for needed _ in module's func-names diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index f751e60..2223719 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,8 @@ +1998-10-20 Elliot Lee + + * glib.h, gstrfuncs.c: Move string join/split/free routines from + libgnome/gnome-string, rename, and add g_str_chug. + 1998-10-20 Sebastian Wilhelmi * configure.in: made the check for needed _ in module's func-names diff --git a/glib.h b/glib.h index 3f668f7..f0563aa 100644 --- a/glib.h +++ b/glib.h @@ -1351,6 +1351,7 @@ gchar* g_strnfill (guint length, gchar fill_char); gchar* g_strconcat (const gchar *string1, ...); /* NULL terminated */ +#define g_str_join g_strconcat gdouble g_strtod (const gchar *nptr, gchar **endptr); gchar* g_strerror (gint errnum); @@ -1363,6 +1364,26 @@ void g_strreverse (gchar *string); gpointer g_memdup (gconstpointer mem, guint byte_size); +/* removes leading spaces */ +gchar * g_str_chug (gchar *astring, + gboolean in_place); +/* removes trailing spaces */ +gchar * g_str_chomp (gchar *astring, + gboolean in_place); +/* removes leading & trailing spaces */ +#define g_str_strip(astring, in_place) \ + g_str_chomp(g_str_chug(astring, in_place), FALSE) + +/* these routines that work with string arrays in which the last + element is NULL */ +gchar ** g_str_split (const gchar *string, + const gchar *delim, + gint max_tokens); +gchar* g_strconcatv (const gchar **strarray); +#define g_str_joinv g_str_concatv +void g_str_array_free (gchar **strarray); + + /* calculate a string size, guarranteed to fit format + args. */ guint g_printf_string_upper_bound (const gchar* format, diff --git a/glib/glib.h b/glib/glib.h index 3f668f7..f0563aa 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -1351,6 +1351,7 @@ gchar* g_strnfill (guint length, gchar fill_char); gchar* g_strconcat (const gchar *string1, ...); /* NULL terminated */ +#define g_str_join g_strconcat gdouble g_strtod (const gchar *nptr, gchar **endptr); gchar* g_strerror (gint errnum); @@ -1363,6 +1364,26 @@ void g_strreverse (gchar *string); gpointer g_memdup (gconstpointer mem, guint byte_size); +/* removes leading spaces */ +gchar * g_str_chug (gchar *astring, + gboolean in_place); +/* removes trailing spaces */ +gchar * g_str_chomp (gchar *astring, + gboolean in_place); +/* removes leading & trailing spaces */ +#define g_str_strip(astring, in_place) \ + g_str_chomp(g_str_chug(astring, in_place), FALSE) + +/* these routines that work with string arrays in which the last + element is NULL */ +gchar ** g_str_split (const gchar *string, + const gchar *delim, + gint max_tokens); +gchar* g_strconcatv (const gchar **strarray); +#define g_str_joinv g_str_concatv +void g_str_array_free (gchar **strarray); + + /* calculate a string size, guarranteed to fit format + args. */ guint g_printf_string_upper_bound (const gchar* format, diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 721509b..60fd28b 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -985,3 +985,134 @@ g_strdelimit (gchar *string, *c = new_delim; } } + +/* blame Elliot for these next five routines */ +char ** +g_str_split(const gchar *string, const gchar *delim, gint max_tokens) +{ + /* this could more easily be implemented using a GPtrArray */ + gchar **retval = NULL; + GList *items = NULL, *anode = NULL; + gint numitems = 0, dlen, i; + gchar *src, *cur, *nxt; + + g_return_val_if_fail(string != NULL, NULL); + g_return_val_if_fail(delim != NULL, NULL); + + if(max_tokens < 0) + max_tokens = INT_MAX; + + dlen = strlen(delim); + nxt = strstr(string, delim); + if(!nxt) { + retval = g_malloc(sizeof(gchar *) * 2); + retval[0] = g_strdup(string); + retval[1] = NULL; + return retval; + } + src = cur = g_strdup(string); + nxt = strstr(src, delim); + + while(nxt && numitems < (max_tokens - 1)) { + *nxt = '\0'; + items = g_list_append(items, g_strdup(cur)); + cur = nxt + dlen; + nxt = strstr(cur, delim); + numitems++; + } + /* We have to take the rest of the string and put it as last token */ + if(*cur) { + items = g_list_append(items, g_strdup(cur)); + numitems++; + } + g_free(src); + + retval = g_malloc(sizeof(gchar *) * (numitems + 1)); + for(anode = items, i = 0; anode; anode = anode->next, i++) + retval[i] = anode->data; + retval[i] = NULL; + g_list_free(items); + + return retval; +} + +gchar * +g_str_chug(gchar *astring, gboolean in_place) +{ + int i; + gchar *retval, *start; + + g_return_val_if_fail(astring != NULL, NULL); + + for(start = retval; *start && isspace(*start); start++) + /* */; + + if(in_place) { + retval = astring; + g_memmove(retval, start, strlen(start) + 1); + } else + retval = g_strdup(start); + + return retval; +} + +gchar * +g_str_chomp(gchar *astring, gboolean in_place) +{ + int i; + gchar *retval, *end; + + g_return_val_if_fail(astring != NULL, NULL); + + if(in_place) + retval = astring; + else + retval = g_strdup(astring); + + i = strlen (retval); + if (!i) + return retval; + + end = retval + i - 1; + for (; end >= retval && isspace (*end); end--) + *end = '\0'; + + return retval; +} + +void +g_str_array_free(gchar **strarray) +{ + int i; + + if(strarray == NULL) return; /* Don't use g_return_if_fail, + because this is legal */ + + for(i = 0; strarray[i]; i++) + g_free(strarray[i]); + + g_free(strarray); +} + +gchar* +g_strconcatv (const gchar **strarray) +{ + guint l; + va_list args; + gchar *s; + gchar *concat; + int i; + + g_return_val_if_fail (strarray != NULL, NULL); + + for(i = 0, l = 1; strarray[i]; i++) + l += strlen(strarray[i]); + + concat = g_new (gchar, l); + *concat = '\0'; + + for(i = 0; strarray[i]; i++) + strcat (concat, strarray[i]); + + return concat; +} diff --git a/gstrfuncs.c b/gstrfuncs.c index 721509b..60fd28b 100644 --- a/gstrfuncs.c +++ b/gstrfuncs.c @@ -985,3 +985,134 @@ g_strdelimit (gchar *string, *c = new_delim; } } + +/* blame Elliot for these next five routines */ +char ** +g_str_split(const gchar *string, const gchar *delim, gint max_tokens) +{ + /* this could more easily be implemented using a GPtrArray */ + gchar **retval = NULL; + GList *items = NULL, *anode = NULL; + gint numitems = 0, dlen, i; + gchar *src, *cur, *nxt; + + g_return_val_if_fail(string != NULL, NULL); + g_return_val_if_fail(delim != NULL, NULL); + + if(max_tokens < 0) + max_tokens = INT_MAX; + + dlen = strlen(delim); + nxt = strstr(string, delim); + if(!nxt) { + retval = g_malloc(sizeof(gchar *) * 2); + retval[0] = g_strdup(string); + retval[1] = NULL; + return retval; + } + src = cur = g_strdup(string); + nxt = strstr(src, delim); + + while(nxt && numitems < (max_tokens - 1)) { + *nxt = '\0'; + items = g_list_append(items, g_strdup(cur)); + cur = nxt + dlen; + nxt = strstr(cur, delim); + numitems++; + } + /* We have to take the rest of the string and put it as last token */ + if(*cur) { + items = g_list_append(items, g_strdup(cur)); + numitems++; + } + g_free(src); + + retval = g_malloc(sizeof(gchar *) * (numitems + 1)); + for(anode = items, i = 0; anode; anode = anode->next, i++) + retval[i] = anode->data; + retval[i] = NULL; + g_list_free(items); + + return retval; +} + +gchar * +g_str_chug(gchar *astring, gboolean in_place) +{ + int i; + gchar *retval, *start; + + g_return_val_if_fail(astring != NULL, NULL); + + for(start = retval; *start && isspace(*start); start++) + /* */; + + if(in_place) { + retval = astring; + g_memmove(retval, start, strlen(start) + 1); + } else + retval = g_strdup(start); + + return retval; +} + +gchar * +g_str_chomp(gchar *astring, gboolean in_place) +{ + int i; + gchar *retval, *end; + + g_return_val_if_fail(astring != NULL, NULL); + + if(in_place) + retval = astring; + else + retval = g_strdup(astring); + + i = strlen (retval); + if (!i) + return retval; + + end = retval + i - 1; + for (; end >= retval && isspace (*end); end--) + *end = '\0'; + + return retval; +} + +void +g_str_array_free(gchar **strarray) +{ + int i; + + if(strarray == NULL) return; /* Don't use g_return_if_fail, + because this is legal */ + + for(i = 0; strarray[i]; i++) + g_free(strarray[i]); + + g_free(strarray); +} + +gchar* +g_strconcatv (const gchar **strarray) +{ + guint l; + va_list args; + gchar *s; + gchar *concat; + int i; + + g_return_val_if_fail (strarray != NULL, NULL); + + for(i = 0, l = 1; strarray[i]; i++) + l += strlen(strarray[i]); + + concat = g_new (gchar, l); + *concat = '\0'; + + for(i = 0; strarray[i]; i++) + strcat (concat, strarray[i]); + + return concat; +} -- 2.7.4