simplified wrapper around dirent functions to improve portability of
authorHans Breuer <hans@breuer.org>
Mon, 5 Nov 2001 01:15:38 +0000 (01:15 +0000)
committerHans Breuer <hans@src.gnome.org>
Mon, 5 Nov 2001 01:15:38 +0000 (01:15 +0000)
2001-11-04  Hans Breuer  <hans@breuer.org>

* glib/gdir.[hc] : (new files) simplified wrapper around dirent
functions to improve portability of downstream libs/apps
* glib/makefile.msc.in : use them
* glib/glib.def : export them
* config.h.win32.in : needing HAVE_DIRENT_H defined

12 files changed:
ChangeLog
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
config.h.win32.in
glib/gdir.c [new file with mode: 0644]
glib/gdir.h [new file with mode: 0644]
glib/makefile.msc.in

index 6d0d52ca62941cbfb61c311cae28b3ba4f63b0c8..739b926907c2280622fc0ef06c142cd82e95321e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2001-11-04  Hans Breuer  <hans@breuer.org>
+
+       * glib/gdir.[hc] : (new files) simplified wrapper around dirent 
+       functions to improve portability of downstream libs/apps
+       * glib/makefile.msc.in : use them
+       * glib/glib.def : export them
+       * config.h.win32.in : needing HAVE_DIRENT_H defined
+
 2001-11-04  Tor Lillqvist  <tml@iki.fi>
 
        * tests/Makefile.am: On Win32, create separate .exp file for
index 6d0d52ca62941cbfb61c311cae28b3ba4f63b0c8..739b926907c2280622fc0ef06c142cd82e95321e 100644 (file)
@@ -1,3 +1,11 @@
+2001-11-04  Hans Breuer  <hans@breuer.org>
+
+       * glib/gdir.[hc] : (new files) simplified wrapper around dirent 
+       functions to improve portability of downstream libs/apps
+       * glib/makefile.msc.in : use them
+       * glib/glib.def : export them
+       * config.h.win32.in : needing HAVE_DIRENT_H defined
+
 2001-11-04  Tor Lillqvist  <tml@iki.fi>
 
        * tests/Makefile.am: On Win32, create separate .exp file for
index 6d0d52ca62941cbfb61c311cae28b3ba4f63b0c8..739b926907c2280622fc0ef06c142cd82e95321e 100644 (file)
@@ -1,3 +1,11 @@
+2001-11-04  Hans Breuer  <hans@breuer.org>
+
+       * glib/gdir.[hc] : (new files) simplified wrapper around dirent 
+       functions to improve portability of downstream libs/apps
+       * glib/makefile.msc.in : use them
+       * glib/glib.def : export them
+       * config.h.win32.in : needing HAVE_DIRENT_H defined
+
 2001-11-04  Tor Lillqvist  <tml@iki.fi>
 
        * tests/Makefile.am: On Win32, create separate .exp file for
index 6d0d52ca62941cbfb61c311cae28b3ba4f63b0c8..739b926907c2280622fc0ef06c142cd82e95321e 100644 (file)
@@ -1,3 +1,11 @@
+2001-11-04  Hans Breuer  <hans@breuer.org>
+
+       * glib/gdir.[hc] : (new files) simplified wrapper around dirent 
+       functions to improve portability of downstream libs/apps
+       * glib/makefile.msc.in : use them
+       * glib/glib.def : export them
+       * config.h.win32.in : needing HAVE_DIRENT_H defined
+
 2001-11-04  Tor Lillqvist  <tml@iki.fi>
 
        * tests/Makefile.am: On Win32, create separate .exp file for
index 6d0d52ca62941cbfb61c311cae28b3ba4f63b0c8..739b926907c2280622fc0ef06c142cd82e95321e 100644 (file)
@@ -1,3 +1,11 @@
+2001-11-04  Hans Breuer  <hans@breuer.org>
+
+       * glib/gdir.[hc] : (new files) simplified wrapper around dirent 
+       functions to improve portability of downstream libs/apps
+       * glib/makefile.msc.in : use them
+       * glib/glib.def : export them
+       * config.h.win32.in : needing HAVE_DIRENT_H defined
+
 2001-11-04  Tor Lillqvist  <tml@iki.fi>
 
        * tests/Makefile.am: On Win32, create separate .exp file for
