fixed bugs

This commit is contained in:
Timerix 2024-07-21 08:16:57 +03:00
parent 1708415900
commit 606875b498
2 changed files with 67 additions and 56 deletions

View File

@ -1,7 +1,7 @@
#/bin/sh #!/usr/bin/env bash
set -e set -e
echo 'compiling resource_embedder...' echo 'compiling resource_embedder...'
set -x set -x
cc -Wall -Wextra -O2 resource_embedder.c -o resource_embedder gcc -Wall -Wextra -O2 resource_embedder.c -o resource_embedder
set +x set +x
echo 'done' echo 'done'

View File

@ -33,23 +33,27 @@ 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
if(basename[base_len-1]=='/' || basename[base_len-1]=='\\') if(basename[base_len-1]=='/' || basename[base_len-1]=='\\')
base_len--; // remove trailing path separator base_len--;
if(len>base_len){ if(len>base_len){
while(i<base_len){ while(i<base_len){
if(path[i]!=basename[i]) if(path[i]!=basename[i])
break; break;
i++; i++;
} }
if(i<base_len || basename[i]!='/' || basename[i]!='\\') // skip leading path separator
i=0; if(path[i]=='/' || path[i]=='\\')
else i++; i++;
} }
} }
int rzlt_len=len-i; int rzlt_len=len-i;
char* rzlt=malloc(rzlt_len+1); char* rzlt=malloc(rzlt_len+1);
memcpy(rzlt, path+i, rzlt_len); memcpy(rzlt, path+i, rzlt_len);
rzlt[rzlt_len]=0;
fprintf(stderr, "path without base: %s\n", rzlt);
return rzlt; return rzlt;
} }
@ -83,62 +87,65 @@ void process_file(const char* input_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, "this_file"); bool print_lines=streq(out_file_path, "stdout");
if(!_first_file_processed){ if(!_first_file_processed){
print_line(print_lines, false); print_line(print_lines, false);
int onml=strlen(out_file_path); int onml=strlen(out_file_path);
if(onml>43) onml=0; if(onml>47) onml=0;
char* _sp1=mult_space(43-onml); char* _sp1=mult_space(47-onml);
fprintf(out_file, fprintf(out_file,
// "////////////////////////////////////////////////////////////\n" "////////////////////////////////////////////////////////////////\n"
// "// //\n" "// This file was generated by resource_embedder //\n"
// "//%s%s%s//\n" "// https://timerix.ddns.net:3322/Timerix/resource_embedder //\n"
// "// //\n" "////////////////////////////////////////////////////////////////\n"
"////////////////////////////////////////////////////////////\n"
"// This file has been generated by resource_embedder //\n"
"// src: https://github.com/Timerix22/resource_embedder //\n"
"////////////////////////////////////////////////////////////\n"
"// USAGE: //\n" "// USAGE: //\n"
"// Put inside any .c file //\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" "// #define EMBEDDED_RESOURCE_POSTFIX your_postfix //\n"
"// #include \"%s\"%s//\n" "// #include \"%s\"%s//\n"
"// //\n" "// //\n"
"// Then you can access embedded files through //\n" "// Then you can access embedded files through //\n"
"// EmbeddedResourceFile_table_your_postfix. You can put //\n" "// EmbeddedResource_table_your_postfix. You can get table //\n"
"// table content into a hashtable or get it by index. //\n" "// content by index and put it into a hashtable or a map. //\n"
"////////////////////////////////////////////////////////////\n" "////////////////////////////////////////////////////////////////\n"
"\n", "\n",
// mult_space(28-onml/2), outname, mult_space(28-onml/2+onml%2), out_file_path, _sp1,
out_file_path, _sp1); out_file_path, _sp1);
free(_sp1); free(_sp1);
fprintf(out_file, fprintf(out_file,
"#pragma once\n"
"\n" "\n"
"typedef struct {\n" "typedef struct {\n"
" char* path;\n" " const char* path;\n"
" char* data;\n" " const char* data;\n"
" unsigned long long size;\n" " const unsigned long long size;\n"
"} EmbeddedResourceFile;\n" "} EmbeddedResource;\n"
"\n" "\n"
"#define RSCAT(A,B,C...) A##B##C" "#define RSCAT(A,B,C...) A##B##C"
"\n" "\n"
"#ifdef EMBEDDED_RESOURCE_POSTFIX\n" "#ifdef EMBEDDED_RESOURCE_POSTFIX\n"
" #define _EmbeddedResourceFile_table(P) \\\n" " #define _EmbeddedResource_table(P) \\\n"
" RSCAT(EmbeddedResourceFile_table_, P)\n" " RSCAT(EmbeddedResource_table_, P)\n"
" #define _EmbeddedResourceFile_table_count(P) \\\n" " #define _EmbeddedResource_table_count(P) \\\n"
" RSCAT(EmbeddedResourceFile_table_, P, _count)\n" " RSCAT(EmbeddedResource_table_, P, _count)\n"
"#else\n" "#else\n"
" #define _EmbeddedResourceFile_table(P) \\\n" " #define _EmbeddedResource_table(P) \\\n"
" EmbeddedResourceFile_table\n" " EmbeddedResource_table\n"
" #define _EmbeddedResourceFile_table_count(P) \\\n" " #define _EmbeddedResource_table_count(P) \\\n"
" EmbeddedResourceFile_table_count\n" " EmbeddedResource_table_count\n"
"#endif\n" "#endif\n"
"extern EmbeddedResourceFile _EmbeddedResourceFile_table(EMBEDDED_RESOURCE_POSTFIX)[];\n" "extern const EmbeddedResource _EmbeddedResource_table(EMBEDDED_RESOURCE_POSTFIX)[];\n"
"extern unsigned int _EmbeddedResourceFile_table_count(EMBEDDED_RESOURCE_POSTFIX);" "extern const int _EmbeddedResource_table_count(EMBEDDED_RESOURCE_POSTFIX);\n"
"\n" "\n"
"\n" "\n"
"EmbeddedResourceFile _EmbeddedResourceFile_table(EMBEDDED_RESOURCE_POSTFIX)[]={\n"); "#ifdef EMBEDDED_RESOURCE_DEFINITION\n"
"const EmbeddedResource _EmbeddedResource_table(EMBEDDED_RESOURCE_POSTFIX)[]={\n");
print_line(print_lines, false); print_line(print_lines, false);
} }
@ -198,8 +205,8 @@ void process_file(const char* input_file_path,
//////////////////////////////////////////////// ////////////////////////////////////////////////
int main(const int argc, const char * const* argv){ int main(const int argc, const char * const* argv){
FILE* out_file=NULL; FILE* out_file=stdout;
const char* out_file_path=NULL; const char* out_file_path="stdout";
const char* basedir=""; const char* basedir="";
for(int i=1; i<argc; i++){ for(int i=1; i<argc; i++){
@ -208,8 +215,7 @@ int main(const int argc, const char * const* argv){
fprintf(stderr, "arg: %s\n", arg); fprintf(stderr, "arg: %s\n", arg);
if(argis("h") || argis("help")){ if(argis("h") || argis("help")){
fprintf(stderr, "Usage:\n" fprintf(stderr, "Usage: resource_embedder [options] [file_options] -i input_file [[file_options] -i input_file]...\n"
" resource_embedder [options] [file_options] -i input_file [[file_options] -i input_file]...\n"
"Options:\n" "Options:\n"
" -o output file path (default=stdout)\n" " -o output file path (default=stdout)\n"
"File options:\n" "File options:\n"
@ -218,10 +224,9 @@ int main(const int argc, const char * const* argv){
} }
else if(argis("o")){ else if(argis("o")){
if (i + 1 >= argc)
exit_with_error("expected an argument after -o");
const char* file_path=argv[++i]; const char* file_path=argv[++i];
if(out_file!=NULL)
exit_with_error("can't set out_file to %s, because it is already set to %s",
file_path, out_file_path);
out_file_path=file_path; out_file_path=file_path;
out_file=fopen(out_file_path, "wb"); out_file=fopen(out_file_path, "wb");
if(out_file==NULL) if(out_file==NULL)
@ -229,12 +234,17 @@ int main(const int argc, const char * const* argv){
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);
} }
else if(argis("i")){ else if(argis("d")){
const char* input_file_path=argv[++i]; if (i + 1 >= argc)
if(out_file==NULL){ exit_with_error("expected an argument after -d");
out_file=stdout; basedir=argv[++i];
out_file_path="this_file"; fprintf(stderr, "basedir is set to: %s\n", basedir);
} }
else if(argis("i")){
if (i + 1 >= argc)
exit_with_error("expected an argument after -i");
const char* input_file_path=argv[++i];
char* embedded_path=basedir_exclude(input_file_path, basedir); char* embedded_path=basedir_exclude(input_file_path, basedir);
fprintf(stderr, "input file path: %s\n", arg); fprintf(stderr, "input file path: %s\n", arg);
fprintf(stderr, "embedded path: %s\n", embedded_path); fprintf(stderr, "embedded path: %s\n", embedded_path);
@ -246,15 +256,16 @@ int main(const int argc, const char * const* argv){
} }
if(out_file_path!=NULL){ if(out_file_path!=NULL){
bool out_file_set=!streq(out_file_path, "this_file"); 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, fprintf(out_file,
"\n};\n\n" "\n};\n\n"
"unsigned int _EmbeddedResourceFile_table_count(EMBEDDED_RESOURCE_POSTFIX)=%u;\n" "const int _EmbeddedResource_table_count(EMBEDDED_RESOURCE_POSTFIX)=%u;\n"
"#endif // EMBEDDED_RESOURCE_DEFINITION\n"
"\n" "\n"
"#undef _EmbeddedResourceFile_table\n" "#undef _EmbeddedResource_table\n"
"#undef _EmbeddedResourceFile_table_count\n" "#undef _EmbeddedResource_table_count\n"
"#undef EMBEDDED_RESOURCE_POSTFIX\n" "#undef EMBEDDED_RESOURCE_POSTFIX\n"
"#undef RSCAT\n", "#undef RSCAT\n",
_input_files_n); _input_files_n);