# HG changeset patch # User John W. Eaton # Date 1217907890 14400 # Node ID a2ab20ba78f74bb0adddc7b9903e23be46e5df7e # Parent b0e7bbe7cd47a01f516f0c0918905cf204df727f make file_ops a proper singleton class diff -r b0e7bbe7cd47 -r a2ab20ba78f7 liboctave/ChangeLog --- 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 + * 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 diff -r b0e7bbe7cd47 -r a2ab20ba78f7 liboctave/file-ops.cc --- 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); } /* diff -r b0e7bbe7cd47 -r a2ab20ba78f7 liboctave/file-ops.h --- 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 diff -r b0e7bbe7cd47 -r a2ab20ba78f7 liboctave/oct-env.cc --- 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; diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/ChangeLog --- 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 + + * 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 * parse.y (assign_lhs): Call force_local_variable on all elements diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/defaults.cc --- 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; } diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/dirfns.cc --- 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 (); diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/graphics.cc --- 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) { diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/help.cc --- 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") diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/load-path.cc --- 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)) diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/load-save.cc --- 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)) { diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/ls-mat5.cc --- 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); diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/octave.cc --- 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; diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/ov-fcn-handle.cc --- 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); diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/parse.y --- 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) diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/symtab.cc --- 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); diff -r b0e7bbe7cd47 -r a2ab20ba78f7 src/variables.cc --- 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);