Fix #486: Build error
[geeqie.git] / src / misc.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Authors: Vladimir Nadvornik, Laurent Monin
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "main.h"
22 #include "misc.h"
23 #include "ui_fileops.h"
24
25 gdouble get_zoom_increment(void)
26 {
27         return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 100.0 : 1.0);
28 }
29
30 gchar *utf8_validate_or_convert(const gchar *text)
31 {
32         gint len;
33
34         if (!text) return NULL;
35
36         len = strlen(text);
37         if (!g_utf8_validate(text, len, NULL))
38                 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
39
40         return g_strdup(text);
41 }
42
43 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
44 {
45         gchar *s1_key, *s2_key;
46         gchar *s1_t, *s2_t;
47         gint ret;
48
49         g_assert(g_utf8_validate(s1, -1, NULL));
50         g_assert(g_utf8_validate(s2, -1, NULL));
51
52         if (!case_sensitive)
53                 {
54                 s1_t = g_utf8_casefold(s1, -1);
55                 s2_t = g_utf8_casefold(s2, -1);
56                 }
57         else
58                 {
59                 s1_t = (gchar *) s1;
60                 s2_t = (gchar *) s2;
61                 }
62
63         s1_key = g_utf8_collate_key(s1_t, -1);
64         s2_key = g_utf8_collate_key(s2_t, -1);
65
66         ret = strcmp(s1_key, s2_key);
67
68         g_free(s1_key);
69         g_free(s2_key);
70
71         if (!case_sensitive)
72                 {
73                 g_free(s1_t);
74                 g_free(s2_t);
75                 }
76
77         return ret;
78 }
79
80 /* Borrowed from gtkfilesystemunix.c */
81 gchar *expand_tilde(const gchar *filename)
82 {
83 #ifndef G_OS_UNIX
84         return g_strdup(filename);
85 #else
86         const gchar *notilde;
87         const gchar *slash;
88         const gchar *home;
89
90         if (filename[0] != '~')
91                 return g_strdup(filename);
92
93         notilde = filename + 1;
94         slash = strchr(notilde, G_DIR_SEPARATOR);
95         if (slash == notilde || !*notilde)
96                 {
97                 home = g_get_home_dir();
98                 if (!home)
99                         return g_strdup(filename);
100                 }
101         else
102                 {
103                 gchar *username;
104                 struct passwd *passwd;
105
106                 if (slash)
107                         username = g_strndup(notilde, slash - notilde);
108                 else
109                         username = g_strdup(notilde);
110
111                 passwd = getpwnam(username);
112                 g_free(username);
113
114                 if (!passwd)
115                         return g_strdup(filename);
116
117                 home = passwd->pw_dir;
118                 }
119
120         if (slash)
121                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
122         else
123                 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
124 #endif
125 }
126
127 /* Search for latitude/longitude parameters in a string
128  */
129
130 #define GEOCODE_NAME "geocode-parameters.awk"
131 #define BUFSIZE 128
132
133 gchar *decode_geo_parameters(const gchar *input_text)
134 {
135         gchar *message;
136         gchar *path = g_build_filename(get_rc_dir(), GEOCODE_NAME, NULL);
137         gchar *cmd = g_strconcat("echo \'", input_text, "\'  | awk -f ", path, NULL);
138
139         if (g_file_test(path, G_FILE_TEST_EXISTS))
140                 {
141                 gchar buf[BUFSIZE];
142                 FILE *fp;
143
144                 if ((fp = popen(cmd, "r")) == NULL)
145                         {
146                         message = g_strconcat("Error: opening pipe\n", input_text, NULL);
147                         }
148                 else
149                         {
150                         while (fgets(buf, BUFSIZE, fp))
151                                 {
152                                 DEBUG_1("Output: %s", buf);
153                                 }
154
155                         message = g_strconcat(buf, NULL);
156
157                         if(pclose(fp))
158                                 {
159                                 message = g_strconcat("Error: Command not found or exited with error status\n", input_text, NULL);
160                                 }
161                         }
162                 }
163         else
164                 {
165                 message = g_strconcat(input_text, NULL);
166                 }
167
168         g_free(path);
169         g_free(cmd);
170         return message;
171 }
172
173 /* Run a command like system() but may output debug messages. */
174 int runcmd(gchar *cmd)
175 {
176 #if 1
177         return system(cmd);
178         return 0;
179 #else
180         /* For debugging purposes */
181         int retval = -1;
182         FILE *in;
183
184         DEBUG_1("Running command: %s", cmd);
185
186         in = popen(cmd, "r");
187         if (in)
188                 {
189                 int status;
190                 const gchar *msg;
191                 gchar buf[2048];
192
193                 while (fgets(buf, sizeof(buf), in) != NULL )
194                         {
195                         DEBUG_1("Output: %s", buf);
196                         }
197
198                 status = pclose(in);
199
200                 if (WIFEXITED(status))
201                         {
202                         msg = "Command terminated with exit code";
203                         retval = WEXITSTATUS(status);
204                         }
205                 else if (WIFSIGNALED(status))
206                         {
207                         msg = "Command was killed by signal";
208                         retval = WTERMSIG(status);
209                         }
210                 else
211                         {
212                         msg = "pclose() returned";
213                         retval = status;
214                         }
215
216                 DEBUG_1("%s : %d\n", msg, retval);
217         }
218
219         return retval;
220 #endif
221 }
222
223
224 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */