diff options
Diffstat (limited to 'include/utils.h')
-rw-r--r-- | include/utils.h | 302 |
1 files changed, 43 insertions, 259 deletions
diff --git a/include/utils.h b/include/utils.h index 60bee08..c541741 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,6 +1,7 @@ -// Copyright (C) 2022 The Qt Company Ltd. -// - +/* Copyright (C) 2022 The Qt Company Ltd. + * + * SPDX-License-Identifier: GPL-3.0-only WITH Qt-GPL-exception-1.0 +*/ #pragma once #include <sys/stat.h> @@ -10,253 +11,61 @@ #include <sstream> #include <vector> #include <algorithm> -#if __linux__ - // TODO write some code in the utils to get these - #include <unistd.h> - #define USER_SETTINGS_DIR ".local/share/Qt/" - #include <pwd.h> - #include <grp.h> - #define DIR_SEPARATOR '/' -#elif __APPLE__ || __MACH__ +#if _WIN32 + // Windows + #define WIN32_LEAN_AND_MEAN + #include <windows.h> + #include <stdint.h> // portable: uint64_t MSVC: __int64 + #include <io.h> + #include "../3rdparty/dirent/dirent.h" + #define USER_SETTINGS_DIR "AppData\\Roaming\\Qt"// Windows + #define DIR_SEPARATOR '\\' + #define SYSTEM_ROOT "C:" +#else + // Linux or Mac #include <unistd.h> - #define USER_SETTINGS_DIR "~/TODO/something/else" + #include <dirent.h> #include <pwd.h> #include <grp.h> #define DIR_SEPARATOR '/' -#else - // Windows - #define USER_SETTINGS_DIR "~/TODO/something/else"// Windows - #include <windows.h> - #define DIR_SEPARATOR '\\' + #define SYSTEM_ROOT "/" +#endif +#if __APPLE__ || __MACH__ + #define USER_SETTINGS_DIR "/something/else" // not supported yet +#elif __linux__ + #define USER_SETTINGS_DIR ".local/share/Qt/" #endif - namespace utils { - -static inline void trimLeft(std::string &s); -static inline void trimRight(std::string &s); -static std::string trimStr(const std::string &s); -static std::vector<std::string> splitStr(const std::string &str, const char delimiter=' '); -static std::string strToLower(const std::string &str); -static std::string strToUpper(const std::string &str); -static std::string getUserHomeDir(); -static int writeToFile(const std::string &filepath, const std::string &data, bool append=false); -static int createDir(const std::string &filepath, uint16_t user=0, uint16_t group=0); -static int readFile(std::string &str, const std::string &filepath); -inline bool fileExists (const std::string& name); -static std::string getFileOwnerName(const std::string &filename); -static int getFileOwner(const std::string &filename, uint16_t &owner, uint16_t &group); -static int strToInt(const std::string &str); -template <typename T> T swapEndian(T val); -static std::string getOsName(); - /* * String tools */ - -// Trim whitespaces from start of a string -static inline void trimLeft(std::string &s) -{ - s.erase(s.begin(), std::find_if (s.begin(), s.end(), [](unsigned char ch) - { return !std::isspace(ch); })); -} - -// Trim whitespaces from end of a string -static inline void trimRight(std::string &s) -{ - s.erase(std::find_if (s.rbegin(), s.rend(), [](unsigned char ch) - { return !std::isspace(ch); }) - .base(), - s.end()); -} - -// Trim whitespaces from both ends of a string -static std::string trimStr(const std::string &s) { - std::string trimmed = s; - trimLeft(trimmed); - trimRight(trimmed); - - return trimmed; -} - -// Split a string by a char (Defaults to a space) -static std::vector<std::string> splitStr(const std::string &str, const char delimiter) { - std::vector<std::string> strings; - std::istringstream ss(str); - std::string string; - while (getline(ss, string, delimiter)) { - string = trimStr(string); - strings.push_back(string); - } - return strings; -} - -// Convert string to lower case -static std::string strToLower(const std::string &str) { - std::locale loc; - std::stringstream ss; - for (auto c : str) - ss << std::tolower(c, loc); - return ss.str(); -} - -// Convert string to upper case -static std::string strToUpper(const std::string &str) { - std::locale loc; - std::stringstream ss; - for (auto c : str) - ss << std::toupper(c, loc); - - return ss.str(); -} - +void trimLeft(std::string &s); +void trimRight(std::string &s); +std::string trimStr(const std::string &s); +std::vector<std::string> splitStr(const std::string &str, const char delimiter=' '); +std::string strToLower(const std::string &str); +std::string strToUpper(const std::string &str); +int strToInt(const std::string &str); /* * Filesystem tools */ - -static std::string getUserHomeDir() { - // Try to find current user's home: - std::string homedir; -#if __linux__ || __APPLE__ || __MACH__ - // Linux and Mac - homedir = getenv("HOME"); - if (homedir.length() == 0) { - homedir = getpwuid(getuid())->pw_dir; - } -#else - homedir = getenv("HOMEPATH"); -#endif - return homedir; -} - -// Write data to the file -static int writeToFile(const std::string &filepath, const std::string &data, bool append) { - std::string path = ""; - std::ofstream file; - // can't enable exception now because of gcc bug that raises ios_base::failure with useless message - // file.exceptions(file.exceptions() | std::ios::failbit); - if (append) { - file.open(filepath, std::ios::out | std::ios::app); - } - else { - file.open(filepath, std::ios::out); - } - - if (file.fail()) { - return -1; - } - file << data << std::endl; - file.close(); - return 0; -} - -static int createDir(const std::string &filepath, uint16_t user, uint16_t group) { - const char delimiter = '/'; // TODO:: for Linux, windows have '\' - std::vector<std::string> pathVector = utils::splitStr(filepath, delimiter); - std::string path = ""; - for (std::string dir : pathVector) { - path += delimiter; // Again, this is for Linux, windows must have "C:" - path += dir; - if (!utils::fileExists(path)) { -#if __linux__ || __APPLE__ || __MACH__ - if (mkdir(path.c_str(), 0777) != 0) { - return -1; - } - chown(path.c_str(), user, group); -#else - printf("WARNING: Unimplemented: createDir()\n"); -#endif - } - } - return 0; -} - -static int readFile(std::string &str, const std::string &filepath) { - std::ifstream file; - std::stringstream ss; - try { - file.open(filepath, std::ios::in); - if (file.is_open()) - { - std::string line; - while (getline(file, line)) - { - ss << line; - } - file.close(); - } - else { - return -1; - } - } - catch (...) { - return -1; - } - str = ss.str(); - return 0; -} - -inline bool fileExists (const std::string& name) { -#if __linux__ || __APPLE__ || __MACH__ - struct stat buffer; - return (stat (name.c_str(), &buffer) == 0); -#else - printf("WARNING! Uniplemented: Win version of fileExiss()\n"); - return true; -#endif -} - - -static std::string getFileOwnerName(const std::string &filename) { - std::string retVal; -#if __linux__ || __APPLE__ || __MACH__ - struct stat info; - stat(filename.c_str(), &info); // Error check omitted - struct passwd *pw = getpwuid(info.st_uid); - //struct group *gr = getgrgid(info.st_gid); - if (pw != 0) retVal = pw->pw_name; // contains the user name - //if (gr != 0) group = gr->gr_name; // contains the group name - -#else - printf("WARNING! Uniplemented: Win version of getFileOwnerName()\n"); - retVal = "NULL"; -#endif - return retVal; -} - -static int getFileOwner(const std::string &filename, uint16_t &owner, uint16_t &group) { -#if __linux__ || __APPLE__ || __MACH__ - struct stat info; - stat(filename.c_str(), &info); // Error check omitted - std::string name = getFileOwnerName(filename); - owner = info.st_uid; - group = info.st_gid; -#else - printf("WARNING! Uniplemented: Win version of getFileOwner()\n"); -#endif - //printf("Owner of %s is: %s (uid=%u, gid=%u)\n", filename.c_str(), name.c_str(), owner, group); - return 0; -} - - +std::string getUserHomeDir(); +int writeToFile(const std::string &filepath, const std::string &data, bool append=false); +int createDir(const std::string &filepath, uint16_t user=0, uint16_t group=0); +int readFile(std::string &str, const std::string &filepath); +bool fileExists(const std::string &name); +std::string getFileOwnerName(const std::string &filename); +int getFileOwner(const std::string &filename, uint16_t &owner, uint16_t &group); +std::vector<std::string> getDirListing(const std::string &directory, const std::string &filter = ""); /* -* Miscellaneous tools +* Other */ -// String to int: Because stoi() causes crash upon failing -static int strToInt(const std::string &str) { - int retVal = 0; - try { - retVal = stoi(str); - } - catch (...) { - printf("ERROR: Unable to convert %s to an int\n", str.c_str()); - } - return retVal; -} - +std::string getOsName(); // Swap endian of any data -template <typename T> T swapEndian(T val) { +template <typename T> T swapEndian(T val) +{ T retVal; char *pVal = (char*) &val; char *pRetVal = (char*)&retVal; @@ -266,34 +75,9 @@ template <typename T> T swapEndian(T val) { } return retVal; } - -static std::string getOsName() { - #if _WIN64 || _WIN32 - return "Windows"; - #elif __APPLE__ || __MACH__ - return "macOS"; - #elif __linux__ - return "Linux"; - #elif __FreeBSD__ - return "FreeBSD"; - #elif __unix || __unix__ - return "Unix"; - #else - return "Unknown OS"; -#endif -} - /* -* App-specific utils here (not generic) +* App-specific utils */ -static std::string getQtAppDataLocation() { - std::string retVal = getUserHomeDir(); - retVal += DIR_SEPARATOR; - retVal += USER_SETTINGS_DIR; - retVal += DIR_SEPARATOR; - return retVal; -} - } // namespace utils |