1- // Copyright (c) 2018 Johnny Borov <JohnnyBorov@gmail.com>. Released under MIT License.
1+ // Copyright (c) 2018 Johnny Borov <JohnnyBorov@gmail.com>. Released under MIT
2+ // License.
23
34#include < fstream>
45#include < string>
56
6- void generateResourceDataSourceFile (char * resource_name, char * resource_file_name, char * output_file_name);
7- void generateResourcesConfigSourceFile (char * resource_names_list, char * config_file_name);
7+ void generateResourceDataSourceFile (char *resource_name,
8+ char *resource_file_name,
9+ char *output_file_name);
10+ void generateResourcesConfigSourceFile (char *resource_names_list,
11+ char *config_file_name);
812std::string modifyFileName (std::string file_name);
913
10-
11- int main (int argc, char * argv[]) {
14+ int main (int argc, char *argv[]) {
1215 // working directory = CMAKE_BINARY_DIR when called from cmake custom command
1316 if (std::string (argv[1 ]) == " -data" ) {
1417 if (argc == 5 )
@@ -25,19 +28,26 @@ int main(int argc, char* argv[]) {
2528 return 0 ;
2629}
2730
28-
29- // generate a cpp file (e.g. resources/res.txt.cpp) containing global extern array of const unsigned char
30- // of binary data read from the requested input file (CMAKE_SOURCE_DIR/resources/res.txt) plus null-terminator
31- // in the end of data and a global extern const size_t size of this data in bytes (without null-terminator)
32- void generateResourceDataSourceFile (char * resource_name, char * resource_file_name, char * output_file_name) {
33- std::string name{ resource_name }; // e.g. "resources/res.txt"
34- std::string modified_name = modifyFileName (name); // becomes "resources__slash__res__dot__txt"
35-
36- std::ifstream ifs{ resource_file_name, std::ios::binary }; // e.g. reads from CMAKE_SOURCE_DIR/resources/res.txt
37- std::ofstream ofs{ output_file_name }; // e.g. writes to resources/res.txt.cpp
31+ // generate a cpp file (e.g. resources/res.txt.cpp) containing global extern
32+ // array of const unsigned char of binary data read from the requested input
33+ // file (CMAKE_SOURCE_DIR/resources/res.txt) plus null-terminator in the end of
34+ // data and a global extern const size_t size of this data in bytes (without
35+ // null-terminator)
36+ void generateResourceDataSourceFile (char *resource_name,
37+ char *resource_file_name,
38+ char *output_file_name) {
39+ std::string name{resource_name}; // e.g. "resources/res.txt"
40+ std::string modified_name =
41+ modifyFileName (name); // becomes "resources__slash__res__dot__txt"
42+
43+ std::ifstream ifs{
44+ resource_file_name,
45+ std::ios::binary}; // e.g. reads from CMAKE_SOURCE_DIR/resources/res.txt
46+ std::ofstream ofs{output_file_name}; // e.g. writes to resources/res.txt.cpp
3847
3948 ofs << " #include <cstddef>\n " ;
40- ofs << " extern const unsigned char _resource_" << modified_name << " _data[] = {\n " ;
49+ ofs << " extern const unsigned char _resource_" << modified_name
50+ << " _data[] = {\n " ;
4151
4252 int line_count = 0 ;
4353 char c;
@@ -50,28 +60,31 @@ void generateResourceDataSourceFile(char* resource_name, char* resource_file_nam
5060 }
5161 }
5262
53- ofs << " \'\\ 0\' " ; // null-terminator in case data is going to be interprited as a c string
63+ ofs << " \'\\ 0\' " ; // null-terminator in case data is going to be interprited
64+ // as a c string
5465 ofs << " };\n " ;
5566 // -1 excludes null-terminator
56- ofs << " extern const size_t _resource_" << modified_name << " _len = sizeof(_resource_" << modified_name << " _data) - 1;\n " ;
67+ ofs << " extern const size_t _resource_" << modified_name
68+ << " _len = sizeof(_resource_" << modified_name << " _data) - 1;\n " ;
5769}
5870
59-
60- // generate a cpp file (e.g. __resources__config.cpp) that defines ResourceHandle constructor.
61- // The defenition contains the hardcoded mapping of original resource names (e.g. "resources/res.txt")
62- // to their corresponding global extern variables names. Thus it returns a handle which contains pointer to
63- // the beginning of global extern array of const unsigned char and its size (see -data option description)
64- void generateResourcesConfigSourceFile (char * resource_names_list, char * config_file_name) {
71+ // generate a cpp file (e.g. __resources__config.cpp) that defines
72+ // ResourceHandle constructor. The defenition contains the hardcoded mapping of
73+ // original resource names (e.g. "resources/res.txt") to their corresponding
74+ // global extern variables names. Thus it returns a handle which contains
75+ // pointer to the beginning of global extern array of const unsigned char and
76+ // its size (see -data option description)
77+ void generateResourcesConfigSourceFile (char *resource_names_list,
78+ char *config_file_name) {
6579 std::ofstream ofs (config_file_name); // e.g. writes to __resources__config.cpp
6680
67- ofs <<
68- " #include \" ResourceManager/ResourceHandle.h\"\n "
69- " \n "
70- " ResourceHandle::ResourceHandle(std::string resource_name) {\n "
71- " " ;
72-
81+ ofs << " #include \" ResourceManager/ResourceHandle.h\"\n "
82+ " \n "
83+ " ResourceHandle::ResourceHandle(std::string resource_name) {\n "
84+ " " ;
7385
74- std::string resource_names{resource_names_list}; // e.g. "res.txt;res2.txt;resources/res.txt"
86+ std::string resource_names{
87+ resource_names_list}; // e.g. "res.txt;res2.txt;resources/res.txt"
7588 size_t length = resource_names.length ();
7689 size_t start_pos = 0 ;
7790 do {
@@ -83,59 +96,66 @@ void generateResourcesConfigSourceFile(char* resource_names_list, char* config_f
8396 std::string name = resource_names.substr (start_pos, end_pos - start_pos);
8497 std::string modified_name = modifyFileName (name);
8598
86- ofs <<
87- " if (resource_name == \" " << name << " \" ) {\n "
88- " extern const unsigned char _resource_" << modified_name << " _data[];\n "
89- " extern const size_t _resource_" << modified_name << " _len;\n "
90- " m_data_start = _resource_" << modified_name << " _data;\n "
91- " m_data_len = _resource_" << modified_name << " _len;\n "
92- " } else " ;
93-
94- start_pos = end_pos + 1 ; // e.g. start next read from res2... position in "res.txt;res2.txt;resources/res.txt"
99+ ofs << " if (resource_name == \" " << name
100+ << " \" ) {\n "
101+ " extern const unsigned char _resource_"
102+ << modified_name
103+ << " _data[];\n "
104+ " extern const size_t _resource_"
105+ << modified_name
106+ << " _len;\n "
107+ " m_data_start = _resource_"
108+ << modified_name
109+ << " _data;\n "
110+ " m_data_len = _resource_"
111+ << modified_name
112+ << " _len;\n "
113+ " } else " ;
114+
115+ start_pos = end_pos + 1 ; // e.g. start next read from res2... position in
116+ // "res.txt;res2.txt;resources/res.txt"
95117 } while (start_pos < length);
96118
97-
98- ofs <<
99- " {\n "
100- " #ifdef RM_NO_EXCEPTIONS\n "
101- " m_data_start = nullptr;\n "
102- " m_data_len = 0;\n "
103- " #else\n "
104- " throw ResourceNotFound{resource_name};\n "
105- " #endif\n "
106- " }\n "
107- " }\n " ;
119+ ofs << " {\n "
120+ " #ifdef RM_NO_EXCEPTIONS\n "
121+ " m_data_start = nullptr;\n "
122+ " m_data_len = 0;\n "
123+ " #else\n "
124+ " throw ResourceNotFound{resource_name};\n "
125+ " #endif\n "
126+ " }\n "
127+ " }\n " ;
108128}
109129
110-
111130// replace symbols that cant be used in c++ identeficator name
112131// e.g. "resources/res.txt" -> "resources__slash__res__dot__txt"
113132std::string modifyFileName (std::string file_name) {
114133 size_t search_from_pos = 0 ;
115134 size_t replace_from_pos;
116- while ((replace_from_pos = file_name.find_first_of (" .- /\\ " , search_from_pos)) != std::string::npos) {
117- switch (file_name[replace_from_pos]) {
118- case ' .' :
119- file_name.replace (replace_from_pos, 1 , " __dot__" );
120- search_from_pos = replace_from_pos + 7 ; // shift len(__dot__) = 5 symbols
121- break ;
122- case ' -' :
123- file_name.replace (replace_from_pos, 1 , " __dash__" );
124- search_from_pos = replace_from_pos + 8 ;
125- break ;
126- case ' ' :
127- file_name.replace (replace_from_pos, 1 , " __space__" );
128- search_from_pos = replace_from_pos + 9 ;
129- break ;
130- case ' /' :
131- file_name.replace (replace_from_pos, 1 , " __slash__" );
132- search_from_pos = replace_from_pos + 9 ;
133- break ;
134- case ' \\ ' :
135- file_name.replace (replace_from_pos, 1 , " __bslash__" );
136- search_from_pos = replace_from_pos + 10 ;
137- break ;
138- }
135+ while ((replace_from_pos = file_name.find_first_of (
136+ " .- /\\ " , search_from_pos)) != std::string::npos) {
137+ switch (file_name[replace_from_pos]) {
138+ case ' .' :
139+ file_name.replace (replace_from_pos, 1 , " __dot__" );
140+ search_from_pos = replace_from_pos + 7 ; // shift len(__dot__) = 5 symbols
141+ break ;
142+ case ' -' :
143+ file_name.replace (replace_from_pos, 1 , " __dash__" );
144+ search_from_pos = replace_from_pos + 8 ;
145+ break ;
146+ case ' ' :
147+ file_name.replace (replace_from_pos, 1 , " __space__" );
148+ search_from_pos = replace_from_pos + 9 ;
149+ break ;
150+ case ' /' :
151+ file_name.replace (replace_from_pos, 1 , " __slash__" );
152+ search_from_pos = replace_from_pos + 9 ;
153+ break ;
154+ case ' \\ ' :
155+ file_name.replace (replace_from_pos, 1 , " __bslash__" );
156+ search_from_pos = replace_from_pos + 10 ;
157+ break ;
158+ }
139159 }
140160
141161 return file_name;
0 commit comments