BundleLib.cxx 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <CoreFoundation/CoreFoundation.h>
  6. int fileExists(char* filename)
  7. {
  8. #ifndef R_OK
  9. #define R_OK 04
  10. #endif
  11. if (access(filename, R_OK) != 0) {
  12. printf("Cannot find file: %s\n", filename);
  13. return 0;
  14. }
  15. return 1;
  16. }
  17. int findBundleFile(char* exec, const char* file)
  18. {
  19. int res;
  20. char* nexec = strdup(exec);
  21. char* fpath = (char*)malloc(strlen(exec) + 100);
  22. int cc;
  23. int cnt = 0;
  24. printf("Process executable name: %s\n", exec);
  25. // Remove the executable name and directory name
  26. for (cc = strlen(nexec) - 1; cc > 0; cc--) {
  27. if (nexec[cc] == '/') {
  28. nexec[cc] = 0;
  29. if (cnt == 1) {
  30. break;
  31. }
  32. cnt++;
  33. }
  34. }
  35. printf("Process executable path: %s\n", nexec);
  36. sprintf(fpath, "%s/%s", nexec, file);
  37. printf("Check for file: %s\n", fpath);
  38. res = fileExists(fpath);
  39. free(nexec);
  40. free(fpath);
  41. return res;
  42. }
  43. int foo(char* exec)
  44. {
  45. // Call a CoreFoundation function...
  46. //
  47. CFBundleRef br = CFBundleGetMainBundle();
  48. (void)br;
  49. int res1 = findBundleFile(exec, "Resources/randomResourceFile.plist");
  50. int res2 = findBundleFile(exec, "MacOS/SomeRandomFile.txt");
  51. int res3 = findBundleFile(exec, "MacOS/README.rst");
  52. if (!res1 || !res2 || !res3) {
  53. return 1;
  54. }
  55. return 0;
  56. }