d3b99c165162cdee22c1a176959a05e12f620696
[claws.git] / src / mgutils.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 Match Grun
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * General functions for create common address book entries.
22  */
23
24 #include <stdio.h>
25 #include <glib.h>
26
27 #include "mgutils.h"
28
29 /*
30 * Dump linked list of character strings (for debug).
31 */
32 void mgu_print_list( GSList *list, FILE *stream ) {
33         GSList *node = list;
34         while( node ) {
35                 fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
36                 node = g_slist_next( node );
37         }
38 }
39
40 /*
41 * Dump linked list of character strings (for debug).
42 */
43 void mgu_print_dlist( GList *list, FILE *stream ) {
44         GList *node = list;
45         while( node ) {
46                 fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
47                 node = g_list_next( node );
48         }
49 }
50
51 /*
52 * Free linked list of character strings.
53 */
54 void mgu_free_list( GSList *list ) {
55         GSList *node = list;
56         while( node ) {
57                 g_free( node->data );
58                 node->data = NULL;
59                 node = g_slist_next( node );
60         }
61         g_slist_free( list );
62 }
63
64 /*
65 * Free linked list of character strings.
66 */
67 void mgu_free_dlist( GList *list ) {
68         GList *node = list;
69         while( node ) {
70                 g_free( node->data );
71                 node->data = NULL;
72                 node = g_list_next( node );
73         }
74         g_list_free( list );
75 }
76
77 /*
78 * Coalesce linked list of characaters into one long string.
79 */
80 gchar *mgu_list_coalesce( GSList *list ) {
81         gchar *str = NULL;
82         gchar *buf = NULL;
83         gchar *start = NULL;
84         GSList *node = NULL;
85         gint len;
86
87         if( ! list ) return NULL;
88
89         /* Calculate maximum length of text */
90         len = 0;
91         node = list;
92         while( node ) {
93                 str = node->data;
94                 len += 1 + strlen( str );
95                 node = g_slist_next( node );
96         }
97
98         /* Create new buffer. */
99         buf = g_new0( gchar, len+1 );
100         start = buf;
101         node = list;
102         while( node ) {
103                 str = node->data;
104                 len = strlen( str );
105                 strcpy( start, str );
106                 start += len;
107                 node = g_slist_next( node );
108         }
109         return buf;
110 }
111
112 struct mgu_error_entry {
113         gint    e_code;
114         gchar   *e_reason;
115 };
116
117 static const struct mgu_error_entry mgu_error_list[] = {
118         { MGU_SUCCESS,          "Success" },
119         { MGU_BAD_ARGS,         "Bad arguments" },
120         { MGU_NO_FILE,          "File not specified" },
121         { MGU_OPEN_FILE,        "Error opening file" },
122         { MGU_ERROR_READ,       "Error reading file" },
123         { MGU_EOF,              "End of file encountered" },
124         { MGU_OO_MEMORY,        "Error allocating memory" },
125         { MGU_BAD_FORMAT,       "Bad file format" },
126         { MGU_LDAP_CONNECT,     "Error connecting to LDAP server" },
127         { MGU_LDAP_INIT,        "Error initializing LDAP" },
128         { MGU_LDAP_BIND,        "Error binding to LDAP server" },
129         { MGU_LDAP_SEARCH,      "Error searching LDAP database" },
130         { MGU_LDAP_TIMEOUT,     "Timeout performing LDAP operation" },
131         { MGU_LDAP_CRITERIA,    "Error in LDAP search criteria" },
132         { MGU_LDAP_CRITERIA,    "Error in LDAP search criteria" },
133         { MGU_LDAP_NOENTRIES,   "No LDAP entries found for search criteria" },
134         { MGU_ERROR_WRITE,      "Error writing to file" },
135         { MGU_OPEN_DIRECTORY,   "Error opening directory" },
136         { MGU_NO_PATH,          "No path specified" },
137         { -999,                 NULL }
138 };
139
140 static const struct mgu_error_entry *mgu_error_find( gint err ) {
141         gint i;
142         for ( i = 0; mgu_error_list[i].e_code != -999; i++ ) {
143                 if ( err == mgu_error_list[i].e_code )
144                         return & mgu_error_list[i];
145         }
146         return NULL;
147 }
148
149 /*
150 * Return error message for specified error code.
151 */
152 gchar *mgu_error2string( gint err ) {
153         const struct mgu_error_entry *e;
154         e = mgu_error_find( err );
155         return ( e != NULL ) ? e->e_reason : "Unknown error";
156 }
157
158 /*
159 * Replace existing string with new string.
160 */
161 gchar *mgu_replace_string( gchar *str, const gchar *value ) {
162         if( str ) g_free( str );
163         if( value ) {
164                 str = g_strdup( value );
165                 g_strstrip( str );
166         }
167         else {
168                 str = NULL;
169         }
170         return str;
171 }
172
173 /*
174 * Clear a linked list by setting node data pointers to NULL. Note that
175 * items are not freed.
176 */
177 void mgu_clear_slist( GSList *list ) {
178         GSList *node = list;
179         while( node ) {
180                 node->data = NULL;
181                 node = g_slist_next( node );
182         }
183 }
184
185 /*
186 * Clear a linked list by setting node data pointers to NULL. Note that
187 * items are not freed.
188 */
189 void mgu_clear_list( GList *list ) {
190         GList *node = list;
191         while( node ) {
192                 node->data = NULL;
193                 node = g_list_next( node );
194         }
195 }
196
197 /*
198 * Test and reformat an email address.
199 * Enter:  address.
200 * Return: Address, or NULL if address is empty.
201 * Note: Leading and trailing white space is removed.
202 */
203 gchar *mgu_email_check_empty( gchar *address ) {
204         gchar *retVal = NULL;
205         if( address ) {
206                 retVal = g_strdup( address );
207                 retVal = g_strchug( retVal );
208                 retVal = g_strchomp( retVal );
209                 if( *retVal == '\0' ) {
210                         g_free( retVal );
211                         retVal = NULL;
212                 }
213         }
214         return retVal;
215 }
216
217 /*
218 * Parse string into linked list. Whitespace is used as a delimiter in parsing.
219 * Strings are parsed until maxTokens - 1 is reached. The remainder of the
220 * input string is copied into last element of list.
221 * Enter: line      String to parse.
222 *        maxTokens Maximum number of tokens to parse.
223 *        tokenCnt  If arg supplied, update with count of number of token parsed.
224 * Return: Linked list. The list contents should be g_free'd and list should
225 * freed when done.
226 */
227 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
228         gchar *ptr, *pStart, *pFound, *str;
229         gint  args = 0;
230         GList *list = NULL;
231         gboolean done = FALSE;
232
233         if( tokenCnt ) *tokenCnt = 0;
234         if( line == NULL ) return NULL;
235         if( maxTokens < 1 ) return NULL;
236
237         ptr = line;
238         while( ! done ) {
239                 args++;
240                 /* Skip over leading spaces */
241                 while( *ptr ) {
242                         if( ! isspace( *ptr ) ) break;
243                         ptr++;  
244                 }
245
246                 /* Find terminating space */
247                 pFound = NULL;
248                 pStart = ptr;
249                 while( *ptr ) {
250                         if( isspace( *ptr ) ) {
251                                 pFound = pStart;
252                                 break;
253                         }
254                         ptr++;
255                 }
256
257                 if( pFound ) {
258                         if( args == maxTokens ) {
259                                 /* Rest of string */
260                                 str = g_strdup( pStart );
261                                 done = TRUE;
262                         }
263                         else {
264                                 /* Extract part of string */
265                                 str = g_strndup( pStart, ptr - pFound );
266                         }
267                 }
268                 else {
269                         /* Nothing there - treat as rest of string */
270                         str = g_strdup( pStart );
271                         done = TRUE;
272                 }
273                 list = g_list_append( list, str );
274         }
275         if( tokenCnt ) *tokenCnt = args;
276         return list;
277 }
278
279 /*
280 * End of Source.
281 */