monotone

monotone Mtn Source Tree

Root/txt2c.cc

1#include <iostream>
2#include <fstream>
3#include <string>
4#include <cstring>
5#include <cerrno>
6
7using std::cerr;
8using std::cout;
9using std::ifstream;
10using std::string;
11using std::strerror;
12
13int main(int argc, char **argv)
14{
15 if (argc < 3 || argc > 5)
16 {
17 cerr << "usage: " << argv[0]
18 << " [--strip-trailing] [--no-static] <filename> <arrayname>\n";
19 return 1;
20 }
21
22 bool do_strip_trailing = false;
23 bool do_static = true;
24 int i = 1;
25 if (string(argv[i]) == "--strip-trailing")
26 {
27 do_strip_trailing = true;
28 i++;
29 }
30 if (string(argv[i]) == "--no-static")
31 {
32 do_static = false;
33 i++;
34 }
35 char const * fname = argv[i++];
36 char const * arr = argv[i++];
37
38 ifstream fin(fname);
39 if (!fin)
40 {
41 int e = errno;
42 cerr << "could not open " << fname << " for reading: "
43 << strerror(e) << '\n';
44 return 1;
45 }
46
47 char c;
48 string dat;
49 while(fin.get(c))
50 dat += c;
51
52 if (do_strip_trailing)
53 {
54 int last = dat.find_last_not_of(" \t\n");
55 dat.erase(last + 1);
56 }
57
58 cout << "// DO NOT EDIT\n"
59 << "// this file is automatically generated from " << fname << ",\n"
60 << "// any changes you make will be destroyed when it is regenerated\n"
61 << "\n\n";
62
63 if (do_static)
64 cout << "static ";
65 else
66 // some versions of g++ object to constants marked 'extern' and defined
67 // at the same time (i.e. constants declared with both 'extern' and an
68 // initializer). to shut them up, first declare the constant 'extern',
69 // then define it without 'extern'.
70 cout << "extern char const " << arr << "_constant[];\n";
71
72 cout << "char const " << arr << "_constant[" << (dat.size() + 1) << "] = {\n";
73
74 for (unsigned int i = 0; i < dat.size(); ++i)
75 {
76 if (i == 0) cout << '\t';
77 else if (i % 14 == 0) cout << "\n\t";
78 cout << static_cast<int>(dat[i]) << ", ";
79 }
80 cout << "0\n};\n";
81}
82
83// Local Variables:
84// mode: C++
85// fill-column: 76
86// c-file-style: "gnu"
87// indent-tabs-mode: nil
88// End:
89// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

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