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 string static_decl("static ");
16 int i = 1;
17 if (string(argv[i]) == "--strip-trailing")
18 {
19 do_strip_trailing = true;
20 i++;
21 }
22 if (string(argv[i]) == "--no-static")
23 {
24 static_decl = "";
25 i++;
26 }
27 ifstream fin(argv[i++]);
28 string arr(argv[i++]);
29
30 if (!fin)
31 {
32 cerr << "could not open " << argv[1] << " for reading" << endl;
33 exit(1);
34 }
35
36 char c;
37 string dat;
38 while(fin.get(c))
39 dat += c;
40
41 if (do_strip_trailing)
42 {
43 int last = dat.find_last_not_of(" \t\n");
44 dat.erase(last + 1);
45 }
46
47 cout << "// DO NOT EDIT\n"
48 << "// this file is automatically generated from " << argv[do_strip_trailing?2:1] << ",\n"
49 << "// any changes you make will be destroyed when it is regenerated\n"
50 << "\n\n"
51 << static_decl
52 << "char const " << arr << "_constant[" << (dat.size() + 1) << "] = {\n";
53
54 for (unsigned int i = 0; i < dat.size(); ++i)
55 {
56 if (i == 0) cout << "\t";
57 else if (i % 20 == 0) cout << ",\n\t";
58 else cout << ", ";
59 cout << static_cast<int>(dat[i]);
60 }
61 cout << ", 0\n};\n";
62}

Archive Download this file

Branches

Tags

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