index 6d0d52ca62941cbfb61c311cae28b3ba4f63b0c8..739b926907c2280622fc0ef06c142cd82e95321e 100644 (file)
@@ -1,3 +1,11 @@
+2001-11-04  Hans Breuer  <hans@breuer.org>
+
+       * glib/gdir.[hc] : (new files) simplified wrapper around dirent 
+       functions to improve portability of downstream libs/apps
+       * glib/makefile.msc.in : use them
+       * glib/glib.def : export them
+       * config.h.win32.in : needing HAVE_DIRENT_H defined
+
 2001-11-04  Tor Lillqvist  <tml@iki.fi>
 
        * tests/Makefile.am: On Win32, create separate .exp file for
index 6d0d52ca62941cbfb61c311cae28b3ba4f63b0c8..739b926907c2280622fc0ef06c142cd82e95321e 100644 (file)
@@ -1,3 +1,11 @@
+2001-11-04  Hans Breuer  <hans@breuer.org>
+
+       * glib/gdir.[hc] : (new files) simplified wrapper around dirent 
+       functions to improve portability of downstream libs/apps
+       * glib/makefile.msc.in : use them
+       * glib/glib.def : export them
+       * config.h.win32.in : needing HAVE_DIRENT_H defined
+
 2001-11-04  Tor Lillqvist  <tml@iki.fi>
 
        * tests/Makefile.am: On Win32, create separate .exp file for
index 6d0d52ca62941cbfb61c311cae28b3ba4f63b0c8..739b926907c2280622fc0ef06c142cd82e95321e 100644 (file)
@@ -1,3 +1,11 @@
+2001-11-04  Hans Breuer  <hans@breuer.org>
+
+       * glib/gdir.[hc] : (new files) simplified wrapper around dirent 
+       functions to improve portability of downstream libs/apps
+       * glib/makefile.msc.in : use them
+       * glib/glib.def : export them
+       * config.h.win32.in : needing HAVE_DIRENT_H defined
+
 2001-11-04  Tor Lillqvist  <tml@iki.fi>
 
        * tests/Makefile.am: On Win32, create separate .exp file for
index dad493d3dcad3878476197390132c6139c597ecd..dae918eef8ca461fdf08f8e83378b8c9192cd137 100644 (file)
 /* Define if you have the `dcgettext' function. */
 #define HAVE_DCGETTEXT 1
 
+/* Define if you have the <dirent.h> header file. */
+#define HAVE_DIRENT_H 1
+
 /* Define if you have the <dlfcn.h> header file. */
 /* #undef HAVE_DLFCN_H */
 
diff --git a/glib/gdir.c b/glib/gdir.c
new file mode 100644 (file)
index 0000000..eab0a6c
--- /dev/null
@@ -0,0 +1,136 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * gdir.c: Simplified wrapper around the DIRENT functions.
+ *
+ * Copyright 2001 Hans Breuer
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include <string.h> /* strerror, strcmp */
+#ifdef HAVE_DIRENT_H
+#include <dirent.h>
+#endif
+
+#include "glib.h"
+#include "gdir.h"
+
+#include "glibintl.h"
+
+struct _GDir
+{
+  /*< private >*/
+  DIR *dir;
+};
+
+/**
+ * g_dir_open:
+ * @path: the path to the directory you are interested in
+ * @flags: for future binary compatible extensions 
+ * @error: return location for a #GError
+ *
+ * Return value: a #GDir* on success, NULL if error is set
+ **/
+GDir *
+g_dir_open (const gchar  *path,
+            guint         flags,
+            GError      **error)
+{
+  GDir *dir = g_new (GDir, 1);
+
+  dir->dir = opendir (path);
+
+  if (dir->dir)
+    return dir;
+
+  /* error case */
+  g_set_error (error,
+               G_FILE_ERROR,
+               g_file_error_from_errno (errno),
+               _("Error opening dir '%s': %s"),
+                 path, strerror (errno));
+
+  g_free (dir);
+  return NULL;
+}
+
+/**
+ * g_dir_read_name:
+ * @dir: a #GDir* created by g_dir_open()
+ *
+ * Iterator which delivers the next directory entries name
+ * with each call. The '.' and '..' entries are omitted.
+ *
+ * BTW: using these functions will simplify porting of
+ * your app, at least to Windows.
+ *
+ * Return value: The entries name or NULL if there are no 
+ * more entries. Don't free this value!
+ **/
+G_CONST_RETURN gchar*
+g_dir_read_name (GDir    *dir)
+{
+  struct dirent *entry;
+
+  g_return_val_if_fail (dir != NULL, NULL);
+
+  entry = readdir (dir->dir);
+  while (entry 
+         && (   0 == strcmp (entry->d_name, ".") 
+             || 0 == strcmp (entry->d_name, "..")))
+    entry = readdir (dir->dir);
+
+  return entry->d_name;
+}
+
+/**
+ * g_dir_rewind:
+ * @dir: a #GDir* created by g_dir_open()
+ *
+ * Resets the given directory. The next call to g_dir_read_name()
+ * will return the first entry again.
+ **/
+void
+g_dir_rewind (GDir *dir)
+{
+  g_return_if_fail (dir != NULL);
+  
+  rewinddir (dir->dir);
+}
+
+/**
+ * g_dir_close:
+ * @dir: a #GDir* created by g_dir_open()
+ *
+ * Closes the directory and deallocates all related resources.
+ *
+ * Return value: TRUE on success, FALSE otherwise.
+ **/
+gboolean
+g_dir_close (GDir *dir)
+{
+  int ret = 0;
+
+  g_return_val_if_fail (dir != NULL, FALSE);
+
+  ret = closedir (dir->dir);
+  g_free (dir);
+
+  return !ret; 
+}
diff --git a/glib/gdir.h b/glib/gdir.h
new file mode 100644 (file)
index 0000000..8206191
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef __G_DIR_H__
+#define __G_DIR_H__
+
+#include <glib/gerror.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GDir GDir;
+
+GDir    *g_dir_open (const gchar *path, guint flags, GError **error);
+G_CONST_RETURN
+gchar   *g_dir_read_name (GDir *dir);
+void     g_dir_rewind (GDir *dir);
+gboolean g_dir_close (GDir *dir);
+
+G_END_DECLS
+
+#endif /* __G_DIR_H__ */
index 043eaf46379a9c9944d24ff99da5ee00960a879b..a258f9a570eba4abbdbf95be7e456b2f10fb8a3b 100644 (file)
@@ -9,7 +9,7 @@ TOP = ..\..
 
 INCLUDES = -I . -I ..
 DEFINES = -DHAVE_CONFIG_H -DGLIB_COMPILATION -DG_LOG_DOMAIN=g_log_domain_glib -DG_ENABLE_DEBUG
