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 char const * static_decl = "static ";
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 static_decl = "";
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 << static_decl
63 << "char const " << arr << "_constant[" << (dat.size() + 1) << "] = {\n";
64
65 for (unsigned int i = 0; i < dat.size(); ++i)
66 {
67 if (i == 0) cout << '\t';
68 else if (i % 14 == 0) cout << "\n\t";
69 cout << static_cast<int>(dat[i]) << ", ";
70 }
71 cout << "0\n};\n";
72}
73
74// Local Variables:
75// mode: C++
76// fill-column: 76
77// c-file-style: "gnu"
78// indent-tabs-mode: nil
79// End:
80// 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