basix_doc 0.1
|
00001 00002 /****************************************************************************** 00003 * MODULE : string_port.cpp 00004 * DESCRIPTION: Strings as input and output ports 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 /****************************************************************************** 00021 * Output string ports 00022 ******************************************************************************/ 00023 00024 class output_string_port_rep: public port_rep { 00025 string& s; 00026 00027 public: 00028 virtual syntactic expression () const { 00029 return syn ("output_string_port", syntactic (quote (s))); } 00030 bool is_output_port () { return true; } 00031 bool busy () { return true; } 00032 nat can_write () { return (nat) (-1); } 00033 void write (const char* x, nat n) { s << string (x, n); } 00034 00035 public: 00036 inline output_string_port_rep (string& s2): s (s2) {} 00037 }; 00038 00039 port 00040 output_string_port (string& s) { 00041 return (port_rep*) new output_string_port_rep (s); 00042 } 00043 00044 /****************************************************************************** 00045 * Input string ports 00046 ******************************************************************************/ 00047 00048 class input_string_port_rep: public port_rep { 00049 string s; 00050 nat pos; 00051 00052 public: 00053 virtual syntactic expression () const { 00054 return syn ("input_string_port", syntactic (quote (s (pos, N(s))))); } 00055 bool is_input_port () { return true; } 00056 bool busy () { return pos < N(s); } 00057 nat can_read () { return N(s) - pos; } 00058 void read (char* x, nat n) { 00059 ASSERT (pos + n <= N(s), "no more input"); 00060 mem_copy (x, inside (s, pos), n); 00061 pos += n; } 00062 00063 public: 00064 inline input_string_port_rep (const string& s2): s (s2), pos (0) {} 00065 }; 00066 00067 port 00068 input_string_port (const string& s) { 00069 return (port_rep*) new input_string_port_rep (s); 00070 } 00071 00072 /****************************************************************************** 00073 * Input output string ports 00074 ******************************************************************************/ 00075 00076 class input_output_string_port_rep: public port_rep { 00077 string& s; 00078 nat pos; 00079 00080 public: 00081 virtual syntactic expression () const { 00082 return syn ("input_output_string_port", 00083 syntactic (quote (s (pos, N(s))))); } 00084 bool is_output_port () { return true; } 00085 bool is_input_port () { return true; } 00086 bool busy () { return true; } 00087 nat can_write () { return (nat) (-1); } 00088 nat can_read () { return N(s) - pos; } 00089 void write (const char* x, nat n) { s << string (x, n); } 00090 void read (char* x, nat n) { 00091 ASSERT (pos + n <= N(s), "no more input"); 00092 mem_copy (x, inside (s, pos), n); 00093 pos += n; 00094 if (pos > ((N(s) >> 1) + 1024)) { 00095 s= s (pos, N(s)); 00096 pos= 0; 00097 } 00098 } 00099 00100 public: 00101 inline input_output_string_port_rep (string& s2): s (s2), pos (0) {} 00102 }; 00103 00104 port 00105 input_output_string_port (string& s) { 00106 return (port_rep*) new input_output_string_port_rep (s); 00107 } 00108 00109 } // namespace mmx