changeset 21926:24215a16f3b2

hide dirent.h header * liboctave/wrappers/dirent-wrappers.c, liboctave/wrappers/dirent-wrappers.h: New files. * liboctave/wrappers/module.mk: Update. * dir-ops.cc, dir-ops.h: Use new wrapper functions. (dir_entry::close): Return status. (dir_entry::max_name_length): New function. * kpse.cc: Use octave::sys::dir_entry::max_name_length function instead of NAME_MAX macro.
author John W. Eaton <jwe@octave.org>
date Thu, 16 Jun 2016 10:02:49 -0400
parents a53f46577e39
children 6f62bd248919
files liboctave/system/dir-ops.cc liboctave/system/dir-ops.h liboctave/util/kpse.cc liboctave/wrappers/dirent-wrappers.c liboctave/wrappers/dirent-wrappers.h liboctave/wrappers/module.mk
diffstat 6 files changed, 155 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/system/dir-ops.cc	Thu Jun 16 08:42:29 2016 -0700
+++ b/liboctave/system/dir-ops.cc	Thu Jun 16 10:02:49 2016 -0400
@@ -31,7 +31,7 @@
 #include <list>
 #include <string>
 
-#include <dirent.h>
+#include "dirent-wrappers.h"
 
 #include "dir-ops.h"
 #include "file-ops.h"
@@ -46,8 +46,6 @@
     bool
     dir_entry::open (const std::string& n)
     {
-      fail = true;
-
       if (! n.empty ())
         name = n;
 
@@ -57,17 +55,15 @@
 
           std::string fullname = octave::sys::file_ops::tilde_expand (name);
 
-          dir = static_cast<void *> (gnulib::opendir (fullname.c_str ()));
+          dir = octave_opendir_wrapper (fullname.c_str ());
 
-          if (dir)
-            fail = false;
-          else
+          if (! dir)
             errmsg = gnulib::strerror (errno);
         }
       else
         errmsg = "dir_entry::open: empty filename";
 
-      return ! fail;
+      return dir != 0;
     }
 
     string_vector
@@ -79,15 +75,10 @@
         {
           std::list<std::string> dirlist;
 
-          struct dirent *dir_ent;
+          char *fname;
 
-          while ((dir_ent = gnulib::readdir (static_cast<DIR *> (dir))))
-            {
-              if (dir_ent)
-                dirlist.push_back (dir_ent->d_name);
-              else
-                break;
-            }
+          while ((fname = octave_readdir_wrapper (dir)))
+            dirlist.push_back (fname);
 
           retval = string_vector (dirlist);
         }
@@ -95,13 +86,20 @@
       return retval;
     }
 
-    void
+    bool
     dir_entry::close (void)
     {
-      if (dir)
-        gnulib::closedir (static_cast<DIR *> (dir));
+      bool retval = (octave_closedir_wrapper (dir) == 0);
 
       dir = 0;
+
+      return retval;
+    }
+
+    unsigned int
+    dir_entry::max_name_length (void)
+    {
+      return octave_name_max_wrapper ();
     }
   }
 }
--- a/liboctave/system/dir-ops.h	Thu Jun 16 08:42:29 2016 -0700
+++ b/liboctave/system/dir-ops.h	Thu Jun 16 10:02:49 2016 -0400
@@ -68,7 +68,7 @@
 
       string_vector read (void);
 
-      void close (void);
+      bool close (void);
 
       bool ok (void) const { return dir && ! fail; }
 
@@ -76,6 +76,8 @@
 
       std::string error (void) const { return ok () ? "" : errmsg; }
 
+      static unsigned int max_name_length (void);
+
     private:
 
       // Name of the directory.
--- a/liboctave/util/kpse.cc	Thu Jun 16 08:42:29 2016 -0700
+++ b/liboctave/util/kpse.cc	Thu Jun 16 10:02:49 2016 -0400
@@ -40,9 +40,7 @@
 #include <iostream>
 #include <string>
 
-// Needed for NAME_MAX.
-#include <dirent.h>
-
+#include "dir-ops.h"
 #include "file-ops.h"
 #include "file-stat.h"
 #include "kpse.h"
@@ -70,23 +68,6 @@
 #define ENV_SEP_STRING octave::directory_path::path_sep_str ()
 #define IS_ENV_SEP(ch) octave::directory_path::is_path_sep (ch)
 
-// Define NAME_MAX, the maximum length of a single component in a
-// filename.  No such limit may exist, or may vary depending on the
-// filesystem.  We don't use this value to create a character string, we
-// only truncate given file names to this length if attempts to get info
-// about the file fail with errno == ENAMETOOLONG.
-
-// Most likely the system will truncate filenames if it is not POSIX,
-// and so we can use the BSD value here.
-
-#if ! defined (_POSIX_NAME_MAX)
-#  define _POSIX_NAME_MAX 255
-#endif
-
-#if ! defined (NAME_MAX)
-#  define NAME_MAX _POSIX_NAME_MAX
-#endif
-
 // If NO_DEBUG is defined (not recommended), skip all this.
 #if ! defined (NO_DEBUG)
 
