diff options
author | Elliot Lee <sopwith@src.gnome.org> | 1998-10-20 21:41:55 +0000 |
---|---|---|
committer | Elliot Lee <sopwith@src.gnome.org> | 1998-10-20 21:41:55 +0000 |
commit | 039de051ef7fc237b22d94ac5e4e3b4a90d52842 (patch) | |
tree | c0fef80ba4de759150952d940e19e5edaafb2acb /gstrfuncs.c | |
parent | fb20a36775148be885a124aed0f548146a018dc5 (diff) | |
download | glib-039de051ef7fc237b22d94ac5e4e3b4a90d52842.tar.gz |
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.
Diffstat (limited to 'gstrfuncs.c')
-rw-r--r-- | gstrfuncs.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/gstrfuncs.c b/gstrfuncs.c index 721509bbf..60fd28b49 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; +} |