initial
[prop.git] / lib-src / pretty / prostream.cc
blobcc2863766cb13142552b1135f574ca7b96f0b491
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
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
9 // your programs.
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
16 // code.
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)
22 // 1994-1997
23 //////////////////////////////////////////////////////////////////////////////
25 #include <iostream.h>
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()
34 { indentation = 0;
35 last_token = DELIMITER;
36 spaces_in_tab = 3;
39 ostream& PrettyOStream::set_stream(ostream& s)
40 { ostream * old = the_stream;
41 the_stream = &s;
42 return *old;
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++)
55 (*the_stream) << ' ';
56 last_token = PrettyOStream::DELIMITER;
57 return *this;
60 PrettyOStream& PrettyOStream::newline()
61 { last_token = PrettyOStream::DELIMITER; (*the_stream) << '\n'; return *this; }
63 PrettyOStream& PrettyOStream::nospace()
64 { last_token = PrettyOStream::DELIMITER;
65 return *this;
68 PrettyOStream& PrettyOStream::space()
69 { if (last_token != DELIMITER) (*the_stream) << ' ';
70 last_token = PrettyOStream::NONE;
71 return *this;
74 // PrettyOStream& PrettyOStream::operator << (void *)
75 // {
76 // return *this;
77 // }
79 PrettyOStream& PrettyOStream::operator << (char x)
80 { switch (x)
81 { case '[': case '(': case '{': case ',': case ';': case '<':
82 last_token = DELIMITER; break;
83 default:
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(); }