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("%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
37basic_io::stanza::stanza() : indent(0)
38{}
39
40void basic_io::stanza::push_hex_pair(std::string const & k, std::string const & v)
41{
42 for (std::string::const_iterator i = k.begin(); i != k.end(); ++i)
43 I(std::isalnum(*i) || *i == '_');
44
45 for (std::string::const_iterator i = v.begin(); i != v.end(); ++i)
46 I(std::isxdigit(*i));
47
48 entries.push_back(std::make_pair(k, "[" + v + "]"));
49 if (k.size() > indent)
50 indent = k.size();
51}
52
53void basic_io::stanza::push_str_pair(std::string const & k, std::string const & v)
54{
55 for (std::string::const_iterator i = k.begin(); i != k.end(); ++i)
56 I(std::isalnum(*i) || *i == '_');
57
58 std::string escaped;
59
60 for (std::string::const_iterator i = v.begin();
61 i != v.end(); ++i)
62 {
63 switch (*i)
64{
65case '\\':
66case '"':
67 escaped += '\\';
68default:
69 escaped += *i;
70}
71 }
72
73
74 entries.push_back(std::make_pair(k, "\"" + escaped + "\""));
75 if (k.size() > indent)
76 indent = k.size();
77}
78
79
80basic_io::printer::printer(std::ostream & ost)
81 : empty_output(true), out(ost)
82{}
83
84void basic_io::printer::print_stanza(stanza const & st)
85{
86 if (empty_output)
87 empty_output = false;
88 else
89 out.put('\n');
90
91 for (std::vector<std::pair<std::string, std::string> >::const_iterator i = st.entries.begin();
92 i != st.entries.end(); ++i)
93 {
94 for (size_t k = i->first.size(); k < st.indent; ++k)
95out.put(' ');
96 out.write(i->first.data(), i->first.size());
97 out.put(' ');
98 out.write(i->second.data(), i->second.size());
99 out.put('\n');
100 }
101}
102
103void basic_io::parser::err(std::string const & s)
104{
105 tok.err(s);
106}
107
108std::string basic_io::parser::tt2str(token_type tt)
109{
110 switch (tt)
111 {
112 case basic_io::TOK_STRING:
113 return "TOK_STRING";
114 case basic_io::TOK_SYMBOL:
115 return "TOK_SYMBOL";
116 case basic_io::TOK_HEX:
117 return "TOK_HEX";
118 case basic_io::TOK_NONE:
119 return "TOK_NONE";
120 }
121 return "TOK_UNKNOWN";
122}
123
124

Archive Download this file

Branches

Tags

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