@@ -164,7 +145,7 @@
           /* At a directory delimiter, reset component length.  */
           c_len = 0;
         }
-      else if (c_len > NAME_MAX)
+      else if (c_len > dir_entry::max_name_length ())
         {
           /* If past the max for a component, ignore this character.  */
           continue;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/wrappers/dirent-wrappers.c	Thu Jun 16 10:02:49 2016 -0400
@@ -0,0 +1,83 @@
+/*
+
+Copyright (C) 2016 John W. Eaton
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+Octave 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 General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+// These functions may be provided by gnulib.  We don't include gnulib
+// headers directly in Octave's C++ source files to avoid problems that
+// may be caused by the way that gnulib overrides standard library
+// functions.
+
+#if defined (HAVE_CONFIG_H)
+#  include "config.h"
+#endif
+
+#include <dirent.h>
+
+#include "dirent-wrappers.h"
+
+void *
+octave_opendir_wrapper (const char *dname)
+{
+  return opendir (dname);
+}
+
+char *
+octave_readdir_wrapper (void *dir)
+{
+  struct dirent *d_ent = readdir ((DIR *) dir);
+
+  return d_ent ? d_ent->d_name : 0;
+}
+
+void
+octave_rewinddir_wrapper (void *dir)
+{
+  rewinddir ((DIR *) dir);
+}
+  
+int
+octave_closedir_wrapper (void *dir)
+{
+  return closedir ((DIR *) dir);
+}
+
+// Define NAME_MAX, the maximum length of a single component in a
+// filename.  No such limit may exist, or may vary depending on the
+// filesystem.  This value should be avoided for creating character
+// strings and used only to truncate given file names to this length if
+// attempts to get info about the file fail with errno == ENAMETOOLONG.
+
+// Most likely the system will truncate filenames if it is not POSIX,
+// and so we can use the BSD value here.
+
+#if ! defined (_POSIX_NAME_MAX)
+#  define _POSIX_NAME_MAX 255
+#endif
+
+#if ! defined (NAME_MAX)
+#  define NAME_MAX _POSIX_NAME_MAX
+#endif
+
+unsigned int
+octave_name_max_wrapper (void)
+{
+  return NAME_MAX;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/liboctave/wrappers/dirent-wrappers.h	Thu Jun 16 10:02:49 2016 -0400
@@ -0,0 +1,48 @@
+/*
+
+Copyright (C) 2016 John W. Eaton
+
+This file is part of Octave.
+
+Octave is free software; you can redistribute it and/or modify it
+under the terms of the GNU General Public License as published by the
+Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+Octave 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 General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with Octave; see the file COPYING.  If not, see
+<http://www.gnu.org/licenses/>.
+
+*/
+
+#if ! defined (octave_dirent_wrappers_h)
+#define octave_dirent_wrappers_h 1
+
+#if ! defined (__cplusplus)
+#  include <stdbool.h>
+#endif
+
+#if defined __cplusplus
+extern "C" {
+#endif
+
+extern void *octave_opendir_wrapper (const char *dname);
+
+extern char *octave_readdir_wrapper (void *dir);
+
+extern void octave_rewinddir_wrapper (void *dir);
+  
+extern int octave_closedir_wrapper (void *dir);
+
+extern unsigned int octave_name_max_wrapper (void);
+
+#if defined __cplusplus
+}
+#endif
+
+#endif
--- a/liboctave/wrappers/module.mk	Thu Jun 16 08:42:29 2016 -0700
+++ b/liboctave/wrappers/module.mk	Thu Jun 16 10:02:49 2016 -0400
@@ -2,6 +2,7 @@
   liboctave/wrappers/areadlink-wrapper.h \
   liboctave/wrappers/base64-wrappers.h \
   liboctave/wrappers/canonicalize-file-name-wrapper.h \
+  liboctave/wrappers/dirent-wrappers.h \
   liboctave/wrappers/fcntl-wrappers.h \
   liboctave/wrappers/filepos-wrappers.h \
   liboctave/wrappers/fpucw-wrapper.h \
@@ -28,6 +29,7 @@
   liboctave/wrappers/areadlink-wrapper.c \
   liboctave/wrappers/base64-wrappers.c \
   liboctave/wrappers/canonicalize-file-name-wrapper.c \
+  liboctave/wrappers/dirent-wrappers.c \
   liboctave/wrappers/fcntl-wrappers.c \
   liboctave/wrappers/filepos-wrappers.c \
   liboctave/wrappers/fpucw-wrapper.c \