Add year 2009 to copyright info everywhere.
[geeqie.git] / src / misc.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 - 2009 The Geeqie Team
4  *
5  * Authors: Vladimir Nadvornik / Laurent Monin
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include "main.h"
13 #include "misc.h"
14
15 gdouble get_zoom_increment(void)
16 {
17         return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 10.0 : 1.0);
18 }
19
20 gchar *utf8_validate_or_convert(const gchar *text)
21 {
22         gint len;
23
24         if (!text) return NULL;
25         
26         len = strlen(text);
27         if (!g_utf8_validate(text, len, NULL))
28                 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
29
30         return g_strdup(text);
31 }
32
33 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
34 {
35         gchar *s1_key, *s2_key;
36         gchar *s1_t, *s2_t;
37         gint ret;
38
39         g_assert(g_utf8_validate(s1, -1, NULL));
40         g_assert(g_utf8_validate(s2, -1, NULL));
41
42         if (!case_sensitive)
43                 {
44                 s1_t = g_utf8_casefold(s1, -1);
45                 s2_t = g_utf8_casefold(s2, -1);
46                 }
47         else
48                 {
49                 s1_t = (gchar *) s1;
50                 s2_t = (gchar *) s2;
51                 }
52
53         s1_key = g_utf8_collate_key(s1_t, -1);
54         s2_key = g_utf8_collate_key(s2_t, -1);
55
56         ret = strcmp(s1_key, s2_key);
57
58         g_free(s1_key);
59         g_free(s2_key);
60
61         if (!case_sensitive)
62                 {
63                 g_free(s1_t);
64                 g_free(s2_t);
65                 }
66
67         return ret;
68 }
69
70 /* Borrowed from gtkfilesystemunix.c */
71 gchar *expand_tilde(const gchar *filename)
72 {
73 #ifndef G_OS_UNIX
74         return g_strdup(filename);
75 #else
76         const gchar *notilde;
77         const gchar *slash;
78         const gchar *home;
79
80         if (filename[0] != '~')
81                 return g_strdup(filename);
82
83         notilde = filename + 1;
84         slash = strchr(notilde, G_DIR_SEPARATOR);
85         if (slash == notilde || !*notilde)
86                 {
87                 home = g_get_home_dir();
88                 if (!home)
89                         return g_strdup(filename);
90                 }
91         else
92                 {
93                 gchar *username;
94                 struct passwd *passwd;
95
96                 if (slash)
97                         username = g_strndup(notilde, slash - notilde);
98                 else
99                         username = g_strdup(notilde);
100
101                 passwd = getpwnam(username);
102                 g_free(username);
103
104                 if (!passwd)
105                         return g_strdup(filename);
106
107                 home = passwd->pw_dir;
108                 }
109
110         if (slash)
111                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
112         else
113                 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
114 #endif
115 }
116
117 /*
118    returns text without quotes or NULL for empty or broken string
119    any text up to first '"' is skipped
120    tail is set to point at the char after the second '"'
121    or at the ending \0
122
123 */
124
125 gchar *quoted_value(const gchar *text, const gchar **tail)
126 {
127         const gchar *ptr;
128         gint c = 0;
129         gint l = strlen(text);
130         gchar *retval = NULL;
131
132         if (tail) *tail = text;
133
134         if (l == 0) return retval;
135
136         while (c < l && text[c] != '"') c++;
137         if (text[c] == '"')
138                 {
139                 gint e;
140                 c++;
141                 ptr = text + c;
142                 e = c;
143                 while (e < l)
144                         {
145                         if (text[e-1] != '\\' && text[e] == '"') break;
146                         e++;
147                         }
148                 if (text[e] == '"')
149                         {
150                         if (e - c > 0)
151                                 {
152                                 gchar *substring = g_strndup(ptr, e - c);
153
154                                 if (substring)
155                                         {
156                                         retval = g_strcompress(substring);
157                                         g_free(substring);
158                                         }
159                                 }
160                         }
161                 if (tail) *tail = text + e + 1;
162                 }
163         else
164                 /* for compatibility with older formats (<0.3.7)
165                  * read a line without quotes too */
166                 {
167                 c = 0;
168                 while (c < l && text[c] != '\n' && !g_ascii_isspace(text[c])) c++;
169                 if (c != 0)
170                         {
171                         retval = g_strndup(text, c);
172                         }
173                 if (tail) *tail = text + c;
174                 }
175
176         return retval;
177 }
178
179 gchar *escquote_value(const gchar *text)
180 {
181         gchar *e;
182
183         if (!text) return g_strdup("\"\"");
184
185         e = g_strescape(text, "");
186         if (e)
187                 {
188                 gchar *retval = g_strdup_printf("\"%s\"", e);
189                 g_free(e);
190                 return retval;
191                 }
192         return g_strdup("\"\"");
193 }
194
195 /* Run a command like system() but may output debug messages. */
196 int runcmd(gchar *cmd)
197 {
198 #if 1
199         return system(cmd);
200         return 0;
201 #else
202         /* For debugging purposes */
203         int retval = -1;
204         FILE *in;
205
206         DEBUG_1("Running command: %s", cmd);
207
208         in = popen(cmd, "r");
209         if (in)
210                 {
211                 int status;
212                 const gchar *msg;
213                 gchar buf[2048];
214
215                 while (fgets(buf, sizeof(buf), in) != NULL )
216                         {
217                         DEBUG_1("Output: %s", buf);
218                         }
219
220                 status = pclose(in);
221
222                 if (WIFEXITED(status))
223                         {
224                         msg = "Command terminated with exit code";
225                         retval = WEXITSTATUS(status);
226                         }
227                 else if (WIFSIGNALED(status))
228                         {
229                         msg = "Command was killed by signal";
230                         retval = WTERMSIG(status);
231                         }
232                 else
233                         {
234                         msg = "pclose() returned";
235                         retval = status;
236                         }
237
238                 DEBUG_1("%s : %d\n", msg, retval);
239         }
240         
241         return retval;
242 #endif
243 }
244
245
246 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */