Update contrib.
     1 /* GLIB - Library of useful routines for C programming
 
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 
     4  * gdir.c: Simplified wrapper around the DIRENT functions.
 
     6  * Copyright 2001 Hans Breuer
 
     7  * Copyright 2004 Tor Lillqvist
 
     8  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
 
    10  * This library is free software; you can redistribute it and/or
 
    11  * modify it under the terms of the GNU Lesser General Public
 
    12  * License as published by the Free Software Foundation; either
 
    13  * version 2 of the License, or (at your option) any later version.
 
    15  * This library is distributed in the hope that it will be useful,
 
    16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
    17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
    18  * Lesser General Public License for more details.
 
    20  * You should have received a copy of the GNU Lesser General Public
 
    21  * License along with this library; if not, write to the
 
    22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
    23  * Boston, MA 02111-1307, USA.
 
    34 #include <sys/types.h>
 
    45 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
 
    46 #include "../build/win32/dirent/dirent.h"
 
    47 #include "../build/win32/dirent/wdirent.c"
 
    58   gchar utf8_buf[FILENAME_MAX*4];
 
    64  * @path: the path to the directory you are interested in. On Unix
 
    65  *         in the on-disk encoding. On Windows in UTF-8
 
    66  * @flags: Currently must be set to 0. Reserved for future use.
 
    67  * @error: return location for a #GError, or %NULL.
 
    68  *         If non-%NULL, an error will be set if and only if
 
    71  * Opens a directory for reading. The names of the files in the
 
    72  * directory can then be retrieved using g_dir_read_name().
 
    74  * Return value: a newly allocated #GDir on success, %NULL on failure.
 
    75  *   If non-%NULL, you must free the result with g_dir_close()
 
    76  *   when you are finished with it.
 
    79 g_dir_open (const gchar  *path,
 
    90   g_return_val_if_fail (path != NULL, NULL);
 
    93   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
 
    98   dir = g_new (GDir, 1);
 
   100   dir->wdirp = _wopendir (wpath);
 
   110 	       g_file_error_from_errno (errno),
 
   111 	       _("Error opening directory '%s': %s"),
 
   112 	       path, g_strerror (errno));
 
   118   dir = g_new (GDir, 1);
 
   120   dir->dirp = opendir (path);
 
   126   utf8_path = g_filename_to_utf8 (path, -1,
 
   130                g_file_error_from_errno (errno),
 
   131                _("Error opening directory '%s': %s"),
 
   132 	       utf8_path, g_strerror (errno));
 
   141 #if defined (G_OS_WIN32) && !defined (_WIN64)
 
   143 /* The above function actually is called g_dir_open_utf8, and it's
 
   144  * that what applications compiled with this GLib version will
 
   150 /* Binary compatibility version. Not for newly compiled code. */
 
   153 g_dir_open (const gchar  *path,
 
   157   gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
 
   160   if (utf8_path == NULL)
 
   163   retval = g_dir_open_utf8 (utf8_path, flags, error);
 
   173  * @dir: a #GDir* created by g_dir_open()
 
   175  * Retrieves the name of the next entry in the directory.  The '.' and
 
   176  * '..' entries are omitted. On Windows, the returned name is in
 
   177  * UTF-8. On Unix, it is in the on-disk encoding.
 
   179  * Return value: The entry's name or %NULL if there are no 
 
   180  *   more entries. The return value is owned by GLib and
 
   181  *   must not be modified or freed.
 
   183 EXPORT_C G_CONST_RETURN gchar*
 
   184 g_dir_read_name (GDir *dir)
 
   188   struct _wdirent *wentry;
 
   190   struct dirent *entry;
 
   193   g_return_val_if_fail (dir != NULL, NULL);
 
   198       wentry = _wreaddir (dir->wdirp);
 
   200 	     && (0 == wcscmp (wentry->d_name, L".") ||
 
   201 		 0 == wcscmp (wentry->d_name, L"..")))
 
   202 	wentry = _wreaddir (dir->wdirp);
 
   207       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
 
   209       if (utf8_name == NULL)
 
   210 	continue;		/* Huh, impossible? Skip it anyway */
 
   212       strcpy (dir->utf8_buf, utf8_name);
 
   215       return dir->utf8_buf;
 
   218   entry = readdir (dir->dirp);
 
   220          && (0 == strcmp (entry->d_name, ".") ||
 
   221              0 == strcmp (entry->d_name, "..")))
 
   222     entry = readdir (dir->dirp);
 
   225     return entry->d_name;
 
   231 #if defined (G_OS_WIN32) && !defined (_WIN64)
 
   233 /* Ditto for g_dir_read_name */
 
   235 #undef g_dir_read_name
 
   237 /* Binary compatibility version. Not for newly compiled code. */
 
   239 G_CONST_RETURN gchar*
 
   240 g_dir_read_name (GDir *dir)
 
   244       const gchar *utf8_name = g_dir_read_name_utf8 (dir);
 
   247       if (utf8_name == NULL)
 
   250       retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
 
   254 	  strcpy (dir->utf8_buf, retval);
 
   257 	  return dir->utf8_buf;
 
   266  * @dir: a #GDir* created by g_dir_open()
 
   268  * Resets the given directory. The next call to g_dir_read_name()
 
   269  * will return the first entry again.
 
   272 g_dir_rewind (GDir *dir)
 
   274   g_return_if_fail (dir != NULL);
 
   277   _wrewinddir (dir->wdirp);
 
   279   rewinddir (dir->dirp);
 
   285  * @dir: a #GDir* created by g_dir_open()
 
   287  * Closes the directory and deallocates all related resources.
 
   290 g_dir_close (GDir *dir)
 
   292   g_return_if_fail (dir != NULL);
 
   295   _wclosedir (dir->wdirp);
 
   297   closedir (dir->dirp);
 
   303 #include "galiasdef.c"