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 * Test and reformat an email address.
139 * Return: Address, or NULL if address is empty.
140 * Note: Leading and trailing white space is removed.
142 gchar *mgu_email_check_empty( gchar *address ) {
143 gchar *retVal = NULL;
145 retVal = g_strdup( address );
146 retVal = g_strstrip( retVal );
147 if( *retVal == '\0' ) {
156 * Parse string into linked list. Whitespace is used as a delimiter in parsing.
157 * Strings are parsed until maxTokens - 1 is reached. The remainder of the
158 * input string is copied into last element of list.
159 * Enter: line String to parse.
160 * maxTokens Maximum number of tokens to parse.
161 * tokenCnt If arg supplied, update with count of number of token parsed.
162 * Return: Linked list. The list contents should be g_free'd and list should
165 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
166 gchar *ptr, *pStart, *pFound, *str;
169 gboolean done = FALSE;
171 if( tokenCnt ) *tokenCnt = 0;
172 if( line == NULL ) return NULL;
173 if( maxTokens < 1 ) return NULL;
178 /* Skip over leading spaces */
180 if( ! isspace( *ptr ) ) break;
184 /* Find terminating space */
188 if( isspace( *ptr ) ) {
196 if( args == maxTokens ) {
198 str = g_strdup( pStart );
202 /* Extract part of string */
203 str = g_strndup( pStart, ptr - pFound );
207 /* Nothing there - treat as rest of string */
208 str = g_strdup( pStart );
211 list = g_list_append( list, str );
213 if( tokenCnt ) *tokenCnt = args;
218 * Unescape characters by removing backslash character from input string.
219 * Enter: str String to process.
221 void mgu_str_unescape( gchar *str ) {
228 ilen = strlen( p + 1 );
229 memmove( p, p + 1, ilen );
236 * Replace leading and trailing characters (eg, quotes) in input string
237 * with spaces. Only matching non-blank characters that appear at both
238 * start and end of string are replaces. Control characters are also
239 * replaced with spaces.
240 * Enter: str String to process.
241 * chlea Lead character to remove.
242 * chtail Matching trailing character.
244 void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) {
248 /* Search forwards for first non-space match */
250 ae = -1 + str + strlen( str );
253 if( *as == chlead ) {
254 /* Search backwards from end for match */
257 if( *ae == chtail ) {
265 else if( *ae == 127 ) {
278 else if( *as == 127 ) {
291 * Return reference to longest entry in the specified linked list.
292 * It is assumed that the list contains only gchar objects.
293 * Enter: list List of gchar strings to examine.
294 * Return: Reference to longest entry, or NULL if nothing found.
296 gchar *mgu_slist_longest_entry( GSList *list ) {
299 gint iLen = 0, iLenT = 0;
305 iLen = strlen( name );
308 iLenT = strlen( node->data );
314 node = g_slist_next( node );
320 * Test whether string appears in list of strings, ignoring case. NULL or empty
321 * strings will be ignored.
322 * Enter: list List to process.
323 * str String to test.
324 * Return: TRUE if string is unique.
326 gboolean mgu_slist_test_unq_nc( GSList *list, gchar *str ) {
330 if( strlen( str ) > 0 ) {
333 if( g_utf8_collate( str, node->data ) == 0 )
335 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_list_test_unq_nc( GList *list, gchar *str ) {
354 if( strlen( str ) > 0 ) {
357 if( g_utf8_collate( str, node->data ) == 0 )
359 node = g_list_next( node );