cmake/Tests/BundleTest/BundleLib.cxx

64 lines
1.3 KiB
C++
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
2016-07-09 11:21:54 +02:00
#include <string.h>
#include <unistd.h>
2011-06-19 15:41:06 +03:00
#include <CoreFoundation/CoreFoundation.h>
int fileExists(char* filename)
{
#ifndef R_OK
2016-07-09 11:21:54 +02:00
#define R_OK 04
#endif
2016-07-09 11:21:54 +02:00
if (access(filename, R_OK) != 0) {
printf("Cannot find file: %s\n", filename);
return 0;
2016-07-09 11:21:54 +02:00
}
return 1;
}
int findBundleFile(char* exec, const char* file)
{
int res;
char* nexec = strdup(exec);
char* fpath = (char*)malloc(strlen(exec) + 100);
int cc;
int cnt = 0;
printf("Process executable name: %s\n", exec);
// Remove the executable name and directory name
2016-07-09 11:21:54 +02:00
for (cc = strlen(nexec) - 1; cc > 0; cc--) {
if (nexec[cc] == '/') {
nexec[cc] = 0;
2016-07-09 11:21:54 +02:00
if (cnt == 1) {
break;
}
2016-07-09 11:21:54 +02:00
cnt++;
}
2016-07-09 11:21:54 +02:00
}
printf("Process executable path: %s\n", nexec);
sprintf(fpath, "%s/%s", nexec, file);
printf("Check for file: %s\n", fpath);
res = fileExists(fpath);
free(nexec);
free(fpath);
return res;
}
2016-07-09 11:21:54 +02:00
int foo(char* exec)
{
2011-06-19 15:41:06 +03:00
// Call a CoreFoundation function...
//
CFBundleRef br = CFBundleGetMainBundle();
2016-07-09 11:21:54 +02:00
(void)br;
int res1 = findBundleFile(exec, "Resources/randomResourceFile.plist");
int res2 = findBundleFile(exec, "MacOS/SomeRandomFile.txt");
2014-08-03 19:52:23 +02:00
int res3 = findBundleFile(exec, "MacOS/README.rst");
2016-07-09 11:21:54 +02:00
if (!res1 || !res2 || !res3) {
return 1;
2016-07-09 11:21:54 +02:00
}
return 0;
}