-DEPCFLAGS = $(INTL_CFLAGS) $(LIBICONV_CFLAGS)
+DEPCFLAGS = $(INTL_CFLAGS) $(LIBICONV_CFLAGS) $(DIRENT_CFLAGS)
 
 all :                          \
        ..\config.h             \
@@ -28,6 +28,7 @@ glib_OBJECTS =                        \
        gconvert.obj            \
        gdataset.obj            \
        gdate.obj               \
+       gdir.obj        \
        gerror.obj              \
        gfileutils.obj          \
        ghash.obj               \
@@ -81,7 +82,7 @@ glib-@GLIB_MAJOR_VERSION@.@GLIB_MINOR_VERSION@s.lib : $(glib_OBJECTS)
        lib /out:glib-@GLIB_MAJOR_VERSION@.@GLIB_MINOR_VERSION@s.lib $(glib_OBJECTS)
 
 libglib-@GLIB_MAJOR_VERSION@.@GLIB_MINOR_VERSION@-@LT_CURRENT_MINUS_AGE@.dll : $(glib_OBJECTS) glib.def
-       $(CC) $(CFLAGS) -LD -Fe$@ $(glib_OBJECTS) $(LIBICONV_LIBS) $(INTL_LIBS) user32.lib advapi32.lib wsock32.lib $(LDFLAGS) /implib:glib-@GLIB_MAJOR_VERSION@.@GLIB_MINOR_VERSION@.lib /def:glib.def
+       $(CC) $(CFLAGS) -LD -Fe$@ $(glib_OBJECTS) $(LIBICONV_LIBS) $(INTL_LIBS) $(DIRENT_LIBS) user32.lib advapi32.lib wsock32.lib $(LDFLAGS) /implib:glib-@GLIB_MAJOR_VERSION@.@GLIB_MINOR_VERSION@.lib /def:glib.def
 
 gspawn-win32-helper.exe : gspawn-win32-helper.c libglib-@GLIB_MAJOR_VERSION@.@GLIB_MINOR_VERSION@-@LT_CURRENT_MINUS_AGE@.dll
        $(CC) $(CFLAGS) -Fe$@ -DG_LOG_DOMAIN=\"gspawn-win32-helper\" gspawn-win32-helper.c glib-@GLIB_MAJOR_VERSION@.@GLIB_MINOR_VERSION@.lib $(LDFLAGS) /subsystem:windows user32.lib