libcm is a C development framework with an emphasis on audio signal processing applications.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cmFileSysOsx.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //| Copyright: (C) 2009-2020 Kevin Larke <contact AT larke DOT org>
  2. //| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. #include "cmPrefix.h"
  4. #include "cmGlobal.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmFileSys.h"
  12. #include "cmFileSysOsx.h"
  13. #include <Carbon/Carbon.h>
  14. #include <glob.h>
  15. // Convert a CFString to a C String allocated on the linked heap.
  16. cmChar_t* _cmCFStringToCString( _cmFsOsx_t* p, CFStringRef cfString )
  17. {
  18. cmChar_t* cp = NULL;
  19. CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfString);
  20. CFStringNormalize(cfMutableString,kCFStringNormalizationFormC);
  21. CFIndex n = CFStringGetLength(cfMutableString);
  22. cmChar_t buf[ n + 1];
  23. Boolean noErrFl = CFStringGetCString(cfMutableString,buf,n+1, kCFStringEncodingASCII);
  24. if( noErrFl )
  25. {
  26. cp = cmLhAllocZ(p->lhH,cmChar_t,n+1);
  27. strncpy(cp,buf,n);
  28. buf[n] = 0;
  29. }
  30. CFRelease(cfMutableString);
  31. return cp;
  32. }
  33. // based on: wxWindows/src/common/filefn.cpp:wxMacFSRefToPath()
  34. cmChar_t* _cmFSRefToPath( _cmFsOsx_t* p, const FSRef *fsRef )
  35. {
  36. CFURLRef fullURLRef;
  37. fullURLRef = CFURLCreateFromFSRef(NULL, fsRef);
  38. CFStringRef cfString = CFURLCopyFileSystemPath(fullURLRef, kCFURLPOSIXPathStyle);
  39. CFRelease( fullURLRef ) ;
  40. cmChar_t* cp = _cmCFStringToCString(p,cfString);
  41. CFRelease( cfString );
  42. return cp;
  43. //return wxMacCFStringHolder(cfMutableString).AsString();
  44. }
  45. // based on wsWindows/src/mac/carbon/utils.cpp
  46. cmChar_t* _cmFindFolder(_cmFsOsx_t* p, short vol, OSType folderType, Boolean createFolder)
  47. {
  48. FSRef fsRef;
  49. if ( FSFindFolder( vol, folderType, createFolder, &fsRef) == noErr)
  50. return _cmFSRefToPath( p, &fsRef );
  51. return NULL;
  52. }
  53. // based on wxWidgets/src/mac/corefoundation/stdPaths_cf.cpp:GetResourcesDir()
  54. cmChar_t* _cmGetBundleDir( _cmFsOsx_t* p )
  55. {
  56. // we get the Bundle Resource directory - although many other similar functions could
  57. // be called to get other standard directories.
  58. CFURLRef relativeURL = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
  59. CFURLRef absoluteURL = CFURLCopyAbsoluteURL(relativeURL);
  60. CFStringRef cfStrPath = CFURLCopyFileSystemPath(absoluteURL,kCFURLPOSIXPathStyle);
  61. CFRelease(absoluteURL);
  62. cmChar_t* cp = _cmCFStringToCString(p,cfStrPath);
  63. CFRelease(cfStrPath);
  64. return cp;
  65. }
  66. //http://developer.apple.com/library/mac/#qa/qa1549/_index.html
  67. cmChar_t* _cmCreatePathByExpandingTildePath( _cmFsOsx_t* p, const char* path )
  68. {
  69. glob_t globbuf;
  70. char **v;
  71. char *expandedPath = NULL, *result = NULL;
  72. assert(path != NULL);
  73. if (glob(path, GLOB_TILDE, NULL, &globbuf) == 0) //success
  74. {
  75. v = globbuf.gl_pathv; //list of matched pathnames
  76. expandedPath = v[0]; //number of matched pathnames, gl_pathc == 1
  77. result = cmLhAllocZ(p->lhH,cmChar_t,strlen(expandedPath) + 1);
  78. //result = (char*)calloc(1, strlen(expandedPath) + 1); //the extra char is for the null-termination
  79. if(result)
  80. strncpy(result, expandedPath, strlen(expandedPath) + 1); //copy the null-termination as well
  81. globfree(&globbuf);
  82. }
  83. return result;
  84. }
  85. cmFsRC_t _cmOsxFileSysInit( _cmFsOsx_t** pp, cmLHeapH_t lhH, cmErr_t* err )
  86. {
  87. _cmFsOsx_t* p = cmLhAllocZ(lhH,_cmFsOsx_t,1);
  88. p->err = err;
  89. p->lhH = lhH;
  90. p->prefDir = _cmFindFolder( p, (short) kUserDomain, kPreferencesFolderType, kDontCreateFolder );
  91. p->rsrcDir = _cmGetBundleDir(p);
  92. p->userDir = _cmCreatePathByExpandingTildePath(p,"~");
  93. *pp = p;
  94. return kOkFsRC;
  95. }
  96. cmFsRC_t _cmOsxFileSysFinalize( _cmFsOsx_t* p )
  97. {
  98. return kOkFsRC;
  99. }