Commit c64e1a28 authored by Ramiro Estrugo's avatar Ramiro Estrugo

New function. Return the string list items concatenated into a single

* libnautilus-extensions/nautilus-string-list.c:
(nautilus_string_list_as_concatenated_string),
(nautilus_self_check_string_list):
* libnautilus-extensions/nautilus-string-list.h:
New function.  Return the string list items concatenated into a
single string delimited by a given delimeter.
parent 012025c7
2000-06-05 Ramiro Estrugo <ramiro@eazel.com>
* libnautilus-extensions/nautilus-string-list.c:
(nautilus_string_list_as_concatenated_string),
(nautilus_self_check_string_list):
* libnautilus-extensions/nautilus-string-list.h:
New function. Return the string list items concatenated into a
single string delimited by a given delimeter.
2000-06-05 Andy Hertzfeld <andy@eazel.com>
* libnautilus-extensions/nautilus-file-utilities.c:
......
......@@ -291,6 +291,55 @@ nautilus_string_list_get_index_for_string (const NautilusStringList *string_list
return NAUTILUS_STRING_LIST_NOT_FOUND;
}
/**
* nautilus_string_list_as_tokens
*
* @string_list: A NautilusStringList
* @delimiter: The string to use a delimeter, can be NULL.
*
* Return value: An newly allocated string concatenation of all the items in the list.
* The string is delimited by 'delimiter'.
*/
char *
nautilus_string_list_as_concatenated_string (const NautilusStringList *string_list,
const char *delimiter)
{
char *result = NULL;
guint length;
g_return_val_if_fail (string_list != NULL, NULL);
length = nautilus_string_list_get_length (string_list);
if (length > 0) {
guint n;
GList *iterator;
GString *tokens;
n = 0;
tokens = g_string_new (NULL);
for (iterator = string_list->strings; iterator != NULL; iterator = iterator->next) {
const char *current = (const char *) iterator->data;
g_string_append (tokens, current);
n++;
if (delimiter && (n != length)) {
g_string_append (tokens, delimiter);
}
}
result = tokens->str;
g_string_free (tokens, FALSE);
}
return result;
}
#if !defined (NAUTILUS_OMIT_SELF_CHECK)
......@@ -464,6 +513,30 @@ nautilus_self_check_string_list (void)
nautilus_string_list_free (fruits);
}
/*
* nautilus_string_list_as_concatenated_string
*
*/
{
NautilusStringList *l;
fruits = nautilus_string_list_new ();
nautilus_string_list_insert (l, "x");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, NULL), "x");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, ":"), "x");
nautilus_string_list_insert (l, "y");
nautilus_string_list_insert (l, "z");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, NULL), "xyz");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, ""), "xyz");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, ":"), "x:y:z");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, "abc"), "xabcyabcz");
nautilus_string_list_free (l);
}
}
#endif /* !NAUTILUS_OMIT_SELF_CHECK */
......@@ -33,48 +33,64 @@
typedef struct _NautilusStringList NautilusStringList;
/* Construct an empty string list. */
NautilusStringList *nautilus_string_list_new (void);
NautilusStringList *nautilus_string_list_new (void);
/* Construct a string list with a single element */
NautilusStringList *nautilus_string_list_new_from_string (const char *string);
NautilusStringList *nautilus_string_list_new_from_string (const char *string);
/* Construct a string list that is a copy of another string list */
NautilusStringList *nautilus_string_list_new_from_string_list (const NautilusStringList *other);
NautilusStringList *nautilus_string_list_new_from_string_list (const NautilusStringList *other);
/* Construct a string list from tokens delimited by the given string and delimeter */
NautilusStringList *nautilus_string_list_new_from_tokens (const char *string,
const char *delimiter);
NautilusStringList *nautilus_string_list_new_from_tokens (const char *string,
const char *delimiter);
/* Free a string list */
void nautilus_string_list_free (NautilusStringList *string_list);
void nautilus_string_list_free (NautilusStringList *string_list);
/* Insert a string into the collection. */
void nautilus_string_list_insert (NautilusStringList *string_list,
const gchar *string);
void nautilus_string_list_insert (NautilusStringList *string_list,
const char *string);
/* Clear the collection. */
void nautilus_string_list_clear (NautilusStringList *string_list);
void nautilus_string_list_clear (NautilusStringList *string_list);
/* Access the nth string in the collection. Returns an strduped string. */
gchar * nautilus_string_list_nth (const NautilusStringList *string_list,
guint n);
char * nautilus_string_list_nth (const NautilusStringList *string_list,
guint n);
/* Does the string list contain the given string ? */
gboolean nautilus_string_list_contains (const NautilusStringList *string_list,
const char *string);
gboolean nautilus_string_list_contains (const NautilusStringList *string_list,
const char *string);
/* How many strings are currently in the collection ? */
guint nautilus_string_list_get_length (const NautilusStringList *string_list);
guint nautilus_string_list_get_length (const NautilusStringList *string_list);
/* Get the index for the given string. Return NAUTILUS_STRING_LIST_NOT_FOUND if not found. */
gint nautilus_string_list_get_index_for_string (const NautilusStringList *string_list,
const char *string);
gint nautilus_string_list_get_index_for_string (const NautilusStringList *string_list,
const char *string);
/* Does the string list a equal string list b ? */
gboolean nautilus_string_list_equals (const NautilusStringList *a,
const NautilusStringList *b);
gboolean nautilus_string_list_equals (const NautilusStringList *a,
const NautilusStringList *b);
/* Return the string list in a GList. Must deep free the result with nautilus_g_list_free_deep() */
GList * nautilus_string_list_as_g_list (const NautilusStringList *string_list);
GList * nautilus_string_list_as_g_list (const NautilusStringList *string_list);
/* Return the string list as a concatenation of all the items delimeted by delimeter. */
char * nautilus_string_list_as_concatenated_string (const NautilusStringList *string_list,
const char *delimiter);
#endif /* NAUTILUS_STRING_LIST_H */
......@@ -291,6 +291,55 @@ nautilus_string_list_get_index_for_string (const NautilusStringList *string_list
return NAUTILUS_STRING_LIST_NOT_FOUND;
}
/**
* nautilus_string_list_as_tokens
*
* @string_list: A NautilusStringList
* @delimiter: The string to use a delimeter, can be NULL.
*
* Return value: An newly allocated string concatenation of all the items in the list.
* The string is delimited by 'delimiter'.
*/
char *
nautilus_string_list_as_concatenated_string (const NautilusStringList *string_list,
const char *delimiter)
{
char *result = NULL;
guint length;
g_return_val_if_fail (string_list != NULL, NULL);
length = nautilus_string_list_get_length (string_list);
if (length > 0) {
guint n;
GList *iterator;
GString *tokens;
n = 0;
tokens = g_string_new (NULL);
for (iterator = string_list->strings; iterator != NULL; iterator = iterator->next) {
const char *current = (const char *) iterator->data;
g_string_append (tokens, current);
n++;
if (delimiter && (n != length)) {
g_string_append (tokens, delimiter);
}
}
result = tokens->str;
g_string_free (tokens, FALSE);
}
return result;
}
#if !defined (NAUTILUS_OMIT_SELF_CHECK)
......@@ -464,6 +513,30 @@ nautilus_self_check_string_list (void)
nautilus_string_list_free (fruits);
}
/*
* nautilus_string_list_as_concatenated_string
*
*/
{
NautilusStringList *l;
fruits = nautilus_string_list_new ();
nautilus_string_list_insert (l, "x");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, NULL), "x");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, ":"), "x");
nautilus_string_list_insert (l, "y");
nautilus_string_list_insert (l, "z");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, NULL), "xyz");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, ""), "xyz");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, ":"), "x:y:z");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_list_as_concatenated_string (l, "abc"), "xabcyabcz");
nautilus_string_list_free (l);
}
}
#endif /* !NAUTILUS_OMIT_SELF_CHECK */
......@@ -33,48 +33,64 @@
typedef struct _NautilusStringList NautilusStringList;
/* Construct an empty string list. */
NautilusStringList *nautilus_string_list_new (void);
NautilusStringList *nautilus_string_list_new (void);
/* Construct a string list with a single element */
NautilusStringList *nautilus_string_list_new_from_string (const char *string);
NautilusStringList *nautilus_string_list_new_from_string (const char *string);
/* Construct a string list that is a copy of another string list */
NautilusStringList *nautilus_string_list_new_from_string_list (const NautilusStringList *other);
NautilusStringList *nautilus_string_list_new_from_string_list (const NautilusStringList *other);
/* Construct a string list from tokens delimited by the given string and delimeter */
NautilusStringList *nautilus_string_list_new_from_tokens (const char *string,
const char *delimiter);
NautilusStringList *nautilus_string_list_new_from_tokens (const char *string,
const char *delimiter);
/* Free a string list */
void nautilus_string_list_free (NautilusStringList *string_list);
void nautilus_string_list_free (NautilusStringList *string_list);
/* Insert a string into the collection. */
void nautilus_string_list_insert (NautilusStringList *string_list,
const gchar *string);
void nautilus_string_list_insert (NautilusStringList *string_list,
const char *string);
/* Clear the collection. */
void nautilus_string_list_clear (NautilusStringList *string_list);
void nautilus_string_list_clear (NautilusStringList *string_list);
/* Access the nth string in the collection. Returns an strduped string. */
gchar * nautilus_string_list_nth (const NautilusStringList *string_list,
guint n);
char * nautilus_string_list_nth (const NautilusStringList *string_list,
guint n);
/* Does the string list contain the given string ? */
gboolean nautilus_string_list_contains (const NautilusStringList *string_list,
const char *string);
gboolean nautilus_string_list_contains (const NautilusStringList *string_list,
const char *string);
/* How many strings are currently in the collection ? */
guint nautilus_string_list_get_length (const NautilusStringList *string_list);
guint nautilus_string_list_get_length (const NautilusStringList *string_list);
/* Get the index for the given string. Return NAUTILUS_STRING_LIST_NOT_FOUND if not found. */
gint nautilus_string_list_get_index_for_string (const NautilusStringList *string_list,
const char *string);
gint nautilus_string_list_get_index_for_string (const NautilusStringList *string_list,
const char *string);
/* Does the string list a equal string list b ? */
gboolean nautilus_string_list_equals (const NautilusStringList *a,
const NautilusStringList *b);
gboolean nautilus_string_list_equals (const NautilusStringList *a,
const NautilusStringList *b);
/* Return the string list in a GList. Must deep free the result with nautilus_g_list_free_deep() */
GList * nautilus_string_list_as_g_list (const NautilusStringList *string_list);
GList * nautilus_string_list_as_g_list (const NautilusStringList *string_list);
/* Return the string list as a concatenation of all the items delimeted by delimeter. */
char * nautilus_string_list_as_concatenated_string (const NautilusStringList *string_list,
const char *delimiter);
#endif /* NAUTILUS_STRING_LIST_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment