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
echo 'compiling resource_embedder...'
set -x
cc -Wall -Wextra -O2 resource_embedder.c -o resource_embedder
gcc -Wall -Wextra -O2 resource_embedder.c -o resource_embedder
set +x
echo 'done'

View File

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