2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Match Grun
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.
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.
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.
21 * General functions for create common address book entries.
32 * Dump linked list of character strings (for debug).
34 void mgu_print_list( GSList *list, FILE *stream ) {
37 fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
38 node = g_slist_next( node );
43 * Dump linked list of character strings (for debug).
45 void mgu_print_dlist( GList *list, FILE *stream ) {
48 fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
49 node = g_list_next( node );
54 * Free linked list of character strings.
56 void mgu_free_list( GSList *list ) {
61 node = g_slist_next( node );
67 * Free linked list of character strings.
69 void mgu_free_dlist( GList *list ) {
74 node = g_list_next( node );
80 * Coalesce linked list of characaters into one long string.
82 gchar *mgu_list_coalesce( GSList *list ) {
89 if( ! list ) return NULL;
91 /* Calculate maximum length of text */
96 len += 1 + strlen( str );
97 node = g_slist_next( node );
100 /* Create new buffer. */
101 buf = g_new0( gchar, len+1 );
107 strcpy( start, str );
109 node = g_slist_next( node );
114 struct mgu_error_entry {
119 static const struct mgu_error_entry mgu_error_list[] = {
120 { MGU_SUCCESS, "Success" },
121 { MGU_BAD_ARGS, "Bad arguments" },
122 { MGU_NO_FILE, "File not specified" },
123 { MGU_OPEN_FILE, "Error opening file" },
124 { MGU_ERROR_READ, "Error reading file" },
125 { MGU_EOF, "End of file encountered" },
126 { MGU_OO_MEMORY, "Error allocating memory" },
127 { MGU_BAD_FORMAT, "Bad file format" },
128 { MGU_LDAP_CONNECT, "Error connecting to LDAP server" },
129 { MGU_LDAP_INIT, "Error initializing LDAP" },
130 { MGU_LDAP_BIND, "Error binding to LDAP server" },
131 { MGU_LDAP_SEARCH, "Error searching LDAP database" },
132 { MGU_LDAP_TIMEOUT, "Timeout performing LDAP operation" },
133 { MGU_LDAP_CRITERIA, "Error in LDAP search criteria" },
134 { MGU_LDAP_CRITERIA, "Error in LDAP search criteria" },
135 { MGU_LDAP_NOENTRIES, "No LDAP entries found for search criteria" },
136 { MGU_ERROR_WRITE, "Error writing to file" },
137 { MGU_OPEN_DIRECTORY, "Error opening directory" },
138 { MGU_NO_PATH, "No path specified" },
142 static const struct mgu_error_entry *mgu_error_find( gint err ) {
144 for ( i = 0; mgu_error_list[i].e_code != -999; i++ ) {
145 if ( err == mgu_error_list[i].e_code )
146 return & mgu_error_list[i];
152 * Return error message for specified error code.
154 gchar *mgu_error2string( gint err ) {
155 const struct mgu_error_entry *e;
156 e = mgu_error_find( err );
157 return ( e != NULL ) ? e->e_reason : "Unknown error";
161 * Replace existing string with new string.
163 gchar *mgu_replace_string( gchar *str, const gchar *value ) {
164 if( str ) g_free( str );
166 str = g_strdup( value );
176 * Clear a linked list by setting node data pointers to NULL. Note that
177 * items are not freed.
179 void mgu_clear_slist( GSList *list ) {
183 node = g_slist_next( node );
188 * Clear a linked list by setting node data pointers to NULL. Note that
189 * items are not freed.
191 void mgu_clear_list( GList *list ) {
195 node = g_list_next( node );
200 * Test and reformat an email address.
202 * Return: Address, or NULL if address is empty.
203 * Note: Leading and trailing white space is removed.
205 gchar *mgu_email_check_empty( gchar *address ) {
206 gchar *retVal = NULL;
208 retVal = g_strdup( address );
209 retVal = g_strchug( retVal );
210 retVal = g_strchomp( retVal );
211 if( *retVal == '\0' ) {
220 * Parse string into linked list. Whitespace is used as a delimiter in parsing.
221 * Strings are parsed until maxTokens - 1 is reached. The remainder of the
222 * input string is copied into last element of list.
223 * Enter: line String to parse.
224 * maxTokens Maximum number of tokens to parse.
225 * tokenCnt If arg supplied, update with count of number of token parsed.
226 * Return: Linked list. The list contents should be g_free'd and list should
229 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
230 gchar *ptr, *pStart, *pFound, *str;
233 gboolean done = FALSE;
235 if( tokenCnt ) *tokenCnt = 0;
236 if( line == NULL ) return NULL;
237 if( maxTokens < 1 ) return NULL;
242 /* Skip over leading spaces */
244 if( ! isspace( *ptr ) ) break;
248 /* Find terminating space */
252 if( isspace( *ptr ) ) {
260 if( args == maxTokens ) {
262 str = g_strdup( pStart );
266 /* Extract part of string */
267 str = g_strndup( pStart, ptr - pFound );
271 /* Nothing there - treat as rest of string */
272 str = g_strdup( pStart );
275 list = g_list_append( list, str );
277 if( tokenCnt ) *tokenCnt = args;
282 * Unescape characters by removing backslash character from input string.
283 * Enter: str String to process.
285 void mgu_str_unescape( gchar *str ) {
292 ilen = strlen( p + 1 );
293 memmove( p, p + 1, ilen );
300 * Replace leading and trailing characters (eg, quotes) in input string
301 * with spaces. Only matching non-blank characters that appear at both
302 * start and end of string are replaces. Control characters are also
303 * replaced with spaces.
304 * Enter: str String to process.
305 * chlea Lead character to remove.
306 * chtail Matching trailing character.
308 void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) {
312 /* Search forwards for first non-space match */
314 ae = -1 + str + strlen( str );
317 if( *as == chlead ) {
318 /* Search backwards from end for match */
321 if( *ae == chtail ) {
329 else if( *ae == 127 ) {
342 else if( *as == 127 ) {