monotone

monotone Mtn Source Tree

Root/txt2c.cc

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

Archive Download this file

Branches

Tags

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