cbb5761d58bec783516cdc5dc26f3795c01de6b0
[claws.git] / src / common / mgutils.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2015 Match Grun and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 /*
20  * Definitions for generic functions.
21  */
22
23 #include <glib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27
28 #include "mgutils.h"
29
30 /*
31 * Dump linked list of character strings (for debug).
32 */
33 void mgu_print_list( GSList *list, FILE *stream ) {
34         GSList *node = list;
35         while( node ) {
36                 int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
37                 if (r < 0) {
38                         perror("fprintf");
39                         break;
40                 }
41                 node = g_slist_next( node );
42         }
43 }
44
45 /*
46 * Dump linked list of character strings (for debug).
47 */
48 void mgu_print_dlist( GList *list, FILE *stream ) {
49         GList *node = list;
50         while( node ) {
51                 int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
52                 if (r < 0) {
53                         perror("fprintf");
54                         break;
55                 }
56                 node = g_list_next( node );
57         }
58 }
59
60 /*
61 * Free linked list of character strings.
62 */
63 void mgu_free_list( GSList *list ) {
64         GSList *node = list;
65         while( node ) {
66                 g_free( node->data );
67                 node->data = NULL;
68                 node = g_slist_next( node );
69         }
70         g_slist_free( list );
71 }
72
73 /*
74 * Free linked list of character strings.
75 */
76 void mgu_free_dlist( GList *list ) {
77         GList *node = list;
78         while( node ) {
79                 g_free( node->data );
80                 node->data = NULL;
81                 node = g_list_next( node );
82         }
83         g_list_free( list );
84 }
85
86 /*
87 * Coalesce linked list of characaters into one long string.
88 */
89 gchar *mgu_list_coalesce( GSList *list ) {
90         gchar *str = NULL;
91         gchar *buf = NULL;
92         gchar *start = NULL;
93         GSList *node = NULL;
94         gint len;
95
96         if( ! list ) return NULL;
97
98         /* Calculate maximum length of text */
99         len = 0;
100         node = list;
101         while( node ) {
102                 str = node->data;
103                 len += 1 + strlen( str );
104                 node = g_slist_next( node );
105         }
106
107         /* Create new buffer. */
108         buf = g_new0( gchar, len+1 );
109         start = buf;
110         node = list;
111         while( node ) {
112                 str = node->data;
113                 len = strlen( str );
114                 strcpy( start, str );
115                 start += len;
116                 node = g_slist_next( node );
117         }
118         return buf;
119 }
120
121 /*
122 * Replace existing string with new string.
123 */
124 gchar *mgu_replace_string( gchar *str, const gchar *value ) {
125         g_free( str );
126         if( value ) {
127                 str = g_strdup( value );
128                 g_strstrip( str );
129         }
130         else {
131                 str = NULL;
132         }
133         return str;
134 }
135
136 /*
137 * Clear a linked list by setting node data pointers to NULL. Note that
138 * items are not freed.
139 */
140 void mgu_clear_slist( GSList *list ) {
141         GSList *node = list;
142         while( node ) {
143                 node->data = NULL;
144                 node = g_slist_next( node );
145         }
146 }
147
148 /*
149 * Clear a linked list by setting node data pointers to NULL. Note that
150 * items are not freed.
151 */
152 void mgu_clear_list( GList *list ) {
153         GList *node = list;
154         while( node ) {
155                 node->data = NULL;
156                 node = g_list_next( node );
157         }
158 }
159
160 /*
161 * Test and reformat an email address.
162 * Enter:  address.
163 * Return: Address, or NULL if address is empty.
164 * Note: Leading and trailing white space is removed.
165 */
166 gchar *mgu_email_check_empty( gchar *address ) {
167         gchar *retVal = NULL;
168         if( address ) {
169                 retVal = g_strdup( address );
170                 retVal = g_strstrip( retVal );
171                 if( *retVal == '\0' ) {
172                         g_free( retVal );
173                         retVal = NULL;
174                 }
175         }
176         return retVal;
177 }
178
179 /*
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
187 * freed when done.
188 */
189 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
190         gchar *ptr, *pStart, *pFound, *str;
191         gint  args = 0;
192         GList *list = NULL;
193         gboolean done = FALSE;
194
195         if( tokenCnt ) *tokenCnt = 0;
196         if( line == NULL ) return NULL;
197         if( maxTokens < 1 ) return NULL;
198
199         ptr = line;
200         while( ! done ) {
201                 args++;
202                 /* Skip over leading spaces */
203                 while( *ptr ) {
204                         if( ! isspace( *ptr ) ) break;
205                         ptr++;  
206                 }
207
208                 /* Find terminating space */
209                 pFound = NULL;
210                 pStart = ptr;
211                 while( *ptr ) {
212                         if( isspace( *ptr ) ) {
213                                 pFound = pStart;
214                                 break;
215                         }
216                         ptr++;
217                 }
218
219                 if( pFound ) {
220                         if( args == maxTokens ) {
221                                 /* Rest of string */
222                                 str = g_strdup( pStart );
223                                 done = TRUE;
224                         }
225                         else {
226                                 /* Extract part of string */
227                                 str = g_strndup( pStart, ptr - pFound );
228                         }
229                 }
230                 else {
231                         /* Nothing there - treat as rest of string */
232                         str = g_strdup( pStart );
233                         done = TRUE;
234                 }
235                 list = g_list_append( list, str );
236         }
237         if( tokenCnt ) *tokenCnt = args;
238         return list;
239 }
240
241 /*
242  * Unescape characters by removing backslash character from input string.
243  * Enter: str String to process.
244  */
245 void mgu_str_unescape( gchar *str ) {
246         gchar *p;
247         gint ilen;
248
249         p = str;
250         while( *p ) {
251                 if( *p == '\\' ) {
252                         ilen = strlen( p + 1 );
253                         memmove( p, p + 1, ilen );
254                 }
255                 p++;
256         }
257 }
258
259 /*
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.
267  */
268 void mgu_str_ltc2space( gchar *str, gchar chlead, gchar chtail ) {
269         gchar *as;
270         gchar *ae;
271
272         /* Search forwards for first non-space match */
273         as = str;
274         ae = -1 + str + strlen( str );
275         while( as < ae ) {
276                 if( *as != ' ' ) {
277                         if( *as == chlead ) {
278                                 /* Search backwards from end for match */
279                                 while( ae > as ) {
280                                         if( *ae != ' ' ) {
281                                                 if( *ae == chtail ) {
282                                                         *as = ' ';
283                                                         *ae = ' ';
284                                                         return;
285                                                 }
286                                                 if( *ae < 32 ) {
287                                                         *ae = ' ';
288                                                 }
289                                                 else if( *ae == 127 ) {
290                                                         *ae = ' ';
291                                                 }
292                                                 else {
293                                                         return;
294                                                 }
295                                         }
296                                         ae--;
297                                 }
298                         }
299                         if( *as < 32 ) {
300                                 *as = ' ';
301                         }
302                         else if( *as == 127 ) {
303                                 *as = ' ';
304                         }
305                         else {
306                                 return;
307                         }
308                 }
309                 as++;
310         }
311         return;
312 }
313
314 /*
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.
319  */
320 gchar *mgu_slist_longest_entry( GSList *list ) {
321         GSList *node;
322         gchar *name = NULL;
323         gint iLen = 0, iLenT = 0;
324
325         node = list;
326         while( node ) {
327                 if( name == NULL ) {
328                         name = node->data;
329                         iLen = strlen( name );
330                 }
331                 else {
332                         iLenT = strlen( node->data );
333                         if( iLenT > iLen ) {
334                                 name = node->data;
335                                 iLen = iLenT;
336                         }
337                 }
338                 node = g_slist_next( node );
339         }
340         return name;
341 }       
342
343 /*
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.
349  */
350 gboolean mgu_slist_test_unq_nc( GSList *list, gchar *str ) {
351         GSList *node;
352
353         if( str ) {
354                 if( strlen( str ) > 0 ) {
355                         node = list;
356                         while( node ) {
357                                 if( g_utf8_collate( str, node->data ) == 0 )
358                                         return FALSE;
359                                 node = g_slist_next( node );
360                         }
361                         return TRUE;
362                 }
363         }
364         return FALSE;
365 }
366
367 /*
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.
373  */
374 gboolean mgu_list_test_unq_nc( GList *list, gchar *str ) {
375         GList *node;
376
377         if( str ) {
378                 if( strlen( str ) > 0 ) {
379                         node = list;
380                         while( node ) {
381                                 if( g_utf8_collate( str, node->data ) == 0 )
382                                         return FALSE;
383                                 node = g_list_next( node );
384                         }
385                         return TRUE;
386                 }
387         }
388         return FALSE;
389 }
390
391 /*
392 * End of Source.
393 */