cprover
Loading...
Searching...
No Matches
file_converter.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Convert file contents to C strings
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
11
12#ifndef CPROVER_ANSI_C_FILE_CONVERTER_H
13#define CPROVER_ANSI_C_FILE_CONVERTER_H
14
15#include <istream>
16#include <string>
17
23inline void file_converter_emit_bytes(std::ostream &out, const std::string &s)
24{
25 for(const char c : s)
26 {
27 const unsigned char ch = static_cast<unsigned char>(c);
28 if(ch >= 0x80)
29 out << "(char)" << unsigned(ch) << ',';
30 else
31 out << unsigned(ch) << ',';
32 }
33}
34
44 std::istream &in,
45 std::ostream &out,
46 bool line_marker,
47 const std::string &file_name)
48{
49 if(line_marker)
50 {
51 file_converter_emit_bytes(out, "#line 1 \"" + file_name + "\"\n");
52 out << '\n';
53 }
54
55 std::string line;
56 while(std::getline(in, line))
57 {
58 if(!line.empty() && line.back() == '\r')
59 line.pop_back();
61 out << "'\\n',\n";
62 }
63}
64
65#endif // CPROVER_ANSI_C_FILE_CONVERTER_H
void file_converter_append(std::istream &in, std::ostream &out, bool line_marker, const std::string &file_name)
Append the contents of in to out as the body of a C character-array initialiser; the enclosing braces...
void file_converter_emit_bytes(std::ostream &out, const std::string &s)
Emit the bytes of s to out as comma-separated decimal byte values (not character literals).