From 5a1eca115fb50cba752baf539db85cf08b89abcd Mon Sep 17 00:00:00 2001 From: Mark Kim Date: Tue, 23 Mar 2021 23:48:53 -0400 Subject: [PATCH] save/open support in ios --- src/ios.c | 29 ++++++++++++++++++++++++++--- src/macos.c | 20 ++++++++++---------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/ios.c b/src/ios.c index d6e6249fe..d565ac875 100644 --- a/src/ios.c +++ b/src/ios.c @@ -19,9 +19,10 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA (See COPYING.txt) */ -#include -#include #include +#include +#include +#include #include "ios.h" #define IOS_FONTS_PATH "../Library/Fonts" @@ -29,6 +30,19 @@ #define IOS_PICTURES_PATH "../Documents" +/* Recursively mkdir */ +static int _mkdir_r(const char *path) +{ + const char parent[PATH_MAX]; + + if(!dirname_r(path, parent)) return 1; /* parent = dirname(path) */ + if(strcmp(parent, ".") == 0) return 0; /* if(parent == ".") return */ + + _mkdir_r(parent); /* mkdir_r(parent) */ + return mkdir(path, 0777); /* mkdir(path) */ +} + + const char *apple_fontsPath(void) { return IOS_FONTS_PATH; @@ -37,13 +51,22 @@ const char *apple_fontsPath(void) const char *apple_preferencesPath(void) { + static int init = 0; + + /* Ensure the preferences path exists */ + if(!init) { + _mkdir_r(IOS_PREFERENCES_PATH); + + init = 1; + } + return IOS_PREFERENCES_PATH; } const char *apple_globalPreferencesPath(void) { - return IOS_PREFERENCES_PATH; + return apple_preferencesPath(); } diff --git a/src/macos.c b/src/macos.c index 92dc5dbf5..923dc3472 100644 --- a/src/macos.c +++ b/src/macos.c @@ -36,9 +36,9 @@ const char *apple_fontsPath(void) if(!p) { const char *home = getenv("HOME"); - + p = malloc(strlen(home) + strlen(MACOS_FONTS_PATH) + 1); - + if(p) sprintf(p, MACOS_FONTS_PATH, getenv("HOME")); else perror("apple_fontsPath"); } @@ -50,16 +50,16 @@ const char *apple_fontsPath(void) const char *apple_preferencesPath(void) { static char *p = NULL; - + if(!p) { const char *home = getenv("HOME"); - + p = malloc(strlen(home) + strlen(MACOS_PREFERENCES_PATH) + 1); - + if(p) sprintf(p, MACOS_PREFERENCES_PATH, getenv("HOME")); else perror("apple_preferencesPath"); } - + return p; } @@ -73,15 +73,15 @@ const char *apple_globalPreferencesPath(void) const char *apple_picturesPath(void) { static char *p = NULL; - + if(!p) { const char *home = getenv("HOME"); - + p = malloc(strlen(home) + strlen(MACOS_PICTURES_PATH) + 1); - + if(p) sprintf(p, MACOS_PICTURES_PATH, getenv("HOME")); else perror("apple_picturesPath"); } - + return p; }