Skip to content

Commit 69258d4

Browse files
committed
Retrieve config files for bundle in MacOS X
1 parent c8ade3d commit 69258d4

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ set_target_properties(suscan PROPERTIES COMPILE_FLAGS "${SIGUTILS_SPC_CFLAGS}")
208208
set_target_properties(suscan PROPERTIES LINK_FLAGS "${SIGUTILS_SPC_LDFLAGS}")
209209

210210
# Required dependencies
211+
if(APPLE)
212+
# Required to retrieve bundle path
213+
target_link_libraries(suscan "-framework CoreFoundation")
214+
endif()
215+
211216
target_link_libraries(suscan m ${SIGUTILS_LIBRARIES})
212217

213218
target_include_directories(suscan SYSTEM PUBLIC ${SNDFILE_INCLUDE_DIRS})

util/confdb.c

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,71 @@
2626
#include <sys/stat.h>
2727
#include <fcntl.h>
2828

29+
#ifdef __APPLE__
30+
# include <CoreFoundation/CoreFoundation.h>
31+
#endif /* __APPLE__ */
32+
2933
#define SU_LOG_DOMAIN "confdb"
3034

3135
#include <sigutils/log.h>
3236
#include "confdb.h"
3337

3438
PTR_LIST(SUPRIVATE suscan_config_context_t, context);
3539

36-
SUPRIVATE const char *confdb_system_path = PKGDATADIR "/config";
40+
SUPRIVATE const char *confdb_system_path;
3741
SUPRIVATE const char *confdb_user_path;
3842

43+
#ifdef __APPLE__
3944
const char *
40-
suscan_confdb_get_system_path(void)
45+
suscan_confdb_get_bundle_path(void)
4146
{
47+
CFBundleRef main_bundle = NULL;
48+
CFURLRef dir_url = NULL;
49+
CFStringRef dir_path = NULL;
50+
CFStringEncoding encmethod;
4251
const char *path = NULL;
52+
53+
if ((main_bundle = CFBundleGetMainBundle()) != NULL) {
54+
SU_TRYCATCH(
55+
dir_url = CFBundleCopyResourceURL(
56+
main_bundle,
57+
CFSTR("suscan/config"),
58+
NULL, /* resourceType */
59+
NULL /* dirName */),
60+
goto done);
61+
62+
SU_TRYCATCH(
63+
dir_path = CFURLCopyFileSystemPath(dir_url, kCFURLPOSIXPathStyle),
64+
goto done);
65+
66+
encmethod = CFStringGetSystemEncoding();
67+
path = CFStringGetCStringPtr(dir_path, encmethod);
68+
}
69+
70+
done:
71+
return path;
72+
}
73+
#endif /* __APPLE__ */
4374

44-
if ((path = getenv("SUSCAN_CONFIG_PATH")) != NULL)
45-
confdb_system_path = path;
75+
const char *
76+
suscan_confdb_get_system_path(void)
77+
{
78+
const char *path = NULL;
79+
80+
if (confdb_system_path == NULL) {
81+
if ((path = getenv("SUSCAN_CONFIG_PATH")) != NULL) {
82+
confdb_system_path = path;
83+
} else {
84+
confdb_system_path = PKGDATADIR "/config";
85+
86+
/* For MacOS there are better alternatives */
87+
#ifdef __APPLE__
88+
path = suscan_confdb_get_bundle_path();
89+
if (path != NULL)
90+
confdb_system_path = path;
91+
#endif /* __APPLE__ */
92+
}
93+
}
4694

4795
return confdb_system_path;
4896
}

0 commit comments

Comments
 (0)