aboutsummaryrefslogtreecommitdiffstats
path: root/camel/gstring-util.c
blob: a0c1345b71f341258d91cef0410d46613885c7ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* gstring-util : utilities for gstring object  */

/* 
 *
 * Author : 
 *  Bertrand Guiheneuf <bertrand@helixcode.com>
 *
 * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */



#include <config.h>
#include "gstring-util.h"
#include <string.h>

/**
 * g_string_equals : test if two string are equal
 *
 * @string1 : first string
 * @string2 : second string
 *
 * @Return Value : true if the strings equal, false otherwise
 **/
gboolean
g_string_equals (GString *string1, GString *string2)
{
    g_assert (string1);
    g_assert (string2);
    return !strcmp (string1->str, string2->str);
}




/**
 * g_string_clone : clone a GString
 *
 * @string : the string to clone
 *
 * @Return Value : the clone ...
 **/
GString *
g_string_clone (GString *string)
{
    return g_string_new (string->str);
}

/**
 * g_string_append_g_string : append a GString to another  GString
 *
 * @dest_string : string which will be appended
 * @other_string : string to append
 *
 **/
void 
g_string_append_g_string(GString *dest_string, GString *other_string)
{
    g_assert(other_string);
    g_assert(dest_string);

    if (other_string->len)
        g_string_append(dest_string, other_string->str);
}

/**
 * g_string_equal_for_hash: test equality of two GStrings for hash tables
 * @v: string 1
 * @v2: string 2
 * 
 * 
 * 
 * Return value: 
 **/
gint
g_string_equal_for_hash (gconstpointer v, gconstpointer v2)
{
  return strcmp ( ((const GString*)v)->str, ((const GString*)v2)->str) == 0;
}

gint
g_string_equal_for_glist (gconstpointer v, gconstpointer v2)
{
  return !strcmp ( ((const GString*)v)->str, ((const GString*)v2)->str) == 0;
}


/**
 * g_string_hash: computes a hash value for a Gstring
 * @v: Gstring object
 * 
 * 
 * 
 * Return value: 
 **/
guint 
g_string_hash (gconstpointer v)
{
    return g_str_hash(((const GString*)v)->str);
}




/* utility func : frees a GString element in a GList */
static void 
__g_string_list_free_string (gpointer data, gpointer user_data)
{
    GString *string = (GString *)data;
    g_string_free(string, TRUE);
}


void 
g_string_list_free (GList *string_list)
{
    g_list_foreach(string_list, __g_string_list_free_string, NULL);
    g_list_free(string_list);
}






GList *
g_string_split (GString *string, char sep, gchar *trim_chars, GStringTrimOption trim_options)
{
    GList *result = NULL;
    gint first, last, pos;
    gchar *str;
    gchar *new_str;
    GString *new_gstring;

    g_assert (string);
    str = string->str;
    if (!str) return NULL;

    first = 0;
    last = strlen(str) - 1;
    
    /* strip leading and trailing separators */
    while ( (first<=last) && (str[first]==sep) )
        first++;
    while ( (first<=last) && (str[last]==sep) )
        last--;

    
    while (first<=last)  {
        pos = first;
        /* find next separator */
        while ((pos<=last) && (str[pos]!=sep)) pos++;
        if (first != pos) {
            new_str = g_strndup (str+first, pos-first);
            new_gstring = g_string_new (new_str);
            g_free (new_str);
            /* could do trimming in line to speed up this code */
            if (trim_chars) g_string_trim (new_gstring, trim_chars, trim_options);
            result = g_list_append (result, new_gstring);
        }   
        first = pos + 1;
    }

    return result;
}


