Clean io/path.h

This commit is contained in:
Martchus 2017-01-14 00:33:02 +01:00
parent c4ed080b34
commit 8d95b4497f
2 changed files with 21 additions and 28 deletions

View File

@ -1,31 +1,28 @@
#include "./path.h" #include "./path.h"
#include "../conversion/widen.h"
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <cstdlib> #include <cstdlib>
#ifdef PLATFORM_UNIX #if defined(PLATFORM_UNIX)
# include <unistd.h> # include <unistd.h>
# include <sys/types.h> # include <sys/types.h>
# include <sys/stat.h> # include <sys/stat.h>
# include <pwd.h> # include <pwd.h>
# include <dirent.h> # include <dirent.h>
#else #elif defined(PLATFORM_WINDOWS)
# ifdef PLATFORM_WINDOWS # ifdef UNICODE
# ifdef UNICODE # undef UNICODE
# undef UNICODE
# endif
# ifdef _UNICODE
# undef _UNICODE
# endif
# include <windows.h>
# endif # endif
# ifdef _UNICODE
# undef _UNICODE
# endif
# include <windows.h>
#else
# error Platform not supported.
#endif #endif
using namespace std; using namespace std;
using namespace ConversionUtilities;
namespace IoUtilities { namespace IoUtilities {
@ -92,11 +89,13 @@ void removeInvalidChars(std::string &fileName)
* \param result Specifies a string to store the path in. * \param result Specifies a string to store the path in.
* \param applicationDirectoryName Specifies the name for the application subdirectory. * \param applicationDirectoryName Specifies the name for the application subdirectory.
* \param createApplicationDirectory Indicates wheter the application subdirectory should be created if not present. * \param createApplicationDirectory Indicates wheter the application subdirectory should be created if not present.
* \returns Returns if a settings directory could be located. * \returns Returns whether a settings directory could be located.
* \deprecated This function has FIXMEs. Since it is not used actually also a good candidate for being removed.
*/ */
bool settingsDirectory(std::string &result, std::string applicationDirectoryName, bool createApplicationDirectory) bool settingsDirectory(std::string &result, std::string applicationDirectoryName, bool createApplicationDirectory)
{ {
result.clear(); result.clear();
// FIXME: this kind of configuration is not actually used so get rid of it, maybe just read env variable instead
fstream pathConfigFile("path.config", ios_base::in); fstream pathConfigFile("path.config", ios_base::in);
if(pathConfigFile.good()) { if(pathConfigFile.good()) {
for(string line; getline(pathConfigFile, line); ) { for(string line; getline(pathConfigFile, line); ) {
@ -110,22 +109,19 @@ bool settingsDirectory(std::string &result, std::string applicationDirectoryName
} }
} }
if(!result.empty()) { if(!result.empty()) {
#ifdef PLATFORM_UNIX #if defined(PLATFORM_UNIX)
struct stat sb; struct stat sb;
return (stat(result.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)); return (stat(result.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode));
#else #else // PLATFORM_WINDOWS
# ifdef PLATFORM_WINDOWS // FIXME: use UTF-16 API to support unicode, or rewrite using fs abstraction lib
DWORD ftyp = GetFileAttributesA(result.c_str()); DWORD ftyp = GetFileAttributesA(result.c_str());
return (ftyp != INVALID_FILE_ATTRIBUTES) && (ftyp & FILE_ATTRIBUTE_DIRECTORY); return (ftyp != INVALID_FILE_ATTRIBUTES) && (ftyp & FILE_ATTRIBUTE_DIRECTORY);
# else
# error Platform not supported.
# endif
#endif #endif
} else { } else {
if(!applicationDirectoryName.empty()) { if(!applicationDirectoryName.empty()) {
removeInvalidChars(applicationDirectoryName); removeInvalidChars(applicationDirectoryName);
} }
#ifdef PLATFORM_UNIX #if defined(PLATFORM_UNIX) || defined(PLATFORM_MAC)
if(char *homeDir = getenv("HOME")) { if(char *homeDir = getenv("HOME")) {
result = string(homeDir); result = string(homeDir);
} else { } else {
@ -147,13 +143,13 @@ bool settingsDirectory(std::string &result, std::string applicationDirectoryName
} }
} }
} }
#else #else // PLATFORM_WINDOWS
# ifdef PLATFORM_WINDOWS
if(char *appData = getenv("appdata")) { if(char *appData = getenv("appdata")) {
result = appData; result = appData;
if(!applicationDirectoryName.empty()) { if(!applicationDirectoryName.empty()) {
result += "\\" + applicationDirectoryName; result += "\\" + applicationDirectoryName;
if(createApplicationDirectory) { if(createApplicationDirectory) {
// FIXME: use UTF-16 API to support unicode, or rewrite using fs abstraction lib
DWORD ftyp = GetFileAttributesA(result.c_str()); DWORD ftyp = GetFileAttributesA(result.c_str());
if(ftyp == INVALID_FILE_ATTRIBUTES) { if(ftyp == INVALID_FILE_ATTRIBUTES) {
return false; return false;
@ -171,9 +167,6 @@ bool settingsDirectory(std::string &result, std::string applicationDirectoryName
} else { } else {
return false; return false;
} }
# else
# error Platform not supported.
# endif
#endif #endif
} }
return true; return true;
@ -181,6 +174,7 @@ bool settingsDirectory(std::string &result, std::string applicationDirectoryName
/*! /*!
* \brief Returns the names of the directory entries in the specified \a path with the specified \a types. * \brief Returns the names of the directory entries in the specified \a path with the specified \a types.
* \deprecated This function has FIXMEs. Since it can be replaced by using fs abstraction lib it is a good candidate for being replaced.
*/ */
std::list<std::string> directoryEntries(const char *path, DirectoryEntryType types) std::list<std::string> directoryEntries(const char *path, DirectoryEntryType types)
{ {

View File

@ -1,8 +1,7 @@
#ifndef IOUTILITIES_PATHHELPER_H #ifndef IOUTILITIES_PATHHELPER_H
#define IOUTILITIES_PATHHELPER_H #define IOUTILITIES_PATHHELPER_H
#include "./binarywriter.h" #include "../global.h"
#include "./binaryreader.h"
#include <string> #include <string>
#include <list> #include <list>