File: to_c_array.cpp

package info (click to toggle)
jsonnet 0.17.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,344 kB
  • sloc: cpp: 23,062; python: 1,705; ansic: 865; sh: 708; javascript: 576; makefile: 187; java: 140
file content (43 lines) | stat: -rw-r--r-- 996 bytes parent folder | download | duplicates (4)
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
// Converts stdin string to a comma-separated list of byte values and prints the
// result. Used for transforming the standard library into a C array.
//
// Usage:
//   to_c_array <infile> <outfile>
#include <fstream>
#include <iostream>
#include <iterator>

int main(int argc, char *argv[])
{
    if (argc < 3) {
        std::cerr << "usage: to_c_array <infile> <outfile>\n";
        return 1;
    }

    std::ifstream in_file(argv[1]);
    if (!in_file.is_open()) {
        std::cerr << "Can't open input file.";
        return 1;
    }

    std::ofstream out_file(argv[2]);
    if (!out_file.is_open()) {
        std::cerr << "Can't open output file.";
        return 1;
    }

    char c;
    bool first_character = true;
    while (in_file.get(c)) {
        if (first_character) {
            first_character = false;
        } else {
            out_file << ",";
        }
        // Write byte value of c to stdout.
        out_file << (int)c;
    }
    out_file << ",0";

    return 0;
}