| 19 | #include <cassert> |
| 20 | |
| 21 | int fileToArray(const std::string &source, const std::string &dest, const std::string &arrName) |
| 22 | { |
| 23 | FILE *fs = fopen(source.c_str(), "rb"); |
| 24 | if (!fs) |
| 25 | { |
| 26 | std::cerr << "source file not found: <" << source << ">" << std::endl; |
| 27 | return -1; |
| 28 | } |
| 29 | |
| 30 | std::ofstream fo(dest.c_str()); |
| 31 | if (fo.fail()) |
| 32 | { |
| 33 | std::cerr << "cannot generate file: <" << dest << ">" << std::endl; |
| 34 | fclose(fs); |
| 35 | return -1; |
| 36 | } |
| 37 | |
| 38 | std::cout << "generating <" << dest << ">" << std::endl; |
| 39 | |
| 40 | fo << "#ifndef _" << arrName << "_H_" << std::endl; |
| 41 | fo << "#define _" << arrName << "_H_" << std::endl; |
| 42 | |
| 43 | fo << "const char " << arrName << "[] = {" << std::endl; |
| 44 | |
| 45 | int is_error = fseek(fs, 0L, SEEK_SET); |
| 46 | assert(!is_error); |
| 47 | (void)is_error; |
| 48 | size_t bytes; |
| 49 | do |
| 50 | { |
| 51 | char buf[1024]; |
| 52 | bytes = fread(buf, 1, sizeof(buf), fs); |
| 53 | assert(!ferror(fs) && "file read error"); |
| 54 | |
| 55 | // convert line |
| 56 | for (size_t i = 0; i < bytes; i++) |
| 57 | { |
| 58 | fo << "0x" << std::hex << static_cast<int>(buf[i]) << ", "; |
| 59 | } |
| 60 | } while (bytes != 0); |
| 61 | |
| 62 | fo << "};" << std::endl; |
| 63 | |
| 64 | fo << std::endl; |
| 65 | fo << "#endif /* _" << arrName << "_H_ */" << std::endl; |
| 66 | |
| 67 | fo.flush(); |
| 68 | fclose(fs); |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | std::string extractFileName(std::string path) |
| 74 | { |