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.
33 * Dump linked list of character strings (for debug).
35 void mgu_print_list( GSList
*list
, FILE *stream
) {
38 int r
= fprintf( stream
, "\t- >%s<\n", (gchar
*)node
->data
);
43 node
= g_slist_next( node
);
48 * Dump linked list of character strings (for debug).
50 void mgu_print_dlist( GList
*list
, FILE *stream
) {
53 int r
= fprintf( stream
, "\t- >%s<\n", (gchar
*)node
->data
);
58 node
= g_list_next( node
);
63 * Coalesce linked list of characaters into one long string.
65 gchar
*mgu_list_coalesce( GSList
*list
) {
72 if( ! list
) return NULL
;
74 /* Calculate maximum length of text */
79 len
+= 1 + strlen( str
);
80 node
= g_slist_next( node
);
83 /* Create new buffer. */
84 buf
= g_new0( gchar
, len
+1 );
92 node
= g_slist_next( node
);
98 * Replace existing string with new string.
100 gchar
*mgu_replace_string( gchar
*str
, const gchar
*value
) {
103 str
= g_strdup( value
);
113 * Test and reformat an email address.
115 * Return: Address, or NULL if address is empty.
116 * Note: Leading and trailing white space is removed.
118 gchar
*mgu_email_check_empty( gchar
*address
) {
119 gchar
*retVal
= NULL
;
121 retVal
= g_strdup( address
);
122 retVal
= g_strstrip( retVal
);
123 if( *retVal
== '\0' ) {
132 * Parse string into linked list. Whitespace is used as a delimiter in parsing.
133 * Strings are parsed until maxTokens - 1 is reached. The remainder of the
134 * input string is copied into last element of list.
135 * Enter: line String to parse.
136 * maxTokens Maximum number of tokens to parse.
137 * tokenCnt If arg supplied, update with count of number of token parsed.
138 * Return: Linked list. The list contents should be g_free'd and list should
141 GList
*mgu_parse_string( gchar
*line
, const gint maxTokens
, gint
*tokenCnt
) {
142 gchar
*ptr
, *pStart
, *pFound
, *str
;
145 gboolean done
= FALSE
;
147 if( tokenCnt
) *tokenCnt
= 0;
148 if( line
== NULL
) return NULL
;
149 if( maxTokens
< 1 ) return NULL
;
154 /* Skip over leading spaces */
156 if( ! isspace( *ptr
) ) break;
160 /* Find terminating space */
164 if( isspace( *ptr
) ) {
172 if( args
== maxTokens
) {
174 str
= g_strdup( pStart
);
178 /* Extract part of string */
179 str
= g_strndup( pStart
, ptr
- pFound
);
183 /* Nothing there - treat as rest of string */
184 str
= g_strdup( pStart
);
187 list
= g_list_append( list
, str
);
189 if( tokenCnt
) *tokenCnt
= args
;
194 * Unescape characters by removing backslash character from input string.
195 * Enter: str String to process.
197 void mgu_str_unescape( gchar
*str
) {
204 ilen
= strlen( p
+ 1 );
205 memmove( p
, p
+ 1, ilen
);
212 * Replace leading and trailing characters (eg, quotes) in input string
213 * with spaces. Only matching non-blank characters that appear at both
214 * start and end of string are replaces. Control characters are also
215 * replaced with spaces.
216 * Enter: str String to process.
217 * chlea Lead character to remove.
218 * chtail Matching trailing character.
220 void mgu_str_ltc2space( gchar
*str
, gchar chlead
, gchar chtail
) {
224 /* Search forwards for first non-space match */
226 ae
= -1 + str
+ strlen( str
);
229 if( *as
== chlead
) {
230 /* Search backwards from end for match */
233 if( *ae
== chtail
) {
241 else if( *ae
== 127 ) {
254 else if( *as
== 127 ) {
267 * Return reference to longest entry in the specified linked list.
268 * It is assumed that the list contains only gchar objects.
269 * Enter: list List of gchar strings to examine.
270 * Return: Reference to longest entry, or NULL if nothing found.
272 gchar
*mgu_slist_longest_entry( GSList
*list
) {
275 gint iLen
= 0, iLenT
= 0;
281 iLen
= strlen( name
);
284 iLenT
= strlen( node
->data
);
290 node
= g_slist_next( node
);