File: txt2c.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (211 lines) | stat: -rw-r--r-- 4,766 bytes parent folder | download | duplicates (4)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (C) 2003 Tom Tromey <tromey@redhat.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 <fstream>
#include <sstream>
#include <string>

#include <cstring> // for strerror
#include <cstdio>  // for rename
#include <cerrno>  // for errno

using std::back_inserter;
using std::cerr;
using std::copy;
using std::filebuf;
using std::ios;
using std::istreambuf_iterator;
using std::ostream;
using std::ostringstream;
using std::ofstream;
using std::rename;
using std::remove;
using std::strerror;
using std::string;

struct ioerror
{
  int errcode;
  string fname;
  string operation;

  ioerror(char const * f, char const * op)
    : errcode(errno), fname(f), operation(op)
  {}
  ioerror(string const & f, char const * op)
    : errcode(errno), fname(f), operation(op)
  {}
};

ostream &
operator<<(ostream & s, ioerror const & e)
{
  s << e.fname << ": "
    << e.operation << " failed: "
    << strerror(errno) << '\n';
  return s;
}

static void
generate_code(string & result,
              char const * fname,
              char const * arrayname,
              bool static_array,
              bool strip_trailing)
{
  string text;

  {
    filebuf fin;
    if (!fin.open(fname, ios::in))
      throw ioerror(fname, "open");
    copy(istreambuf_iterator<char>(&fin), istreambuf_iterator<char>(),
         back_inserter(text));
  }
  if (strip_trailing)
    {
      int last = text.find_last_not_of(" \t\n");
      text.erase(last + 1);
    }

  ostringstream os;

  os << "// DO NOT EDIT\n"
     << "// this file is automatically generated from " << fname << ",\n"
     << "// any changes you make will be destroyed when it is regenerated\n"
     << "\n\n";

  if (static_array)
    os << "static ";
  else
    // some versions of g++ object to constants marked 'extern' and defined
    // at the same time (i.e. constants declared with both 'extern' and an
    // initializer).  to shut them up, first declare the constant 'extern',
    // then define it without 'extern'.
    os << "extern char const " << arrayname << "_constant[];\n";

  os << "char const " << arrayname << "_constant["
     << (text.size() + 1) << "] = {\n";

  for (unsigned int i = 0; i < text.size(); ++i)
    {
      if (i == 0) os << '\t';
      else if (i % 14 == 0) os << "\n\t";
      os << static_cast<int>(text[i]) << ", ";
    }
  os << "0\n};\n";

  result = os.str();
}

static bool
compare_contents(char const *fname, string const & text)
{
  filebuf fin;
  if (!fin.open(fname, ios::in))
    {
      if (errno != ENOENT)
        throw ioerror(fname, "open");
      return false;
    }

  istreambuf_iterator<char> fp(&fin), fend;
  string::const_iterator tp(text.begin()), tend(text.end());

  // cannot use std::equal() because it does not check that the
  // sequences are the same length
  while (fp != fend && tp != tend)
    {
      if (*fp != *tp)
        return false;
      fp++;
      tp++;
    }

  return fp == fend && tp == tend;
}

static void
atomic_update_if_changed(char const *ofname, string const & text)
{
  if (compare_contents(ofname, text))
    return;

  string tfname(ofname);
  tfname += "T";

  {
    ofstream fout(tfname.c_str());
    if (!fout)
      throw ioerror(tfname, "open");

    fout.write(text.data(), text.size());
    if (!fout.flush())
      throw ioerror(tfname, "write");
  }

  if (rename(tfname.c_str(), ofname))
    {
      remove(ofname);
      if (rename(tfname.c_str(), ofname))
        throw ioerror(ofname, "rename");
    }
}

int
main(int argc, char **argv)
{
  bool do_strip_trailing = false;
  bool do_static = false;

  int i = 1;
  if (i < argc && string(argv[i]) == "--strip-trailing")
    {
      do_strip_trailing = true;
      i++;
    }
  if (i < argc && string(argv[i]) == "--static")
    {
      do_static = true;
      i++;
    }
  if (argc - i != 3)
    {
      cerr << "usage: " << argv[0]
           << (" [--strip-trailing] [--static]"
               " <arrayname> <input> <output>\n");
      return 1;
    }

  char const * arr = argv[i++];
  char const * ifname = argv[i++];
  char const * ofname = argv[i];

  try
    {
      string text;
      generate_code(text, ifname, arr, do_static, do_strip_trailing);
      atomic_update_if_changed(ofname, text);
    }
  catch (ioerror const & e)
    {
      cerr << e;
      return 1;
    }
  return 0;
}

// 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: