# HG changeset patch # User Joonas Lipping # Date 1393799686 -7200 # Mon Mar 03 00:34:46 2014 +0200 # Node ID 9cff86a6e377fb5ac80c0205627afe7969f4e33b # Parent f958e8cd6348e2ed77f34b539d421b5986a71516 Added doxygen comments to input.h and cleaned up input.cc a little. diff -r f958e8cd6348 -r 9cff86a6e377 libinterp/corefcn/input.cc --- a/libinterp/corefcn/input.cc Sat Mar 01 22:11:32 2014 -0800 +++ b/libinterp/corefcn/input.cc Mon Mar 03 00:34:46 2014 +0200 @@ -176,21 +176,6 @@ } } -static std::string -gnu_readline (const std::string& s, bool& eof) -{ - octave_quit (); - - eof = false; - - std::string retval = command_editor::readline (s, eof); - - if (! eof && retval.empty ()) - retval = "\n"; - - return retval; -} - static inline std::string interactive_input (const std::string& s, bool& eof) { @@ -211,7 +196,22 @@ return "\n"; } - return gnu_readline (s, eof); + octave_quit (); + + std::string retval = command_editor::readline (s, eof); + + if (! eof && retval.empty ()) + retval = "\n"; + + return retval; +} + +static inline std::string +interactive_input (const std::string& s) +{ + bool eof; + + return interactive_input(s, eof); } std::string @@ -219,8 +219,6 @@ { octave_quit (); - eof = false; - std::string retval; // Process pre input event hook function prior to flushing output and @@ -618,8 +616,6 @@ { octave_quit (); - eof = false; - return octave_gets (eof); } @@ -630,8 +626,6 @@ { octave_quit (); - eof = false; - return octave_fgets (file, eof); } @@ -642,18 +636,14 @@ { octave_quit (); - eof = false; - - std::string retval; - - retval = eval_string; + std::string retval = ""; // Clear the eval string so that the next call will return // an empty character string with EOF = true. - eval_string = ""; + // The old value of eval_string is placed in retval. + retval.swap(eval_string); - if (retval.empty ()) - eof = true; + eof = retval.empty(); return retval; } @@ -687,9 +677,7 @@ octave_diary << prompt; - bool eof = false; - - std::string input_buf = interactive_input (prompt.c_str (), eof); + std::string input_buf = interactive_input (prompt.c_str ()); if (! (error_state || input_buf.empty ())) { @@ -783,9 +771,7 @@ while (1) { - bool eof = false; - - std::string input_buf = interactive_input (prompt_string, eof); + std::string input_buf = interactive_input (prompt_string); if (input_buf == "yes") return true; diff -r f958e8cd6348 -r 9cff86a6e377 libinterp/corefcn/input.h --- a/libinterp/corefcn/input.h Sat Mar 01 22:11:32 2014 -0800 +++ b/libinterp/corefcn/input.h Mon Mar 03 00:34:46 2014 +0200 @@ -81,6 +81,19 @@ extern octave_time Vlast_prompt_time; +/** + * @brief Base class for input readers. + * + * Inheriting classes are used to retrieve lines from various sources + * (command prompt, file or string) for the lexer to process. + * + * The octave_lexer class doesn't interact with these classes directly. + * Instead, it uses the unified wrapper octave_input_reader, + * which determines which reader class to use based on the arguments + * given to its constructor. + * + * This class provides a reference counter. + */ class octave_base_reader { @@ -88,28 +101,65 @@ friend class octave_input_reader; + /** + * @brief Constructor. + * + * @param lxr A pointer to the associated lexer. + */ octave_base_reader (octave_base_lexer *lxr) : count (1), pflag (0), lexer (lxr) { } + /** + * @brief Copy constructor. + * + * Both the new copy and the old copy share the same lexer. + * + * The new copy's prompt flag is initialized to the same value as the old + * copy's prompt flag. After that, the values will be independent of one + * another. + * + * The new copy's reference count starts at 1. + * The reference counts are independent of each other. + */ octave_base_reader (const octave_base_reader& x) : count (1), pflag (x.pflag), lexer (x.lexer) { } + /** Destructor. */ virtual ~octave_base_reader (void) { } virtual std::string get_input (bool& eof) = 0; + /** + * @brief Returns a description of the input source. + * + * @returns A string describing the input source. + */ virtual std::string input_source (void) const { return in_src; } + /** @brief Sets the prompt flag back to 1. */ void reset (void) { promptflag (1); } + /** @brief Increments the prompt flag. */ void increment_promptflag (void) { pflag++; } + /** @brief Decrements the prompt flag. */ void decrement_promptflag (void) { pflag--; } + /** + * @brief Returns the value of the prompt flag. + * + * @returns The value of the prompt flag. + */ int promptflag (void) const { return pflag; } + /** + * @brief Sets the prompt flag to `n`. + * + * @param n The new value for the prompt flag. + * @returns The old value of the prompt flag. + */ int promptflag (int n) { int retval = pflag; @@ -117,18 +167,70 @@ return retval; } + /** + * @brief Retrieves one line from command_editor. + * + * Retrieves one line of text from the command_editor instance. + * In case of default_command_editor this means reading from some file + * (not necessarily `stdin`). + * + * @param eof This is set to false if the source has no more data. + * @returns The retrieved string. + */ std::string octave_gets (bool& eof); + /** + * @brief Is what is being read a function file? + * + * If the reader has a valid lexer, + * this function returns the lexer's answer to the same query. + * Otherwise, returns false. + * + * @returns False if lexer is `NULL` or not reading a function file; true otherwise. + */ virtual bool reading_fcn_file (void) const; + /** + * @brief Is what is being read a classdef file? + * + * If the reader has a valid lexer, + * this function returns the lexer's answer to the same query. + * Otherwise, returns false. + * + * @returns False if lexer is `NULL` or not reading a classdef file; true otherwise. + */ virtual bool reading_classdef_file (void) const; + /** + * @brief Is what is being read a script file? + * + * If the reader has a valid lexer, + * this function returns the lexer's answer to the same query. + * Otherwise, returns false. + * + * @returns False if lexer is `NULL` or not reading a script file; true otherwise. + */ virtual bool reading_script_file (void) const; + /** + * @brief Is this input being read from the terminal? + * + * @returns True if input is from the terminal; false otherwise. + */ virtual bool input_from_terminal (void) const { return false; } + /** + * @brief Is this input being read from a file? + * + * @returns True if input is from a file; false otherwise. + */ virtual bool input_from_file (void) const { return false; } + /** + * @brief Is this input being read from an eval string? + * + * @returns True if input is from an eval string; false otherwise. + */ virtual bool input_from_eval_string (void) const { return false; } private: @@ -139,6 +241,7 @@ octave_base_lexer *lexer; + /** @brief Echo the given input line, if specified in settings. */ void do_input_echo (const std::string&) const; static const std::string in_src; @@ -153,6 +256,14 @@ : octave_base_reader (lxr) { } + /** + * @brief Retrieves one line from the terminal. + * + * This function calls octave_base_reader::octave_gets. + * + * @param eof This is set to false if the source has no more data. + * @returns The string that was retrieved. + */ std::string get_input (bool& eof); std::string input_source (void) const { return in_src; } @@ -172,6 +283,12 @@ octave_file_reader (FILE *f_arg, octave_base_lexer *lxr = 0) : octave_base_reader (lxr), file (f_arg) { } + /** + * @brief Retrieves one line from the file. + * + * @param eof This is set to false if EOF has been reached. + * @returns The string that was retrieved. + */ std::string get_input (bool& eof); std::string input_source (void) const { return in_src; } @@ -195,6 +312,12 @@ : octave_base_reader (lxr), eval_string (str) { } + /** + * @brief Returns the eval string. + * + * @param eof This is set to false if the string has previously been read. + * @returns The eval_string, or `""` if it has previously been read. + */ std::string get_input (bool& eof); std::string input_source (void) const { return in_src; } @@ -208,28 +331,74 @@ static const std::string in_src; }; +/** + * @brief A wrapper for the input reader classes. + * + * This class provides a single way to access all input reader classes. + * The caller uses the appropriate constructor based on the input source, + * and octave_input_reader creates the appropriate input reader object. + * The functions in this class map directly to the similarly named functions + * of the wrapped object. + */ class octave_input_reader { public: + /** + * @brief Constructor (terminal). + * + * This creates a octave_terminal_reader object. + * + * @param lxr The lexer that the reader is associated with. + */ octave_input_reader (octave_base_lexer *lxr = 0) : rep (new octave_terminal_reader (lxr)) { } + /** + * @brief Constructor (file). + * + * This creates a octave_file_reader object. + * + * @param file The file that the reader will read. + * @param lxr The lexer that the reader is associated with. + */ octave_input_reader (FILE *file, octave_base_lexer *lxr = 0) : rep (new octave_file_reader (file, lxr)) { } + /** + * @brief Constructor (eval string). + * + * This creates a octave_eval_string_reader object. + * + * @param str The string that is being read. + * @param lxr The lexer that the reader is associated with. + */ octave_input_reader (const std::string& str, octave_base_lexer *lxr = 0) : rep (new octave_eval_string_reader (str, lxr)) { } + /** + * @brief Copy constructor. + * + * The new copy will interact with the same underlying object as the old one. + * + * The underlying object's reference counter is incremented. + */ octave_input_reader (const octave_input_reader& ir) { rep = ir.rep; rep->count++; } + /** + * @brief Assignment constructor. + * + * The new copy will interact with the same underlying object as the old one. + * + * The underlying object's reference counter is incremented. + */ octave_input_reader& operator = (const octave_input_reader& ir) { if (&ir != this) @@ -241,42 +410,88 @@ return *this; } + /** + * @brief Destructor. + * + * The underlying object's reference counter is decremented. + * + * If there are no more references to the underlying object, it is deleted. + */ ~octave_input_reader (void) { if (--rep->count == 0) delete rep; } + /** @brief Sets the prompt flag of the underlying object back to 1. */ void reset (void) { return rep->reset (); } + /** @brief Increments the prompt flag of the underlying object. */ void increment_promptflag (void) { rep->increment_promptflag (); } + /** @brief Decrements the prompt flag of the underlying object. */ void decrement_promptflag (void) { rep->decrement_promptflag (); } + /** + * @brief Returns the value of the prompt flag of the underlying object. + * + * @returns The value of the prompt flag of the underlying object. + */ int promptflag (void) const { return rep->promptflag (); } + /** + * @brief Sets the prompt flag of the underlying object to `n`. + * + * @param n The new value for the prompt flag. + * @returns The old value of the prompt flag. + */ int promptflag (int n) { return rep->promptflag (n); } + /** + * @brief Retrieves one line of input from the underlying object. + * + * @param eof This flag is set to `true` if `EOF` is reached. + */ std::string get_input (bool& eof) { return rep->get_input (eof); } + /** + * @brief Returns a description of the input source of the underlying object. + * + * @returns A string describing the input source. + */ std::string input_source (void) const { return rep->input_source (); } + /** + * @brief Is this input being read from the terminal? + * + * @returns True if input is from the terminal; false otherwise. + */ bool input_from_terminal (void) const { return rep->input_from_terminal (); } + /** + * @brief Is this input being read from a file? + * + * @returns True if input is from a file; false otherwise. + */ bool input_from_file (void) const { return rep->input_from_file (); } + /** + * @brief Is this input being read from an eval string? + * + * @returns True if input is from an eval string; false otherwise. + */ bool input_from_eval_string (void) const { return rep->input_from_eval_string (); @@ -284,6 +499,7 @@ private: + /** The underlying input reader object. */ octave_base_reader *rep; }; # HG changeset patch # User Joonas Lipping # Date 1393801479 -7200 # Mon Mar 03 01:04:39 2014 +0200 # Node ID d9c22438b6452d7fcf7aa950e0fda8fdd45b7227 # Parent 9cff86a6e377fb5ac80c0205627afe7969f4e33b Doxygen comments for input.h and cleanup of input.cc diff -r 9cff86a6e377 -r d9c22438b645 libinterp/corefcn/input.cc --- a/libinterp/corefcn/input.cc Mon Mar 03 00:34:46 2014 +0200 +++ b/libinterp/corefcn/input.cc Mon Mar 03 01:04:39 2014 +0200 @@ -211,7 +211,7 @@ { bool eof; - return interactive_input(s, eof); + return interactive_input (s, eof); } std::string @@ -641,9 +641,9 @@ // Clear the eval string so that the next call will return // an empty character string with EOF = true. // The old value of eval_string is placed in retval. - retval.swap(eval_string); + retval.swap (eval_string); - eof = retval.empty(); + eof = retval.empty (); return retval; }