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