Skip to content
Snippets Groups Projects
Commit f15cbed5 authored by edgrif's avatar edgrif
Browse files

add new GString derived function to replace one string with another in a GString.

parent 766964e6
No related branches found
No related tags found
No related merge requests found
......@@ -26,9 +26,9 @@
* glib but not included with their distribution.
*
* HISTORY:
* Last edited: Mar 1 14:55 2006 (edgrif)
* Last edited: Mar 27 12:20 2006 (edgrif)
* Created: Thu Oct 13 15:56:54 2005 (edgrif)
* CVS info: $Id: zmapGLibUtils.h,v 1.4 2006-03-03 08:07:21 edgrif Exp $
* CVS info: $Id: zmapGLibUtils.h,v 1.5 2006-03-27 12:10:54 edgrif Exp $
*-------------------------------------------------------------------
*/
#ifndef ZMAP_GLIBUTILS_H
......@@ -66,6 +66,8 @@ void zMap_g_list_foreach_directional(GList *list,
gpointer user_data,
ZMapGListDirection forward);
gboolean zMap_g_string_replace(GString *string, char *target, char *source) ;
/* Returns a pointer to an element of the array instead of the element itself. */
#define zMap_g_array_index_ptr(a, t, i) (&(((t*) (a)->data) [(i)]))
......
......@@ -26,12 +26,13 @@
*
* Exported functions: See ZMap/zmapGLibUtils.h
* HISTORY:
* Last edited: Mar 1 14:56 2006 (edgrif)
* Last edited: Mar 27 12:42 2006 (edgrif)
* Created: Thu Oct 13 15:22:35 2005 (edgrif)
* CVS info: $Id: zmapGLibUtils.c,v 1.4 2006-03-03 08:07:20 edgrif Exp $
* CVS info: $Id: zmapGLibUtils.c,v 1.5 2006-03-27 12:10:54 edgrif Exp $
*-------------------------------------------------------------------
*/
#include <string.h>
#include <ZMap/zmapUtils.h>
#include <ZMap/zmapGLibUtils.h>
......@@ -156,10 +157,7 @@ void zMap_g_list_foreach_directional(GList *list,
/* My new routine for returning a pointer to an element in the array, the array will be
* expanded to include the requested element if necessary. If "clear" was not set when
* the array was created then the element may contain random junk instead of zeros. */
GArray*
zMap_g_array_element (GArray *farray,
guint index,
gpointer *element_out)
GArray* zMap_g_array_element (GArray *farray, guint index, gpointer *element_out)
{
GRealArray *array = (GRealArray*) farray;
gint length;
......@@ -179,6 +177,59 @@ zMap_g_array_element (GArray *farray,
/*!
* Additions to GString
*
* GString is a very good package but there are some more operations it could usefully do.
*
*/
/*!
* Substitute one string for another in a GString.
*
* You should note that this routine simply uses the existing GString erase/insert
* functions and so may not be incredibly efficient. If the target string was not
* found the GString remains unaltered and FALSE is returned.
*
* @param string A valid GString.
* @param target The string to be replaced.
* @param source The string to be inserted.
* @return TRUE if a string was replaced, FALSE otherwise.
* */
gboolean zMap_g_string_replace(GString *string, char *target, char *source)
{
gboolean result = FALSE ;
int source_len ;
int target_len ;
int target_pos ;
char *template_ptr ;
target_len = strlen(target) ;
source_len = strlen(source) ;
template_ptr = string->str ;
while ((template_ptr = strstr(template_ptr, target)))
{
result = TRUE ;
target_pos = template_ptr - string->str ;
string = g_string_erase(string, target_pos, target_len) ;
string = g_string_insert(string, target_pos, source) ;
template_ptr = string->str + target_pos + source_len ; /* Shouldn't go off the end. */
}
return result ;
}
/*!
* Additions to GQuark
*
......
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