20090122
[gdash.git] / src / util.c
blobf66b25a4937a17dd7a89b865851116424b4ec8dc
1 /*
2 * Copyright (c) 2007, 2008, 2009, Czirkos Zoltan <cirix@fw.hu>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <glib.h>
17 #include <glib/gi18n.h>
18 #include <string.h>
19 #include "util.h"
20 #include "settings.h"
22 GList *gd_errors=NULL;
23 static gboolean has_new_error=FALSE;
24 static char *error_context=NULL;
27 static void
28 error_free(GdErrorMessage *error)
30 g_free(error->message);
31 g_free(error);
34 void
35 gd_clear_error_flag()
37 has_new_error=FALSE;
40 void
41 gd_clear_errors()
43 g_list_foreach(gd_errors, (GFunc) error_free, NULL);
44 g_list_free(gd_errors);
45 gd_errors=NULL;
46 gd_clear_error_flag();
49 void
50 gd_error_set_context(const char *format, ...)
52 va_list ap;
54 g_free(error_context);
55 error_context=NULL;
56 if (!format)
57 return;
58 va_start(ap, format);
59 error_context=g_strdup_vprintf(format, ap);
60 va_end(ap);
63 static void
64 log_func(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
66 GdErrorMessage *error;
67 error=g_new(GdErrorMessage, 1);
68 error->flags=log_level & G_LOG_LEVEL_MASK;
69 if (error_context)
70 error->message=g_strdup_printf("%s: %s", error_context, message);
71 else
72 error->message=g_strdup(message);
74 if ((log_level&G_LOG_LEVEL_MASK) <= G_LOG_LEVEL_WARNING)
75 has_new_error=TRUE;
77 gd_errors=g_list_append(gd_errors, error);
78 /* also call default handler to print to console; but with processed string */
79 g_log_default_handler(log_domain, log_level, error->message, user_data);
82 gboolean gd_has_new_error()
84 return has_new_error;
87 void
88 gd_install_log_handler()
90 g_log_set_default_handler (log_func, NULL);
98 /* returns a static string which contains the utf8 representation of the filename in system encoding */
99 const char *
100 gd_filename_to_utf8(const char *filename)
102 static char *utf8=NULL;
103 GError *error=NULL;
105 g_free(utf8);
106 utf8=g_filename_to_utf8(filename, -1, NULL, NULL, &error);
107 if (error) {
108 g_error_free(error);
109 utf8=g_strdup(filename); /* use without conversion */
111 return utf8;
117 static const char *
118 find_file_try_path(const char *path, const char *filename)
120 static char *result=NULL;
122 g_free(result);
124 result=g_build_path(G_DIR_SEPARATOR_S, path, filename, NULL);
125 /* if the file exists, return the full filename */
126 if (g_file_test(result, G_FILE_TEST_EXISTS))
127 return result;
129 /* otherwise return nothing */
130 return NULL;
135 /* tries to find a file in the gdash installation and returns a path (owned by the above function, not to be g_free()d) */
136 const char *
137 gd_find_file(const char *filename)
139 const char *result;
141 result=find_file_try_path(gd_user_config_dir, filename);
142 if (result!=NULL)
143 return result;
145 result=find_file_try_path(gd_system_data_dir, filename);
146 if (result!=NULL)
147 return result;
149 result=find_file_try_path(gd_system_sound_dir, filename);
150 if (result!=NULL)
151 return result;
153 result=find_file_try_path(".", filename);
154 if (result!=NULL)
155 return result;
157 /* for testing */
158 result=find_file_try_path("./sound", filename);
159 if (result!=NULL);
160 return result;
162 g_critical("cannot find file: %s", gd_filename_to_utf8(filename));
163 return NULL;
170 char *
171 gd_wrap_text(const char *orig, int width)
173 GString *wrapped;
174 char **lines;
175 int l;
177 g_assert(orig!=NULL);
178 wrapped=g_string_new(NULL);
179 lines=g_strsplit_set(orig, "\n", -1);
180 for (l=0; lines[l]!=NULL; l++) {
181 char **words;
182 int w;
183 int curwidth;
185 words=g_strsplit_set(lines[l], " ", -1);
186 curwidth=0;
187 for (w=0; words[w]!=NULL; w++) {
188 int curlen;
190 curlen=strlen(words[w]);
191 if (curwidth==0 || curwidth+curlen<width) {
192 if (w!=0) {
193 curwidth++;
194 g_string_append_c(wrapped, ' ');
196 g_string_append(wrapped, words[w]);
197 curwidth+=curlen;
198 } else {
199 g_string_append_c(wrapped, '\n');
200 g_string_append(wrapped, words[w]);
201 curwidth=curlen;
204 g_strfreev(words);
206 g_string_append_c(wrapped, '\n');
208 g_strfreev(lines);
210 return g_string_free(wrapped, FALSE);
214 gd_lines_in_text(const char *text)
216 int lines, i;
218 g_assert(text!=NULL);
219 lines=0;
220 for (i=0; text[i]!=0; i++)
221 if (text[i]=='\n')
222 lines++;
224 return lines+1;
227 /* return current date in 2008-12-04 format */
228 const char *
229 gd_get_current_date()
231 GDate *dat;
232 static char dats[128];
234 dat=g_date_new();
235 g_date_set_time_t(dat, time(NULL));
236 g_date_strftime(dats, sizeof(dats), "%Y-%m-%d", dat);
237 g_date_free(dat);
239 return dats;
242 /* return current date in 2008-12-04 12:34 format */
243 const char *
244 gd_get_current_date_time()
246 GDate *dat;
247 static char dats[128];
249 dat=g_date_new();
250 g_date_set_time_t(dat, time(NULL));
251 g_date_strftime(dats, sizeof(dats), "%Y-%m-%d %H:%I", dat);
252 g_date_free(dat);
254 return dats;
258 gd_clamp(int val, int min, int max)
260 g_assert(min<=max);
262 if (val<min)
263 return min;
264 if (val>max)
265 return max;
266 return val;