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.
31 * Dump linked list of character strings (for debug).
33 void mgu_print_list( GSList *list, FILE *stream ) {
36 fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
37 node = g_slist_next( node );
42 * Dump linked list of character strings (for debug).
44 void mgu_print_dlist( GList *list, FILE *stream ) {
47 fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
48 node = g_list_next( node );
53 * Free linked list of character strings.
55 void mgu_free_list( GSList *list ) {
60 node = g_slist_next( node );
66 * Free linked list of character strings.
68 void mgu_free_dlist( GList *list ) {
73 node = g_list_next( node );
79 * Coalesce linked list of characaters into one long string.
81 gchar *mgu_list_coalesce( GSList *list ) {
88 if( ! list ) return NULL;
90 /* Calculate maximum length of text */
95 len += 1 + strlen( str );
96 node = g_slist_next( node );
99 /* Create new buffer. */
100 buf = g_new0( gchar, len+1 );
106 strcpy( start, str );
108 node = g_slist_next( node );
113 struct mgu_error_entry {
118 static const struct mgu_error_entry mgu_error_list[] = {
119 { MGU_SUCCESS, "Success" },
120 { MGU_BAD_ARGS, "Bad arguments" },
121 { MGU_NO_FILE, "File not specified" },
122 { MGU_OPEN_FILE, "Error opening file" },
123 { MGU_ERROR_READ, "Error reading file" },
124 { MGU_EOF, "End of file encountered" },
125 { MGU_OO_MEMORY, "Error allocating memory" },
126 { MGU_BAD_FORMAT, "Bad file format" },
127 { MGU_LDAP_CONNECT, "Error connecting to LDAP server" },
128 { MGU_LDAP_INIT, "Error initializing LDAP" },
129 { MGU_LDAP_BIND, "Error binding to LDAP server" },
130 { MGU_LDAP_SEARCH, "Error searching LDAP database" },
131 { MGU_LDAP_TIMEOUT, "Timeout performing LDAP operation" },
132 { MGU_LDAP_CRITERIA, "Error in LDAP search criteria" },
133 { MGU_LDAP_CRITERIA, "Error in LDAP search criteria" },
134 { MGU_LDAP_NOENTRIES, "No LDAP entries found for search criteria" },
135 { MGU_ERROR_WRITE, "Error writing to file" },
136 { MGU_OPEN_DIRECTORY, "Error opening directory" },
137 { MGU_NO_PATH, "No path specified" },
141 static const struct mgu_error_entry *mgu_error_find( gint err ) {
143 for ( i = 0; mgu_error_list[i].e_code != -999; i++ ) {
144 if ( err == mgu_error_list[i].e_code )
145 return & mgu_error_list[i];
151 * Return error message for specified error code.
153 gchar *mgu_error2string( gint err ) {
154 const struct mgu_error_entry *e;
155 e = mgu_error_find( err );
156 return ( e != NULL ) ? e->e_reason : "Unknown error";
160 * Replace existing string with new string.
162 gchar *mgu_replace_string( gchar *str, const gchar *value ) {
163 if( str ) g_free( str );
165 str = g_strdup( value );
175 * Clear a linked list by setting node data pointers to NULL. Note that
176 * items are not freed.
178 void mgu_clear_slist( GSList *list ) {
182 node = g_slist_next( node );
187 * Clear a linked list by setting node data pointers to NULL. Note that
188 * items are not freed.
190 void mgu_clear_list( GList *list ) {
194 node = g_list_next( node );
199 * Test and reformat an email address.
201 * Return: Address, or NULL if address is empty.
202 * Note: Leading and trailing white space is removed.
204 gchar *mgu_email_check_empty( gchar *address ) {
205 gchar *retVal = NULL;
207 retVal = g_strdup( address );
208 retVal = g_strchug( retVal );
209 retVal = g_strchomp( retVal );
210 if( *retVal == '\0' ) {
219 * Parse string into linked list. Whitespace is used as a delimiter in parsing.
220 * Strings are parsed until maxTokens - 1 is reached. The remainder of the
221 * input string is copied into last element of list.
222 * Enter: line String to parse.
223 * maxTokens Maximum number of tokens to parse.
224 * tokenCnt If arg supplied, update with count of number of token parsed.
225 * Return: Linked list. The list contents should be g_free'd and list should
228 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
229 gchar *ptr, *pStart, *pFound, *str;
232 gboolean done = FALSE;
234 if( tokenCnt ) *tokenCnt = 0;
235 if( line == NULL ) return NULL;
236 if( maxTokens < 1 ) return NULL;
241 /* Skip over leading spaces */
243 if( ! isspace( *ptr ) ) break;
247 /* Find terminating space */
251 if( isspace( *ptr ) ) {
259 if( args == maxTokens ) {
261 str = g_strdup( pStart );
265 /* Extract part of string */
266 str = g_strndup( pStart, ptr - pFound );
270 /* Nothing there - treat as rest of string */
271 str = g_strdup( pStart );
274 list = g_list_append( list, str );
276 if( tokenCnt ) *tokenCnt = args;
281 * Unescape characters by removing backslash character from input string.
282 * Enter: str String to process.
284 void mgu_str_unescape( gchar *str ) {
291 ilen = strlen( p + 1 );
292 memmove( p, p + 1, ilen );
299 * Replace leading and trailing characters (eg, quotes) in input string
300 * with spaces. Only matching non-blank characters that appear at both
301 * start and end of string are replaces. Control characters are also
302 * replaced with spaces.
303 * Enter: str String to process.
304 * chlea Lead character to remove.
305 * chtail Matching trailing character.
307 void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) {
311 /* Search forwards for first non-space match */
313 ae = -1 + str + strlen( str );
316 if( *as == chlead ) {
317 /* Search backwards from end for match */
320 if( *ae == chtail ) {
328 else if( *ae == 127 ) {
341 else if( *as == 127 ) {