2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Definitions for generic functions.
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 );
115 * Replace existing string with new string.
117 gchar *mgu_replace_string( gchar *str, const gchar *value ) {
118 if( str ) g_free( str );
120 str = g_strdup( value );
130 * Clear a linked list by setting node data pointers to NULL. Note that
131 * items are not freed.
133 void mgu_clear_slist( GSList *list ) {
137 node = g_slist_next( node );
142 * Clear a linked list by setting node data pointers to NULL. Note that
143 * items are not freed.
145 void mgu_clear_list( GList *list ) {
149 node = g_list_next( node );
154 * Test and reformat an email address.
156 * Return: Address, or NULL if address is empty.
157 * Note: Leading and trailing white space is removed.
159 gchar *mgu_email_check_empty( gchar *address ) {
160 gchar *retVal = NULL;
162 retVal = g_strdup( address );
163 retVal = g_strchug( retVal );
164 retVal = g_strchomp( retVal );
165 if( *retVal == '\0' ) {
174 * Parse string into linked list. Whitespace is used as a delimiter in parsing.
175 * Strings are parsed until maxTokens - 1 is reached. The remainder of the
176 * input string is copied into last element of list.
177 * Enter: line String to parse.
178 * maxTokens Maximum number of tokens to parse.
179 * tokenCnt If arg supplied, update with count of number of token parsed.
180 * Return: Linked list. The list contents should be g_free'd and list should
183 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
184 gchar *ptr, *pStart, *pFound, *str;
187 gboolean done = FALSE;
189 if( tokenCnt ) *tokenCnt = 0;
190 if( line == NULL ) return NULL;
191 if( maxTokens < 1 ) return NULL;
196 /* Skip over leading spaces */
198 if( ! isspace( *ptr ) ) break;
202 /* Find terminating space */
206 if( isspace( *ptr ) ) {
214 if( args == maxTokens ) {
216 str = g_strdup( pStart );
220 /* Extract part of string */
221 str = g_strndup( pStart, ptr - pFound );
225 /* Nothing there - treat as rest of string */
226 str = g_strdup( pStart );
229 list = g_list_append( list, str );
231 if( tokenCnt ) *tokenCnt = args;
236 * Unescape characters by removing backslash character from input string.
237 * Enter: str String to process.
239 void mgu_str_unescape( gchar *str ) {
246 ilen = strlen( p + 1 );
247 memmove( p, p + 1, ilen );
254 * Replace leading and trailing characters (eg, quotes) in input string
255 * with spaces. Only matching non-blank characters that appear at both
256 * start and end of string are replaces. Control characters are also
257 * replaced with spaces.
258 * Enter: str String to process.
259 * chlea Lead character to remove.
260 * chtail Matching trailing character.
262 void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) {
266 /* Search forwards for first non-space match */
268 ae = -1 + str + strlen( str );
271 if( *as == chlead ) {
272 /* Search backwards from end for match */
275 if( *ae == chtail ) {
283 else if( *ae == 127 ) {
296 else if( *as == 127 ) {
309 * Return reference to longest entry in the specified linked list.
310 * It is assumed that the list contains only gchar objects.
311 * Enter: list List of gchar strings to examine.
312 * Return: Reference to longest entry, or NULL if nothing found.
314 gchar *mgu_slist_longest_entry( GSList *list ) {
317 gint iLen = 0, iLenT = 0;
323 iLen = strlen( name );
326 iLenT = strlen( node->data );
332 node = g_slist_next( node );
338 * Return reference to longest entry in the specified linked list.
339 * It is assumed that the list contains only gchar objects.
340 * Enter: list List of gchar strings to examine.
341 * Return: Reference to longest entry, or NULL if nothing found.
343 gchar *mgu_list_longest_entry( GList *list ) {
346 gint iLen = 0, iLenT = 0;
352 iLen = strlen( name );
355 iLenT = strlen( node->data );
361 node = g_list_next( node );
367 * Test whether string appears in list of strings, ignoring case. NULL or empty
368 * strings will be ignored.
369 * Enter: list List to process.
370 * str String to test.
371 * Return: TRUE if string is unique.
373 gboolean mgu_slist_test_unq_nc( GSList *list, gchar *str ) {
377 if( strlen( str ) > 0 ) {
380 if( g_utf8_collate( str, node->data ) == 0 )
382 node = g_slist_next( node );
391 * Test whether string appears in list of strings, ignoring case. NULL or empty
392 * strings will be ignored.
393 * Enter: list List to process.
394 * str String to test.
395 * Return: TRUE if string is unique.
397 gboolean mgu_list_test_unq_nc( GList *list, gchar *str ) {
401 if( strlen( str ) > 0 ) {
404 if( g_utf8_collate( str, node->data ) == 0 )
406 node = g_list_next( node );