1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.Free
16  */
17 
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 #include "config.h"
26 
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include <glib/gstdio.h>
33 #include <gmodule.h>
34 
35 #include "gtkutilsprivate.h"
36 
37 /* Copied from pango-utils.c */
38 
39 /* We need to call getc() a lot in a loop. This is suboptimal,
40  * as getc() does thread locking on the FILE it is given.
41  * To optimize that, lock the file first, then call getc(),
42  * then unlock.
43  * If locking functions are not present in libc, fall back
44  * to the suboptimal getc().
45  */
46 #if !defined(HAVE_FLOCKFILE) && !defined(HAVE__LOCK_FILE)
47 #  define flockfile(f) (void)1
48 #  define funlockfile(f) (void)1
49 #  define getc_unlocked(f) getc(f)
50 #elif !defined(HAVE_FLOCKFILE) && defined(HAVE__LOCK_FILE)
51 #  define flockfile(f) _lock_file(f)
52 #  define funlockfile(f) _unlock_file(f)
53 #  define getc_unlocked(f) _getc_nolock(f)
54 #endif
55 
56 gboolean
gtk_scan_string(const char ** pos,GString * out)57 gtk_scan_string (const char **pos, GString *out)
58 {
59   const char *p = *pos, *q = *pos;
60   char *tmp, *tmp2;
61   gboolean quoted;
62 
63   while (g_ascii_isspace (*p))
64     p++;
65 
66   if (!*p)
67     return FALSE;
68   else if (*p == '"')
69     {
70       p++;
71       quoted = FALSE;
72       for (q = p; (*q != '"') || quoted; q++)
73         {
74           if (!*q)
75             return FALSE;
76           quoted = (*q == '\\') && !quoted;
77         }
78 
79       tmp = g_strndup (p, q - p);
80       tmp2 = g_strcompress (tmp);
81       g_string_truncate (out, 0);
82       g_string_append (out, tmp2);
83       g_free (tmp);
84       g_free (tmp2);
85     }
86 
87   q++;
88   *pos = q;
89 
90   return TRUE;
91 }
92 
93 gboolean
gtk_skip_space(const char ** pos)94 gtk_skip_space (const char **pos)
95 {
96   const char *p = *pos;
97 
98   while (g_ascii_isspace (*p))
99     p++;
100 
101   *pos = p;
102 
103   return !(*p == '\0');
104 }
105 
106 gint
gtk_read_line(FILE * stream,GString * str)107 gtk_read_line (FILE *stream, GString *str)
108 {
109   gboolean quoted = FALSE;
110   gboolean comment = FALSE;
111   int n_read = 0;
112   int lines = 1;
113 
114   flockfile (stream);
115 
116   g_string_truncate (str, 0);
117 
118   while (1)
119     {
120       int c;
121 
122       c = getc_unlocked (stream);
123 
124       if (c == EOF)
125         {
126           if (quoted)
127             g_string_append_c (str, '\\');
128 
129           goto done;
130         }
131       else
132         n_read++;
133 
134       if (quoted)
135         {
136           quoted = FALSE;
137 
138           switch (c)
139             {
140             case '#':
141               g_string_append_c (str, '#');
142               break;
143             case '\r':
144             case '\n':
145               {
146                 int next_c = getc_unlocked (stream);
147 
148                 if (!(next_c == EOF ||
149                       (c == '\r' && next_c == '\n') ||
150                       (c == '\n' && next_c == '\r')))
151                   ungetc (next_c, stream);
152 
153                 lines++;
154 
155                 break;
156               }
157             default:
158               g_string_append_c (str, '\\');
159               g_string_append_c (str, c);
160             }
161         }
162       else
163         {
164           switch (c)
165             {
166             case '#':
167               comment = TRUE;
168               break;
169             case '\\':
170               if (!comment)
171                 quoted = TRUE;
172               break;
173             case '\n':
174               {
175                 int next_c = getc_unlocked (stream);
176 
177                 if (!(c == EOF ||
178                       (c == '\r' && next_c == '\n') ||
179                       (c == '\n' && next_c == '\r')))
180                   ungetc (next_c, stream);
181 
182                 goto done;
183               }
184             default:
185               if (!comment)
186                g_string_append_c (str, c);
187             }
188         }
189     }
190 
191  done:
192   funlockfile (stream);
193 
194   return (n_read > 0) ? lines : 0;
195 }
196 
197 char *
gtk_trim_string(const char * str)198 gtk_trim_string (const char *str)
199 {
200   int len;
201 
202   g_return_val_if_fail (str != NULL, NULL);
203 
204   while (*str && g_ascii_isspace (*str))
205     str++;
206 
207   len = strlen (str);
208   while (len > 0 && g_ascii_isspace (str[len - 1]))
209     len--;
210 
211   return g_strndup (str, len);
212 }
213 
214 char **
gtk_split_file_list(const char * str)215 gtk_split_file_list (const char *str)
216 {
217   int i = 0;
218   int j;
219   char **files;
220 
221   files = g_strsplit (str, G_SEARCHPATH_SEPARATOR_S, -1);
222 
223   while (files[i])
224     {
225       char *file = gtk_trim_string (files[i]);
226 
227       /* If the resulting file is empty, skip it */
228       if (file[0] == '\0')
229         {
230           g_free (file);
231           g_free (files[i]);
232 
233           for (j = i + 1; files[j]; j++)
234             files[j - 1] = files[j];
235 
236           files[j - 1] = NULL;
237 
238           continue;
239         }
240 
241 #ifndef G_OS_WIN32
242       /* '~' is a quite normal and common character in file names on
243        * Windows, especially in the 8.3 versions of long file names, which
244        * still occur now and then. Also, few Windows user are aware of the
245        * Unix shell convention that '~' stands for the home directory,
246        * even if they happen to have a home directory.
247        */
248       if (file[0] == '~' && file[1] == G_DIR_SEPARATOR)
249         {
250           char *tmp = g_strconcat (g_get_home_dir(), file + 1, NULL);
251           g_free (file);
252           file = tmp;
253         }
254       else if (file[0] == '~' && file[1] == '\0')
255         {
256           g_free (file);
257           file = g_strdup (g_get_home_dir ());
258         }
259 #endif
260 
261       g_free (files[i]);
262       files[i] = file;
263 
264       i++;
265     }
266 
267   return files;
268 }
269 
270 GBytes *
gtk_file_load_bytes(GFile * file,GCancellable * cancellable,GError ** error)271 gtk_file_load_bytes (GFile         *file,
272                      GCancellable  *cancellable,
273                      GError       **error)
274 {
275   gchar *contents;
276   gsize len;
277 
278   g_return_val_if_fail (G_IS_FILE (file), NULL);
279   g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), NULL);
280 
281   if (g_file_has_uri_scheme (file, "resource"))
282     {
283       gchar *uri, *unescaped;
284       GBytes *bytes;
285 
286       uri = g_file_get_uri (file);
287       unescaped = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
288       g_free (uri);
289 
290       bytes = g_resources_lookup_data (unescaped, 0, error);
291       g_free (unescaped);
292 
293       return bytes;
294     }
295 
296   /* contents is always \0 terminated, but we don't include that in the bytes */
297   if (g_file_load_contents (file, cancellable, &contents, &len, NULL, error))
298     return g_bytes_new_take (contents, len);
299 
300   return NULL;
301 }
302