os/ossrv/glib/glib/gdir.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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  * Copyright 2004 Tor Lillqvist
     8  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
     9  *
    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.
    14  *
    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.
    19  *
    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.
    24  */
    25 
    26 #include "config.h"
    27 
    28 #include <errno.h>
    29 #include <string.h>
    30 #include <stdio.h>
    31 #include <sys/stat.h>
    32 
    33 #ifdef HAVE_DIRENT_H
    34 #include <sys/types.h>
    35 #include <dirent.h>
    36 #endif
    37 
    38 #include "glib.h"
    39 #include "gdir.h"
    40 
    41 #include "glibintl.h"
    42 
    43 #include "galias.h"
    44 
    45 #if defined (_MSC_VER) && !defined (HAVE_DIRENT_H)
    46 #include "../build/win32/dirent/dirent.h"
    47 #include "../build/win32/dirent/wdirent.c"
    48 #endif
    49 
    50 struct _GDir
    51 {
    52 #ifdef G_OS_WIN32
    53   _WDIR *wdirp;
    54 #else
    55   DIR *dirp;
    56 #endif
    57 #ifdef G_OS_WIN32
    58   gchar utf8_buf[FILENAME_MAX*4];
    59 #endif
    60 };
    61 
    62 /**
    63  * g_dir_open:
    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
    69  *         g_dir_open() fails.
    70  *
    71  * Opens a directory for reading. The names of the files in the
    72  * directory can then be retrieved using g_dir_read_name().
    73  *
    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.
    77  **/
    78 EXPORT_C GDir *
    79 g_dir_open (const gchar  *path,
    80             guint         flags,
    81             GError      **error)
    82 {
    83   GDir *dir;
    84 #ifdef G_OS_WIN32
    85   wchar_t *wpath;
    86 #else
    87   gchar *utf8_path;
    88 #endif
    89 
    90   g_return_val_if_fail (path != NULL, NULL);
    91 
    92 #ifdef G_OS_WIN32
    93   wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, error);
    94 
    95   if (wpath == NULL)
    96     return NULL;
    97 
    98   dir = g_new (GDir, 1);
    99 
   100   dir->wdirp = _wopendir (wpath);
   101   g_free (wpath);
   102 
   103   if (dir->wdirp)
   104     return dir;
   105 
   106   /* error case */
   107 
   108   g_set_error (error,
   109 	       G_FILE_ERROR,
   110 	       g_file_error_from_errno (errno),
   111 	       _("Error opening directory '%s': %s"),
   112 	       path, g_strerror (errno));
   113   
   114   g_free (dir);
   115       
   116   return NULL;
   117 #else
   118   dir = g_new (GDir, 1);
   119 
   120   dir->dirp = opendir (path);
   121 
   122   if (dir->dirp)
   123     return dir;
   124 
   125   /* error case */
   126   utf8_path = g_filename_to_utf8 (path, -1,
   127 				  NULL, NULL, NULL);
   128   g_set_error (error,
   129                G_FILE_ERROR,
   130                g_file_error_from_errno (errno),
   131                _("Error opening directory '%s': %s"),
   132 	       utf8_path, g_strerror (errno));
   133 
   134   g_free (utf8_path);
   135   g_free (dir);
   136 
   137   return NULL;
   138 #endif
   139 }
   140 
   141 #if defined (G_OS_WIN32) && !defined (_WIN64)
   142 
   143 /* The above function actually is called g_dir_open_utf8, and it's
   144  * that what applications compiled with this GLib version will
   145  * use.
   146  */
   147 
   148 #undef g_dir_open
   149 
   150 /* Binary compatibility version. Not for newly compiled code. */
   151 
   152 GDir *
   153 g_dir_open (const gchar  *path,
   154             guint         flags,
   155             GError      **error)
   156 {
   157   gchar *utf8_path = g_locale_to_utf8 (path, -1, NULL, NULL, error);
   158   GDir *retval;
   159 
   160   if (utf8_path == NULL)
   161     return NULL;
   162 
   163   retval = g_dir_open_utf8 (utf8_path, flags, error);
   164 
   165   g_free (utf8_path);
   166 
   167   return retval;
   168 }
   169 #endif
   170 
   171 /**
   172  * g_dir_read_name:
   173  * @dir: a #GDir* created by g_dir_open()
   174  *
   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.
   178  *
   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.
   182  **/
   183 EXPORT_C G_CONST_RETURN gchar*
   184 g_dir_read_name (GDir *dir)
   185 {
   186 #ifdef G_OS_WIN32
   187   gchar *utf8_name;
   188   struct _wdirent *wentry;
   189 #else
   190   struct dirent *entry;
   191 #endif
   192 
   193   g_return_val_if_fail (dir != NULL, NULL);
   194 
   195 #ifdef G_OS_WIN32
   196   while (1)
   197     {
   198       wentry = _wreaddir (dir->wdirp);
   199       while (wentry 
   200 	     && (0 == wcscmp (wentry->d_name, L".") ||
   201 		 0 == wcscmp (wentry->d_name, L"..")))
   202 	wentry = _wreaddir (dir->wdirp);
   203 
   204       if (wentry == NULL)
   205 	return NULL;
   206 
   207       utf8_name = g_utf16_to_utf8 (wentry->d_name, -1, NULL, NULL, NULL);
   208 
   209       if (utf8_name == NULL)
   210 	continue;		/* Huh, impossible? Skip it anyway */
   211 
   212       strcpy (dir->utf8_buf, utf8_name);
   213       g_free (utf8_name);
   214 
   215       return dir->utf8_buf;
   216     }
   217 #else
   218   entry = readdir (dir->dirp);
   219   while (entry 
   220          && (0 == strcmp (entry->d_name, ".") ||
   221              0 == strcmp (entry->d_name, "..")))
   222     entry = readdir (dir->dirp);
   223 
   224   if (entry)
   225     return entry->d_name;
   226   else
   227     return NULL;
   228 #endif
   229 }
   230 
   231 #if defined (G_OS_WIN32) && !defined (_WIN64)
   232 
   233 /* Ditto for g_dir_read_name */
   234 
   235 #undef g_dir_read_name
   236 
   237 /* Binary compatibility version. Not for newly compiled code. */
   238 
   239 G_CONST_RETURN gchar*
   240 g_dir_read_name (GDir *dir)
   241 {
   242   while (1)
   243     {
   244       const gchar *utf8_name = g_dir_read_name_utf8 (dir);
   245       gchar *retval;
   246       
   247       if (utf8_name == NULL)
   248 	return NULL;
   249 
   250       retval = g_locale_from_utf8 (utf8_name, -1, NULL, NULL, NULL);
   251 
   252       if (retval != NULL)
   253 	{
   254 	  strcpy (dir->utf8_buf, retval);
   255 	  g_free (retval);
   256 
   257 	  return dir->utf8_buf;
   258 	}
   259     }
   260 }
   261 
   262 #endif
   263 
   264 /**
   265  * g_dir_rewind:
   266  * @dir: a #GDir* created by g_dir_open()
   267  *
   268  * Resets the given directory. The next call to g_dir_read_name()
   269  * will return the first entry again.
   270  **/
   271 EXPORT_C void
   272 g_dir_rewind (GDir *dir)
   273 {
   274   g_return_if_fail (dir != NULL);
   275   
   276 #ifdef G_OS_WIN32
   277   _wrewinddir (dir->wdirp);
   278 #else
   279   rewinddir (dir->dirp);
   280 #endif
   281 }
   282 
   283 /**
   284  * g_dir_close:
   285  * @dir: a #GDir* created by g_dir_open()
   286  *
   287  * Closes the directory and deallocates all related resources.
   288  **/
   289 EXPORT_C void
   290 g_dir_close (GDir *dir)
   291 {
   292   g_return_if_fail (dir != NULL);
   293 
   294 #ifdef G_OS_WIN32
   295   _wclosedir (dir->wdirp);
   296 #else
   297   closedir (dir->dirp);
   298 #endif
   299   g_free (dir);
   300 }
   301 
   302 #define __G_DIR_C__
   303 #include "galiasdef.c"