File: basic_io.cc

package info (click to toggle)
monotone 0.18-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 16,440 kB
  • ctags: 13,394
  • sloc: sh: 130,618; ansic: 70,657; cpp: 51,980; perl: 421; makefile: 359; python: 184; lisp: 132; sql: 83
file content (124 lines) | stat: -rw-r--r-- 2,867 bytes parent folder | download
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
#include <iostream>
#include <sstream>
#include <string>
#include <cctype>
#include <cstdlib>

#include <boost/lexical_cast.hpp>

#include "basic_io.hh"
#include "sanity.hh"
#include "vocab.hh"

// copyright (C) 2004 graydon hoare <graydon@pobox.com>
// all rights reserved.
// licensed to the public under the terms of the GNU GPL (>= 2)
// see the file COPYING for details

// this file provides parsing and printing primitives used by the higher
// level parser and printer routines for the two datatypes change_set and
// revision_set. every revision_set contains a number of change_sets, so
// their i/o routines are somewhat related.


void basic_io::input_source::err(std::string const & s)
{
  L(F("error in %s:%d:%d:E: %s") % name % line % col % s);
  throw informative_failure((F("%s:%d:%d:E: %s") 
			     % name % line % col % s).str());
}


void basic_io::tokenizer::err(std::string const & s)
{
  in.err(s);
}

basic_io::stanza::stanza() : indent(0)
{}

void basic_io::stanza::push_hex_pair(std::string const & k, std::string const & v)
{
  for (std::string::const_iterator i = k.begin(); i != k.end(); ++i)
    I(std::isalnum(*i) || *i == '_');

  for (std::string::const_iterator i = v.begin(); i != v.end(); ++i)
    I(std::isxdigit(*i));
  
  entries.push_back(std::make_pair(k, "[" + v + "]"));
  if (k.size() > indent)
    indent = k.size();
}

void basic_io::stanza::push_str_pair(std::string const & k, std::string const & v)
{
  for (std::string::const_iterator i = k.begin(); i != k.end(); ++i)
    I(std::isalnum(*i) || *i == '_');

  std::string escaped;

  for (std::string::const_iterator i = v.begin();
       i != v.end(); ++i)
    {
      switch (*i)
	{
	case '\\':
	case '"':
	  escaped += '\\';
	default:
	  escaped += *i;
	}
    }

  
  entries.push_back(std::make_pair(k, "\"" + escaped + "\""));
  if (k.size() > indent)
    indent = k.size();
}


basic_io::printer::printer(std::ostream & ost) 
  : empty_output(true), out(ost)
{}

void basic_io::printer::print_stanza(stanza const & st)
{
  if (empty_output)
    empty_output = false;
  else
    out.put('\n');
  
  for (std::vector<std::pair<std::string, std::string> >::const_iterator i = st.entries.begin();
       i != st.entries.end(); ++i)
    {
      for (size_t k = i->first.size(); k < st.indent; ++k)
	out.put(' ');
      out.write(i->first.data(), i->first.size());
      out.put(' ');
      out.write(i->second.data(), i->second.size());
      out.put('\n');
    }
}

void basic_io::parser::err(std::string const & s)
{
  tok.err(s);
}

std::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";
}