libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmFileSysOsx.c 3.5KB

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