monotone

monotone Mtn Source Tree

Root/basic_io.cc

1#include <iostream>
2#include <sstream>
3#include <string>
4#include <cctype>
5#include <cstdlib>
6
7#include <boost/lexical_cast.hpp>
8
9#include "basic_io.hh"
10#include "sanity.hh"
11#include "vocab.hh"
12
13// copyright (C) 2004 graydon hoare <graydon@pobox.com>
14// all rights reserved.
15// licensed to the public under the terms of the GNU GPL (>= 2)
16// see the file COPYING for details
17
18// this file provides parsing and printing primitives used by the higher
19// level parser and printer routines for the two datatypes change_set and
20// revision_set. every revision_set contains a number of change_sets, so
21// their i/o routines are somewhat related.
22
23
24void basic_io::input_source::err(std::string const & s)
25{
26 L(F("error in %s:%d:%d:E: %s") % name % line % col % s);
27 throw informative_failure((F("error in %s:%d:%d:E: %s")
28 % name % line % col % s).str());
29}
30
31
32void basic_io::tokenizer::err(std::string const & s)
33{
34 in.err(s);
35}
36
37std::string basic_io::escape(std::string const & s)
38{
39 std::string escaped;
40 escaped.reserve(s.size() + 8);
41
42 escaped += "\"";
43
44 for (std::string::const_iterator i = s.begin(); i != s.end(); ++i)
45 {
46 switch (*i)
47 {
48 case '\\':
49 case '"':
50 escaped += '\\';
51 default:
52 escaped += *i;
53 }
54 }
55
56 escaped += "\"";
57
58 return escaped;
59}
60
61basic_io::stanza::stanza() : indent(0)
62{}
63
64void basic_io::stanza::push_hex_pair(std::string const & k, std::string const & v)
65{
66 for (std::string::const_iterator i = k.begin(); i != k.end(); ++i)
67 I(std::isalnum(*i) || *i == '_');
68
69 for (std::string::const_iterator i = v.begin(); i != v.end(); ++i)
70 I(std::isxdigit(*i));
71
72 entries.push_back(std::make_pair(k, "[" + v + "]"));
73 if (k.size() > indent)
74 indent = k.size();
75}
76
77void basic_io::stanza::push_str_pair(std::string const & k, std::string const & v)
78{
79 for (std::string::const_iterator i = k.begin(); i != k.end(); ++i)
80 I(std::isalnum(*i) || *i == '_');
81
82 entries.push_back(std::make_pair(k, escape(v)));
83 if (k.size() > indent)
84 indent = k.size();
85}
86
87void basic_io::stanza::push_file_pair(std::string const & k, file_path const & v)
88{
89 push_str_pair(k, v.as_internal());
90}
91
92void basic_io::stanza::push_str_multi(std::string const & k,
93 std::vector<std::string> const & v)
94{
95 for (std::string::const_iterator i = k.begin(); i != k.end(); ++i)
96 I(std::isalnum(*i) || *i == '_');
97
98 std::string val;
99 bool first = true;
100 for (std::vector<std::string>::const_iterator i = v.begin();
101 i != v.end(); ++i)
102 {
103 if (!first)
104 val += " ";
105 val += escape(*i);
106 first = false;
107 }
108 entries.push_back(std::make_pair(k, val));
109 if (k.size() > indent)
110 indent = k.size();
111}
112
113basic_io::printer::printer(std::ostream & ost)
114 : empty_output(true), out(ost)
115{}
116
117void basic_io::printer::print_stanza(stanza const & st)
118{
119 if (empty_output)
120 empty_output = false;
121 else
122 out.put('\n');
123
124 for (std::vector<std::pair<std::string, std::string> >::const_iterator i = st.entries.begin();
125 i != st.entries.end(); ++i)
126 {
127 for (size_t k = i->first.size(); k < st.indent; ++k)
128 out.put(' ');
129 out.write(i->first.data(), i->first.size());
130 out.put(' ');
131 out.write(i->second.data(), i->second.size());
132 out.put('\n');
133 }
134}
135
136void basic_io::parser::err(std::string const & s)
137{
138 tok.err(s);
139}
140
141std::string basic_io::parser::tt2str(token_type tt)
142{
143 switch (tt)
144 {
145 case basic_io::TOK_STRING:
146 return "TOK_STRING";
147 case basic_io::TOK_SYMBOL:
148 return "TOK_SYMBOL";
149 case basic_io::TOK_HEX:
150 return "TOK_HEX";
151 case basic_io::TOK_NONE:
152 return "TOK_NONE";
153 }
154 return "TOK_UNKNOWN";
155}
156
157

Archive Download this file

Branches

Tags

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