66#include " FTB.hpp"
77
88#include < assert.h>
9- #include < sstream>
109#include < stdio.h>
1110#include < stdlib.h>
12- #include < string.h>
1311
12+ #include < fstream>
1413#include < iomanip>
14+ #include < ios>
15+ #include < sstream>
16+ #include < string>
17+ #include < vector>
1518
1619typedef unsigned int stb_uint;
1720typedef unsigned char stb_uchar;
1821stb_uint stb_compress (stb_uchar *out, stb_uchar *in, stb_uint len);
1922
20- std::string Convert (const char *filename, const char *symbol) {
21- FILE *f;
22- errno_t err;
23- err = fopen_s (&f, filename, " rb" );
23+ std::string Convert (std::string file_path, std::string array_name) {
24+ std::ifstream file (file_path, std::ios::in | std::ios::binary);
2425
25- if (!f )
26- return " " ;
26+ if (!file. is_open () )
27+ return " File failed to open " ;
2728
28- if (err != 0 )
29- return " " ;
29+ file.seekg (0 , std::ios::end);
30+ std::streamsize data_sz = file.tellg ();
31+ file.seekg (0 , std::ios::beg);
3032
31- int data_sz;
32-
33- if (fseek (f, 0 , SEEK_END ) || (data_sz = (int )ftell (f)) == -1 ||
34- fseek (f, 0 , SEEK_SET )) {
35- fclose (f);
33+ if (data_sz == -1 ) {
3634 return " " ;
3735 }
3836
39- char *data = new char [data_sz + 4 ];
40-
41- if (fread (data, 1 , data_sz, f) != (size_t )data_sz) {
42- fclose (f);
43- delete[] data;
37+ std::vector<char > data (data_sz + 4 );
38+ if (!file.read (data.data (), data_sz)) {
4439 return " " ;
4540 }
4641
47- memset ((void *)(((char *)data) + data_sz), 0 , 4 );
48- fclose (f);
42+ memset (data.data () + data_sz, 0 , 4 );
4943
5044 int maxlen = data_sz + 512 + (data_sz >> 2 ) + sizeof (int );
51- char *compressed = new char [maxlen];
52- int compressed_sz =
53- stb_compress ((stb_uchar *)compressed, (stb_uchar *)data, data_sz);
45+ std::vector<char > compressed (maxlen);
46+ int compressed_sz = stb_compress (
47+ reinterpret_cast <stb_uchar *>(compressed.data ()),
48+ reinterpret_cast <stb_uchar *>(data.data ()), static_cast <int >(data_sz));
5449
55- memset (compressed + compressed_sz, 0 , maxlen - compressed_sz);
50+ memset (compressed. data () + compressed_sz, 0 , maxlen - compressed_sz);
5651
5752 std::ostringstream stream;
5853
@@ -61,15 +56,15 @@ std::string Convert(const char *filename, const char *symbol) {
6156 stream << " *\t Generated by File To Byte\n " ;
6257 stream << " *\t https://github.com/JerimiahOfficial/FileToByte\n " ;
6358 stream << " */\n\n " ;
64- stream << " static const unsigned int " << symbol
59+ stream << " static const unsigned int " << array_name
6560 << " _compressed_size = " << (int )compressed_sz << " ;\n " ;
66- stream << " static const unsigned int " << symbol << " _compressed_data["
61+ stream << " static const unsigned int " << array_name << " _compressed_data["
6762 << (int )((compressed_sz + 3 ) / 4 ) * 4 << " / 4] = {" ;
6863
6964 int column = 0 ;
7065 for (int i = 0 ; i < compressed_sz; i += 4 ) {
71- unsigned int d = *( unsigned int *) (compressed + i);
72- if ((column++ % 12 ) == 0 )
66+ unsigned int d = *reinterpret_cast < unsigned int *> (compressed. data () + i);
67+ if ((column++ % 8 ) == 0 )
7368 stream << " \n\t 0x" << std::hex << std::setw (8 ) << std::setfill (' 0' ) << d
7469 << " , " ;
7570 else
@@ -78,9 +73,6 @@ std::string Convert(const char *filename, const char *symbol) {
7873 }
7974 stream << " \n };\n " ;
8075
81- delete[] data;
82- delete[] compressed;
83-
8476 return stream.str ();
8577}
8678
0 commit comments