File indexing completed on 2024-05-19 09:43:03
0001 /***************************************************************************** 0002 * Copyright 2013 - 2015 Yichao Yu <yyc1992@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify * 0005 * it under the terms of the GNU Lesser General Public License as * 0006 * published by the Free Software Foundation; either version 2.1 of the * 0007 * License, or (at your option) version 3, or any later version accepted * 0008 * by the membership of KDE e.V. (or its successor approved by the * 0009 * membership of KDE e.V.), which shall act as a proxy defined in * 0010 * Section 6 of version 3 of the license. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 0015 * Lesser General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU Lesser General Public * 0018 * License along with this library. If not, * 0019 * see <http://www.gnu.org/licenses/>. * 0020 *****************************************************************************/ 0021 0022 #ifndef _QTC_UTILS_DIRS_H_ 0023 #define _QTC_UTILS_DIRS_H_ 0024 0025 /** 0026 * \file dirs.h 0027 * \author Yichao Yu <yyc1992@gmail.com> 0028 * \brief Standard directories and file system related functions. 0029 */ 0030 0031 #include "utils.h" 0032 0033 #include <string> 0034 #include <map> 0035 #include <utility> 0036 0037 #include <unistd.h> 0038 #include <sys/stat.h> 0039 0040 namespace QtCurve { 0041 /** 0042 * Get QtCurve configure directory. The directory will be created if it doesn't 0043 * exist. The returned string is guaranteed to end with '/' 0044 */ 0045 const char *getConfDir(); 0046 0047 /** 0048 * Get home directory. The returned string is guaranteed to end with '/' 0049 */ 0050 const char *getHome(); 0051 0052 /** 0053 * Get XDG_DATA_HOME directory. This is usually `~/.local/share/` 0054 * The returned string is guaranteed to end with '/' 0055 */ 0056 const char *getXDGDataHome(); 0057 0058 /** 0059 * Get XDG_CONFIG_HOME directory. This is usually `~/.config/` 0060 * The returned string is guaranteed to end with '/' 0061 */ 0062 const char *getXDGConfigHome(); 0063 0064 /** 0065 * Return the absolute path of \param file with the QtCurve configure directory 0066 * as the current directory. If the optional argument \param buff is not NULL 0067 * it will be realloc'ed to hold the result. If \param path is a relative path 0068 * the QtCurve configure directory will be created. 0069 */ 0070 char *getConfFile(const char *file, char *buff=nullptr); 0071 0072 static inline std::string 0073 getConfFile(const std::string &file) 0074 { 0075 if (file[0] == '/') 0076 return file; 0077 return getConfDir() + file; 0078 } 0079 0080 static inline std::string 0081 getConfFile(std::string &&file) 0082 { 0083 if (file[0] == '/') 0084 return std::move(file); 0085 return getConfDir() + std::move(file); 0086 } 0087 0088 /** 0089 * Create directory \param path with mode \param mode. Its parent directories 0090 * will also be created as needed. 0091 */ 0092 void makePath(const char *path, int mode); 0093 0094 /** 0095 * Check whether \param path is (or is a symlink that is pointing to) 0096 * a directory with read and execute permissions. 0097 */ 0098 static inline bool 0099 isDir(const char *path) 0100 { 0101 struct stat stats; 0102 return (stat(path, &stats) == 0 && S_ISDIR(stats.st_mode) && 0103 access(path, R_OK | X_OK) == 0); 0104 } 0105 0106 /** 0107 * Check whether \param path is (or is a symlink that is pointing to) 0108 * a regular file with read permission. 0109 */ 0110 static inline bool 0111 isRegFile(const char *path) 0112 { 0113 struct stat stats; 0114 return (stat(path, &stats) == 0 && S_ISREG(stats.st_mode) && 0115 access(path, R_OK) == 0); 0116 } 0117 0118 /** 0119 * Check whether \param path is a symlink. 0120 */ 0121 static inline bool 0122 isSymLink(const char *path) 0123 { 0124 struct stat stats; 0125 return lstat(path, &stats) == 0 && S_ISLNK(stats.st_mode); 0126 } 0127 0128 std::map<std::string, std::string> getPresets(); 0129 0130 } 0131 0132 #endif