diff libinterp/interpfcn/input.h @ 16195:b52d2f9294b6

use class for reading lexer input * input.h, input.cc (octave_base_input_reader, octave_terminal_reader, octave_file_reader, octave_eval_string_reader): New classes. (octave_gets): Now a member function of octave_base_reader. (get_user_input): Delete function. Move actions to octave_terminal_reader::get_input and octave_eval_string_reader::get_input. function. Call from octave_file_reader::get_input. Don't check whether reading an eval string. (current_eval_string, get_input_from_eval_string): Delete global variables. (get_debug_input): Check CURR_LEXER->input_from_eval_string instead of global get_input_from_eval_string variable. Don't protect get_input_from_eval_string. * lex.h (octave_lexer::input_reader): New data member. (octave_lexer::octave_lexer (void)): Initialize it. (octave_lexer::octave_lexer (const std::string&), octave_lexer::octave_lexer (FILE *)): New constructors. * lex.h, lex.cc (octave_lexer::input_buffer::fill): New function. (octave_lexer::input_buffer::read): Delete. (octave_lexer::read): Rename from octave_read. Call reader to get input, and then hand it to the input_buffer instead of asking the input buffer to read input. Change all callers. (octave_lexer::input_source, octave_lexer::input_from_eval_string): New functions. Call octave_lexer::input_from_eval_string instead of using get_input_from_eval_string. * oct-parse.in.yy (octave_parser::octave_parser (FILE *), octave_parser::octave_parser (const std::string&)): New constructors. (parse_fcn_file): Pass FILE pointer to octave_parser constructor. (eval_string): Pass string to octave_parser constructor instead of setting global current_eval_string variable.
author John W. Eaton <jwe@octave.org>
date Tue, 05 Mar 2013 10:19:51 -0500
parents 24b3800d30e7
children 2c5c538be353
line wrap: on
line diff
--- a/libinterp/interpfcn/input.h	Tue Mar 05 10:19:47 2013 -0500
+++ b/libinterp/interpfcn/input.h	Tue Mar 05 10:19:51 2013 -0500
@@ -35,19 +35,11 @@
 
 class octave_value;
 
-extern OCTINTERP_API std::string get_user_input (bool& eof);
-
 extern OCTINTERP_API FILE *get_input_from_file (const std::string& name,
                                                 int warn = 1);
 
 extern OCTINTERP_API FILE *get_input_from_stdin (void);
 
-// Global pointer for eval().
-extern std::string current_eval_string;
-
-// TRUE means get input from current_eval_string.
-extern bool get_input_from_eval_string;
-
 // TRUE means that input is coming from a file that was named on
 // the command line.
 extern bool input_from_command_line_file;
@@ -119,4 +111,139 @@
 
 extern octave_time Vlast_prompt_time;
 
+class
+octave_base_reader
+{
+public:
+
+  friend class octave_input_reader;
+
+  octave_base_reader (void) : count (1) { }
+
+  octave_base_reader (const octave_base_reader&) : count (1) { }
+
+  virtual ~octave_base_reader (void) { }
+
+  virtual std::string get_input (bool& eof) = 0;
+
+  virtual std::string input_source (void) const { return in_src; }
+
+  std::string octave_gets (bool& eof);
+
+private:
+
+  int count;
+
+  static const std::string in_src;
+};
+
+class
+octave_terminal_reader : public octave_base_reader
+{
+public:
+
+  octave_terminal_reader (void) : octave_base_reader () { }
+
+  std::string get_input (bool& eof);
+
+  std::string input_source (void) const { return in_src; }
+
+private:
+
+  static const std::string in_src;
+};
+
+class
+octave_file_reader : public octave_base_reader
+{
+public:
+
+  octave_file_reader (FILE *f_arg)
+    : octave_base_reader (), file (f_arg) { }
+
+  std::string get_input (bool& eof);
+
+  std::string input_source (void) const { return in_src; }
+
+private:
+
+  FILE *file;
+
+  static const std::string in_src;
+};
+
+class
+octave_eval_string_reader : public octave_base_reader
+{
+public:
+
+  octave_eval_string_reader (const std::string& str)
+    : octave_base_reader (), eval_string (str)
+  { }
+
+  std::string get_input (bool& eof);
+
+  std::string input_source (void) const { return in_src; }
+
+private:
+
+  std::string eval_string;
+
+  static const std::string in_src;
+};
+
+class
+octave_input_reader
+{
+public:
+  octave_input_reader (void)
+    : rep (new octave_terminal_reader ())
+  { }
+
+  octave_input_reader (FILE *file)
+    : rep (new octave_file_reader (file))
+  { }
+
+  octave_input_reader (const std::string& str)
+    : rep (new octave_eval_string_reader (str))
+  { }
+
+  octave_input_reader (const octave_input_reader& ir)
+  {
+    rep = ir.rep;
+    rep->count++;
+  }
+
+  octave_input_reader& operator = (const octave_input_reader& ir)
+  {
+    if (&ir != this)
+      {
+        rep = ir.rep;
+        rep->count++;
+      }
+
+    return *this;
+  }
+
+  ~octave_input_reader (void)
+  {
+    if (--rep->count == 0)
+      delete rep;
+  }
+
+  std::string get_input (bool& eof)
+  {
+    return rep->get_input (eof);
+  }
+
+  std::string input_source (void) const
+  {
+    return rep->input_source ();
+  }
+
+private:
+
+  octave_base_reader *rep;
+};
+
 #endif