00001 00002 /****************************************************************************** 00003 * MODULE : port.cpp 00004 * DESCRIPTION: Abstract input and output 00005 * COPYRIGHT : (C) 2010 Joris van der Hoeven 00006 ******************************************************************************* 00007 * This software falls under the GNU general public license and comes WITHOUT 00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00009 * If you don't have this file, write to the Free Software Foundation, Inc., 00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00011 ******************************************************************************/ 00012 00013 #include <basix/port.hpp> 00014 #include <basix/string.hpp> 00015 00017 00018 namespace mmx { 00019 00020 bool texmacs_mode= false; 00021 bool math_mode= false; 00022 bool color_mode= false; 00023 00024 /****************************************************************************** 00025 * Default implementations of virtual methods 00026 ******************************************************************************/ 00027 00028 bool 00029 port_rep::is_output_port () { 00030 return false; 00031 } 00032 00033 bool 00034 port_rep::is_input_port () { 00035 return false; 00036 } 00037 00038 bool 00039 port_rep::error_flag () { 00040 return false; 00041 } 00042 00043 string 00044 port_rep::error_message () { 00045 return ""; 00046 } 00047 00048 bool 00049 port_rep::busy () { 00050 return false; 00051 } 00052 00053 nat 00054 port_rep::can_write () { 00055 return 0; 00056 } 00057 00058 nat 00059 port_rep::can_read () { 00060 return 0; 00061 } 00062 00063 void 00064 port_rep::write (const char* s, nat n) { 00065 (void) s; (void) n; 00066 mmerr << "port= " << expression () << lf; 00067 ERROR ("output port expected"); 00068 } 00069 00070 void 00071 port_rep::read (char* s, nat n) { 00072 (void) n; 00073 mmerr << "port= " << expression () << lf; 00074 ERROR ("input port expected"); 00075 } 00076 00077 bool 00078 port_rep::wait (int msecs) { 00079 (void) msecs; 00080 return false; 00081 } 00082 00083 void 00084 port_rep::flush () { 00085 } 00086 00087 port 00088 port_rep::accept () { 00089 mmerr << "port= " << expression () << lf; 00090 ERROR ("socket server port expected"); 00091 } 00092 00093 port 00094 port_rep::component (const string& name) { 00095 mmerr << "port= " << expression () << lf; 00096 ERROR ("composite port expected"); 00097 } 00098 00099 void 00100 port_rep::format (const print_format& fm) { 00101 mmerr << "port= " << expression () << lf; 00102 ERROR ("formatting port expected"); 00103 } 00104 00105 string error_message (const port& p) { 00106 return inside (p)->error_message (); } 00107 port component (const port& p, const string& name) { 00108 return inside (p)->component (name); } 00109 00110 /****************************************************************************** 00111 * Error ports (for ports which did not open, for instance) 00112 ******************************************************************************/ 00113 00114 class error_port_rep: public port_rep { 00115 string s; 00116 00117 public: 00118 virtual syntactic expression () const { 00119 return syn ("error_port", syntactic (quote (s))); } 00120 bool error_flag () { return true; } 00121 string error_message () { return s; } 00122 00123 public: 00124 inline error_port_rep (const string& s2): s (s2) {} 00125 }; 00126 00127 port 00128 error_port (const string& s) { 00129 return (port_rep*) new error_port_rep (s); 00130 } 00131 00132 port::port (): rep ((port_rep*) new error_port_rep ("no port")) {} 00133 00134 } // namespace mmx