Convert filename to UTF-8 before using it in the error message. (#146054,
[platform/upstream/glib.git] / glib / gdir.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gdir.c: Simplified wrapper around the DIRENT functions.
5  *
6  * Copyright 2001 Hans Breuer
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <string.h> /* strcmp */
28
29 #ifdef HAVE_DIRENT_H
30 #include <sys/types.h>
31 #include <dirent.h>
32 #endif
33
34 #include "glib.h"
35 #include "gdir.h"
36
37 #include "glibintl.h"
38
39 struct _GDir
40 {
41   DIR *dir;
42 };
43
44 /**
45  * g_dir_open:
46  * @path: the path to the directory you are interested in
47  * @flags: Currently must be set to 0. Reserved for future use.
48  * @error: return location for a #GError, or %NULL.
49  *         If non-%NULL, an error will be set if and only if
50  *         g_dir_open_fails.
51  *
52  * Opens a directory for reading. The names of the files
53  * in the directory can then be retrieved using
54  * g_dir_read_name().
55  *
56  * Return value: a newly allocated #GDir on success, %NULL on failure.
57  *   If non-%NULL, you must free the result with g_dir_close()
58  *   when you are finished with it.
59  **/
60 GDir *
61 g_dir_open (const gchar  *path,
62             guint         flags,
63             GError      **error)
64 {
65   GDir *dir;
66   gchar *utf8_path;
67
68   g_return_val_if_fail (path != NULL, NULL);
69
70   dir = g_new (GDir, 1);
71
72   dir->dir = opendir (path);
73
74   if (dir->dir)
75     return dir;
76
77   /* error case */
78   utf8_path = g_filename_to_utf8 (path, -1,
79                                   NULL, NULL, NULL);
80   g_set_error (error,
81                G_FILE_ERROR,
82                g_file_error_from_errno (errno),
83                _("Error opening directory '%s': %s"),
84                utf8_path, g_strerror (errno));
85
86   g_free (utf8_path);
87   g_free (dir);
88
89   return NULL;
90 }
91
92 /**
93  * g_dir_read_name:
94  * @dir: a #GDir* created by g_dir_open()
95  *
96  * Retrieves the name of the next entry in the directory.
97  * The '.' and '..' entries are omitted. The returned name is in 
98  * the encoding used for filenames. Use g_filename_to_utf8() to 
99  * convert it to UTF-8.
100  *
101  * Return value: The entries name or %NULL if there are no 
102  *   more entries. The return value is owned by GLib and
103  *   must not be modified or freed.
104  **/
105 G_CONST_RETURN gchar*
106 g_dir_read_name (GDir *dir)
107 {
108   struct dirent *entry;
109
110   g_return_val_if_fail (dir != NULL, NULL);
111
112   entry = readdir (dir->dir);
113   while (entry 
114          && (0 == strcmp (entry->d_name, ".") ||
115              0 == strcmp (entry->d_name, "..")))
116     entry = readdir (dir->dir);
117
118   if (entry)
119     return entry->d_name;
120   else
121     return NULL;
122 }
123
124 /**
125  * g_dir_rewind:
126  * @dir: a #GDir* created by g_dir_open()
127  *
128  * Resets the given directory. The next call to g_dir_read_name()
129  * will return the first entry again.
130  **/
131 void
132 g_dir_rewind (GDir *dir)
133 {
134   g_return_if_fail (dir != NULL);
135   
136   rewinddir (dir->dir);
137 }
138
139 /**
140  * g_dir_close:
141  * @dir: a #GDir* created by g_dir_open()
142  *
143  * Closes the directory and deallocates all related resources.
144  **/
145 void
146 g_dir_close (GDir *dir)
147 {
148   g_return_if_fail (dir != NULL);
149
150   closedir (dir->dir);
151   g_free (dir);
152 }