使用C+访问OS X上的标准目录+; < >从C++中,对OSX标准目录访问的标准化方法是什么?比如 ~>库/应用支持> /COD>或 ~/库/首选项< /C> > /P>
我看到有人提到过它的使用,但也有人提到它已被弃用,我很难找到允许我做的不仅仅是粘贴代码的文档使用C+访问OS X上的标准目录+; < >从C++中,对OSX标准目录访问的标准化方法是什么?比如 ~>库/应用支持> /COD>或 ~/库/首选项< /C> > /P>,c++,macos,C++,Macos,我看到有人提到过它的使用,但也有人提到它已被弃用,我很难找到允许我做的不仅仅是粘贴代码的文档 我发现提到使用目标C++,但是关于这个选项的大部分信息都围绕着调用来自目标C的C++代码,而且,苹果的文档似乎相当稀疏。或者至少我还没有成功地找到它。看看下面的技术问答,它解决了在您不想引入NS名称空间的环境中进行tilde扩展的问题: 我没有遇到类似于NSString的expandTildeAtPath的内置CoreFoundation调用。至于您提到的CoreServices示例,其中包括FsRe
我发现提到使用目标C++,但是关于这个选项的大部分信息都围绕着调用来自目标C的C++代码,而且,苹果的文档似乎相当稀疏。或者至少我还没有成功地找到它。
看看下面的技术问答,它解决了在您不想引入NS名称空间的环境中进行tilde扩展的问题:我没有遇到类似于NSString的expandTildeAtPath的内置CoreFoundation调用。至于您提到的CoreServices示例,其中包括FsRefs。这些是旧的Carbon调用和用于处理系统文件的对象。那些已经被弃用了,而不是CoreFoundation。好吧,有一个鲜为人知的
NSSystemDirectories.h
(位于/usr/include/NSSystemDirectories.h
)头,存在于每个版本的OS X上,我一直检查到OS X 10.3。它是libc的一部分:
提供了一个C API,虽然创建ObjuleC++封装器并不太困难:创建一个纯C++接口,在实现中,使用Objvi-C.< FolderManager.h:
#include <stdio.h>
namespace fm {
enum {
NSApplicationDirectory = 1,
NSDemoApplicationDirectory,
NSDeveloperApplicationDirectory,
NSAdminApplicationDirectory,
NSLibraryDirectory,
NSDeveloperDirectory,
NSUserDirectory,
NSDocumentationDirectory,
NSDocumentDirectory,
NSCoreServiceDirectory,
NSAutosavedInformationDirectory = 11,
NSDesktopDirectory = 12,
NSCachesDirectory = 13,
NSApplicationSupportDirectory = 14,
NSDownloadsDirectory = 15,
NSInputMethodsDirectory = 16,
NSMoviesDirectory = 17,
NSMusicDirectory = 18,
NSPicturesDirectory = 19,
NSPrinterDescriptionDirectory = 20,
NSSharedPublicDirectory = 21,
NSPreferencePanesDirectory = 22,
NSApplicationScriptsDirectory = 23,
NSItemReplacementDirectory = 99,
NSAllApplicationsDirectory = 100,
NSAllLibrariesDirectory = 101,
NSTrashDirectory = 102
};
typedef unsigned long SearchPathDirectory;
enum {
NSUserDomainMask = 1, // user's home directory --- place to install user's personal items (~)
NSLocalDomainMask = 2, // local to the current machine --- place to install items available to everyone on this machine (/Library)
NSNetworkDomainMask = 4, // publically available location in the local area network --- place to install items available on the network (/Network)
NSSystemDomainMask = 8, // provided by Apple, unmodifiable (/System)
NSAllDomainsMask = 0x0ffff // all domains: all of the above and future items
};
typedef unsigned long SearchPathDomainMask;
class FolderManager {
public:
FolderManager();
~FolderManager();
const char *pathForDirectory(SearchPathDirectory directory, SearchPathDomainMask domainMask);
const char *pathForDirectoryAppropriateForItemAtPath(SearchPathDirectory directory, SearchPathDomainMask domainMask, const char *itemPath, bool create = false);
private:
void *m_autoreleasePool;
};
};
当我在我的机器上运行它时,向它传递一个参数/Volumes/Untitled3/folder/testFile.chm
,它将输出以下内容:
folderPath == /Users/mdouma46/Library/Application Support
folderPath == /Library/Application Support
folderPath == /Users/mdouma46/.Trash
folderPath == /Volumes/Untitled 3/.Trashes/501
folderPath == /Volumes/Untitled 3/.TemporaryItems/folders.501/TemporaryItems/(A Document Being Saved By findFolder)
**关于关于NSAutoreleasePool
重要性的内存管理评论:我在OS X Yosemite,Xcode 7.2.1中测试了它,通过评论NSAutoreleasePool
的创建,在我的一生中,我无法将任何错误记录到控制台,所以我不确定发生了什么
我所期望的是在OS X 10.6的Xcode 3.2.6中编译它时得到的结果:
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x10010c6d0 of class NSCFArray autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x10010c820 of class NSCFString autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111540 of class NSCFString autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111710 of class NSPathStore2 autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111770 of class NSPathStore2 autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111a20 of class NSCFArray autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111a60 of class NSCFArray autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111d10 of class NSCFString autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111ac0 of class NSURL autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111c20 of class NSCFArray autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111de0 of class NSCFString autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111cb0 of class NSConcreteData autoreleased with no pool in place - just leaking
folderPath == /Users/mdouma46/Library/Application Support
这是因为许多Objective-C调用返回自动删除的对象:
NSFileManager
的URLsForDirectory:inDomain:
返回一个由NSURL
组成的自动删除数组。调用NSURL
的path
方法返回(创建)自动删除的NSString
s(实际上,是NSPathStore2
私有子类)等…感谢您的响应,虽然这样做有效,但它的不雅让我感到厌烦。我决定去Objic C++,这几乎允许我使用这个C++ API,同时尽可能地远离C++。虽然我仍然不确定是否<代码> [NSFLIMEMAGER URLsFrordCudio:IDECOMANE] 或使用<代码> NSURL是最好的(我现在使用<代码> NSFLIMEMANER ),至少它是用简单的代码而不是核心的基础工作。我不知道这是否是另一个问题,但是,当您在类实例上调用“pathForDirectory”时,为什么自动释放池会隐式地用于任何分配?从10.12开始:“此API已被sysdir.h中的API完全替换。”
folderPath == /Users/mdouma46/Library/Application Support
folderPath == /Library/Application Support
folderPath == /Users/mdouma46/.Trash
folderPath == /Volumes/Untitled 3/.Trashes/501
folderPath == /Volumes/Untitled 3/.TemporaryItems/folders.501/TemporaryItems/(A Document Being Saved By findFolder)
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x10010c6d0 of class NSCFArray autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x10010c820 of class NSCFString autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111540 of class NSCFString autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111710 of class NSPathStore2 autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111770 of class NSPathStore2 autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111a20 of class NSCFArray autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111a60 of class NSCFArray autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111d10 of class NSCFString autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111ac0 of class NSURL autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111c20 of class NSCFArray autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111de0 of class NSCFString autoreleased with no pool in place - just leaking
findFolder[300] *** __NSAutoreleaseNoPool(): Object 0x100111cb0 of class NSConcreteData autoreleased with no pool in place - just leaking
folderPath == /Users/mdouma46/Library/Application Support