void 
g_string_trim (GString *string, gchar *chars, GStringTrimOption options)
{
    gint first_ok;
    gint last_ok;
    guint length;
    gchar *str;

    if ((!string) || (!string->str))
        return; 
    str = string->str;
    length = strlen (str);
    if (!length)
        return;

    first_ok = 0;
    last_ok = length - 1;
    
    if (options & GSTRING_TRIM_STRIP_LEADING)
        while  ( (first_ok <= last_ok) && (strchr (chars, str[first_ok])) )
            first_ok++;

    if (options & GSTRING_TRIM_STRIP_TRAILING)
        while  ( (first_ok <= last_ok) && (strchr (chars, str[last_ok])) )
            last_ok++;

    if (first_ok > 0)
        g_string_erase (string, 0, first_ok);

    if (last_ok < length-1)
        g_string_truncate (string, last_ok - first_ok +1);
    
}
h is no longer used by the OpenOffice ports andgerald2009-07-191-0/+1 * - Remove math/R-sp port was renamed to math/R-cran-spmiwi2009-07-171-1/+1 * - Document firefox-devel removalmiwi2009-07-151-0/+2 * DEPRECATED= Use net-mgmt/net-snmp port insteadmezz2009-07-141-1/+1 * - Update adminer to 1.11.1miwi2009-07-141-0/+2 * - Remove port as it is no longer supported upstream and is included inwxs2009-07-101-0/+1 * This plugin is not longer developed/supported by claws-mail team due tomiwi2009-07-081-0/+1 * - Update citrix entriestabthorpe2009-07-071-2/+2 * 2009-07-07 audio/gai-album: abandoned project, does not buildmiwi2009-07-071-0/+9 * - Dropping tcl8.0 supportmm2009-07-071-0/+1 * - Dropping tcl8.0 supportmm2009-07-071-0/+1 * security/gnutls-devel was re-added 2009/01/09, so delete it from this file.linimon2009-07-061-1/+0 * - Remove misc/koffice-i18n*; unlink from buildtabthorpe2009-06-291-0/+48 * - Update lang/mono to 2.4.flz2009-06-221-0/+1 * - Updelete games/njamamdmi32009-06-191-1/+0 * - Project abandoned.araujo2009-06-181-0/+1 * Be more descriptive about some removal reasons.linimon2009-06-181-2/+2 * - py-anygui is no longer being actively developed or supported.araujo2009-06-161-1/+1 * - This project came to an official end in 2008 there is no more supportaraujo2009-06-161-0/+1 * - Disconnect net/p5-ZConf-BGSetmiwi2009-06-161-0/+1 * - Fix previous commitmiwi2009-06-161-1/+1 * - Document net/p5-ZConf-Mail removalmiwi2009-06-161-0/+2 * Remove pm3-* and friends as it hasn't been able to build forerwin2009-06-151-0/+7 * - Move x11-wm/enlightenment-devel to x11-wm/enlightenmentgahr2009-06-151-0/+1 * - Remove multimedia/mlt++. multimedia/mlt++ was incorporated intobeat2009-06-151-0/+1 * Remove x11-toolkits/py-pyxfce as it has been marked BROKEN for over 3 months.erwin2009-06-141-0/+1 * Remove x11-themes/nimbus as it has been marked BROKEN for over 4 monthserwin2009-06-141-0/+1 * Remove x11-drivers/xf86-video-vga as it has been marked BROKEN for overerwin2009-06-141-0/+1 * Remove www/toofpy as it has been marked BROKEN for over 10 months.erwin2009-06-141-0/+1 * Remove www/thttpd-st as it has been marked BROKEN for over 5 months.erwin2009-06-141-0/+1 * Remove www/p5-HTML-Embperl as it has been marked BROKEN for over 4 months.erwin2009-06-141-0/+1 * Remove www/gforge as it has has been marked BROKEN for 11 months.erwin2009-06-141-0/+1 * Remove www/cacheboy as it has been marked BROKEN for over 8 months.erwin2009-06-141-0/+1 * Remove science/openfoam as it has been marked BROKEN for over 5 months.erwin2009-06-141-0/+1 * Remove science/elmerfront as it has been marked BROKEN over 3 months ago.erwin2009-06-141-0/+1 * Remove news/inn-current as it has been marked BROKEN 2 years and 4 months ago.erwin2009-06-141-0/+1 * Remove net/openpbx.org as it has been marked BROKEN for over 2 years.erwin2009-06-141-0/+1 * Remove net/evolution-rss as it has been marked BROKEN for over 4 months.erwin2009-06-141-0/+1 * Remove net-mgmt/nav as it has been marked BROKEN for over 5 months.erwin2009-06-141-0/+1 * Remove net-im/ocaml-jabbr as it has been marked BROKEN for over 5 months.erwin2009-06-141-0/+1 * Remove net-im/icqnd as it has been marked BROKEN over 5 months ago.erwin2009-06-141-0/+1 * Remove multimedia/smilutils as it has been marked BROKEN for over 9 months.erwin2009-06-141-0/+1 * Remove multimedia/dirac-reference as it has been marked BROKEN forerwin2009-06-141-0/+1 * Remove mail/silkymail as it has been marked BROKEN for over 11 months.erwin2009-06-141-0/+1 * Remove irc/ircg as it has been marked BROKEN for over 7 months.erwin2009-06-141-0/+1 * Remove graphics/demeter as it has been marked BROKEN for over 4 months.erwin2009-06-141-0/+1 * Remove games/njam as it has been marked BROKEN for over 3 months.erwin2009-06-141-0/+1 * Remove games/fgsd as it has been marked BROKEN for over 4 months.erwin2009-06-141-0/+1 * Remove devel/tcl-trf as it has been marked BORKEN for over 5 months.erwin2009-06-141-0/+1 * Remove devel/ruby-p4 as it has been marked BROKEN for over 6 months.erwin2009-06-141-0/+1 * Remove devel/p5-VCP as it has been marked BROKEN for over 4 months.erwin2009-06-141-0/+1 * Remove deskutils/mical as it has been marked BROKEN for over 8 months.erwin2009-06-141-0/+1 * Remove deskutils/kio_locate as it has been marked BROKEN for over 5 months.erwin2009-06-141-0/+1 * Remove comms/gfax as it has been marked BROKEN for over 4 months.erwin2009-06-141-0/+1 * Removed comms/xcept: stale old code, underlying protocol no longer in usejoerg2009-06-141-0/+1 * Remove cad/freecad as it has been BROKEN for over 4 months.erwin2009-06-141-0/+1 * Remove audio/sineshaper as it has been BROKEN for over 4 months.erwin2009-06-141-0/+1 * Remove audio/p5-Audio-TagLib as it has been marked BROKEN for over 11 months.erwin2009-06-141-0/+1 * Remove audio/gnomad2 as it has been BROKEN of over 5 months.erwin2009-06-141-0/+1 * Remove x11-toolkits/wxmozilla: has been broken for more than 6 monthserwin2009-06-141-0/+1 * Remove x11-toolkits/p5-GtkXmHTML: depends on a broken, expired porterwin2009-06-141-0/+1 * Remove www/winhelpcgi, does not compile with GCC 4.2erwin2009-06-141-0/+1 * Remove www/mnogosearch31: Use www/mnogosearch instead, this version is ancienterwin2009-06-141-0/+2 * Remove expired multimedia/manencodeerwin2009-06-141-0/+1 * Remove graphics/libimg-tk83: tcl8.3 support is going to be droppederwin2009-06-141-0/+1 * Remove expired ports in audio:erwin2009-06-131-0/+13 * Remove www/awstats-devel which was marked BROKEN over 5 months ago.erwin2009-06-131-0/+1 * Remove www/amaya which was marked FORBIDDEN over 4 months ago.erwin2009-06-131-0/+1 * Remove chinese/wordpress which has been mark FORBIDDEN over 6 months ago.erwin2009-06-131-0/+1 * Remove www/tdiary-devel: has been forbidden for more than 17 monthserwin2009-06-131-0/+1 * Remove japanese/slirc. The port has been BROKEN of over 4 monthserwin2009-06-131-0/+1 * Make GCC 4.3.x the default version.joerg2009-06-121-0/+1 * Remove epiphany-webkit.kwm2009-06-101-0/+1 * - Move dplay to playdavl2009-06-101-0/+1 * Remove expired port lang/perl5.6skv2009-06-081-0/+1 * 2009-06-09 www/apache-jserv: superceeded by tomcatpgollucci2009-06-081-0/+1 * - Move x11-wm/enlightenment to x11-wm/e16gahr2009-06-081-0/+1 * - Delete expired devel/powerpc-gcc and devel/powerpc-binutils ports.stas2009-06-061-0/+2 * 2009-05-31 biology/p5-bioperl-devel: no longer under developmentmiwi2009-06-061-0/+4 * - net/tftp-hpa and ftp/tftp-hpa are the same thing. Remove the one morepav2009-06-011-0/+1 * Ports for Japanese fonts have been reorganized in the following way:hrs2009-05-311-0/+7 * 2009-05-22 misc/sonytv: tcl8.3 support is going to be droppedmiwi2009-05-291-0/+3 * Indicate that net/ntp-rc has been promoted from relese candidate tocy2009-05-231-1/+1 * - Remove german/swissgerman-ding. german/swissgerman-ding is nowbeat2009-05-131-0/+1 * - Renamed port directoryglarkin2009-05-081-0/+1 * Remove devel/oskit per deprecation/expiration note.gerald2009-05-081-0/+1 * - Restore festival+OGImiwi2009-05-051-1/+0 * - Update to 1.8.0miwi2009-05-051-0/+1 * 2009-04-28 audio/festival+OGI: unmaintained, does not work with current versi...miwi2009-05-051-0/+2 * Remove textproc/sp per deprecation/expiration note.gerald2009-05-041-0/+1 * 2009-04-22 audio/snowstar: tcl8.0 support is going to be droppedmiwi2009-05-021-0/+9 * - Add missing fileddhn2009-05-011-1/+1 * - Fix previous commitdhn2009-05-011-1/+1 * - Remove net/vnstati, use net/vnstatdhn2009-05-011-0/+1 * Remove linux-megacli2, linux-megacli was updated to replace this portpav2009-04-301-0/+1 * - Delete original port after repo copypgollucci2009-04-291-0/+1 * - Move russian/xneur to deskutils - it is more suitable category as the tool ...amdmi32009-04-271-0/+1 * - Update to 0.9.14amdmi32009-04-271-0/+1 * Move emulators/cpmtools27 to emulators/cpmtools2makc2009-04-261-0/+1 * Ports `nvidia-driver-71xx' and `nvidia-driver-96xx' were renamed to betterdanfe2009-04-131-0/+2 * www/mod_mysqluserdir||2009-04-13|Depends on broken databases/mysql323-clientpgollucci2009-04-131-0/+1 * www/mod_mya||2009-04-13|Depends on broken databases/mysql323-clientpgollucci2009-04-131-0/+1 * www/mod_snake||2009-04-12|Doesn't compile; abandoned upstream since 2002pgollucci2009-04-121-0/+1 * Fix format of previous entries. Also add www/rt2, www/rt32, www/rt34.linimon2009-04-121-19/+22 * Note the removal of the following expired ports: devel/*-rtems-g77,linimon2009-04-121-0/+19 * 2009-03-22 science/oof: Broken with GCC 4.2 and beyondmiwi2009-04-111-0/+2 * Presenting GNOME 2.26 for FreeBSD. Seemarcus2009-04-101-0/+2 * Update to 0.8.2rnoland2009-04-081-0/+3 * 2009-03-26 www/drupal4-attachment: Drupal 4.7.x is end-of-life since 200802. ...miwi2009-04-051-0/+10 * Bring devel/p5-Getopt-Mixed back, this time without EXPIRATION_DATE.wxs2009-04-011-1/+0 * 2009-03-27 net/sipxcalllib: old version, needs updatingmiwi2009-03-291-0/+12 * - ruby-zoom -> rubygem-zoomclsung2009-03-261-0/+1 * 2009-03-22 comms/plp: unmaintained, does not work with current versions of GC...miwi2009-03-251-0/+1 * 2009-03-22 mbone/nte: tcl8.0 support is going to be droppedmiwi2009-03-241-0/+3 * 2009-03-22 emulators/dlx: unmaintained, does not work with current versions o...miwi2009-03-241-0/+17 * - Remove misc/bibletime-develtabthorpe2009-03-241-1/+0 * Has been gone from CPAN for ages, so can't build anyway. Uselbr2009-03-231-0/+1 * Move oclock from x11 to x11-clocks category.flz2009-03-231-0/+1 * Remove net/penguintv-devel since the development version has notedwin2009-03-231-0/+1 * Add an entry for audio/rhythmbox-devel.marcus2009-03-221-0/+1 * - Remove net-im/ruby-xmpp4r (renamed to net-im/rubygem-xmpp4r)miwi2009-03-211-0/+1 * Added entry about games/PySolFC to games/pysolfc renaming.mva2009-03-201-0/+1 * - Remove sysutils/libisoburn project was renamed to sysutils/xorrisomiwi2009-03-171-0/+1 * Remove lang/gcc33 per deprecation/expiration note.gerald2009-03-151-0/+1 * Complete rename of misc/git to misc/gnuit.gerald2009-03-141-0/+1 * bash3 is now a port that should exist on its own.obrien2009-03-141-1/+0 * 2009-02-11 x11-toolkits/inti-gconf: not maintain by upstream for more than fi...miwi2009-03-101-0/+3 * 2009-02-15 ftp/downloader: sources unavailable - website dissappearedmiwi2009-03-101-0/+6 * 2009-03-01 databases/rubygem-rrdtool: port no longer maintained by upstream u...miwi2009-03-101-0/+3 * - Rename www/p5-Cookie-XS to www/p5-CGI-Cookie-XSskv2009-03-091-0/+1 * 2009-02-21 japanese/ruby-tk: uses old Tk, not required with newver versionsstas2009-03-091-0/+1 * - Remove graphics/libgdgeda: is not required anymore.stas2009-03-091-0/+1 * Remove multimedia/libspiff, now that herrie depends on libxspf.ed2009-03-091-0/+1 * - Remove php5-pcre due to reintroduction by ale@ with lang/php 5.2.9mm2009-03-081-1/+0 * Remove textproc/latte and textproc/nicetext per expiration note.gerald2009-03-081-0/+2 * Remove devel/prcs and devel/tvision per expiration note.gerald2009-03-071-0/+2 * Remove math/cxsc expiration note.gerald2009-03-071-0/+1 * Remove audio/quelcom per expiration note.gerald2009-03-071-0/+1 * - Remove x11-toolkits/xg. It expired on 2007-10-28.wxs2009-03-071-0/+1 * - Use the correct date for last two entries.wxs2009-03-071-2/+2 * - Remove x11-toolkits/scx: Version branch long since retired. This portwxs2009-03-071-0/+1 * Remove devel/p5-Getopt-Mixed: expired on 2007-04-23. Please usewxs2009-03-061-0/+1 * The port comms/python-gammu was incorporated into comms/gammubsam2009-03-051-0/+1 * update Xfce to 4.6oliver2009-03-031-0/+4 * - Fix MOVED file formatgabor2009-03-031-1/+1 * - Remove finance/eqonomize, use finance/eqonomize-kde3tabthorpe2009-03-021-0/+1 * 2009-02-18 math/slsc: No longer maintained by authormiwi2009-03-011-0/+1 * - Remove misc/bibletime and misc/bibletime-develtabthorpe2009-02-271-0/+2 * - Add an entry for editors/sam being removed.wxs2009-02-201-0/+1 * Submitted by: timurtimur2009-02-17