7 #if defined(PLATFORM_UNIX) 11 #include <sys/types.h> 13 #elif defined(PLATFORM_WINDOWS) 22 #error Platform not supported. 34 size_t lastSlash = path.rfind(
'/');
35 size_t lastBackSlash = path.rfind(
'\\');
37 if (lastSlash == string::npos && lastBackSlash == string::npos) {
39 }
else if (lastSlash == string::npos) {
40 lastSeparator = lastBackSlash;
41 }
else if (lastBackSlash == string::npos) {
42 lastSeparator = lastSlash;
44 lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
46 return path.substr(lastSeparator + 1);
54 size_t lastSlash = path.rfind(
'/');
55 size_t lastBackSlash = path.rfind(
'\\');
57 if (lastSlash == string::npos && lastBackSlash == string::npos) {
59 }
else if (lastSlash == string::npos) {
60 lastSeparator = lastBackSlash;
61 }
else if (lastBackSlash == string::npos) {
62 lastSeparator = lastSlash;
64 lastSeparator = lastSlash > lastBackSlash ? lastSlash : lastBackSlash;
66 return path.substr(0, lastSeparator + 1);
77 static const char invalidPathChars[] = {
'\"',
'<',
'>',
'?',
'!',
'*',
'|',
'/',
':',
'\\',
'\n' };
78 for (
const char *
i = invalidPathChars, *end = invalidPathChars +
sizeof(invalidPathChars);
i != end; ++
i) {
80 while (startPos != string::npos) {
81 fileName.replace(startPos, 1,
string());
95 bool settingsDirectory(std::string &result, std::string applicationDirectoryName,
bool createApplicationDirectory)
99 fstream pathConfigFile(
"path.config", ios_base::in);
100 if (pathConfigFile.good()) {
101 for (
string line; getline(pathConfigFile, line);) {
102 string::size_type p = line.find(
'=');
103 if ((p != string::npos) && (p + 1 < line.length())) {
104 string fieldName = line.substr(0, p);
105 if (fieldName ==
"settings") {
106 result.assign(line.substr(p + 1));
111 if (!result.empty()) {
112 #if defined(PLATFORM_UNIX) 114 return (stat(result.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode));
115 #elif defined(PLATFORM_WINDOWS) 117 DWORD ftyp = GetFileAttributesA(result.c_str());
118 return (ftyp != INVALID_FILE_ATTRIBUTES) && (ftyp & FILE_ATTRIBUTE_DIRECTORY);
121 if (!applicationDirectoryName.empty()) {
124 #if defined(PLATFORM_UNIX) || defined(PLATFORM_MAC) 125 if (
char *homeDir = getenv(
"HOME")) {
128 struct passwd *pw = getpwuid(getuid());
132 result +=
"/.config";
133 if (createApplicationDirectory && !(stat(result.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))) {
134 if (mkdir(result.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
138 if (!applicationDirectoryName.empty()) {
140 result += applicationDirectoryName;
141 if (createApplicationDirectory && !(stat(result.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode))) {
142 if (mkdir(result.c_str(), S_IRUSR | S_IWUSR | S_IXUSR) != 0) {
147 #elif defined(PLATFORM_WINDOWS) 148 if (
char *appData = getenv(
"appdata")) {
150 if (!applicationDirectoryName.empty()) {
152 result += applicationDirectoryName;
153 if (createApplicationDirectory) {
155 DWORD ftyp = GetFileAttributesA(result.c_str());
156 if (ftyp == INVALID_FILE_ATTRIBUTES) {
158 }
else if (ftyp & FILE_ATTRIBUTE_DIRECTORY) {
161 if (CreateDirectory(result.c_str(), NULL) == 0) {
184 list<string> entries;
185 if (
auto dir = opendir(path)) {
186 while (
auto dirEntry = readdir(dir)) {
188 switch (dirEntry->d_type) {
190 filter = (types & DirectoryEntryType::File) != DirectoryEntryType::None;
193 filter = (types & DirectoryEntryType::Directory) != DirectoryEntryType::None;
196 filter = (types & DirectoryEntryType::Symlink) != DirectoryEntryType::None;
199 filter = (types & DirectoryEntryType::All) != DirectoryEntryType::None;
202 entries.emplace_back(dirEntry->d_name);
209 return list<string>();
CPP_UTILITIES_EXPORT std::list< std::string > directoryEntries(const char *path, DirectoryEntryType types=DirectoryEntryType::All)
Returns the names of the directory entries in the specified path with the specified types.
CPP_UTILITIES_EXPORT bool settingsDirectory(std::string &result, std::string applicationDirectoryName=std::string(), bool createApplicationDirectory=false)
Locates a directory meant to store application settings.
Contains utility classes helping to read and write streams.
CPP_UTILITIES_EXPORT std::string directory(const std::string &path)
Returns the directory of the specified path string (including trailing slash).
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path)
Returns the file name and extension of the specified path string.
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName)
Removes invalid characters from the specified fileName.
DirectoryEntryType
The DirectoryEntryType enum specifies the type of a directory entry (file, directory or symlink).