2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2009 Match Grun and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
21 * Definitions for generic functions.
32 * Dump linked list of character strings (for debug).
34 void mgu_print_list( GSList *list, FILE *stream ) {
37 int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
42 node = g_slist_next( node );
47 * Dump linked list of character strings (for debug).
49 void mgu_print_dlist( GList *list, FILE *stream ) {
52 int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
57 node = g_list_next( node );
62 * Free linked list of character strings.
64 void mgu_free_list( GSList *list ) {
69 node = g_slist_next( node );
75 * Free linked list of character strings.
77 void mgu_free_dlist( GList *list ) {
82 node = g_list_next( node );
88 * Coalesce linked list of characaters into one long string.
90 gchar *mgu_list_coalesce( GSList *list ) {
97 if( ! list ) return NULL;
99 /* Calculate maximum length of text */
104 len += 1 + strlen( str );
105 node = g_slist_next( node );
108 /* Create new buffer. */
109 buf = g_new0( gchar, len+1 );
115 strcpy( start, str );
117 node = g_slist_next( node );
123 * Replace existing string with new string.
125 gchar *mgu_replace_string( gchar *str, const gchar *value ) {
128 str = g_strdup( value );
138 * Clear a linked list by setting node data pointers to NULL. Note that
139 * items are not freed.
141 void mgu_clear_slist( GSList *list ) {
145 node = g_slist_next( node );
150 * Clear a linked list by setting node data pointers to NULL. Note that
151 * items are not freed.
153 void mgu_clear_list( GList *list ) {
157 node = g_list_next( node );
162 * Test and reformat an email address.
164 * Return: Address, or NULL if address is empty.
165 * Note: Leading and trailing white space is removed.
167 gchar *mgu_email_check_empty( gchar *address ) {
168 gchar *retVal = NULL;
170 retVal = g_strdup( address );
171 retVal = g_strchug( retVal );
172 retVal = g_strchomp( retVal );
173 if( *retVal == '\0' ) {
182 * Parse string into linked list. Whitespace is used as a delimiter in parsing.
183 * Strings are parsed until maxTokens - 1 is reached. The remainder of the
184 * input string is copied into last element of list.
185 * Enter: line String to parse.
186 * maxTokens Maximum number of tokens to parse.
187 * tokenCnt If arg supplied, update with count of number of token parsed.
188 * Return: Linked list. The list contents should be g_free'd and list should
191 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
192 gchar *ptr, *pStart, *pFound, *str;
195 gboolean done = FALSE;
197 if( tokenCnt ) *tokenCnt = 0;
198 if( line == NULL ) return NULL;
199 if( maxTokens < 1 ) return NULL;
204 /* Skip over leading spaces */
206 if( ! isspace( *ptr ) ) break;
210 /* Find terminating space */
214 if( isspace( *ptr ) ) {
222 if( args == maxTokens ) {
224 str = g_strdup( pStart );
228 /* Extract part of string */
229 str = g_strndup( pStart, ptr - pFound );
233 /* Nothing there - treat as rest of string */
234 str = g_strdup( pStart );
237 list = g_list_append( list, str );
239 if( tokenCnt ) *tokenCnt = args;
244 * Unescape characters by removing backslash character from input string.
245 * Enter: str String to process.
247 void mgu_str_unescape( gchar *str ) {
254 ilen = strlen( p + 1 );
255 memmove( p, p + 1, ilen );
262 * Replace leading and trailing characters (eg, quotes) in input string
263 * with spaces. Only matching non-blank characters that appear at both
264 * start and end of string are replaces. Control characters are also
265 * replaced with spaces.
266 * Enter: str String to process.
267 * chlea Lead character to remove.
268 * chtail Matching trailing character.
270 void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) {
274 /* Search forwards for first non-space match */
276 ae = -1 + str + strlen( str );
279 if( *as == chlead ) {
280 /* Search backwards from end for match */
283 if( *ae == chtail ) {
291 else if( *ae == 127 ) {
304 else if( *as == 127 ) {
317 * Return reference to longest entry in the specified linked list.
318 * It is assumed that the list contains only gchar objects.
319 * Enter: list List of gchar strings to examine.
320 * Return: Reference to longest entry, or NULL if nothing found.
322 gchar *mgu_slist_longest_entry( GSList *list ) {
325 gint iLen = 0, iLenT = 0;
331 iLen = strlen( name );
334 iLenT = strlen( node->data );
340 node = g_slist_next( node );
346 * Test whether string appears in list of strings, ignoring case. NULL or empty
347 * strings will be ignored.
348 * Enter: list List to process.
349 * str String to test.
350 * Return: TRUE if string is unique.
352 gboolean mgu_slist_test_unq_nc( GSList *list, gchar *str ) {
356 if( strlen( str ) > 0 ) {
359 if( g_utf8_collate( str, node->data ) == 0 )
361 node = g_slist_next( node );
370 * Test whether string appears in list of strings, ignoring case. NULL or empty
371 * strings will be ignored.
372 * Enter: list List to process.
373 * str String to test.
374 * Return: TRUE if string is unique.
376 gboolean mgu_list_test_unq_nc( GList *list, gchar *str ) {
380 if( strlen( str ) > 0 ) {
383 if( g_utf8_collate( str, node->data ) == 0 )
385 node = g_list_next( node );