查找“~/Library/Application Support”来自 C++?

发布于 2024-10-19 10:21:21 字数 176 浏览 2 评论 0原文

我已经编写了一个 GTKmm 应用程序,并且正在尝试创建一些 OS X 增强功能。我想将配置文件存储在 Application Support/myApp 文件夹中,但是,我无法找到找到该文件夹​​的正确方法。

我尝试查看 Core Foundation 库(我用它来获取 myApp.app 路径),但我找不到任何东西。

I've written a GTKmm application and I'm trying to create some OS X enhancements. I'd like to store my configuration file in the Application Support/myApp folder, however, I can't figure out the proper way to locate this folder.

I've tried looking through the Core Foundation library (that I'm using to get my myApp.app path) but I can't find anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

清眉祭 2024-10-26 10:21:21

在 C/C++ 中执行此操作的正确方法:

#include <CoreServices/CoreServices.h>

FSRef ref;
OSType folderType = kApplicationSupportFolderType;
char path[PATH_MAX];

FSFindFolder( kUserDomain, folderType, kCreateFolder, &ref );

FSRefMakePath( &ref, (UInt8*)&path, PATH_MAX );

// You now have ~/Library/Application Support stored in 'path'

当然,这些都是非常古老的 API,Apple 不再推荐使用它们。尽管如此,如果你想在你的代码库中完全避免 Objective-C,它就可以完成工作。

Proper way to do it in C/C++:

#include <CoreServices/CoreServices.h>

FSRef ref;
OSType folderType = kApplicationSupportFolderType;
char path[PATH_MAX];

FSFindFolder( kUserDomain, folderType, kCreateFolder, &ref );

FSRefMakePath( &ref, (UInt8*)&path, PATH_MAX );

// You now have ~/Library/Application Support stored in 'path'

Naturally, those are very old APIs and their use is no longer recommended by Apple. Despite that it gets the job done if you want to avoid Objective-C completely in your codebase.

烟若柳尘 2024-10-26 10:21:21

看来用于此目的的函数是 NSSearchPathForDirectoriesInDomains(或同一页面上列出的其他一些函数),以 NSApplicationSupportDirectory 作为参数。

It appears that the function to use for this is NSSearchPathForDirectoriesInDomains (or some other functions listed on the same page) with NSApplicationSupportDirectory as the argument.

家住魔仙堡 2024-10-26 10:21:21

在 BSD Unix(包含在 OS-X 中)中,您可以使用以下命令获取运行程序的用户的主目录:

struct passwd *p = getpwuid(getuid());  /* defined in pwd.h, and requires sys/types.h */ 
char *home = p->pw_dir;

使用此命令,您可以使用此命令代替 ~ 构建路径

char *my_app_name = "WHATEVER";
char app_support[MAXPATHLEN];  /* defined in sys/param.h */
snprintf(app_support,MAXPATHLEN,"%s/Library/Application Support/%s", home, my_app_name);

In BSD Unix, included in OS-X, you can get the home directory of the user running the program with this:

struct passwd *p = getpwuid(getuid());  /* defined in pwd.h, and requires sys/types.h */ 
char *home = p->pw_dir;

Using this, you can then construct the path using this in place of ~

char *my_app_name = "WHATEVER";
char app_support[MAXPATHLEN];  /* defined in sys/param.h */
snprintf(app_support,MAXPATHLEN,"%s/Library/Application Support/%s", home, my_app_name);
放低过去 2024-10-26 10:21:21

sysdir 适用于 macOS 10.12+

对于 macOS 10.12 及更高版本,Apple 提供 sysdir.h API:

#include <sysdir.h>  // for sysdir_start_search_path_enumeration
#include <glob.h>    // for glob needed to expand ~ to user dir

std::string expandTilde(const char* str) {
    if (!str) return {};

    glob_t globbuf;
    if (glob(str, GLOB_TILDE, nullptr, &globbuf) == 0) {
        std::string result(globbuf.gl_pathv[0]);
        globfree(&globbuf);
        return result;
    } else {
        throw std::exception("Unable to expand tilde");
    }
}

std::string settingsPath(const char* str) {
    char path[PATH_MAX];
    auto state = sysdir_start_search_path_enumeration(SYSDIR_DIRECTORY_APPLICATION_SUPPORT,
                                                      SYSDIR_DOMAIN_MASK_USER);
    if ((state = sysdir_get_next_search_path_enumeration(state, path))) {
        return expandTilde(path);
    } else {
        throw std::exception("Failed to get settings folder");
    }
}

sysdir 取代了现已弃用的 NSSystemDirectoriesFindFolder 方法。

sysdir for macOS 10.12+

For macOS 10.12 and later, Apple provide the sysdir.h API:

#include <sysdir.h>  // for sysdir_start_search_path_enumeration
#include <glob.h>    // for glob needed to expand ~ to user dir

std::string expandTilde(const char* str) {
    if (!str) return {};

    glob_t globbuf;
    if (glob(str, GLOB_TILDE, nullptr, &globbuf) == 0) {
        std::string result(globbuf.gl_pathv[0]);
        globfree(&globbuf);
        return result;
    } else {
        throw std::exception("Unable to expand tilde");
    }
}

std::string settingsPath(const char* str) {
    char path[PATH_MAX];
    auto state = sysdir_start_search_path_enumeration(SYSDIR_DIRECTORY_APPLICATION_SUPPORT,
                                                      SYSDIR_DOMAIN_MASK_USER);
    if ((state = sysdir_get_next_search_path_enumeration(state, path))) {
        return expandTilde(path);
    } else {
        throw std::exception("Failed to get settings folder");
    }
}

sysdir replaces the now deprecated NSSystemDirectories and FindFolder approaches.

意中人 2024-10-26 10:21:21

这不是应用程序支持,但您可能不想在那里存储文件。而是使用通过调用“HOME”获得的目录:

您可以使用 C 函数 getenv:char *home = getenv("HOME");

并使用以下命令获取 C++ 字符串:字符串(主页)

This is not application support but you probably don't want to store files there anyways. Instead use the directory that you get from calling "HOME":

You can use the C-function getenv: char *home = getenv("HOME");

and to get the C++ string use: string(home)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文