2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2015 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/>.
20 * Definitions for generic functions.
31 * Dump linked list of character strings (for debug).
33 void mgu_print_list( GSList *list, FILE *stream ) {
36 int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
41 node = g_slist_next( node );
46 * Dump linked list of character strings (for debug).
48 void mgu_print_dlist( GList *list, FILE *stream ) {
51 int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
56 node = g_list_next( node );
61 * Free linked list of character strings.
63 void mgu_free_list( GSList *list ) {
68 node = g_slist_next( node );
74 * Free linked list of character strings.
76 void mgu_free_dlist( GList *list ) {
81 node = g_list_next( node );
87 * Coalesce linked list of characaters into one long string.
89 gchar *mgu_list_coalesce( GSList *list ) {
96 if( ! list ) return NULL;
98 /* Calculate maximum length of text */
103 len += 1 + strlen( str );
104 node = g_slist_next( node );
107 /* Create new buffer. */
108 buf = g_new0( gchar, len+1 );
114 strcpy( start, str );
116 node = g_slist_next( node );
122 * Replace existing string with new string.
124 gchar *mgu_replace_string( gchar *str, const gchar *value ) {
127 str = g_strdup( value );
137 * Clear a linked list by setting node data pointers to NULL. Note that
138 * items are not freed.
140 void mgu_clear_slist( GSList *list ) {
144 node = g_slist_next( node );
149 * Clear a linked list by setting node data pointers to NULL. Note that
150 * items are not freed.
152 void mgu_clear_list( GList *list ) {
156 node = g_list_next( node );
161 * Test and reformat an email address.
163 * Return: Address, or NULL if address is empty.
164 * Note: Leading and trailing white space is removed.
166 gchar *mgu_email_check_empty( gchar *address ) {
167 gchar *retVal = NULL;
169 retVal = g_strdup( address );
170 retVal = g_strstrip( retVal );
171 if( *retVal == '\0' ) {
180 * Parse string into linked list. Whitespace is used as a delimiter in parsing.
181 * Strings are parsed until maxTokens - 1 is reached. The remainder of the
182 * input string is copied into last element of list.
183 * Enter: line String to parse.
184 * maxTokens Maximum number of tokens to parse.
185 * tokenCnt If arg supplied, update with count of number of token parsed.
186 * Return: Linked list. The list contents should be g_free'd and list should
189 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
190 gchar *ptr, *pStart, *pFound, *str;
193 gboolean done = FALSE;
195 if( tokenCnt ) *tokenCnt = 0;
196 if( line == NULL ) return NULL;
197 if( maxTokens < 1 ) return NULL;
202 /* Skip over leading spaces */
204 if( ! isspace( *ptr ) ) break;
208 /* Find terminating space */
212 if( isspace( *ptr ) ) {
220 if( args == maxTokens ) {
222 str = g_strdup( pStart );
226 /* Extract part of string */
227 str = g_strndup( pStart, ptr - pFound );
231 /* Nothing there - treat as rest of string */
232 str = g_strdup( pStart );
235 list = g_list_append( list, str );
237 if( tokenCnt ) *tokenCnt = args;
242 * Unescape characters by removing backslash character from input string.
243 * Enter: str String to process.
245 void mgu_str_unescape( gchar *str ) {
252 ilen = strlen( p + 1 );
253 memmove( p, p + 1, ilen );
260 * Replace leading and trailing characters (eg, quotes) in input string
261 * with spaces. Only matching non-blank characters that appear at both
262 * start and end of string are replaces. Control characters are also
263 * replaced with spaces.
264 * Enter: str String to process.
265 * chlea Lead character to remove.
266 * chtail Matching trailing character.
268 void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) {
272 /* Search forwards for first non-space match */
274 ae = -1 + str + strlen( str );
277 if( *as == chlead ) {
278 /* Search backwards from end for match */
281 if( *ae == chtail ) {
289 else if( *ae == 127 ) {
302 else if( *as == 127 ) {
315 * Return reference to longest entry in the specified linked list.
316 * It is assumed that the list contains only gchar objects.
317 * Enter: list List of gchar strings to examine.
318 * Return: Reference to longest entry, or NULL if nothing found.
320 gchar *mgu_slist_longest_entry( GSList *list ) {
323 gint iLen = 0, iLenT = 0;
329 iLen = strlen( name );
332 iLenT = strlen( node->data );
338 node = g_slist_next( node );
344 * Test whether string appears in list of strings, ignoring case. NULL or empty
345 * strings will be ignored.
346 * Enter: list List to process.
347 * str String to test.
348 * Return: TRUE if string is unique.
350 gboolean mgu_slist_test_unq_nc( GSList *list, gchar *str ) {
354 if( strlen( str ) > 0 ) {
357 if( g_utf8_collate( str, node->data ) == 0 )
359 node = g_slist_next( node );
368 * Test whether string appears in list of strings, ignoring case. NULL or empty
369 * strings will be ignored.
370 * Enter: list List to process.
371 * str String to test.
372 * Return: TRUE if string is unique.
374 gboolean mgu_list_test_unq_nc( GList *list, gchar *str ) {
378 if( strlen( str ) > 0 ) {
381 if( g_utf8_collate( str, node->data ) == 0 )
383 node = g_list_next( node );