basix_doc 0.1
|
00001 00002 /****************************************************************************** 00003 * MODULE : formatting_port.cpp 00004 * DESCRIPTION: Sugar for formatted 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 00016 00017 namespace mmx { 00018 00019 /****************************************************************************** 00020 * Formatting ports 00021 ******************************************************************************/ 00022 00023 class formatting_port_rep: public port_rep { 00024 port p; 00025 string prefix; 00026 bool need_indent; 00027 string indenter; 00028 00029 public: 00030 syntactic expression () const { 00031 return syn ("formatting_port", flatten (p)); } 00032 bool is_output_port () { 00033 return mmx::is_output_port (p); } 00034 bool is_input_port () { 00035 return mmx::is_input_port (p); } 00036 bool error_flag () { 00037 return mmx::error_flag (p); } 00038 string error_message () { 00039 return mmx::error_message (p); } 00040 bool busy () { 00041 return mmx::busy (p); } 00042 nat can_write () { 00043 return mmx::can_write (p); } 00044 nat can_read () { 00045 return mmx::can_read (p); } 00046 void write (const char* s, nat n) { 00047 for (nat i=0; i<n; ) { 00048 nat start= i; 00049 while (i<n && s[i] != '\n') i++; 00050 if (i>start && need_indent) mmx::write (p, prefix); 00051 need_indent= (i<n); 00052 if (need_indent) i++; 00053 mmx::write (p, s+start, i-start); 00054 } } 00055 void read (char* s, nat n) { 00056 mmx::read (p, s, n); } 00057 void flush () { 00058 mmx::flush (p); } 00059 bool wait (int msecs) { 00060 return mmx::wait (p, msecs); } 00061 port component (const string& name) { 00062 if (name == "wrapped") return p; 00063 ERROR ("port not found"); } 00064 00065 void format (const print_format& fm) { 00066 switch (fm) { 00067 case blank: 00068 indenter= " "; 00069 break; 00070 case stroke: 00071 indenter= "| "; 00072 break; 00073 case indent: 00074 prefix << indenter; 00075 break; 00076 case unindent: 00077 inside (prefix) -> resize (max (N(prefix), N(indenter)) - N(indenter)); 00078 break; 00079 case lf: 00080 write ("\n", 1); 00081 break; 00082 case hrule: 00083 write ("--------------------------------------------------------------------------------", 80); 00084 break; 00085 case flush_now: 00086 flush (); 00087 break; 00088 } 00089 } 00090 00091 public: 00092 inline formatting_port_rep (const port& p2): 00093 p (p2), prefix (""), need_indent (true), indenter (" ") {} 00094 }; 00095 00096 /****************************************************************************** 00097 * Public constructor 00098 ******************************************************************************/ 00099 00100 port 00101 formatting_port (const port& p) { 00102 return (port_rep*) new formatting_port_rep (p); 00103 } 00104 00105 } // namespace mmx