aboutsummaryrefslogtreecommitdiffstats
path: root/camel/string-utils.c
blob: bfda0f0f8d573bf183a6faea45ce31fb1daf13f4 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* string-util : utilities for gchar* strings  */

/* 
 *
 * Copyright (C) 1999 Bertrand Guiheneuf <Bertrand.Guiheneuf@inria.fr> .
 *
 * 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 "string-utils.h"
#include "camel-log.h"
#include "string.h"



gboolean
string_equal_for_glist (gconstpointer v, gconstpointer v2)
{
  return (!strcmp ( ((const gchar *)v), ((const gchar*)v2))) == 0;
}

/**
 * string_dichotomy:
 * @sep : separator
 * @prefix: pointer to be field by the prefix object
 *   the prefix is not returned when the given pointer is NULL
 * @suffix: pointer to be field by the suffix object
 *   the suffix is not returned when the given pointer is NULL
 *
 * Return the strings before and/or after 
 * the last occurence of the specified separator
 *
 * This routine returns the string before and/or after
 * a character given as an argument. 
 * if the separator is the last character, prefix and/or
 * suffix is set to NULL and result is set to 'l'
 * if the separator is not in the list, prefix and/or
 * suffix is set to NULL and result is set to 'n'
 * When the operation succedeed, the return value is 'o'
 *
 * @Return Value : result of the operation ('o', 'l' or 'n')
 *
 **/
gchar
string_dichotomy (const gchar *string, gchar sep, gchar **prefix, gchar **suffix,
            StringDichotomyOption options)
{
    gchar *tmp_str;
    gint sep_pos, first, last, len;
    
    g_assert (string);
    CAMEL_LOG_FULL_DEBUG (\
          "string_dichotomy:: string=\"%s\"\n\tseparator=\"%c\" \n\tprefix=%p \n\tsuffix=%p \n\toptions=%ld\n",\
          string, sep, prefix, suffix, options);
    len = strlen (string);
    if (!len) {
        if (prefix)
            *prefix=NULL;
        if (suffix)
            *suffix=NULL;
        CAMEL_LOG_FULL_DEBUG ("string_dichotomy:: input string is empty\n");
        return 'n';
    }
    first = 0;
    last = len-1;
    
    if ( (options & STRING_DICHOTOMY_STRIP_LEADING ) && (string[first] == sep) )
        do {first++;} while ( (first<len) && (string[first] == sep) );
    
    if (options & STRING_DICHOTOMY_STRIP_TRAILING )
        while ((string[last] == sep) && (last>first))
            last--;
    
    if (first==last) {
        if (prefix) *prefix=NULL;
        if (suffix) *suffix=NULL;
        CAMEL_LOG_FULL_DEBUG ("string_dichotomy: after stripping, string is empty\n");
        return 'n';
    }
    
    if (options & STRING_DICHOTOMY_RIGHT_DIR) {
        sep_pos = last;
        while ((sep_pos>=first) && (string[sep_pos]!=sep)) {
            sep_pos--;
        }
    } else {
        sep_pos = first;
        while ((sep_pos<=last) && (string[sep_pos]!=sep)) {
            sep_pos++;
        }
        
    }
    
    if ( (sep_pos<first) || (sep_pos>last) ) 
        {
            if (suffix) *suffix=NULL;
            if (prefix) *prefix=NULL;
            CAMEL_LOG_FULL_DEBUG ("string_dichotomy: separator not found\n");
            return 'n';
        }
    
    /* if we have stripped trailing separators, we should */
    /* never enter here */
    if (sep_pos==last) 
        {
            if (suffix) *suffix=NULL;
            if (prefix) *prefix=NULL;
            CAMEL_LOG_FULL_DEBUG ("string_dichotomy: separator is last character\n");
            return 'l';
        }
    /* if we have stripped leading separators, we should */
    /* never enter here */
    if (sep_pos==first)
        {
            if (suffix) *suffix=NULL;
            if (prefix) *prefix=NULL;
            CAMEL_LOG_FULL_DEBUG ("string_dichotomy: separator is first character\n");
            return 'l';
        }
    CAMEL_LOG_FULL_DEBUG ("string_dichotomy: separator found at :%d\n", sep_pos);
    if (prefix) { /* return the prefix */
        *prefix = g_strndup(string+first,sep_pos-first);
        CAMEL_LOG_FULL_DEBUG ( "string_dichotomy:: prefix:\"%s\"\n", *prefix);
    }
    if (suffix) { /* return the suffix */
        *suffix = g_strndup(string+sep_pos+1, last-sep_pos);
        CAMEL_LOG_FULL_DEBUG ( "string_dichotomy:: suffix:\"%s\"\n", *suffix);
    }
     
    return 'o';
}






/* utility func : frees a gchar element in a GList */
static void 
__string_list_free_string (gpointer data, gpointer user_data)
{
    gchar *string = (gchar *)data;
    g_free (string);
}


void 
string_list_free (GList *string_list)
{
    if (string_list == NULL) return; 

    g_list_foreach (string_list, __string_list_free_string, NULL);
    g_list_free (string_list);
}






GList *
string_split (const gchar *string, char sep, const gchar *trim_chars, StringTrimOption trim_options)
{
    GList *result = NULL;
    gint first, last, pos;
    gchar *new_string;

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

    
    CAMEL_LOG_FULL_DEBUG ("string_split:: trim options: %d\n", trim_options);

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

    return result;
}


void 
string_trim (gchar *string, const gchar *trim_chars, StringTrimOption options)
{
    gint first_ok;
    gint last_ok;
    guint length;

    CAMEL_LOG_FULL_DEBUG ("string-utils:: Entering string_trim::\n");
    CAMEL_LOG_FULL_DEBUG ("string_trim:: trim_chars:\"%s\"", trim_chars);
    CAMEL_LOG_FULL_DEBUG ("string_trim:: trim_options:%d\n", options);

    g_return_if_fail (string);
    length = strlen (string);
    g_return_if_fail (length > 0);
    
    first_ok = 0;
    last_ok = length - 1;

    if (options & STRING_TRIM_STRIP_LEADING)
        while  ( (first_ok <= last_ok) && (strchr (trim_chars, string[first_ok])!=NULL) )
            first_ok++;
    
    if (options & STRING_TRIM_STRIP_TRAILING)
        while  ( (first_ok <= last_ok) && (strchr (trim_chars, string[last_ok])!=NULL) )
            last_ok--;
    CAMEL_LOG_FULL_DEBUG ("string_trim::\n\t\"%s\":first ok:%d last_ok:%d\n",
           string, first_ok, last_ok);
    
    if (first_ok > 0)
        memmove (string, string+first_ok, last_ok - first_ok +2);
    
}







/***/
/* use these two funcs for case insensitive hash table */

gint 
g_strcase_equal (gconstpointer a, gconstpointer b)
{
    return (g_strcasecmp ((gchar *)a, (gchar *)b) == 0);
}


/* modified g_str_hash from glib/gstring.c
   because it would have been too slow to
   us g_strdown() on the string */
/* a char* hash function from ASU */
guint
g_strcase_hash (gconstpointer v)
{
    const char *s = (char*)v;
    const char *p;
    char c;
    guint h=0, g;
    
    for(p = s; *p != '\0'; p += 1) {
        c = isupper ((guchar)*p) ? tolower ((guchar)*p) : *p;
        h = ( h << 4 ) + c;
        if ( ( g = h & 0xf0000000 ) ) {
            h = h ^ (g >> 24);
            h = h ^ g;
        }
  }

  return h /* % M */;
}









/***/