无法读取 FSRef 中包含特殊字符的路径
使用此代码,我尝试从 fsRefAEDesc 获取 const char *pathPtr 中的路径。如果 fsRefAEDesc 中的文件名中没有特殊字符,它会给出正确的名称和路径。现在,如果路径有一些特殊字符 /Users/XYZ/.rtf 我无法从 AEGetDescData() 获得正确的 fsRef。我相信这与编码有关并尝试了一些但可以使其工作。
FSRef fsRef;
//AEDesc fsRefAEDesc; //comes from some where.
status = AEGetDescData( &fsRefAEDesc, (void*)(&fsRef), sizeof(FSRef));
//OSErr result = FSMakeFSRefUnicode(&fsRef, 1024, (UniCharPtr)(&fsRef), kTextEncodingUnknown, &fileRef);
AEDisposeDesc( &fsRefAEDesc );
CFURLRef* gotURLRef = CFURLCreateFromFSRef(NULL, &fsRef);
CFStringRef macPath = CFURLCopyFileSystemPath(gotURLRef, kCFURLPOSIXPathStyle);
const char *pathPtr = CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
有什么方法可以读取这样的路径吗?
With this code I am trying to get the path in const char *pathPtr from fsRefAEDesc. It gives the correct name and path if there are no special characters in the name of file which is there in fsRefAEDesc. Now if path has some special characters /Users/XYZ/.rtf I don't get a correct fsRef from AEGetDescData(). I believe it has some thing to do with Encoding and tried some them but could make it work.
FSRef fsRef;
//AEDesc fsRefAEDesc; //comes from some where.
status = AEGetDescData( &fsRefAEDesc, (void*)(&fsRef), sizeof(FSRef));
//OSErr result = FSMakeFSRefUnicode(&fsRef, 1024, (UniCharPtr)(&fsRef), kTextEncodingUnknown, &fileRef);
AEDisposeDesc( &fsRefAEDesc );
CFURLRef* gotURLRef = CFURLCreateFromFSRef(NULL, &fsRef);
CFStringRef macPath = CFURLCopyFileSystemPath(gotURLRef, kCFURLPOSIXPathStyle);
const char *pathPtr = CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
Is there is any way to read such paths?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中什么时候出现问题?例如,如果插入
CFShow(macPath)
,您在调试日志中看到正确的路径吗?如果是这样,那么您没有将正确的编码传递给CFStringGetCStringPtr
。使用 UTF-8。At what point in your code does the problem occur? For instance, if you insert
CFShow(macPath)
, do you see the right path in the debug log? If so, then you are not passing the right encoding toCFStringGetCStringPtr
. Use UTF-8.Unicode 转义序列是您在执行 CFURL 调用时得到的结果。网址的字符范围非常有限。
您可以尝试
FSRefMakePath
。它将从 FSRef 获取 UTF8 编码路径。The Unicode escape sequence is what you get when going through CFURL calls. URL has very limited character range.
You can try
FSRefMakePath
. It will get you UTF8 encoded path from a FSRef.