d87d50663dc29c00b78ba48ebc6ad5ff1cbe6618
[platform/upstream/evolution-data-server.git] / camel / string-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *
5  *  Copyright 2002 Ximian, Inc. (www.ximian.com)
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
20  *
21  */
22
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "string-utils.h"
32
33
34 int 
35 g_strcase_equal (gconstpointer a, gconstpointer b)
36 {
37         return (strcasecmp ((const char *) a, (const char *) b) == 0);
38 }
39
40 guint
41 g_strcase_hash (gconstpointer v)
42 {
43         const char *p = (char *) v;
44         guint h = 0, g;
45         
46         for ( ; *p != '\0'; p++) {
47                 h = (h << 4) + toupper (*p);
48                 if ((g = h & 0xf0000000)) {
49                         h = h ^ (g >> 24);
50                         h = h ^ g;
51                 }
52         }
53         
54         return h;
55 }
56
57
58 static void
59 free_string (gpointer string, gpointer user_data)
60 {
61         g_free (string);
62 }
63
64 void 
65 string_list_free (GList *string_list)
66 {
67         if (string_list == NULL)
68                 return; 
69         
70         g_list_foreach (string_list, free_string, NULL);
71         g_list_free (string_list);
72 }
73
74 char *
75 camel_strstrcase (const char *haystack, const char *needle)
76 {
77         /* find the needle in the haystack neglecting case */
78         const char *ptr;
79         guint len;
80         
81         g_return_val_if_fail (haystack != NULL, NULL);
82         g_return_val_if_fail (needle != NULL, NULL);
83         
84         len = strlen (needle);
85         if (len > strlen (haystack))
86                 return NULL;
87         
88         if (len == 0)
89                 return (char *) haystack;
90         
91         for (ptr = haystack; *(ptr + len - 1) != '\0'; ptr++)
92                 if (!strncasecmp (ptr, needle, len))
93                         return (char *) ptr;
94         
95         return NULL;
96 }
97
98
99 const char *
100 camel_strdown (char *str)
101 {
102         register char *s = str;
103         
104         while (*s) {
105                 if (*s >= 'A' && *s <= 'Z')
106                         *s += 0x20;
107                 s++;
108         }
109         
110         return str;
111 }