[go: up one dir, main page]

File: chex.cpp

package info (click to toggle)
tora 1.3.23-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,984 kB
  • ctags: 14,460
  • sloc: cpp: 123,554; sh: 16,181; makefile: 966; xml: 69
file content (44 lines) | stat: -rw-r--r-- 1,002 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>

std::string global_string = "static const unsigned char tora_toad[] =\n\"";

inline std::string stringify(unsigned char x)
{
    std::string s;
    std::ostringstream o;
    o << std::hex << std::setw(2) << std::setfill('0') << static_cast<unsigned short>(x);
    s = o.str();
   return s;
 }

std::string readBinaryFile(const std::string& filename)
{
    std::ifstream input(filename.c_str(), std::ios::in | std::ios::binary);
    char c;
    std::string tmp, rc;

    while (input.get(c)) {
	tmp = "\\x";
	tmp += stringify(static_cast<unsigned char>(c));
	rc.append(tmp);
    }
    return rc;
}


int main(int argc, char** argv)
{
    std::string s = readBinaryFile(argv[1]);
    
    for (size_t i=0; i < s.length(); ++i){
	if ( ( i>0 ) && ( i%160 == 0 ) )
	    global_string.append("\"\n\"");
	global_string.push_back(s[i]);
    }
    global_string.append("\"\n;\n");
    std::cout << global_string;
}