BundleLib.cxx 1.4 KB

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