-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathfile_converter.cpp
60 lines (47 loc) · 1.14 KB
/
file_converter.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*******************************************************************\
Module: Convert file contents to C strings
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Convert file contents to C strings
#include <fstream> // IWYU pragma: keep
#include <iostream>
#include <string>
static void convert_line(const std::string &line)
{
std::cout << "\"";
for(std::size_t i = 0; i < line.size(); i++)
{
const char ch = line[i];
if(ch == '\\')
std::cout << "\\\\";
else if(ch == '"')
std::cout << "\\\"";
else if(ch == '\r' || ch == '\n')
{
}
else if((ch & 0x80) != 0)
{
std::cout << "\\x" << std::hex << (unsigned(ch) & 0xff) << std::dec;
}
else
std::cout << ch;
}
std::cout << "\\n\"\n";
}
int main(int argc, char *argv[])
{
std::string line;
for(int i = 1; i < argc; ++i)
{
std::ifstream input_file(argv[i]);
if(!input_file)
{
std::cerr << "Failed to open " << argv[i] << '\n';
return 1;
}
while(getline(input_file, line))
convert_line(line);
}
return 0;
}