Indentation fixes, some rewriting of docs to conform to gtk-doc standard.
[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 <string.h> /* strerror, strcmp */
27 #ifdef HAVE_DIRENT_H
28 #include <dirent.h>
29 #endif
30
31 #include "glib.h"
32 #include "gdir.h"
33
34 #include "glibintl.h"
35
36 struct _GDir
37 {
38   DIR *dir;
39 };
40
41 /**
42  * g_dir_open:
43  * @path: the path to the directory you are interested in
44  * @flags: Currently must be set to 0. Reserved for future use.
45  * @error: return location for a #GError, or %NULL.
46  *         If non-%NULL, an error will be set if and only if
47  *         g_dir_open_fails.
48  *
49  * Opens a directory for reading. The names of the files
50  * in the directory can then be retrieved using
51  * g_dir_get_name().
52  *
53  * Return value: a newly allocated #GDir on success, %NULL on failure.
54  *   If non-%NULL, you must free the result with g_dir_close()
55  *   when you are finished with it.
56  **/
57 GDir *
58 g_dir_open (const gchar  *path,
59             guint         flags,
60             GError      **error)
61 {
62   GDir *dir = g_new (GDir, 1);
63
64   dir->dir = opendir (path);
65
66   if (dir->dir)
67     return dir;
68
69   /* error case */
70   g_set_error (error,
71                G_FILE_ERROR,
72                g_file_error_from_errno (errno),
73                _("Error opening directory '%s': %s"),
74                path, strerror (errno));
75
76   g_free (dir);
77   return NULL;
78 }
79
80 /**
81  * g_dir_read_name:
82  * @dir: a #GDir* created by g_dir_open()
83  *
84  * Retrieves the name of the next entry in the directory.
85  * The '.' and '..' entries are omitted.
86  *
87  * Return value: The entries name or %NULL if there are no 
88  *   more entries. The return value is owned by GLib and
89  *   must not be modified or freed.
90  **/
91 G_CONST_RETURN gchar*
92 g_dir_read_name (GDir *dir)
93 {
94   struct dirent *entry;
95
96   g_return_val_if_fail (dir != NULL, NULL);
97
98   entry = readdir (dir->dir);
99   while (entry 
100          && (0 == strcmp (entry->d_name, ".") ||
101              0 == strcmp (entry->d_name, "..")))
102     entry = readdir (dir->dir);
103
104   return entry->d_name;
105 }
106
107 /**
108  * g_dir_rewind:
109  * @dir: a #GDir* created by g_dir_open()
110  *
111  * Resets the given directory. The next call to g_dir_read_name()
112  * will return the first entry again.
113  **/
114 void
115 g_dir_rewind (GDir *dir)
116 {
117   g_return_if_fail (dir != NULL);
118   
119   rewinddir (dir->dir);
120 }
121
122 /**
123  * g_dir_close:
124  * @dir: a #GDir* created by g_dir_open()
125  *
126  * Closes the directory and deallocates all related resources.
127  **/
128 void
129 g_dir_close (GDir *dir)
130 {
131   int ret = 0;
132
133   g_return_val_if_fail (dir != NULL, FALSE);
134
135   closedir (dir->dir);
136   g_free (dir);
137 }