From f913a8513f71e593353d92fff06ae47f83144dfb Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Sun, 18 Apr 2021 17:08:35 +0200 Subject: [PATCH] Testing functions to open files from bundle --- src/bundle_open.h | 9 +++++++++ src/bundle_open.m | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 7 +++++++ src/meson.build | 4 ++++ 4 files changed, 67 insertions(+) create mode 100644 src/bundle_open.h create mode 100644 src/bundle_open.m diff --git a/src/bundle_open.h b/src/bundle_open.h new file mode 100644 index 00000000..4932075f --- /dev/null +++ b/src/bundle_open.h @@ -0,0 +1,9 @@ +#ifndef BUNDLE_OPEN_H +#define BUNDLE_OPEN_H + +#include + +const char *resource_path_from_bundle(); +FILE* open_fp_from_bundle_or_fallback(const char *file, const char *mode); + +#endif diff --git a/src/bundle_open.m b/src/bundle_open.m new file mode 100644 index 00000000..847c07dd --- /dev/null +++ b/src/bundle_open.m @@ -0,0 +1,47 @@ +#include +#include +#include + +#import + +#include "bundle_open.h" + +const char *resource_path_from_bundle() +{ @autoreleasepool +{ + NSFileManager* file_manager = [NSFileManager defaultManager]; + NSString* resource_path = [[NSBundle mainBundle] resourcePath]; + const char *resource_path_c = [resource_path UTF8String]; + size_t len = strlen(resource_path_c); + char *resource_path_s = malloc(len + 1); + if (resource_path_s) { + memcpy(resource_path_s, resource_path_c, len + 1); + } + return resource_path_s; +}} + +FILE* open_fp_from_bundle_or_fallback(const char *file, const char *mode) +{ @autoreleasepool +{ + FILE* fp = NULL; + + /* If the file mode is writable, skip all the bundle stuff because generally the bundle is read-only. */ + if(strcmp("r", mode) && strcmp("rb", mode)) { + return fopen(file, mode); + } + + NSFileManager* file_manager = [NSFileManager defaultManager]; + NSString* resource_path = [[NSBundle mainBundle] resourcePath]; + + NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)]; + + NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component]; + if([file_manager fileExistsAtPath:full_path_with_file_to_try]) { + fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode); + } else { + fp = fopen(file, mode); + } + + return fp; +}} + diff --git a/src/main.c b/src/main.c index 9533d4a4..3fe8743d 100644 --- a/src/main.c +++ b/src/main.c @@ -13,6 +13,7 @@ #include #elif __APPLE__ #include + #include "bundle_open.h" #endif @@ -102,6 +103,12 @@ int main(int argc, char **argv) { SetProcessDPIAware(); #endif +#ifdef __APPLE__ + const char *resource_path = resource_path_from_bundle(); + fprintf(stderr, "resource path: %s\n", resource_path); + free(resource_path); +#endif + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); SDL_EnableScreenSaver(); SDL_EventState(SDL_DROPFILE, SDL_ENABLE); diff --git a/src/meson.build b/src/meson.build index 6a56bae6..2800d3d4 100644 --- a/src/meson.build +++ b/src/meson.build @@ -9,6 +9,10 @@ lite_sources = [ 'main.c', ] +if host_machine.system() == 'darwin' + lite_sources += 'bundle_open.m' +endif + executable('lite', lite_sources + lite_rc, include_directories: [lite_include, font_renderer_include],