1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are welcomed to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
21 // Allen Leung (leunga@cs.nyu.edu)
23 //////////////////////////////////////////////////////////////////////////////
26 #include <AD/strings/charesc.h>
27 #include <AD/pretty/postream.h>
29 PrettyOStream::PrettyOStream() : the_stream(&cout
) { init(); }
30 PrettyOStream::PrettyOStream(ostream
& s
) : the_stream(&s
) { init(); }
31 PrettyOStream::~PrettyOStream() { init(); }
33 void PrettyOStream::init()
35 last_token
= DELIMITER
;
39 ostream
& PrettyOStream::set_stream(ostream
& s
)
40 { ostream
* old
= the_stream
;
45 PrettyOStream
& PrettyOStream::set_tab_spacing(int s
)
46 { spaces_in_tab
= s
; return *this; }
48 PrettyOStream
& PrettyOStream::indent() { indentation
++; return *this; }
50 PrettyOStream
& PrettyOStream::unindent() { indentation
--; return *this; }
52 PrettyOStream
& PrettyOStream::tab()
53 { int spaces
= indentation
* spaces_in_tab
;
54 for (int i
= 0; i
< spaces
; i
++)
56 last_token
= PrettyOStream::DELIMITER
;
60 PrettyOStream
& PrettyOStream::newline()
61 { last_token
= PrettyOStream::DELIMITER
; (*the_stream
) << '\n'; return *this; }
63 PrettyOStream
& PrettyOStream::nospace()
64 { last_token
= PrettyOStream::DELIMITER
;
68 PrettyOStream
& PrettyOStream::space()
69 { if (last_token
!= DELIMITER
) (*the_stream
) << ' ';
70 last_token
= PrettyOStream::NONE
;
74 // PrettyOStream& PrettyOStream::operator << (void *)
79 PrettyOStream
& PrettyOStream::operator << (char x
)
81 { case '[': case '(': case '{': case ',': case ';': case '<':
82 last_token
= DELIMITER
; break;
84 last_token
= NONE
; break;
86 (*the_stream
) << x
; return *this;
89 PrettyOStream
& PrettyOStream::operator << ( int x
)
90 { if (last_token
!= DELIMITER
) (*the_stream
) << ' ';
91 last_token
= NUMBER
; (*the_stream
) << x
; return *this;
94 PrettyOStream
& PrettyOStream::operator << (double x
)
95 { if (last_token
!= DELIMITER
) (*the_stream
) << ' ';
96 last_token
= NUMBER
; (*the_stream
) << x
; return *this;
99 PrettyOStream
& PrettyOStream::operator << (const char * x
)
100 { if (last_token
!= DELIMITER
) (*the_stream
) << ' ';
101 last_token
= IDENTIFIER
; (*the_stream
) << x
; return *this;
104 void indent(PrettyOStream
& s
) { s
.indent(); }
105 void unindent(PrettyOStream
& s
) { s
.unindent(); }
106 void tab(PrettyOStream
& s
) { s
.tab(); }
107 void newline(PrettyOStream
& s
) { s
.newline(); }
108 void nospace(PrettyOStream
& s
) { s
.nospace(); }
109 void space(PrettyOStream
& s
) { s
.space(); }