monotone

monotone Mtn Source Tree

Root/txt2c.cc

1#include <iostream>
2#include <fstream>
3#include <string>
4
5using std::cerr;
6using std::cout;
7using std::endl;
8using std::ifstream;
9using std::string;
10using std::exit;
11
12int main(int argc, char **argv)
13{
14 if (argc < 3 || argc > 5)
15 {
16 cerr << "usage: " << argv[0] << " [--strip-trailing] [--no-static] <filename> <arrayname> " << endl;
17 exit(1);
18 }
19
20 bool do_strip_trailing = false;
21 bool do_static = false;
22 string static_decl("static ");
23 int i = 1;
24 if (string(argv[i]) == "--strip-trailing")
25 {
26 do_strip_trailing = true;
27 i++;
28 }
29 if (string(argv[i]) == "--no-static")
30 {
31 do_static = true;
32 static_decl = "";
33 i++;
34 }
35 ifstream fin(argv[i++]);
36 string arr(argv[i++]);
37
38 if (!fin)
39 {
40 cerr << "could not open " << argv[1] << " for reading" << endl;
41 exit(1);
42 }
43
44 char c;
45 string dat;
46 while(fin.get(c))
47 dat += c;
48
49 if (do_strip_trailing)
50 {
51 int last = dat.find_last_not_of(" \t\n");
52 dat.erase(last + 1);
53 }
54
55 cout << "// DO NOT EDIT\n"
56 << "// this file is automatically generated from " << argv[1 + do_strip_trailing + do_static] << ",\n"
57 << "// any changes you make will be destroyed when it is regenerated\n"
58 << "\n\n"
59 << static_decl
60 << "char const " << arr << "_constant[" << (dat.size() + 1) << "] = {\n";
61
62 for (unsigned int i = 0; i < dat.size(); ++i)
63 {
64 if (i == 0) cout << "\t";
65 else if (i % 20 == 0) cout << "\n\t";
66 cout << static_cast<int>(dat[i]) << ", ";
67 }
68 cout << "0\n};\n";
69}
70
71// Local Variables:
72// mode: C++
73// fill-column: 76
74// c-file-style: "gnu"
75// indent-tabs-mode: nil
76// End:
77// 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