ca174831d9dce067ee27798a8f80b827b12ed309
[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
24 gdouble get_zoom_increment(void)
25 {
26         return ((options->image.zoom_increment != 0) ? (gdouble)options->image.zoom_increment / 100.0 : 1.0);
27 }
28
29 gchar *utf8_validate_or_convert(const gchar *text)
30 {
31         gint len;
32
33         if (!text) return NULL;
34
35         len = strlen(text);
36         if (!g_utf8_validate(text, len, NULL))
37                 return g_convert(text, len, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
38
39         return g_strdup(text);
40 }
41
42 gint utf8_compare(const gchar *s1, const gchar *s2, gboolean case_sensitive)
43 {
44         gchar *s1_key, *s2_key;
45         gchar *s1_t, *s2_t;
46         gint ret;
47
48         g_assert(g_utf8_validate(s1, -1, NULL));
49         g_assert(g_utf8_validate(s2, -1, NULL));
50
51         if (!case_sensitive)
52                 {
53                 s1_t = g_utf8_casefold(s1, -1);
54                 s2_t = g_utf8_casefold(s2, -1);
55                 }
56         else
57                 {
58                 s1_t = (gchar *) s1;
59                 s2_t = (gchar *) s2;
60                 }
61
62         s1_key = g_utf8_collate_key(s1_t, -1);
63         s2_key = g_utf8_collate_key(s2_t, -1);
64
65         ret = strcmp(s1_key, s2_key);
66
67         g_free(s1_key);
68         g_free(s2_key);
69
70         if (!case_sensitive)
71                 {
72                 g_free(s1_t);
73                 g_free(s2_t);
74                 }
75
76         return ret;
77 }
78
79 /* Borrowed from gtkfilesystemunix.c */
80 gchar *expand_tilde(const gchar *filename)
81 {
82 #ifndef G_OS_UNIX
83         return g_strdup(filename);
84 #else
85         const gchar *notilde;
86         const gchar *slash;
87         const gchar *home;
88
89         if (filename[0] != '~')
90                 return g_strdup(filename);
91
92         notilde = filename + 1;
93         slash = strchr(notilde, G_DIR_SEPARATOR);
94         if (slash == notilde || !*notilde)
95                 {
96                 home = g_get_home_dir();
97                 if (!home)
98                         return g_strdup(filename);
99                 }
100         else
101                 {
102                 gchar *username;
103                 struct passwd *passwd;
104
105                 if (slash)
106                         username = g_strndup(notilde, slash - notilde);
107                 else
108                         username = g_strdup(notilde);
109
110                 passwd = getpwnam(username);
111                 g_free(username);
112
113                 if (!passwd)
114                         return g_strdup(filename);
115
116                 home = passwd->pw_dir;
117                 }
118
119         if (slash)
120                 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
121         else
122                 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
123 #endif
124 }
125
126
127 /* Run a command like system() but may output debug messages. */
128 int runcmd(gchar *cmd)
129 {
130 #if 1
131         return system(cmd);
132         return 0;
133 #else
134         /* For debugging purposes */
135         int retval = -1;
136         FILE *in;
137
138         DEBUG_1("Running command: %s", cmd);
139
140         in = popen(cmd, "r");
141         if (in)
142                 {
143                 int status;
144                 const gchar *msg;
145                 gchar buf[2048];
146
147                 while (fgets(buf, sizeof(buf), in) != NULL )
148                         {
149                         DEBUG_1("Output: %s", buf);
150                         }
151
152                 status = pclose(in);
153
154                 if (WIFEXITED(status))
155                         {
156                         msg = "Command terminated with exit code";
157                         retval = WEXITSTATUS(status);
158                         }
159                 else if (WIFSIGNALED(status))
160                         {
161                         msg = "Command was killed by signal";
162                         retval = WTERMSIG(status);
163                         }
164                 else
165                         {
166                         msg = "pclose() returned";
167                         retval = status;
168                         }
169
170                 DEBUG_1("%s : %d\n", msg, retval);
171         }
172
173         return retval;
174 #endif
175 }
176
177
178 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */