changeset 8007:a2ab20ba78f7

make file_ops a proper singleton class
author John W. Eaton <jwe@octave.org>
date Mon, 04 Aug 2008 23:44:50 -0400
parents b0e7bbe7cd47
children 4d13a7a2f6ab
files liboctave/ChangeLog liboctave/file-ops.cc liboctave/file-ops.h liboctave/oct-env.cc src/ChangeLog src/defaults.cc src/dirfns.cc src/graphics.cc src/help.cc src/load-path.cc src/load-save.cc src/ls-mat5.cc src/octave.cc src/ov-fcn-handle.cc src/parse.y src/symtab.cc src/variables.cc
diffstat 17 files changed, 159 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/ChangeLog	Mon Aug 04 22:32:47 2008 -0400
+++ b/liboctave/ChangeLog	Mon Aug 04 23:44:50 2008 -0400
@@ -1,5 +1,27 @@
 2008-08-04  John W. Eaton  <jwe@octave.org>
 
+	* oct-env.cc (octave_env::do_set_program_name,
+	octave_env::do_base_pathname): Fix usage of
+	file_ops::dir_sep_chars.
+	(octave_env::do_make_absolute): Fix usage of
+	file_ops::dir_sep_chars and file_ops::dir_sep_str.
+	(octave_env::do_get_home_directory): Fix usage of
+	file_ops::dir_sep_str.
+
+	* file-ops.h (file_ops::do_is_dir_sep): New function.
+	(file_ops_::is_dir_sep): Call it.
+	* file-ops.cc (class file_ops): Make it a proper singleton object.
+	(file_ops::file_ops): New constructor.
+	(file_ops::instance_ok): New function.
+	(file_ops::xdir_sep_char): Now private.  No longer static.  Rename
+	from dir_sep_char.
+	(file_ops::xdir_sep_str): Likewise, from dir_sep_str.
+	(file_ops::xdir_sep_chars): Likewise, from dir_sep_chars.
+	(file_ops::dir_sep_char, file_ops::dir_sep_str,
+	file_ops::dir_sep_chars): New functions.
+	(file_ops::recursive_rmdir): Fix usage of file_ops::dir_sep_str.
+	(file_ops::concat): Fix usage of file_ops::dir_sep_char.
+
 	* oct-env.cc (octave_env::instance_ok): Fix typo in error message.
 
 2008-07-30  John W. Eaton  <jwe@octave.org>
--- a/liboctave/file-ops.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/liboctave/file-ops.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -51,24 +51,46 @@
 #include "statdefs.h"
 #include "str-vec.h"
 
+file_ops::file_ops (void)
+  :
+#if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
+  xdir_sep_char ('\\'),
+  xdir_sep_str ("\\"),
+#else
+  xdir_sep_char ('/'),
+  xdir_sep_str ("/"), 
+#endif
+#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
+  xdir_sep_chars ("/\\")
+#else
+  xdir_sep_chars (xdir_sep_str)
+#endif
+{ }
+
+file_ops *file_ops::instance = 0;
+
+bool
+file_ops::instance_ok (void)
+{
+  bool retval = true;
+
+  if (! instance)
+    instance = new file_ops ();
+
+  if (! instance)
+    {
+      (*current_liboctave_error_handler)
+	("unable to create file_ops object!");
+
+      retval = false;
+    }
+
+  return retval;
+}
+
 #define NOT_SUPPORTED(nm) \
   nm ": not supported on this system"
 
-#if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) \
-     && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
-char file_ops::dir_sep_char = '\\';
-std::string file_ops::dir_sep_str ("\\");
-#else
-char file_ops::dir_sep_char = '/';
-std::string file_ops::dir_sep_str ("/");
-#endif
-
-#if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
-std::string file_ops::dir_sep_chars ("/\\");
-#else
-std::string file_ops::dir_sep_chars (file_ops::dir_sep_str);
-#endif
-
 // We provide a replacement for mkdir().
 
 int
@@ -346,7 +368,7 @@
 	  if (nm == "." || nm == "..")
 	    continue;
 
-	  std::string fullnm = name + file_ops::dir_sep_str + nm;
+	  std::string fullnm = name + file_ops::dir_sep_str () + nm;
 
 	  // Get info about the file.  Don't follow links.
 	  file_stat fs (fullnm, false);
@@ -851,12 +873,6 @@
   return status;
 }
 
-bool
-file_ops::is_dir_sep (char c)
-{
-  return dir_sep_chars.find (c) != NPOS;
-}
-
 std::string
 file_ops::concat (const std::string& dir, const std::string& file)
 {
@@ -864,7 +880,7 @@
     ? file
     : (is_dir_sep (dir[dir.length()-1])
        ? dir + file
-       : dir + file_ops::dir_sep_char + file);
+       : dir + file_ops::dir_sep_char () + file);
 }
 
 /*
--- a/liboctave/file-ops.h	Mon Aug 04 22:32:47 2008 -0400
+++ b/liboctave/file-ops.h	Mon Aug 04 23:44:50 2008 -0400
@@ -35,6 +35,12 @@
 OCTAVE_API
 file_ops
 {
+protected:
+
+  file_ops (void);
+
+public:
+
   static int mkdir (const std::string&, mode_t);
   static int mkdir (const std::string&, mode_t, std::string&);
 
@@ -84,13 +90,46 @@
   static int unlink (const std::string&);
   static int unlink (const std::string&, std::string&);
 
-  static bool is_dir_sep (char);
+  static bool is_dir_sep (char c)
+  {
+    return instance_ok () ? instance->do_is_dir_sep (c) : false;
+  }
 
   static std::string concat (const std::string&, const std::string&);
 
-  static char dir_sep_char;
-  static std::string dir_sep_str;
-  static std::string dir_sep_chars;
+  static char dir_sep_char (void)
+  {
+    return instance_ok () ? instance->xdir_sep_char : 0;
+  }
+
+  static std::string dir_sep_str (void)
+  {
+    return instance_ok () ? instance->xdir_sep_str : std::string ();
+  }
+
+  static std::string dir_sep_chars (void)
+  {
+    return instance_ok () ? instance->xdir_sep_chars : std::string ();
+  }
+
+private:
+
+  // No copying!
+
+  file_ops (const file_ops&);
+
+  file_ops& operator = (const file_ops&);
+
+  // The real thing.
+  static file_ops *instance;
+
+  static bool instance_ok (void);
+
+  char xdir_sep_char;
+  std::string xdir_sep_str;
+  std::string xdir_sep_chars;
+
+  bool do_is_dir_sep (char c) { return xdir_sep_chars.find (c) != NPOS; }
 };
 
 #endif
--- a/liboctave/oct-env.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/liboctave/oct-env.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -218,7 +218,8 @@
 {
   program_invocation_name = s;
 
-  size_t pos = program_invocation_name.find_last_of (file_ops::dir_sep_chars);
+  size_t pos
+    = program_invocation_name.find_last_of (file_ops::dir_sep_chars ());
 
   program_name = (pos == NPOS)
     ? program_invocation_name : program_invocation_name.substr (pos+1);
@@ -302,7 +303,7 @@
   if (! (do_absolute_pathname (s) || do_rooted_relative_pathname (s)))
     return s;
 
-  size_t pos = s.find_last_of (file_ops::dir_sep_chars);
+  size_t pos = s.find_last_of (file_ops::dir_sep_chars ());
 
   if (pos == NPOS)
     return s;
@@ -333,7 +334,7 @@
   size_t pos = current_dir.length () - 1;
 
   if (! file_ops::is_dir_sep (current_dir[pos]))
-    current_dir.append (file_ops::dir_sep_str);
+    current_dir.append (file_ops::dir_sep_str ());
 
   // FIXME -- this is probably not correct for all systems.
 
@@ -367,7 +368,7 @@
 	    }
 	}
 
-      size_t tmp = s.find_first_of (file_ops::dir_sep_chars, i);
+      size_t tmp = s.find_first_of (file_ops::dir_sep_chars (), i);
 
       if (tmp == NPOS)
 	{
@@ -422,7 +423,7 @@
     {
       octave_passwd pw = octave_passwd::getpwuid (octave_syscalls::getuid ());
 
-      hd = pw ? pw.dir () : std::string (file_ops::dir_sep_str);
+      hd = pw ? pw.dir () : std::string (file_ops::dir_sep_str ());
     }
 
   return hd;
--- a/src/ChangeLog	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/ChangeLog	Mon Aug 04 23:44:50 2008 -0400
@@ -1,3 +1,27 @@
+2008-08-04  John W. Eaton  <jwe@octave.org>
+
+	* symtab.cc (symbol_table::fcn_info::fcn_info_rep::find_autoload):
+	Fix usage of file_ops::dir_sep_chars.
+	* variables.cc (looks_like_struct):
+	Likewise.
+	* ov-fcn-handle.cc (octave_fcn_handle::set_fcn): Likewise.
+	* octave.cc (execute_command_line_file): Likewise.
+	* ls-mat5.cc (read_mat5_binary_element): Likewise.
+	* load-save.cc (find_file_to_load): Likewise.
+	* load-path.cc (load_path::do_find_file): Likewise.
+	* graphics.cc (drawnow): Likewise.
+	* parse.y (frob_function): Likewise.
+
+	* octave.cc (initialize_pathsearch): Fix usage of
+	file_ops::dir_sep_str.
+	* help.cc (Flookfor): Likewise.
+	* dirfns.cc (Ffilesep): Likewise.
+	(Fautoload): Likewise.
+
+	* defaults.cc (subst_octave_home): Fix usage of
+	file_ops::dir_sep_char.
+	(Fmfilename): Likewise.
+
 2008-07-31  John W. Eaton  <jwe@octave.org>
 
 	* parse.y (assign_lhs): Call force_local_variable on all elements
--- a/src/defaults.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/defaults.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -113,8 +113,9 @@
 	retval.replace (0, len, Voctave_home);
     }
 
-  if (file_ops::dir_sep_char != '/')
-    std::replace (retval.begin (), retval.end (), '/', file_ops::dir_sep_char);
+  if (file_ops::dir_sep_char () != '/')
+    std::replace (retval.begin (), retval.end (), '/',
+		  file_ops::dir_sep_char ());
 
   return retval;
 }
--- a/src/dirfns.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/dirfns.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -644,7 +644,7 @@
   octave_value retval;
 
   if (args.length () == 0)
-    retval = file_ops::dir_sep_str;
+    retval = file_ops::dir_sep_str ();
   else
     print_usage ();
 
--- a/src/graphics.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/graphics.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -4574,7 +4574,7 @@
 
 	      if (! error_state)
 		{
-		  size_t pos = file.find_last_of (file_ops::dir_sep_chars);
+		  size_t pos = file.find_last_of (file_ops::dir_sep_chars ());
 
 		  if (pos != NPOS)
 		    {
--- a/src/help.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/help.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -2022,7 +2022,7 @@
 		      std::string dir = dirs[i];
 
 		      if (! file_ops::is_dir_sep (dir[dir.length()-1]))
-			dir += file_ops::dir_sep_str;
+			dir += file_ops::dir_sep_str ();
 
 		      if (file_name == dir + name + ".oct"
 			  || file_name == dir + name + ".m")
--- a/src/load-path.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/load-path.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -1007,7 +1007,7 @@
 {
   std::string retval;
 
-  if (file.find_first_of (file_ops::dir_sep_chars) != NPOS)
+  if (file.find_first_of (file_ops::dir_sep_chars ()) != NPOS)
     {
       if (octave_env::absolute_pathname (file)
 	  || octave_env::rooted_relative_pathname (file))
--- a/src/load-save.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/load-save.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -505,7 +505,7 @@
     }
 
   size_t dot_pos = fname.rfind (".");
-  size_t sep_pos = fname.find_last_of (file_ops::dir_sep_chars);
+  size_t sep_pos = fname.find_last_of (file_ops::dir_sep_chars ());
     
   if (dot_pos == NPOS || (sep_pos != NPOS && dot_pos < sep_pos))
     {
--- a/src/ls-mat5.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/ls-mat5.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -774,7 +774,8 @@
 
 		    if (fs.exists ())
 		      {
-			size_t xpos = str.find_last_of (file_ops::dir_sep_chars);
+			size_t xpos
+			  = str.find_last_of (file_ops::dir_sep_chars ());
 
 			std::string dir_name = str.substr (0, xpos);
 
@@ -802,7 +803,8 @@
 			str = octave_env::make_absolute 
 			  (p.find_first_of (names), octave_env::getcwd ());
 
-			size_t xpos = str.find_last_of (file_ops::dir_sep_chars);
+			size_t xpos
+			  = str.find_last_of (file_ops::dir_sep_chars ());
 
 			std::string dir_name = str.substr (0, xpos);
 
@@ -825,7 +827,8 @@
 		  }
 		else
 		  {
-		    size_t xpos = fpath.find_last_of (file_ops::dir_sep_chars);
+		    size_t xpos
+		      = fpath.find_last_of (file_ops::dir_sep_chars ());
 
 		    std::string dir_name = fpath.substr (0, xpos);
 
--- a/src/octave.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/octave.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -220,8 +220,8 @@
     odb = octave_env::getenv ("OCTAVE_DB_DIR");
 
   if (odb.empty ())
-    odb = Vdata_dir + file_ops::dir_sep_str + "octave:"
-      + Vlibexec_dir + file_ops::dir_sep_str + "octave";
+    odb = Vdata_dir + file_ops::dir_sep_str () + "octave:"
+      + Vlibexec_dir + file_ops::dir_sep_str () + "octave";
 }
 
 DEFUN (__version_info__, args, ,
@@ -444,7 +444,7 @@
 
   octave_program_invocation_name = curr_fcn_file_name;
 
-  size_t pos = curr_fcn_file_name.find_last_of (file_ops::dir_sep_chars);
+  size_t pos = curr_fcn_file_name.find_last_of (file_ops::dir_sep_chars ());
   
   std::string tmp = (pos != NPOS)
     ? curr_fcn_file_name.substr (pos+1) : curr_fcn_file_name;
--- a/src/ov-fcn-handle.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/ov-fcn-handle.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -138,7 +138,7 @@
 
       if (fs.exists ())
 	{
-	  size_t xpos = str.find_last_of (file_ops::dir_sep_chars);
+	  size_t xpos = str.find_last_of (file_ops::dir_sep_chars ());
 
 	  std::string dir_name = str.substr (0, xpos);
 
@@ -170,7 +170,7 @@
 	  str = octave_env::make_absolute 
 	    (p.find_first_of (names), octave_env::getcwd ());
 
-	  size_t xpos = str.find_last_of (file_ops::dir_sep_chars);
+	  size_t xpos = str.find_last_of (file_ops::dir_sep_chars ());
 
 	  std::string dir_name = str.substr (0, xpos);
 
@@ -193,7 +193,7 @@
     {
       if (fpath.length () > 0)
 	{
-	  size_t xpos = fpath.find_last_of (file_ops::dir_sep_chars);
+	  size_t xpos = fpath.find_last_of (file_ops::dir_sep_chars ());
 
 	  std::string dir_name = fpath.substr (0, xpos);
 
--- a/src/parse.y	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/parse.y	Mon Aug 04 23:44:50 2008 -0400
@@ -2484,7 +2484,7 @@
 
 	  std::string nm = curr_fcn_file_name;
 
-	  size_t pos = nm.find_last_of (file_ops::dir_sep_chars);
+	  size_t pos = nm.find_last_of (file_ops::dir_sep_chars ());
 
 	  if (pos != NPOS)
 	    nm = curr_fcn_file_name.substr (pos+1);
@@ -3377,7 +3377,7 @@
 		  if (! fname.empty ())
 		    {
 		      fname = octave_env::make_absolute (fname, octave_env::getcwd ());
-		      fname = fname.substr (0, fname.find_last_of (file_ops::dir_sep_str) + 1);
+		      fname = fname.substr (0, fname.find_last_of (file_ops::dir_sep_str ()) + 1);
 
 		      file_stat fs (fname + nm);
 
@@ -3514,7 +3514,7 @@
     retval = fname;
   else
     {
-      size_t dpos = fname.rfind (file_ops::dir_sep_char);
+      size_t dpos = fname.rfind (file_ops::dir_sep_char ());
       size_t epos = fname.rfind ('.');
 
       if (epos <= dpos)
--- a/src/symtab.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/symtab.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -708,7 +708,7 @@
 
       if (! file_name.empty ())
 	{
-	  size_t pos = file_name.find_last_of (file_ops::dir_sep_chars);
+	  size_t pos = file_name.find_last_of (file_ops::dir_sep_chars ());
 
 	  std::string dir_name = file_name.substr (0, pos);
 
--- a/src/variables.cc	Mon Aug 04 22:32:47 2008 -0400
+++ b/src/variables.cc	Mon Aug 04 23:44:50 2008 -0400
@@ -570,7 +570,7 @@
 {
   bool retval = (! text.empty ()
 		 && text != "."
-		 && text.find_first_of (file_ops::dir_sep_chars) == NPOS
+		 && text.find_first_of (file_ops::dir_sep_chars ()) == NPOS
 		 && text.find ("..") == NPOS
 		 && text.rfind ('.') != NPOS);