使用NSURL设置并获取kIsStationary位

时间:2015-12-25 11:43:39

标签: objective-c cocoa nsurl

我移植了一些旧的NSURL代码,以便使用NSFileManagerkIsStationery和朋友。除了在文件上设置和获取Finder FSRef位之外,一切都有效。

有没有办法在不退回已弃用printf("%d", (int)pow(2, 10)); 方法的情况下执行此操作?

1 个答案:

答案 0 :(得分:1)

所以经过一些研究后,这里是设置和获取文具位的答案:

struct FileInfoBuf
{
    u_int32_t info_length;
    union
    {
        u_int32_t padding[8];
        struct
        {
            u_int32_t type;
            u_int32_t creator;
            u_int16_t fdFlags;
            u_int16_t location;
            u_int32_t padding[4];
        }
        info;
    }
    data;
};

bool IsStationeryPad(const std::string& path)
{     
    attrlist attrList;
    FileInfoBuf fileInfo;

    attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
    attrList.reserved = 0;
    attrList.commonattr = ATTR_CMN_FNDRINFO;
    attrList.volattr = 0;
    attrList.dirattr = 0;
    attrList.fileattr = 0;
    attrList.forkattr = 0;

    if (getattrlist(path.c_str(), &attrList, &fileInfo, sizeof(fileInfo), FSOPT_NOFOLLOW) == noErr)
    {
        return (CFSwapInt16BigToHost(fileInfo.data.info.fdFlags) & kIsStationery);
    }

    return false;
}

void SetStationeryPad(const std::string& path, bool isStationery)
{
    OSErr err = noErr;

    attrlist attrList;
    FileInfoBuf fileInfo;

    attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
    attrList.reserved = 0;
    attrList.commonattr = ATTR_CMN_FNDRINFO;
    attrList.volattr = 0;
    attrList.dirattr = 0;
    attrList.fileattr = 0;
    attrList.forkattr = 0;

    err = getattrlist(path.c_str(), &attrList, &fileInfo, sizeof(fileInfo), FSOPT_NOFOLLOW);

    if (err == noErr)
    {
        fileInfo.data.info.fdFlags |= CFSwapInt16HostToBig(kIsStationery);

        setattrlist(path.c_str(), &attrList, &fileInfo.data, sizeof(fileInfo.data), FSOPT_NOFOLLOW);
    }
}

请注意,此代码中没有真正的错误检查。此外,应用程序可能不应该设置此位,这实际上是用户决定,并通过Finder控制。