find_root_dir_assist.mm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file find_root_dir_assist.mm
  10. * @author drose
  11. * @date 2009-04-13
  12. */
  13. #include "find_root_dir.h"
  14. #ifdef __APPLE__
  15. #include <Foundation/Foundation.h>
  16. #include <AppKit/AppKit.h>
  17. /**
  18. * Copy the Objective-C string to a C++ string.
  19. */
  20. static string
  21. NSString_to_cpp_string(NSString *str) {
  22. size_t length = [str length];
  23. string result;
  24. for (size_t i = 0; i < length; ++i) {
  25. result += (char)[str characterAtIndex: i];
  26. }
  27. return result;
  28. }
  29. /**
  30. *
  31. */
  32. static string
  33. call_NSSearchPathForDirectories(NSSearchPathDirectory dirkey, NSSearchPathDomainMask domain) {
  34. // Ensure that Carbon has been initialized, and that we have an auto-release
  35. // pool.
  36. NSApplicationLoad();
  37. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  38. NSArray *paths = NSSearchPathForDirectoriesInDomains(dirkey, domain, YES);
  39. string result;
  40. if ([paths count] != 0) {
  41. result = NSString_to_cpp_string([paths objectAtIndex:0]);
  42. }
  43. [pool release];
  44. return result;
  45. }
  46. /**
  47. *
  48. */
  49. static string
  50. get_osx_home_directory() {
  51. NSApplicationLoad();
  52. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  53. NSString *dir = NSHomeDirectory();
  54. string result = NSString_to_cpp_string(dir);
  55. [pool release];
  56. return result;
  57. }
  58. /**
  59. *
  60. */
  61. std::string
  62. find_osx_root_dir() {
  63. string result = call_NSSearchPathForDirectories(NSCachesDirectory, NSUserDomainMask);
  64. if (!result.empty()) {
  65. return result + "/Panda3D";
  66. }
  67. result = get_osx_home_directory();
  68. if (!result.empty()) {
  69. return result + "/Panda3D";
  70. }
  71. return string();
  72. }
  73. #endif // __APPLE__