Statistics
| Revision:

root / src / mgutils.c @ 1994

History | View | Annotate | Download (5.1 kB)

1 1 hiro
/*
2 1 hiro
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 1 hiro
 * Copyright (C) 2001 Match Grun
4 1 hiro
 *
5 1 hiro
 * This program is free software; you can redistribute it and/or modify
6 1 hiro
 * it under the terms of the GNU General Public License as published by
7 1 hiro
 * the Free Software Foundation; either version 2 of the License, or
8 1 hiro
 * (at your option) any later version.
9 1 hiro
 *
10 1 hiro
 * This program is distributed in the hope that it will be useful,
11 1 hiro
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1 hiro
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 1 hiro
 * GNU General Public License for more details.
14 1 hiro
 *
15 1 hiro
 * You should have received a copy of the GNU General Public License
16 1 hiro
 * along with this program; if not, write to the Free Software
17 1 hiro
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 1 hiro
 */
19 1 hiro
20 1 hiro
/*
21 1 hiro
 * General functions for create common address book entries.
22 1 hiro
 */
23 1 hiro
24 1 hiro
#include <glib.h>
25 1 hiro
#include <stdio.h>
26 1 hiro
#include <string.h>
27 1 hiro
28 1 hiro
#include "mgutils.h"
29 1 hiro
30 1 hiro
/*
31 1 hiro
* Dump linked list of character strings (for debug).
32 1 hiro
*/
33 1 hiro
void mgu_print_list( GSList *list, FILE *stream ) {
34 1 hiro
        GSList *node = list;
35 1 hiro
        while( node ) {
36 1 hiro
                fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
37 1 hiro
                node = g_slist_next( node );
38 1 hiro
        }
39 1 hiro
}
40 1 hiro
41 1 hiro
/*
42 1 hiro
* Dump linked list of character strings (for debug).
43 1 hiro
*/
44 1 hiro
void mgu_print_dlist( GList *list, FILE *stream ) {
45 1 hiro
        GList *node = list;
46 1 hiro
        while( node ) {
47 1 hiro
                fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
48 1 hiro
                node = g_list_next( node );
49 1 hiro
        }
50 1 hiro
}
51 1 hiro
52 1 hiro
/*
53 1 hiro
* Free linked list of character strings.
54 1 hiro
*/
55 1 hiro
void mgu_free_list( GSList *list ) {
56 1 hiro
        GSList *node = list;
57 1 hiro
        while( node ) {
58 1 hiro
                g_free( node->data );
59 1 hiro
                node->data = NULL;
60 1 hiro
                node = g_slist_next( node );
61 1 hiro
        }
62 1 hiro
        g_slist_free( list );
63 1 hiro
}
64 1 hiro
65 1 hiro
/*
66 1 hiro
* Free linked list of character strings.
67 1 hiro
*/
68 1 hiro
void mgu_free_dlist( GList *list ) {
69 1 hiro
        GList *node = list;
70 1 hiro
        while( node ) {
71 1 hiro
                g_free( node->data );
72 1 hiro
                node->data = NULL;
73 1 hiro
                node = g_list_next( node );
74 1 hiro
        }
75 1 hiro
        g_list_free( list );
76 1 hiro
}
77 1 hiro
78 1 hiro
/*
79 1 hiro
* Coalesce linked list of characaters into one long string.
80 1 hiro
*/
81 1 hiro
gchar *mgu_list_coalesce( GSList *list ) {
82 1 hiro
        gchar *str = NULL;
83 1 hiro
        gchar *buf = NULL;
84 1 hiro
        gchar *start = NULL;
85 1 hiro
        GSList *node = NULL;
86 1 hiro
        gint len;
87 1 hiro
88 1 hiro
        if( ! list ) return NULL;
89 1 hiro
90 1 hiro
        /* Calculate maximum length of text */
91 1 hiro
        len = 0;
92 1 hiro
        node = list;
93 1 hiro
        while( node ) {
94 1 hiro
                str = node->data;
95 1 hiro
                len += 1 + strlen( str );
96 1 hiro
                node = g_slist_next( node );
97 1 hiro
        }
98 1 hiro
99 1 hiro
        /* Create new buffer. */
100 1 hiro
        buf = g_new0( gchar, len+1 );
101 1 hiro
        start = buf;
102 1 hiro
        node = list;
103 1 hiro
        while( node ) {
104 1 hiro
                str = node->data;
105 1 hiro
                len = strlen( str );
106 1 hiro
                strcpy( start, str );
107 1 hiro
                start += len;
108 1 hiro
                node = g_slist_next( node );
109 1 hiro
        }
110 1 hiro
        return buf;
111 1 hiro
}
112 1 hiro
113 1 hiro
struct mgu_error_entry {
114 1 hiro
        gint        e_code;
115 1 hiro
        gchar        *e_reason;
116 1 hiro
};
117 1 hiro
118 1 hiro
static const struct mgu_error_entry mgu_error_list[] = {
119 1 hiro
        { MGU_SUCCESS,                "Success" },
120 1 hiro
        { MGU_BAD_ARGS,                "Bad arguments" },
121 1 hiro
        { MGU_NO_FILE,                "File not specified" },
122 1 hiro
        { MGU_OPEN_FILE,        "Error opening file" },
123 1 hiro
        { MGU_ERROR_READ,        "Error reading file" },
124 1 hiro
        { MGU_EOF,                "End of file encountered" },
125 1 hiro
        { MGU_OO_MEMORY,        "Error allocating memory" },
126 1 hiro
        { MGU_BAD_FORMAT,        "Bad file format" },
127 1 hiro
        { MGU_LDAP_CONNECT,        "Error connecting to LDAP server" },
128 1 hiro
        { MGU_LDAP_INIT,        "Error initializing LDAP" },
129 1 hiro
        { MGU_LDAP_BIND,        "Error binding to LDAP server" },
130 1 hiro
        { MGU_LDAP_SEARCH,        "Error searching LDAP database" },
131 1 hiro
        { MGU_LDAP_TIMEOUT,        "Timeout performing LDAP operation" },
132 1 hiro
        { MGU_LDAP_CRITERIA,        "Error in LDAP search criteria" },
133 1 hiro
        { MGU_LDAP_CRITERIA,        "Error in LDAP search criteria" },
134 1 hiro
        { MGU_LDAP_NOENTRIES,        "No LDAP entries found for search criteria" },
135 1 hiro
        { MGU_ERROR_WRITE,        "Error writing to file" },
136 1 hiro
        { MGU_OPEN_DIRECTORY,        "Error opening directory" },
137 1 hiro
        { MGU_NO_PATH,              "No path specified" },
138 1 hiro
        { -999,                        NULL }
139 1 hiro
};
140 1 hiro
141 1 hiro
static const struct mgu_error_entry *mgu_error_find( gint err ) {
142 1 hiro
        gint i;
143 1 hiro
        for ( i = 0; mgu_error_list[i].e_code != -999; i++ ) {
144 1 hiro
                if ( err == mgu_error_list[i].e_code )
145 1 hiro
                        return & mgu_error_list[i];
146 1 hiro
        }
147 1 hiro
        return NULL;
148 1 hiro
}
149 1 hiro
150 1 hiro
/*
151 1 hiro
* Return error message for specified error code.
152 1 hiro
*/
153 1 hiro
gchar *mgu_error2string( gint err ) {
154 1 hiro
        const struct mgu_error_entry *e;
155 1 hiro
        e = mgu_error_find( err );
156 1 hiro
        return ( e != NULL ) ? e->e_reason : "Unknown error";
157 1 hiro
}
158 1 hiro
159 1 hiro
/*
160 1 hiro
* Replace existing string with new string.
161 1 hiro
*/
162 1 hiro
gchar *mgu_replace_string( gchar *str, const gchar *value ) {
163 1 hiro
        if( str ) g_free( str );
164 1 hiro
        if( value ) {
165 1 hiro
                str = g_strdup( value );
166 1 hiro
                g_strstrip( str );
167 1 hiro
        }
168 1 hiro
        else {
169 1 hiro
                str = NULL;
170 1 hiro
        }
171 1 hiro
        return str;
172 1 hiro
}
173 1 hiro
174 1 hiro
/*
175 1 hiro
* Clear a linked list by setting node data pointers to NULL. Note that
176 1 hiro
* items are not freed.
177 1 hiro
*/
178 1 hiro
void mgu_clear_slist( GSList *list ) {
179 1 hiro
        GSList *node = list;
180 1 hiro
        while( node ) {
181 1 hiro
                node->data = NULL;
182 1 hiro
                node = g_slist_next( node );
183 1 hiro
        }
184 1 hiro
}
185 1 hiro
186 1 hiro
/*
187 1 hiro
* Clear a linked list by setting node data pointers to NULL. Note that
188 1 hiro
* items are not freed.
189 1 hiro
*/
190 1 hiro
void mgu_clear_list( GList *list ) {
191 1 hiro
        GList *node = list;
192 1 hiro
        while( node ) {
193 1 hiro
                node->data = NULL;
194 1 hiro
                node = g_list_next( node );
195 1 hiro
        }
196 1 hiro
}
197 1 hiro
198 1 hiro
/*
199 1 hiro
* Test and reformat an email address.
200 1 hiro
* Enter:  address.
201 1 hiro
* Return: Address, or NULL if address is empty.
202 1 hiro
* Note: Leading and trailing white space is removed.
203 1 hiro
*/
204 1 hiro
gchar *mgu_email_check_empty( gchar *address ) {
205 1 hiro
        gchar *retVal = NULL;
206 1 hiro
        if( address ) {
207 1 hiro
                retVal = g_strdup( address );
208 1 hiro
                retVal = g_strchug( retVal );
209 1 hiro
                retVal = g_strchomp( retVal );
210 1 hiro
                if( *retVal == '\0' ) {
211 1 hiro
                        g_free( retVal );
212 1 hiro
                        retVal = NULL;
213 1 hiro
                }
214 1 hiro
        }
215 1 hiro
        return retVal;
216 1 hiro
}
217 1 hiro
218 1 hiro
/*
219 1 hiro
* End of Source.
220 1 hiro
*/