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.
This commit is contained in:
Elliot Lee 1998-10-20 21:41:55 +00:00
parent fb20a36775
commit 039de051ef
12 changed files with 344 additions and 0 deletions

View File

@ -1,3 +1,8 @@
1998-10-20 Elliot Lee <sopwith@redhat.com>
* 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 <wilhelmi@ira.uka.de>
* configure.in: made the check for needed _ in module's func-names

View File

@ -1,3 +1,8 @@
1998-10-20 Elliot Lee <sopwith@redhat.com>
* 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 <wilhelmi@ira.uka.de>
* configure.in: made the check for needed _ in module's func-names

View File

@ -1,3 +1,8 @@
1998-10-20 Elliot Lee <sopwith@redhat.com>
* 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 <wilhelmi@ira.uka.de>
* configure.in: made the check for needed _ in module's func-names

View File

@ -1,3 +1,8 @@
1998-10-20 Elliot Lee <sopwith@redhat.com>
* 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 <wilhelmi@ira.uka.de>
* configure.in: made the check for needed _ in module's func-names

View File

@ -1,3 +1,8 @@
1998-10-20 Elliot Lee <sopwith@redhat.com>
* 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 <wilhelmi@ira.uka.de>
* configure.in: made the check for needed _ in module's func-names

View File

@ -1,3 +1,8 @@
1998-10-20 Elliot Lee <sopwith@redhat.com>
* 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 <wilhelmi@ira.uka.de>
* configure.in: made the check for needed _ in module's func-names

View File

@ -1,3 +1,8 @@
1998-10-20 Elliot Lee <sopwith@redhat.com>
* 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 <wilhelmi@ira.uka.de>
* configure.in: made the check for needed _ in module's func-names

View File

@ -1,3 +1,8 @@
1998-10-20 Elliot Lee <sopwith@redhat.com>
* 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 <wilhelmi@ira.uka.de>
* configure.in: made the check for needed _ in module's func-names

21
glib.h
View File

@ -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,

View File

@ -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,

View File

@ -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;
}

View File

@ -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;
}