GDir as DIR replacement - patch



As some of you may have noticed recently the plain 
DIR emulation was removed from glib (win32) to
avoid namespace pollution.

[see:
  http://mail.gnome.org/archives/gtk-devel-list/2001-October/msg00001.html
  http://mail.gnome.org/archives/gtk-devel-list/2001-October/msg00511.html
]

To fill the gap (and to not require a small extra lib
within most of the GTK+ libs and apps) it would be
nice if some g_dir_* functions could be included in glib.

If the following patch is acceptable I would obviously 
volunteer to write inline docs, a test app and remove all 
the DIR occurences from 'downstream' libs and apps ported 
to win32.

Thanks in advance,
	Hans 

#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, GError **error);
int      g_dir_len (GDir *dir);
G_CONST_RETURN gchar *g_dir_read_name (GDir *dir);
void     g_dir_rewind (GDir *dir);
int      g_dir_tell (GDir *dir);
void     g_dir_seek (GDir *dir, int len);
gboolean g_dir_close (GDir *dir, GError **error);


G_END_DECLS

#endif /* __G_DIR_H__ */

/* GLIB - Library of useful routines for C programming
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * gdir.c: 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 */
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif

#include "glib.h"
#include "gdir.h"

#include "glibintl.h"

struct _GDir
{
  /*< private >*/
  DIR *dir;
  int  len;
};

GDir *
g_dir_open (const gchar  *path, 
            GError      **error)
{
  GDir *dir = g_new (GDir, 1);

  dir->dir = opendir (path);
  dir->len = -1;

  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;
}

int
g_dir_len (GDir *dir)
{
  int pos;
  struct dirent *entry;

  g_return_val_if_fail (dir != NULL, -1);
  g_return_val_if_fail (dir->dir != NULL, -1);

  if (dir->len > -1)
    return dir->len; /* cached */

  pos = telldir (dir->dir);
  rewinddir (dir->dir);

  dir->len = 0;

  while ((entry = readdir (dir->dir)) != NULL)
    dir->len++;

  seekdir (dir->dir, pos);
  return dir->len;
}

G_CONST_RETURN gchar*
g_dir_read_name (GDir *dir)
{
  struct dirent *entry;

  g_return_val_if_fail (dir != NULL, NULL);
  g_return_val_if_fail (dir->dir != NULL, NULL);

  entry = readdir (dir->dir);

  if (entry != NULL)
    return entry->d_name;

  return NULL;
}

void
g_dir_rewind (GDir *dir)
{
  g_return_if_fail (dir != NULL);
  g_return_if_fail (dir->dir != NULL);
  
  rewinddir (dir->dir);
}

int
g_dir_tell (GDir *dir)
{
  g_return_val_if_fail (dir != NULL, -1);
  g_return_val_if_fail (dir->dir != NULL, -1);
  
  return telldir (dir->dir);
}

void
g_dir_seek (GDir *dir, int len)
{
  g_return_if_fail (dir != NULL);
  g_return_if_fail (dir->dir != NULL);
  
  seekdir (dir->dir, len);
}

gboolean
g_dir_close (GDir *dir, GError **error)
{
  int ret = 0;

  g_return_val_if_fail (dir != NULL, FALSE);
  g_return_val_if_fail (dir->dir != NULL, FALSE);

  ret = closedir (dir->dir);

  if (ret)
    {
      /* error case */
      g_set_error (error,
                   G_FILE_ERROR,
                   g_file_error_from_errno (errno),
                   _("Error closeing dir : %s"),
                   strerror (errno));
    }

  return !ret; 
}

-------- Hans "at" Breuer "dot" Org -----------
Tell me what you need, and I'll tell you how to 
get along without it.                -- Dilbert



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]