monotone

monotone Mtn Source Tree

Root/src/basic_io.cc

1// Copyright (C) 2004 Graydon Hoare <graydon@pobox.com>
2// 2008 Stephen Leake <stephen_leake@stephe-leake.org>
3//
4// This program is made available under the GNU GPL version 2.0 or
5// greater. See the accompanying file COPYING for details.
6//
7// This program is distributed WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9// PURPOSE.
10
11#include "base.hh"
12#include "basic_io.hh"
13#include "sanity.hh"
14#include "transforms.hh"
15#include "vocab.hh"
16
17using std::logic_error;
18using std::make_pair;
19using std::pair;
20using std::string;
21using std::vector;
22
23// This file provides parsing and printing primitives used by the
24// higher level parser and printer routines for the datatypes cset,
25// roster/marking_map and revision.
26
27void basic_io::input_source::err(string const & s)
28{
29 E(false, made_from,
30 F("parsing a %s at %d:%d:E: %s") % name % line % col % s);
31}
32
33
34void basic_io::tokenizer::err(string const & s)
35{
36 in.err(s);
37}
38
39string
40basic_io::escape(string const & s)
41{
42 string escaped;
43 escaped.reserve(s.size() + 8);
44
45 escaped += "\"";
46
47 for (string::const_iterator i = s.begin(); i != s.end(); ++i)
48 {
49 switch (*i)
50 {
51 case '\\':
52 case '"':
53 escaped += '\\';
54 default:
55 escaped += *i;
56 }
57 }
58
59 escaped += "\"";
60
61 return escaped;
62}
63
64basic_io::stanza::stanza() : indent(0)
65{}
66
67void basic_io::stanza::push_binary_pair(symbol const & k, id const & v)
68{
69 push_hex_pair(k, hexenc<id>(encode_hexenc(v(), v.made_from), v.made_from));
70}
71
72void
73basic_io::stanza::push_symbol(symbol const & k)
74{
75 entries.push_back(make_pair(k, ""));
76 if (k().size() > indent)
77 indent = k().size();
78}
79
80void basic_io::stanza::push_hex_pair(symbol const & k, hexenc<id> const & v)
81{
82 entries.push_back(make_pair(k, ""));
83 string const & s(v());
84 entries.back().second.reserve(s.size()+2);
85 entries.back().second.push_back('[');
86 entries.back().second.append(s);
87 entries.back().second.push_back(']');
88 if (k().size() > indent)
89 indent = k().size();
90}
91
92void basic_io::stanza::push_binary_triple(symbol const & k,
93 string const & n,
94 id const & v)
95{
96 string const & s(encode_hexenc(v(), v.made_from));
97 entries.push_back(make_pair(k, escape(n) + " " + "[" + s + "]"));
98 if (k().size() > indent)
99 indent = k().size();
100}
101
102void basic_io::stanza::push_str_pair(symbol const & k, string const & v)
103{
104 entries.push_back(make_pair(k, escape(v)));
105 if (k().size() > indent)
106 indent = k().size();
107}
108
109void basic_io::stanza::push_str_pair(symbol const & k, symbol const & v)
110{
111 // Note that this value is a symbol, not a string; the Lua basic_io parser
112 // will return this pair as two lines with no values.
113 entries.push_back(make_pair(k, v()));
114 if (k().size() > indent)
115 indent = k().size();
116}
117
118void basic_io::stanza::push_file_pair(symbol const & k, file_path const & v)
119{
120 push_str_pair(k, v.as_internal());
121}
122
123void basic_io::stanza::push_str_multi(symbol const & k,
124 vector<string> const & v)
125{
126 string val;
127 bool first = true;
128 for (vector<string>::const_iterator i = v.begin();
129 i != v.end(); ++i)
130 {
131 if (!first)
132 val += " ";
133 val += escape(*i);
134 first = false;
135 }
136 entries.push_back(make_pair(k, val));
137 if (k().size() > indent)
138 indent = k().size();
139}
140
141void basic_io::stanza::push_str_multi(symbol const & k1,
142 symbol const & k2,
143 vector<string> const & v)
144{
145 string val = k2();
146 for (vector<string>::const_iterator i = v.begin();
147 i != v.end(); ++i)
148 {
149 val += " ";
150 val += escape(*i);
151 }
152 entries.push_back(make_pair(k1, val));
153 if (k1().size() > indent)
154 indent = k1().size();
155}
156
157void basic_io::stanza::push_str_triple(symbol const & k,
158 string const & n,
159 string const & v)
160{
161 entries.push_back(make_pair(k, escape(n) + " " + escape(v)));
162 if (k().size() > indent)
163 indent = k().size();
164}
165
166
167string basic_io::printer::buf;
168int basic_io::printer::count;
169
170basic_io::printer::printer()
171{
172 I(count == 0);
173 count++;
174 buf.clear();
175}
176
177basic_io::printer::~printer()
178{
179 count--;
180}
181
182void basic_io::printer::print_stanza(stanza const & st)
183{
184 if (LIKELY(!buf.empty()))
185 buf += '\n';
186
187 for (vector<pair<symbol, string> >::const_iterator i = st.entries.begin();
188 i != st.entries.end(); ++i)
189 {
190 for (size_t k = i->first().size(); k < st.indent; ++k)
191 buf += ' ';
192 buf.append(i->first());
193 buf += ' ';
194 buf.append(i->second);
195 buf += '\n';
196 }
197}
198
199void basic_io::parser::err(string const & s)
200{
201 tok.err(s);
202}
203
204string basic_io::parser::tt2str(token_type tt)
205{
206 switch (tt)
207 {
208 case basic_io::TOK_STRING:
209 return "TOK_STRING";
210 case basic_io::TOK_SYMBOL:
211 return "TOK_SYMBOL";
212 case basic_io::TOK_HEX:
213 return "TOK_HEX";
214 case basic_io::TOK_NONE:
215 return "TOK_NONE";
216 }
217 return "TOK_UNKNOWN";
218}
219
220
221// Local Variables:
222// mode: C++
223// fill-column: 76
224// c-file-style: "gnu"
225// indent-tabs-mode: nil
226// End:
227// 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