changeset 27347:2b4cba38818f jwe-experimental

allow octave_stdout to go to handler function
author John W. Eaton <jwe@octave.org>
date Wed, 05 Jun 2019 17:15:38 +0000
parents 254d6e33a1d0
children
files libinterp/corefcn/pager.cc libinterp/corefcn/pager.h
diffstat 2 files changed, 28 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/pager.cc	Fri Aug 16 13:13:33 2019 -0700
+++ b/libinterp/corefcn/pager.cc	Wed Jun 05 17:15:38 2019 +0000
@@ -262,12 +262,13 @@
   }
 
   output_system::output_system (interpreter& interp)
-    : m_interpreter (interp), m_pager_stream (), m_diary_stream (),
-      m_external_pager (nullptr), m_external_diary_file (),
-      m_diary_file_name ("diary"), m_PAGER (default_pager ()),
-      m_PAGER_FLAGS (), m_page_output_immediately (false),
-      m_page_screen_output (false), m_write_to_diary_file (false),
-      m_really_flush_to_pager (false), m_flushing_output_to_pager (false)
+    : m_interpreter (interp), m_output_handler (nullptr),
+      m_pager_stream (), m_diary_stream (), m_external_pager (nullptr),
+      m_external_diary_file (), m_diary_file_name ("diary"),
+      m_PAGER (default_pager ()), m_PAGER_FLAGS (),
+      m_page_output_immediately (false), m_page_screen_output (false),
+      m_write_to_diary_file (false), m_really_flush_to_pager (false),
+      m_flushing_output_to_pager (false)
   { }
 
   octave_value output_system::PAGER (const octave_value_list& args,
@@ -384,6 +385,7 @@
       {
         bool bypass_pager = (! m_interpreter.interactive ()
                              || application::forced_interactive ()
+                             || m_output_handler
                              || ! m_page_screen_output
                              || (m_really_flush_to_pager
                                  && m_page_screen_output
@@ -438,8 +440,13 @@
       {
         if (bypass_pager)
           {
-            std::cout.write (msg, len);
-            std::cout.flush ();
+            if (m_output_handler)
+              m_output_handler (msg, len);
+            else
+              {
+                std::cout.write (msg, len);
+                std::cout.flush ();
+              }
           }
         else
           {
--- a/libinterp/corefcn/pager.h	Fri Aug 16 13:13:33 2019 -0700
+++ b/libinterp/corefcn/pager.h	Wed Jun 05 17:15:38 2019 +0000
@@ -132,6 +132,8 @@
   {
   public:
 
+    typedef void (*output_handler_fcn) (const char *buf, int len);
+
     output_system (interpreter& interp);
 
     output_system (const output_system&) = delete;
@@ -144,6 +146,15 @@
 
     diary_stream& diary (void) { return m_diary_stream; }
 
+    output_handler_fcn output_handler (void) const { return m_output_handler; }
+
+    output_handler_fcn output_handler (output_handler_fcn fcn)
+    {
+      output_handler_fcn ofcn = m_output_handler;
+      m_output_handler = fcn;
+      return ofcn;
+    }
+
     std::string diary_file_name (void) const { return m_diary_file_name; }
 
     std::string diary_file_name (const std::string& nm)
@@ -262,6 +273,8 @@
 
     interpreter& m_interpreter;
 
+    output_handler_fcn m_output_handler;
+
     pager_stream m_pager_stream;
 
     diary_stream m_diary_stream;