Mercurial > hg > th-libs
changeset 187:acee96f864f8
Merged.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 10 Feb 2016 16:34:58 +0200 |
parents | 1ee46a95aa8c (current diff) eabe89702d9d (diff) |
children | d46bbb0d9666 |
files | |
diffstat | 4 files changed, 197 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgsigs Wed Feb 10 16:34:44 2016 +0200 +++ b/.hgsigs Wed Feb 10 16:34:58 2016 +0200 @@ -1,2 +1,3 @@ 50006067bcd1920c0f8165bfce42c04a19dace1c 0 iEYEABECAAYFAlK0wq4ACgkQEKYophN0wRIAOwCfZa2YtalIOhYkSVOOlAxAOjlasLIAnietBSXlakVu+NA1j55GWbhq9DZf 60b0f85d6e1aec365ea9010888672ac6318ba9e9 0 iQIcBAABCgAGBQJSzxekAAoJELcF5zUwe6rjTHEQAKlB6VylOZaybMFlpNY81Bu1QqHjRpiS8DMTdl+ZNqSRN9jWarY9MBcA+gqrXs2ItI3XhqeRysGIbWEe+h6371+Pr2v9tB75OXJRcpdCAX4VgkJpcxlXHJsyiY14U0L62fGnR84ReEVnj3J5vcd8Ck9fm94alYe5Gt5hPsnKK/PmhIX2kshaY4J1SA4Mw8kPg0nvu+OvVaI13/dodRhbSd8apuQvYGBkX9CikKeqin9DkAk/rONdp0msXQU/7NqbQH/57YCnF6/kIC0ubloppBbs9JeHlArXtrwpPpnS6Pjh3WFgmM7kfVtCq6YicH5+QnrtclKfAx5CTw3ll0DFKkab1HGWOkvhXmvTowb/Q9m902rmvMvJyjB7Ic4CZNz5JXHL/IyO3+zQFMkvjvYhVC3wPFu+hhc/1UelaRsm1z/qdF+VSRpCJEqPHQzDIWniRBwuACkshSsxJy6FP5UjGklZO4MqptclytrjJCJSMpR0zpmd2rp1zH13WsGK2O1Zwh1hdri5Lc+7ikZq1EZ/5E9lelfAnfJjIENUH3RO62gTk+6O/10IuK80b873/4/LSlgm0Vv9Hh1X+pGQN4uSzX2K06hnvgL0qZ5zuoFEqtfh9qbSRqsq+cjs8DWb7GsZElLLGDj+Ch5B4fvNDiErbDs3i/z5WiEpJSKewT9pWbcH +b256db93cf2549c1ad141144c681c77ccdc8ec4d 0 iQIcBAABCgAGBQJWu0gcAAoJELcF5zUwe6rjH5UP/355Ilg/qxY3v2Kk02oZqU9yA8IUYuvnqv8yEMKXb582GHD5/ay4kUcpqUOQQC8iSVWaeeGamRSI2mVP9cYa5xmUjzc/TyMjhHy59axbEZY+bydJQ+VSuVM8NlzQZxU6UbOCVTk/uCA0DNIge2moPXFvkx1eU3hFK5jsAlreR/MutdBnMydhnajuL9YzDRXJ4aIC00OG85Zv3PvP8UMnZKsqOoA+pLSiYbIm4tLeV+RevBfM1EkybknVEcOdq//a0hW642NBCnt+uI9piJkk0llEuF2vZMzvcgdwI6paSzXsoTQtIq3pxUDrlWjk1xCsaCVI0zxT6sbNgKqJyQMYZsPmCokqxdU+Q9nbdWOroyTB98AhX655iJGHwmR2uj9WHi9FwT1KVLI57nlMslE6XuBoIDH+az26JDBQWUmKkzXds/1EOPciC4J+ERj4huC6Yix92JEdsbC6Rr4DCX+3poa46emyp7t84RhTVfpVhpmFSNLF5l498dd3uCbrWRnqaHkEBJuDYMASOvJkCb7VXuFVHvl5yLOcF+j4jGPscNMtceUr3UQ2MMnl+Ghu3CKNJU93VdI4NnIgox0ECulaekTCSrLxzI5UBwOFYe57MI+aIRNEZe2gXKT/Bg6n4v2v4w3Tr4h7abup8yw5EZQWI8kEW+ObFQPxMiKU2+ZKAvpM
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/th_file.c Wed Feb 10 16:34:58 2016 +0200 @@ -0,0 +1,143 @@ +/* + * File, directory etc helper functions + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2016 Tecnic Software productions (TNSP) + * + * Please read file 'COPYING' for information on license and distribution. + */ +#include "th_file.h" +#include "th_string.h" +#include <unistd.h> +//#include <fcntl.h> +#ifdef TH_PLAT_WINDOWS +# include <shlwapi.h> +# include <shfolder.h> +#else +//# include <sys/wait.h> +# include <sys/stat.h> +# include <sys/types.h> +#endif + + +char * th_get_home_dir() +{ +#if defined(TH_PLAT_WINDOWS) + char tmpPath[MAX_PATH]; + if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) + return th_strdup(tmpPath); +#endif + return th_strdup(getenv("HOME")); +} + + +char * th_get_data_dir() +{ +#if defined(TH_PLAT_WINDOWS) + char tmpPath[MAX_PATH]; + if (SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, tmpPath) == S_OK) + return th_strdup(tmpPath); +#endif + return th_strdup(getenv("HOME")); +} + + +char * th_get_config_dir() +{ +#if defined(TH_PLAT_WINDOWS) + // For Windows, we just use the appdata directory + return th_get_data_dir(); +#elif defined(USE_XDG) + const char *xdgConfigDir = getenv("XDG_CONFIG_HOME"); + + // If XDG is enabled, try the environment variable first + if (xdgConfigDir != NULL && strcmp(xdgConfigDir, "")) + return th_strdup_printf("%s" TH_DIR_SEPARATOR "%s" TH_DIR_SEPARATOR, xdgConfigDir, SET_XDG_CONFIG_DIR); + else + // Nope, try the obvious alternative + return th_strdup_printf("%s" TH_DIR_SEPARATOR ".config" TH_DIR_SEPARATOR "%s" TH_DIR_SEPARATOR, + setHomeDir, SET_XDG_CONFIG_DIR); +#else + // XDG not enabled + return th_get_data_dir(); +#endif +} + + +BOOL th_stat_path(const char *path, int *flags) +{ + *flags = 0; + +#ifdef TH_PLAT_WINDOWS + DWORD attr = GetFileAttributes(path); + + *flags |= (attr & FILE_ATTRIBUTE_DIRECTORY) ? TH_IS_DIR : 0; + *flags |= (attr & FILE_ATTRIBUTE_READONLY) ? 0 : TH_IS_WRITABLE; + *flags |= TH_IS_READABLE; +#else + uid_t id = geteuid(); + struct stat sb; + if (stat(path, &sb) < 0) + return FALSE; + + *flags |= S_ISDIR(sb.st_mode) ? TH_IS_DIR : 0; + *flags |= (id == sb.st_uid && (sb.st_mode & S_IWUSR)) ? TH_IS_WRITABLE : 0; + *flags |= (id == sb.st_uid && (sb.st_mode & S_IRUSR)) ? TH_IS_READABLE : 0; +#endif + + return TRUE; +} + + +BOOL th_mkdir_path(const char *cpath, int mode) +{ + char save, *path = th_strdup(cpath); + size_t start = 0, end; + BOOL res = FALSE; + + // If mode is 0, default to something sensible + if (mode == 0) + mode = 0711; + + // Start creating the directory stucture + do + { + // Split foremost path element out + for (save = 0, end = start; path[end] != 0; end++) + if (path[end] == TH_DIR_SEPARATOR) + { + save = path[end]; + path[end] = 0; + break; + } + + // If the element is there, create it + if (path[start] != 0) + { + int flags; + BOOL exists = th_stat_path(path, &flags); + if (exists && (flags & TH_IS_DIR) == 0) + goto error; + + if (!exists) + { +#ifdef TH_PLAT_WINDOWS + if (!CreateDirectory(path, NULL)) + goto error; +#else + if (mkdir(path, mode) < 0) + goto error; +#endif + } + } + + // Restore separator character and jump to next element + path[end] = save; + start = end + 1; + } while (save != 0); + + res = TRUE; + +error: + th_free(path); + return res; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/th_file.h Wed Feb 10 16:34:58 2016 +0200 @@ -0,0 +1,47 @@ +/* + * File, directory etc helper functions + * Programmed and designed by Matti 'ccr' Hamalainen + * (C) Copyright 2016 Tecnic Software productions (TNSP) + * + * Please read file 'COPYING' for information on license and distribution. + */ +#ifndef TH_FILE_H +#define TH_FILE_H + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "th_util.h" + + +// Platform specific defines +#if defined(TH_PLAT_WINDOWS) +# define TH_DIR_SEPARATOR '\\' +#else +# define TH_DIR_SEPARATOR '/' +#endif + + +// Flags for th_stat_path() +enum +{ + TH_IS_DIR = 0x1000, + TH_IS_SYMLINK = 0x2000, + + TH_IS_WRITABLE = 0x0002, + TH_IS_READABLE = 0x0004, +}; + + +char * th_get_home_dir(); +char * th_get_config_dir(); + +BOOL th_stat_path(const char *path, int *flags); +BOOL th_mkdir_path(const char *cpath, int mode); + + +#ifdef __cplusplus +} +#endif +#endif // TH_FILE_H
--- a/th_util.h Wed Feb 10 16:34:44 2016 +0200 +++ b/th_util.h Wed Feb 10 16:34:58 2016 +0200 @@ -12,6 +12,12 @@ #include "config.h" #endif +#if defined(__WIN64) || defined(_WIN64) || defined(__WIN32) || defined(_WIN32) +# define TH_PLAT_WINDOWS 1 +#else +# define TH_PLAT_UNIX 1 +#endif + #include "th_types.h" #include <stdio.h> #include <stdarg.h>