GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 51
Functions: 0.0% 0 / 0 / 1
Branches: 0.0% 0 / 0 / 56

embedded/CreateEmbeddedSource.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include <string>
7 #include <fstream>
8 #include <iostream>
9
10 #include <stdio.h>
11 #include <assert.h>
12 #include <zlib.h>
13
14 #if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
15 # include <fcntl.h>
16 # include <io.h>
17 # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
18 #else
19 # define SET_BINARY_MODE(file)
20 #endif
21
22 #define CHUNK 16384
23
24 ✗ int main(int argc, char* argv[]) {
25 ✗ if (argc != 5) {
26 ✗ return 1;
27 }
28
29 ✗ const auto* infile = argv[1];
30 ✗ auto* outfile = argv[2];
31 ✗ const auto* filenum = argv[3];
32 ✗ const auto* embeddedname = argv[4];
33
34 z_stream strm;
35 unsigned char in[CHUNK];
36 unsigned char out[CHUNK];
37
38 /* allocate deflate state */
39 ✗ strm.zalloc = Z_NULL;
40 ✗ strm.zfree = Z_NULL;
41 ✗ strm.opaque = Z_NULL;
42 ✗ int ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
43 ✗ if (ret != Z_OK) return 1;
44
45 ✗ FILE* source = fopen(infile, "rb");
46 ✗ if (source == NULL) {
47 ✗ fputs("File error", stderr);
48 ✗ return EXIT_FAILURE;
49 }
50
51 ✗ std::fstream outstream(outfile, std::fstream::out | std::fstream::trunc);
52
53 ✗ if (outstream.is_open()) {
54 ✗ outstream << "static const uint8_t embedded_file_" << filenum << "[] = {";
55 ✗ int flush = 0;
56 // This is the compressed length in chars;
57 ✗ unsigned length = 0;
58 do {
59 ✗ strm.avail_in = fread(in, 1, CHUNK, source);
60 ✗ if (ferror(source)) {
61 ✗ (void)deflateEnd(&strm);
62 ✗ return EXIT_FAILURE;
63 }
64 ✗ flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
65 ✗ strm.next_in = in;
66
67 /* run deflate() on input until output buffer not full, finish
68 compression if all of source has been read in */
69 do {
70 ✗ strm.avail_out = CHUNK;
71 ✗ strm.next_out = out;
72 ✗ ret = deflate(&strm, flush); /* no bad return value */
73 assert(ret != Z_STREAM_ERROR); /* state not clobbered */
74 ✗ unsigned have = CHUNK - strm.avail_out;
75
76 ✗ for (unsigned i = 0; i != have; ++i) {
77 ✗ if (length != 0) {
78 ✗ outstream << ",";
79 }
80 ✗ outstream << "0x" << std::hex << static_cast<int>(out[i]);
81 ✗ ++length;
82 }
83 ✗ } while (strm.avail_out == 0);
84 assert(strm.avail_in == 0); /* all input will be used */
85
86 /* done when last data in file processed */
87 ✗ } while (flush != Z_FINISH);
88 assert(ret == Z_STREAM_END); /* stream will be complete */
89
90 /* clean up and return */
91 ✗ (void)deflateEnd(&strm);
92
93 ✗ outstream << "};";
94
95 ✗ outstream << "\n";
96 ✗ outstream << "static const char *embedded_file_name_" << filenum << " = \"" << embeddedname << "\";";
97 ✗ outstream << "\n";
98 ✗ outstream << "static const size_t embedded_file_len_" << filenum << " = " << std::dec << length << ";";
99 ✗ outstream << std::endl;
100
101 ✗ outstream.close();
102 ✗ fclose(source);
103 } else {
104 ✗ std::cout << "Could not open '" << outfile << "' for writing" << std::endl;
105 ✗ return EXIT_FAILURE;
106 }
107
108 ✗ return 0;
109 ✗ }
110