changeset 1750:fd0d12493223

[project @ 1996-01-13 09:31:07 by jwe]
author jwe
date Sat, 13 Jan 1996 09:31:07 +0000
parents c7f5599eb177
children fc59af7ea536
files src/dirfns.cc src/file-io.cc src/input.cc src/input.h src/load-save.cc src/oct-hist.cc src/pt-plot.cc src/sysdep.cc src/sysdep.h src/toplev.cc src/utils.cc
diffstat 11 files changed, 131 insertions(+), 150 deletions(-) [+]
line wrap: on
line diff
--- a/src/dirfns.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/dirfns.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -53,16 +53,15 @@
 #include "error.h"
 #include "gripes.h"
 #include "help.h"
-#include "toplev.h"
+#include "oct-obj.h"
 #include "pager.h"
 #include "pathlen.h"
 #include "procstream.h"
+#include "pt-plot.h"
 #include "statdefs.h"
 #include "sysdep.h"
 #include "sysdir.h"
-#include "pt-const.h"
-#include "oct-obj.h"
-#include "pt-plot.h"
+#include "toplev.h"
 #include "unwind-prot.h"
 #include "utils.h"
 #include "variables.h"
@@ -369,14 +368,10 @@
 
   if (argc > 1)
     {
-      static char *dirname = 0;
+      string dirname = oct_tilde_expand (argv[1]);
 
-      if (dirname)
-	free (dirname);
-
-      dirname = tilde_expand (argv[1]);
-
-      if (dirname && ! octave_change_to_directory (dirname))
+      if (dirname.length () > 0
+	  && ! octave_change_to_directory (dirname.c_str ()))
 	{
 	  DELETE_ARGV;
 	  return retval;
@@ -417,11 +412,7 @@
 
   ls_buf << "ls -C ";
   for (int i = 1; i < argc; i++)
-    {
-      char *tmp = tilde_expand (argv[i]);
-      ls_buf << tmp << " ";
-      free (tmp);
-    }
+    ls_buf << oct_tilde_expand (argv[i]) << " ";
 
   ls_buf << ends;
   char *ls_command = ls_buf.str ();
@@ -514,11 +505,9 @@
 	}
       else
 	{
-	  char *tmp = tilde_expand (dirname);
+	  string tmp = oct_tilde_expand (dirname);
 
-	  DIR *dir = opendir (tmp);
-
-	  free (tmp);
+	  DIR *dir = opendir (tmp.c_str ());
 
 	  if (dir)
 	    {
@@ -601,11 +590,9 @@
 	gripe_wrong_type_arg ("mkdir", args(0));
       else
 	{
-	  char *tmp = tilde_expand (dirname);
+	  string tmp = oct_tilde_expand (dirname);
 
-	  int mkdir_retval = mkdir (tmp, 0777);
-
-	  free (tmp);
+	  int mkdir_retval = mkdir (tmp.c_str (), 0777);
 
 	  if (mkdir_retval < 0)
 	    {
@@ -642,11 +629,9 @@
 	gripe_wrong_type_arg ("rmdir", args(0));
       else
 	{
-	  char *tmp = tilde_expand (dirname);
+	  string tmp = oct_tilde_expand (dirname);
 
-	  int rmdir_retval = rmdir (tmp);
-
-	  free (tmp);
+	  int rmdir_retval = rmdir (tmp.c_str ());
 
 	  if (rmdir_retval < 0)
 	    {
--- a/src/file-io.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/file-io.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -64,12 +64,11 @@
 #include "mappers.h"
 #include "oct-map.h"
 #include "oct-hist.h"
+#include "oct-obj.h"
 #include "pager.h"
 #include "statdefs.h"
 #include "sysdep.h"
 #include "syswait.h"
-#include "pt-const.h"
-#include "oct-obj.h"
 #include "utils.h"
 #include "variables.h"
 
@@ -2486,21 +2485,13 @@
 
   if (args.length () == 1)
     {
-      string tstr = args(0).string_value ();
-      const char *name = tstr.c_str ();
-
-      static char *fname = 0;
-
-      if (fname)
-	free (fname);
-
-      fname = tilde_expand (name);
+      string fname = oct_tilde_expand (args(0).string_value ());
 
       if (! error_state)
 	{
 	  struct stat buf;
 
-	  if (stat (fname, &buf) < 0)
+	  if (stat (fname.c_str (), &buf) < 0)
 	    retval = -1.0;
 	  else
 	    retval = tree_constant (mk_stat_map (buf));
@@ -2522,21 +2513,13 @@
 
   if (args.length () == 1)
     {
-      string tstr = args(0).string_value ();
-      const char *name = tstr.c_str ();
-
-      static char *fname = 0;
-
-      if (fname)
-	free (fname);
-
-      fname = tilde_expand (name);
+      string fname = oct_tilde_expand (args(0).string_value ());
 
       if (! error_state)
 	{
 	  struct stat buf;
 
-	  if (lstat (fname, &buf) < 0)
+	  if (lstat (fname.c_str (), &buf) < 0)
 	    retval = -1.0;
 	  else
 	    retval = tree_constant (mk_stat_map (buf));
--- a/src/input.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/input.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -618,15 +618,15 @@
 // warning if the file doesn't exist.
 
 FILE *
-get_input_from_file (const char *name, int warn)
+get_input_from_file (const string& name, int warn)
 {
   FILE *instream = 0;
 
-  if (name && *name)
-    instream = fopen (name, "r");
+  if (name.length () > 0)
+    instream = fopen (name.c_str (), "r");
 
   if (! instream && warn)
-    warning ("%s: no such file or directory", name);
+    warning ("%s: no such file or directory", name.c_str ());
 
   if (reading_fcn_file || reading_script_file)
     ff_instream = instream;
--- a/src/input.h	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/input.h	Sat Jan 13 09:31:07 1996 +0000
@@ -28,8 +28,10 @@
 
 #include <cstdio>
 
+#include <string>
+
 extern int octave_read (char *buf, int max_size);
-extern FILE *get_input_from_file (const char *name, int warn = 1);
+extern FILE *get_input_from_file (const string& name, int warn = 1);
 extern FILE *get_input_from_stdin (void);
 extern void initialize_readline (void);
 
--- a/src/load-save.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/load-save.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -44,12 +44,11 @@
 #include "help.h"
 #include "load-save.h"
 #include "mappers.h"
+#include "oct-obj.h"
 #include "pager.h"
+#include "pt-exp.h"
 #include "symtab.h"
 #include "sysdep.h"
-#include "pt-const.h"
-#include "oct-obj.h"
-#include "pt-exp.h"
 #include "unwind-prot.h"
 #include "user-prefs.h"
 #include "utils.h"
@@ -2024,15 +2023,15 @@
 }
 
 static load_save_format
-get_file_format (const char *fname, const char *orig_fname)
+get_file_format (const string& fname, const string& orig_fname)
 {
   load_save_format retval = LS_UNKNOWN;
 
-  ifstream file (fname);
+  ifstream file (fname.c_str ());
 
   if (! file)
     {
-      error ("load: couldn't open input file `%s'", orig_fname);
+      error ("load: couldn't open input file `%s'", orig_fname.c_str ());
       return retval;
     }
 
@@ -2068,7 +2067,8 @@
   file.close ();
 
   if (retval == LS_UNKNOWN)
-    error ("load: unable to determine file format for `%s'", orig_fname);
+    error ("load: unable to determine file format for `%s'",
+	   orig_fname.c_str ());
 
   return retval;
 }
@@ -2286,12 +2286,7 @@
     }
   else
     {
-      static char *fname = 0;
-
-      if (fname)
-	free (fname);
-
-      fname = tilde_expand (*argv);
+      string fname = oct_tilde_expand (*argv);
 
       if (format == LS_UNKNOWN)
 	format = get_file_format (fname, orig_fname);
@@ -2305,7 +2300,7 @@
 	  if (format == LS_BINARY || format == LS_MAT_BINARY)
 	    mode |= ios::bin;
 
-	  ifstream file (fname, mode);
+	  ifstream file (fname.c_str (), mode);
 
 	  if (file)
 	    {
@@ -3111,12 +3106,7 @@
     }
   else
     {
-      static char *fname = 0;
-
-      if (fname)
-	free (fname);
-
-      fname = tilde_expand (*argv);
+      string fname = oct_tilde_expand (*argv);
 
       argc--;
       argv++;
@@ -3125,7 +3115,7 @@
       if (format == LS_BINARY || format == LS_MAT_BINARY)
 	mode |= ios::bin;
 
-      ofstream file (fname, mode);
+      ofstream file (fname.c_str (), mode);
 
       if (file)
 	{
--- a/src/oct-hist.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/oct-hist.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -36,6 +36,8 @@
 #include <cstdlib>
 #include <cstring>
 
+#include <string>
+
 #include <fstream.h>
 #include <strstream.h>
 
@@ -52,13 +54,12 @@
 #include "error.h"
 #include "input.h"
 #include "oct-hist.h"
-#include "toplev.h"
+#include "oct-obj.h"
 #include "pager.h"
 #include "sighandlers.h"
 #include "statdefs.h"
 #include "sysdep.h"
-#include "pt-const.h"
-#include "oct-obj.h"
+#include "toplev.h"
 #include "unwind-prot.h"
 #include "user-prefs.h"
 #include "utils.h"
@@ -116,14 +117,9 @@
 void
 initialize_history (void)
 {
-  static char *file = 0;
+  string file = oct_tilde_expand (user_pref.history_file);
 
-  if (file)
-    free (file);
-
-  file = tilde_expand (user_pref.history_file);
-
-  read_history (file);
+  read_history (file.c_str ());
 
   using_history ();
 
@@ -133,17 +129,12 @@
 void
 clean_up_history (void)
 {
-  static char *file = 0;
-
-  if (file)
-    free (file);
-
   stifle_history (user_pref.history_size);
 
-  file = tilde_expand (user_pref.history_file);
+  string file = oct_tilde_expand (user_pref.history_file);
 
   if (user_pref.saving_history)
-    write_history (file);
+    write_history (file.c_str ());
 }
 
 void
@@ -177,16 +168,14 @@
 	  && ((*argv)[1] == 'r' || (*argv)[1] == 'w'
 	      || (*argv)[1] == 'a' || (*argv)[1] == 'n'))
 	{
-	  static char *file = 0;
 	  int result = 0;
 
-	  if (file)
-	    free (file);
+	  string file;
 
 	  if (argc > 1)
-	    file = tilde_expand (*(argv+1));
+	    file = oct_tilde_expand (*(argv+1));
 	  else
-	    file = tilde_expand (user_pref.history_file);
+	    file = oct_tilde_expand (user_pref.history_file);
 
 	  switch ((*argv)[1])
 	    {
@@ -196,24 +185,22 @@
 		  {
 		    if (history_lines_this_session < where_history ())
 		      {
-			// If the filename was supplied, then create
-			// it if it doesn't already exist.
+			// Create file if it doesn't already exist.
 
-			if (file)
-			  {
-			    struct stat buf;
+			struct stat buf;
 
-			    if (stat (file, &buf) == -1)
-			      {
-				int tem;
+			if (stat (file.c_str (), &buf) == -1)
+			  {
+			    int tem;
 
-				tem = open (file, O_CREAT, 0666);
-				close (tem);
-			      }
+			    tem = open (file.c_str (), O_CREAT, 0666);
+			    close (tem);
 			  }
 
-			result =
-			  append_history (history_lines_this_session, file);
+			result
+			  = append_history (history_lines_this_session,
+					    file.c_str ());
+
 			history_lines_in_file += history_lines_this_session;
 			history_lines_this_session = 0;
 		      }
@@ -222,11 +209,11 @@
 	      break;
 
 	    case 'w':		// Write entire history.
-	      result = write_history (file);
+	      result = write_history (file.c_str ());
 	      break;
 
 	    case 'r':		// Read entire file.
-	      result = read_history (file);
+	      result = read_history (file.c_str ());
 	      break;
 
 	    case 'n':		// Read `new' history from file.
@@ -235,7 +222,8 @@
 	      // already read.
 
 	      using_history ();
-	      result = read_history_range (file, history_lines_in_file, -1);
+	      result = read_history_range (file.c_str (),
+					   history_lines_in_file, -1);
 	      using_history ();
 	      history_lines_in_file = where_history ();
 	      break;
--- a/src/pt-plot.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/pt-plot.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -52,11 +52,10 @@
 #include "load-save.h"
 #include "mappers.h"
 #include "oct-obj.h"
-#include "sysdep.h"
 #include "pt-cmd.h"
-#include "pt-const.h"
 #include "pt-exp.h"
 #include "pt-plot.h"
+#include "sysdep.h"
 #include "user-prefs.h"
 #include "utils.h"
 
@@ -73,7 +72,7 @@
 static int clear_before_plotting = 1;
 
 // List of files to delete when we exit or crash.
-static SLStack <char *> tmp_files;
+static SLStack <string> tmp_files;
 
 // Pipe to gnuplot.
 static oprocstream *plot_stream = 0;
@@ -725,7 +724,8 @@
 
       if (! error_state && data.is_defined ())
 	{
-	  char *file = 0;
+	  string file;
+
 	  if (data.is_string ())
 	    {
 	      // Should really try to look at data file to determine
@@ -734,19 +734,17 @@
 
 	      int n_max = 0;
 
-	      string tstr = data.string_value ();
+	      file = oct_tilde_expand (data.string_value ());
 
-	      file = tilde_expand (tstr.c_str ());
-	      ifstream ftmp (file);
+	      ifstream ftmp (file.c_str ());
+
 	      if (ftmp)
 		{
 		  plot_buf << " \"" << file << '"';
-		  free (file);
 		}
 	      else
 		{
-		  free (file);
-		  file = 0;
+		  file = "";
 
 		  // Opening as a file failed.  Let's try passing it
 		  // along as a plot command.
@@ -757,6 +755,7 @@
 	      if (using_clause)
 		{
 		  int status = using_clause->print (ndim, n_max, plot_buf);
+
 		  if (status < 0)
 		    return -1;
 		}
@@ -786,7 +785,7 @@
 		      break;
 		    }
 
-		  if (file)
+		  if (file.length () > 0)
 		    {
 		      mark_for_deletion (file);
 		      plot_buf << " \"" << file << '"';
@@ -944,10 +943,9 @@
 }
 
 void
-mark_for_deletion (const char *filename)
+mark_for_deletion (const string& file)
 {
-  char *tmp = strsave (filename);
-  tmp_files.push (tmp);
+  tmp_files.push (file);
 }
 
 void
@@ -955,9 +953,8 @@
 {
   while (! tmp_files.empty ())
     {
-      char *filename = tmp_files.pop ();
-      unlink (filename);
-      delete [] filename;
+      string filename = tmp_files.pop ();
+      unlink (filename.c_str ());
     }
 }
 
--- a/src/sysdep.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/sysdep.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -68,6 +68,7 @@
 #endif
 
 #include <readline/readline.h>
+#include <readline/tilde.h>
 
 extern char *term_clrpag;
 extern "C" void _rl_output_character_function ();
@@ -680,6 +681,43 @@
 }
 #endif
 
+// The check for error state allows us to do this:
+//
+//   string foo = oct_tilde_expand (args(0).string_value ());
+//
+// without having to use a temporary and check error_state before
+// calling oct_tilde_expand.
+
+string
+oct_tilde_expand (const string& name)
+{
+  string retval;
+
+  if (! error_state)
+    {
+      char *tmp = tilde_expand (name.c_str ());
+      retval = tmp;
+      delete [] tmp;
+    }
+
+  return retval;
+}
+
+DEFUN ("tilde_expand", Ftilde_expand, Stilde_expand, 10,
+  "tilde_expand (STRING): perform tilde expansion on STRING")
+{
+  Octave_object retval;
+
+  int nargin = args.length ();
+
+  if (nargin == 1)
+    retval = oct_tilde_expand (args(0).string_value ());
+  else
+    print_usage ("tilde_expand");
+
+  return retval;
+}
+
 /*
 ;;; Local Variables: ***
 ;;; mode: C++ ***
--- a/src/sysdep.h	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/sysdep.h	Sat Jan 13 09:31:07 1996 +0000
@@ -24,7 +24,7 @@
 #if !defined (octave_sysdep_h)
 #define octave_sysdep_h 1
 
-#include <readline/tilde.h>
+#include <string>
 
 extern void sysdep_init (void);
 
@@ -60,6 +60,8 @@
 // Nonzero if the machine we are running on is big-endian.
 extern int octave_words_big_endian;
 
+extern string oct_tilde_expand (const string&);
+
 #endif
 
 /*
--- a/src/toplev.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/toplev.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -55,17 +55,17 @@
 #include "input.h"
 #include "lex.h"
 #include "oct-hist.h"
-#include "toplev.h"
 #include "pager.h"
 #include "parse.h"
 #include "pathsearch.h"
 #include "procstream.h"
+#include "pt-const.h"
+#include "pt-misc.h"
+#include "pt-plot.h"
 #include "sighandlers.h"
 #include "statdefs.h"
 #include "sysdep.h"
-#include "pt-const.h"
-#include "pt-misc.h"
-#include "pt-plot.h"
+#include "toplev.h"
 #include "unwind-prot.h"
 #include "user-prefs.h"
 #include "utils.h"
@@ -181,7 +181,7 @@
 }
 
 void
-parse_and_execute (const char *s, int print, int verbose,
+parse_and_execute (const string& s, int print, int verbose,
 		   const char *warn_for)
 {
   begin_unwind_frame ("parse_and_execute_2");
@@ -190,9 +190,10 @@
   unwind_protect_ptr (curr_fcn_file_full_name);
 
   reading_script_file = 1;
-  curr_fcn_file_full_name = s;
+  curr_fcn_file_full_name = s.c_str ();
 
   FILE *f = get_input_from_file (s, 0);
+
   if (f)
     {
       unwind_protect_int (input_line_number);
@@ -216,7 +217,7 @@
 	cout << "done." << endl;
     }
   else if (warn_for)
-    error ("%s: unable to open file `%s'", warn_for, s);
+    error ("%s: unable to open file `%s'", warn_for, s.c_str ());
 
   run_unwind_frame ("parse_and_execute_2");
 }
@@ -233,19 +234,16 @@
 
   if (nargin == 1)
     {
-      string tstr = args(0).string_value ();
-      const char *file = tstr.c_str ();
+      string file = args(0).string_value ();
 
       if (! error_state)
 	{
-	  file = tilde_expand (file);
+	  file = oct_tilde_expand (file);
 
 	  parse_and_execute (file, 1, 0, "source");
 
 	  if (error_state)
 	    error ("source: error sourcing file `%s'", file);
-
-	  delete [] file;
 	}
       else
 	error ("source: expecting file name as argument");
--- a/src/utils.cc	Sat Jan 13 09:31:07 1996 +0000
+++ b/src/utils.cc	Sat Jan 13 09:31:07 1996 +0000
@@ -66,13 +66,12 @@
 #include "input.h"
 #include "mappers.h"
 #include "oct-hist.h"
-#include "toplev.h"
+#include "oct-obj.h"
 #include "pager.h"
 #include "pathsearch.h"
 #include "sysdep.h"
 #include "sysdir.h"
-#include "pt-const.h"
-#include "oct-obj.h"
+#include "toplev.h"
 #include "unwind-prot.h"
 #include "user-prefs.h"
 #include "utils.h"
@@ -231,9 +230,8 @@
 	  char *end = strchr (ptr, SEPCHAR);
 	  if (end)
 	    *end = '\0';
-	  char *result = tilde_expand (ptr);
-	  path[i] = strsave (result);
-	  free (result);
+	  string result = oct_tilde_expand (ptr);
+	  path[i] = strsave (result.c_str ());
 	  ptr = end + 1;
 	  i++;
 	}