fixed bugs

This commit is contained in:
Timerix 2024-07-30 19:36:44 +03:00
parent 9cc2fd7814
commit b921791be5
2 changed files with 93 additions and 78 deletions

View File

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -e set -eo pipefail
echo 'compiling resource_embedder...' echo 'compiling resource_embedder...'
set -x set -x
gcc -Wall -Wextra -O2 resource_embedder.c -o resource_embedder gcc -Wall -Wextra -O2 resource_embedder.c -o resource_embedder

View File

@ -34,7 +34,6 @@ char* basedir_exclude(const char* path, const char* basename){
int len=strlen(path); int len=strlen(path);
int base_len=strlen(basename); int base_len=strlen(basename);
int i=0; int i=0;
fprintf(stderr, "len=%i base_len=%i\n", len, base_len);
if(len>0 && base_len>0){ if(len>0 && base_len>0){
// remove trailing path separator // remove trailing path separator
if(basename[base_len-1]=='/' || basename[base_len-1]=='\\') if(basename[base_len-1]=='/' || basename[base_len-1]=='\\')
@ -83,74 +82,94 @@ void print_line(bool print_lines, bool start_new_line){
bool _first_file_processed=false; bool _first_file_processed=false;
unsigned int _input_files_n=0; unsigned int _input_files_n=0;
void write_header(FILE* out_file, const char* out_file_path){
int onml=strlen(out_file_path);
if(onml>47)
onml=0;
char* _sp1=mult_space(47-onml);
fprintf(out_file,
"////////////////////////////////////////////////////////////////\n"
"// This file was generated by resource_embedder //\n"
"// https://timerix.ddns.net:3322/Timerix/resource_embedder //\n"
"////////////////////////////////////////////////////////////////\n"
"// USAGE: //\n"
"// Put it in a SOURCE file to define variables //\n"
"// #define EMBEDDED_RESOURCE_DEFINITION //\n"
"// #define EMBEDDED_RESOURCE_POSTFIX your_postfix //\n"
"// #include \"%s\"%s//\n"
"// //\n"
"// Put it in a HEADER file to declare external variables //\n"
"// #define EMBEDDED_RESOURCE_POSTFIX your_postfix //\n"
"// #include \"%s\"%s//\n"
"// //\n"
"// Then you can access embedded files through //\n"
"// EmbeddedResource_table_your_postfix. You can get table //\n"
"// content by index and put it into a hashtable or a map. //\n"
"////////////////////////////////////////////////////////////////\n"
"\n",
out_file_path, _sp1,
out_file_path, _sp1);
free(_sp1);
fprintf(out_file,
"#pragma once\n"
"#if __cplusplus\n"
"extern \"C\" {\n"
"#endif\n"
"\n"
"#include <stdint.h>\n"
"\n"
"typedef struct {\n"
" const char* path;\n"
" const char* data;\n"
" size_t size;\n"
"} EmbeddedResource;\n"
"\n"
"#define RSCAT(A,B,C...) A##B##C"
"\n"
"#ifdef EMBEDDED_RESOURCE_POSTFIX\n"
" #define _EmbeddedResource_table(P) \\\n"
" RSCAT(EmbeddedResource_table_, P)\n"
" #define _EmbeddedResource_table_count(P) \\\n"
" RSCAT(EmbeddedResource_table_, P, _count)\n"
"#else\n"
" #define _EmbeddedResource_table(P) \\\n"
" EmbeddedResource_table\n"
" #define _EmbeddedResource_table_count(P) \\\n"
" EmbeddedResource_table_count\n"
"#endif\n"
"extern const EmbeddedResource _EmbeddedResource_table(EMBEDDED_RESOURCE_POSTFIX)[];\n"
"extern const int _EmbeddedResource_table_count(EMBEDDED_RESOURCE_POSTFIX);\n"
"\n"
"#ifdef EMBEDDED_RESOURCE_DEFINITION\n"
"const EmbeddedResource _EmbeddedResource_table(EMBEDDED_RESOURCE_POSTFIX)[]={\n"
);
}
void write_footer(FILE* out_file){
fprintf(out_file,
"\n};\n\n"
"const int _EmbeddedResource_table_count(EMBEDDED_RESOURCE_POSTFIX)=%u;\n"
"#endif // EMBEDDED_RESOURCE_DEFINITION\n"
"\n"
"#undef _EmbeddedResource_table\n"
"#undef _EmbeddedResource_table_count\n"
"#undef EMBEDDED_RESOURCE_POSTFIX\n"
"#undef RSCAT\n"
"\n"
"#if __cplusplus\n"
"}\n"
"#endif\n",
_input_files_n
);
}
void process_file(const char* input_file_path, void process_file(const char* input_file_path,
const char* embedded_file_path, const char* embedded_file_path,
FILE* out_file, FILE* out_file,
const char* out_file_path) const char* out_file_path)
{ {
bool print_lines=streq(out_file_path, "stdout"); bool print_lines=streq(out_file_path, "stdout");
if(!_first_file_processed){
print_line(print_lines, false);
int onml=strlen(out_file_path);
if(onml>47) onml=0;
char* _sp1=mult_space(47-onml);
fprintf(out_file,
"////////////////////////////////////////////////////////////////\n"
"// This file was generated by resource_embedder //\n"
"// https://timerix.ddns.net:3322/Timerix/resource_embedder //\n"
"////////////////////////////////////////////////////////////////\n"
"// USAGE: //\n"
"// Put it in a SOURCE file to define variables //\n"
"// #define EMBEDDED_RESOURCE_DEFINITION //\n"
"// #define EMBEDDED_RESOURCE_POSTFIX your_postfix //\n"
"// #include \"%s\"%s//\n"
"// //\n"
"// Put it in a HEADER file to declare external variables //\n"
"// #define EMBEDDED_RESOURCE_POSTFIX your_postfix //\n"
"// #include \"%s\"%s//\n"
"// //\n"
"// Then you can access embedded files through //\n"
"// EmbeddedResource_table_your_postfix. You can get table //\n"
"// content by index and put it into a hashtable or a map. //\n"
"////////////////////////////////////////////////////////////////\n"
"\n",
out_file_path, _sp1,
out_file_path, _sp1);
free(_sp1);
fprintf(out_file,
"#pragma once\n"
"#include <stdint.h>\n"
"\n"
"typedef struct {\n"
" const char* path;\n"
" const char* data;\n"
" int32_t size;\n"
"} EmbeddedResource;\n"
"\n"
"#define RSCAT(A,B,C...) A##B##C"
"\n"
"#ifdef EMBEDDED_RESOURCE_POSTFIX\n"
" #define _EmbeddedResource_table(P) \\\n"
" RSCAT(EmbeddedResource_table_, P)\n"
" #define _EmbeddedResource_table_count(P) \\\n"
" RSCAT(EmbeddedResource_table_, P, _count)\n"
"#else\n"
" #define _EmbeddedResource_table(P) \\\n"
" EmbeddedResource_table\n"
" #define _EmbeddedResource_table_count(P) \\\n"
" EmbeddedResource_table_count\n"
"#endif\n"
"extern const EmbeddedResource _EmbeddedResource_table(EMBEDDED_RESOURCE_POSTFIX)[];\n"
"extern const int _EmbeddedResource_table_count(EMBEDDED_RESOURCE_POSTFIX);\n"
"\n"
"\n"
"#ifdef EMBEDDED_RESOURCE_DEFINITION\n"
"const EmbeddedResource _EmbeddedResource_table(EMBEDDED_RESOURCE_POSTFIX)[]={\n");
print_line(print_lines, false);
}
fprintf(stderr, "reading file %s\n", input_file_path); fprintf(stderr, "reading file %s\n", input_file_path);
print_line(print_lines, false); print_line(print_lines, false);
@ -164,18 +183,18 @@ void process_file(const char* input_file_path,
// writing input file code header // writing input file code header
fprintf(out_file, fprintf(out_file,
" {\n" " {\n"
" .path=(char*)\"%s\",\n" " .path=\"%s\",\n"
" .data=(char[]){", " .data=(const char[]){",
embedded_file_path); embedded_file_path);
// writing input file content // writing input file content
int byte=0; int byte=0;
unsigned long long size=0; size_t size=0;
while( (byte=fgetc(input_file)) != EOF ){ while( (byte=fgetc(input_file)) != EOF ){
if(size%16==0) if(size%16==0)
fprintf(out_file, "\n "); fprintf(out_file, "\n ");
fprintf(out_file, "0x%02X,", byte); fprintf(out_file, "0x%02X,", byte);
size+=1; size++;
} }
// trailing zero to use file.data as cstring // trailing zero to use file.data as cstring
fprintf(out_file, "0x0"); fprintf(out_file, "0x0");
@ -234,6 +253,11 @@ int main(const int argc, const char * const* argv){
if(out_file==NULL) if(out_file==NULL)
exit_with_error("can't open file %s", out_file_path); exit_with_error("can't open file %s", out_file_path);
fprintf(stderr, "output file has been set to %s\n", out_file_path); fprintf(stderr, "output file has been set to %s\n", out_file_path);
bool print_lines=streq(out_file_path, "stdout");
print_line(print_lines, false);
write_header(out_file, out_file_path);
print_line(print_lines, false);
} }
else if(argis("d")){ else if(argis("d")){
@ -261,16 +285,7 @@ int main(const int argc, const char * const* argv){
bool out_file_set=!streq(out_file_path, "stdout"); bool out_file_set=!streq(out_file_path, "stdout");
if(!out_file_set) if(!out_file_set)
print_line(true, false); print_line(true, false);
fprintf(out_file, write_footer(out_file);
"\n};\n\n"
"const int _EmbeddedResource_table_count(EMBEDDED_RESOURCE_POSTFIX)=%u;\n"
"#endif // EMBEDDED_RESOURCE_DEFINITION\n"
"\n"
"#undef _EmbeddedResource_table\n"
"#undef _EmbeddedResource_table_count\n"
"#undef EMBEDDED_RESOURCE_POSTFIX\n"
"#undef RSCAT\n",
_input_files_n);
if(!out_file_set) if(!out_file_set)
fclose(out_file); fclose(out_file);
print_line(true, false); print_line(true, false);