CBMC
file_converter.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Convert file contents to C strings
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include <fstream> // IWYU pragma: keep
13 #include <iostream>
14 #include <string>
15 
16 static void convert_line(const std::string &line)
17 {
18  std::cout << "\"";
19 
20  for(std::size_t i = 0; i < line.size(); i++)
21  {
22  const char ch = line[i];
23  if(ch == '\\')
24  std::cout << "\\\\";
25  else if(ch == '"')
26  std::cout << "\\\"";
27  else if(ch == '\r' || ch == '\n')
28  {
29  }
30  else if((ch & 0x80) != 0)
31  {
32  std::cout << "\\x" << std::hex << (unsigned(ch) & 0xff) << std::dec;
33  }
34  else
35  std::cout << ch;
36  }
37 
38  std::cout << "\\n\"\n";
39 }
40 
41 int main(int argc, char *argv[])
42 {
43  std::string line;
44 
45  for(int i = 1; i < argc; ++i)
46  {
47  std::ifstream input_file(argv[i]);
48 
49  if(!input_file)
50  {
51  std::cerr << "Failed to open " << argv[i] << '\n';
52  return 1;
53  }
54 
55  while(getline(input_file, line))
56  convert_line(line);
57  }
58 
59  return 0;
60 }
int main(int argc, char *argv[])
static void convert_line(const std::string &line)