Initialize the gmime for upstream
[platform/upstream/gmime.git] / gmime / gmime-common.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2012 Jeffrey Stedfast
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free
17  *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18  *  02110-1301, USA.
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #include "gmime-table-private.h"
29 #include "gmime-common.h"
30
31 #ifndef g_tolower
32 #define g_tolower(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' + 'a' : (x))
33 #endif
34
35 int
36 g_mime_strcase_equal (gconstpointer v, gconstpointer v2)
37 {
38         return g_ascii_strcasecmp ((const char *) v, (const char *) v2) == 0;
39 }
40
41
42 guint
43 g_mime_strcase_hash (gconstpointer key)
44 {
45         const char *p = key;
46         guint h = 0;
47         
48         while (*p != '\0') {
49                 h = (h << 5) - h + g_tolower (*p);
50                 p++;
51         }
52         
53         return h;
54 }
55
56
57 /**
58  * g_mime_strdup_trim:
59  * @str: The string to duplicate and trim
60  *
61  * Duplicates the given input string while also trimming leading and
62  * trailing whitespace.
63  *
64  * Returns a duplicate string, minus any leading and trailing
65  * whitespace that the original string may have contained.
66  **/
67 char *
68 g_mime_strdup_trim (const char *str)
69 {
70         register const char *inptr = str;
71         register const char *end;
72         const char *start;
73         
74         while (is_lwsp (*inptr))
75                 inptr++;
76         
77         start = inptr;
78         end = inptr;
79         
80         while (*inptr) {
81                 if (!is_lwsp (*inptr++))
82                         end = inptr;
83         }
84         
85         return g_strndup (start, (size_t) (end - start));
86 }