add support for Ayatana indicator to Notification plugin
[claws.git] / src / common / mgutils.c
blob503be7aa013728e251248dba07e575f42a059087
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
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.
23 #include "config.h"
25 #include <glib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <ctype.h>
30 #include "mgutils.h"
33 * Dump linked list of character strings (for debug).
35 void mgu_print_list( GSList *list, FILE *stream ) {
36 GSList *node = list;
37 while( node ) {
38 int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
39 if (r < 0) {
40 perror("fprintf");
41 break;
43 node = g_slist_next( node );
48 * Dump linked list of character strings (for debug).
50 void mgu_print_dlist( GList *list, FILE *stream ) {
51 GList *node = list;
52 while( node ) {
53 int r = fprintf( stream, "\t- >%s<\n", (gchar *)node->data );
54 if (r < 0) {
55 perror("fprintf");
56 break;
58 node = g_list_next( node );
63 * Coalesce linked list of characaters into one long string.
65 gchar *mgu_list_coalesce( GSList *list ) {
66 gchar *str = NULL;
67 gchar *buf = NULL;
68 gchar *start = NULL;
69 GSList *node = NULL;
70 gint len;
72 if( ! list ) return NULL;
74 /* Calculate maximum length of text */
75 len = 0;
76 node = list;
77 while( node ) {
78 str = node->data;
79 len += 1 + strlen( str );
80 node = g_slist_next( node );
83 /* Create new buffer. */
84 buf = g_new0( gchar, len+1 );
85 start = buf;
86 node = list;
87 while( node ) {
88 str = node->data;
89 len = strlen( str );
90 strcpy( start, str );
91 start += len;
92 node = g_slist_next( node );
94 return buf;
98 * Replace existing string with new string.
100 gchar *mgu_replace_string( gchar *str, const gchar *value ) {
101 g_free( str );
102 if( value ) {
103 str = g_strdup( value );
104 g_strstrip( str );
106 else {
107 str = NULL;
109 return str;
113 * Test and reformat an email address.
114 * Enter: 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;
120 if( address ) {
121 retVal = g_strdup( address );
122 retVal = g_strstrip( retVal );
123 if( *retVal == '\0' ) {
124 g_free( retVal );
125 retVal = NULL;
128 return retVal;
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
139 * freed when done.
141 GList *mgu_parse_string( gchar *line, const gint maxTokens, gint *tokenCnt ) {
142 gchar *ptr, *pStart, *pFound, *str;
143 gint args = 0;
144 GList *list = NULL;
145 gboolean done = FALSE;
147 if( tokenCnt ) *tokenCnt = 0;
148 if( line == NULL ) return NULL;
149 if( maxTokens < 1 ) return NULL;
151 ptr = line;
152 while( ! done ) {
153 args++;
154 /* Skip over leading spaces */
155 while( *ptr ) {
156 if( ! isspace( *ptr ) ) break;
157 ptr++;
160 /* Find terminating space */
161 pFound = NULL;
162 pStart = ptr;
163 while( *ptr ) {
164 if( isspace( *ptr ) ) {
165 pFound = pStart;
166 break;
168 ptr++;
171 if( pFound ) {
172 if( args == maxTokens ) {
173 /* Rest of string */
174 str = g_strdup( pStart );
175 done = TRUE;
177 else {
178 /* Extract part of string */
179 str = g_strndup( pStart, ptr - pFound );
182 else {
183 /* Nothing there - treat as rest of string */
184 str = g_strdup( pStart );
185 done = TRUE;
187 list = g_list_append( list, str );
189 if( tokenCnt ) *tokenCnt = args;
190 return list;
194 * Unescape characters by removing backslash character from input string.
195 * Enter: str String to process.
197 void mgu_str_unescape( gchar *str ) {
198 gchar *p;
199 gint ilen;
201 p = str;
202 while( *p ) {
203 if( *p == '\\' ) {
204 ilen = strlen( p + 1 );
205 memmove( p, p + 1, ilen );
207 p++;
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 ) {
221 gchar *as;
222 gchar *ae;
224 /* Search forwards for first non-space match */
225 as = str;
226 ae = -1 + str + strlen( str );
227 while( as < ae ) {
228 if( *as != ' ' ) {
229 if( *as == chlead ) {
230 /* Search backwards from end for match */
231 while( ae > as ) {
232 if( *ae != ' ' ) {
233 if( *ae == chtail ) {
234 *as = ' ';
235 *ae = ' ';
236 return;
238 if( *ae < 32 ) {
239 *ae = ' ';
241 else if( *ae == 127 ) {
242 *ae = ' ';
244 else {
245 return;
248 ae--;
251 if( *as < 32 ) {
252 *as = ' ';
254 else if( *as == 127 ) {
255 *as = ' ';
257 else {
258 return;
261 as++;
263 return;
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 ) {
273 GSList *node;
274 gchar *name = NULL;
275 gint iLen = 0, iLenT = 0;
277 node = list;
278 while( node ) {
279 if( name == NULL ) {
280 name = node->data;
281 iLen = strlen( name );
283 else {
284 iLenT = strlen( node->data );
285 if( iLenT > iLen ) {
286 name = node->data;
287 iLen = iLenT;
290 node = g_slist_next( node );
292 return name;
296 * End of Source.