monotone

monotone Mtn Source Tree

Root/basic_io.cc

1// Copyright (C) 2004 Graydon Hoare <graydon@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#include <iostream>
11#include <sstream>
12#include <string>
13#include <cctype>
14#include <cstdlib>
15
16#include "basic_io.hh"
17#include "sanity.hh"
18#include "vocab.hh"
19
20using std::logic_error;
21using std::make_pair;
22using std::pair;
23using std::string;
24using std::vector;
25
26// This file provides parsing and printing primitives used by the
27// higher level parser and printer routines for the datatypes cset,
28// roster/marking_map and revision.
29
30void basic_io::input_source::err(string const & s)
31{
32 L(FL("error in %s:%d:%d:E: %s") % name % line % col % s);
33 throw logic_error((F("error in %s:%d:%d:E: %s")
34 % name % line % col % s).str());
35}
36
37
38void basic_io::tokenizer::err(string const & s)
39{
40 in.err(s);
41}
42
43string
44basic_io::escape(string const & s)
45{
46 string escaped;
47 escaped.reserve(s.size() + 8);
48
49 escaped += "\"";
50
51 for (string::const_iterator i = s.begin(); i != s.end(); ++i)
52 {
53 switch (*i)
54 {
55 case '\\':
56 case '"':
57 escaped += '\\';
58 default:
59 escaped += *i;
60 }
61 }
62
63 escaped += "\"";
64
65 return escaped;
66}
67
68basic_io::stanza::stanza() : indent(0)
69{}
70
71void basic_io::stanza::push_hex_pair(symbol const & k, hexenc<id> const & v)
72{
73 entries.push_back(make_pair(k, "[" + v() + "]"));
74 if (k().size() > indent)
75 indent = k().size();
76}
77
78void basic_io::stanza::push_hex_triple(symbol const & k,
79 string const & n,
80 hexenc<id> const & v)
81{
82 entries.push_back(make_pair(k, escape(n) + " " + "[" + v() + "]"));
83 if (k().size() > indent)
84 indent = k().size();
85}
86
87void basic_io::stanza::push_str_pair(symbol const & k, string const & v)
88{
89 entries.push_back(make_pair(k, escape(v)));
90 if (k().size() > indent)
91 indent = k().size();
92}
93
94void basic_io::stanza::push_file_pair(symbol const & k, file_path const & v)
95{
96 push_str_pair(k, v.as_internal());
97}
98
99void basic_io::stanza::push_str_multi(symbol const & k,
100 vector<string> const & v)
101{
102 string val;
103 bool first = true;
104 for (vector<string>::const_iterator i = v.begin();
105 i != v.end(); ++i)
106 {
107 if (!first)
108 val += " ";
109 val += escape(*i);
110 first = false;
111 }
112 entries.push_back(make_pair(k, val));
113 if (k().size() > indent)
114 indent = k().size();
115}
116
117void basic_io::stanza::push_str_triple(symbol const & k,
118 string const & n,
119 string const & v)
120{
121 entries.push_back(make_pair(k, escape(n) + " " + escape(v)));
122 if (k().size() > indent)
123 indent = k().size();
124}
125
126
127string basic_io::printer::buf;
128
129basic_io::printer::printer()
130{
131 buf.clear();
132}
133
134void basic_io::printer::print_stanza(stanza const & st)
135{
136 if (LIKELY(!buf.empty()))
137 buf += '\n';
138
139 for (vector<pair<symbol, string> >::const_iterator i = st.entries.begin();
140 i != st.entries.end(); ++i)
141 {
142 for (size_t k = i->first().size(); k < st.indent; ++k)
143 buf += ' ';
144 buf.append(i->first());
145 buf += ' ';
146 buf.append(i->second);
147 buf += '\n';
148 }
149}
150
151void basic_io::parser::err(string const & s)
152{
153 tok.err(s);
154}
155
156string basic_io::parser::tt2str(token_type tt)
157{
158 switch (tt)
159 {
160 case basic_io::TOK_STRING:
161 return "TOK_STRING";
162 case basic_io::TOK_SYMBOL:
163 return "TOK_SYMBOL";
164 case basic_io::TOK_HEX:
165 return "TOK_HEX";
166 case basic_io::TOK_NONE:
167 return "TOK_NONE";
168 }
169 return "TOK_UNKNOWN";
170}
171
172// Local Variables:
173// mode: C++
174// fill-column: 76
175// c-file-style: "gnu"
176// indent-tabs-mode: nil
177// End:
178// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status