From 3a86505c77f967e891a68082b0bc28bd045f1192 Mon Sep 17 00:00:00 2001 From: Hans Breuer Date: Mon, 5 Nov 2001 01:15:38 +0000 Subject: [PATCH] simplified wrapper around dirent functions to improve portability of 2001-11-04 Hans Breuer * 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 --- ChangeLog | 8 +++ ChangeLog.pre-2-0 | 8 +++ ChangeLog.pre-2-10 | 8 +++ ChangeLog.pre-2-12 | 8 +++ ChangeLog.pre-2-2 | 8 +++ ChangeLog.pre-2-4 | 8 +++ ChangeLog.pre-2-6 | 8 +++ ChangeLog.pre-2-8 | 8 +++ config.h.win32.in | 3 + glib/gdir.c | 136 +++++++++++++++++++++++++++++++++++++++++++ glib/gdir.h | 18 ++++++ glib/makefile.msc.in | 5 +- 12 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 glib/gdir.c create mode 100644 glib/gdir.h diff --git a/ChangeLog b/ChangeLog index 6d0d52ca..739b9269 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2001-11-04 Hans Breuer + + * 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 * tests/Makefile.am: On Win32, create separate .exp file for diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index 6d0d52ca..739b9269 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,11 @@ +2001-11-04 Hans Breuer + + * 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 * tests/Makefile.am: On Win32, create separate .exp file for diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 6d0d52ca..739b9269 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,11 @@ +2001-11-04 Hans Breuer + + * 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 * tests/Makefile.am: On Win32, create separate .exp file for diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 6d0d52ca..739b9269 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,11 @@ +2001-11-04 Hans Breuer + + * 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 * tests/Makefile.am: On Win32, create separate .exp file for diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index 6d0d52ca..739b9269 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,11 @@ +2001-11-04 Hans Breuer + + * 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 * tests/Makefile.am: On Win32, create separate .exp file for diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index 6d0d52ca..739b9269 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,11 @@ +2001-11-04 Hans Breuer + + * 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 * tests/Makefile.am: On Win32, create separate .exp file for diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index 6d0d52ca..739b9269 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,11 @@ +2001-11-04 Hans Breuer + + * 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 * tests/Makefile.am: On Win32, create separate .exp file for diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index 6d0d52ca..739b9269 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,11 @@ +2001-11-04 Hans Breuer + + * 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 * tests/Makefile.am: On Win32, create separate .exp file for diff --git a/config.h.win32.in b/config.h.win32.in index dad493d3..dae918ee 100644 --- a/config.h.win32.in +++ b/config.h.win32.in @@ -158,6 +158,9 @@ /* Define if you have the `dcgettext' function. */ #define HAVE_DCGETTEXT 1 +/* Define if you have the header file. */ +#define HAVE_DIRENT_H 1 + /* Define if you have the header file. */ /* #undef HAVE_DLFCN_H */ diff --git a/glib/gdir.c b/glib/gdir.c new file mode 100644 index 00000000..eab0a6c7 --- /dev/null +++ b/glib/gdir.c @@ -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 /* strerror, strcmp */ +#ifdef HAVE_DIRENT_H +#include +#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 index 00000000..82061911 --- /dev/null +++ b/glib/gdir.h @@ -0,0 +1,18 @@ +#ifndef __G_DIR_H__ +#define __G_DIR_H__ + +#include + +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__ */ diff --git a/glib/makefile.msc.in b/glib/makefile.msc.in index 043eaf46..a258f9a5 100644 --- a/glib/makefile.msc.in +++ b/glib/makefile.msc.in @@ -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 -- 2.34.1