| 1 | // txt2c.cpp : This simple app takes a text file |
| 2 | // and generates a .cpp file which will embed the |
| 3 | // contents of the text file as plain text. |
| 4 | // |
| 5 | |
| 6 | #include "StdAfx.h" |
| 7 | |
| 8 | #include <string.h> |
| 9 | #include <iostream> |
| 10 | #include <fstream> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | void |
| 17 | quoteLine(char *d, const char *s) |
| 18 | { |
| 19 | while (*s) |
| 20 | { |
| 21 | switch (*s) |
| 22 | { |
| 23 | case '"': |
| 24 | case '\\': |
| 25 | { |
| 26 | *d++ = '\\'; |
| 27 | *d++ = *s++; |
| 28 | break; |
| 29 | } |
| 30 | default: |
| 31 | *d++ = *s++; |
| 32 | break; |
| 33 | } |
| 34 | } |
| 35 | *d++ = '\0'; |
| 36 | } |
| 37 | |
| 38 | int |
| 39 | main(int argc, char* argv[]) |
| 40 | { |
| 41 | if (argc < 3) |
| 42 | { |
| 43 | cerr << "Usage is " << argv[0] << " source.txt result.cpp" << endl; |
| 44 | cerr << "This will transform source.txt into an embeddable string." << endl; |
| 45 | return -2; |
| 46 | } |
| 47 | |
| 48 | ifstream is(argv[1], ios::in); |
| 49 | |
| 50 | if (!is) |
| 51 | { |
| 52 | cerr << "Couldn't open " << argv[1] << endl; |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | ofstream os(argv[2]); |
| 57 | if (!os) |
| 58 | { |
| 59 | cerr << "Couldn't open for write " << argv[2] << endl; |
| 60 | } |
| 61 | |
| 62 | char line[2005]; |
| 63 | char quote[4000]; |
| 64 | |
| 65 | // Build the name to write to from the name of the file.. |
| 66 | char *name; |
| 67 | |
| 68 | name = strdup(argv[2]); |
| 69 | if (strchr(name, '.')) |
| 70 | *strchr(name, '.') = '\0'; |
| 71 | |
| 72 | os << "const char *glb_txt_" << name << " =" << endl; |
| 73 | |
| 74 | while (is) |
| 75 | { |
| 76 | is.getline(line, 2000); |
| 77 | if (!is) |
| 78 | break; |
| 79 | |
| 80 | quoteLine(quote, line); |
| 81 | |
| 82 | os << "\"" << quote << "\\n\"" << endl; |
| 83 | } |
| 84 | os << ";" << endl; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 |