Browse Source

Implemented DirectoryReference and FilePath::join() on Windows.


git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@64 d34b734f-a00e-4b39-a726-e4eeb87269ab
adeniz_1
adeniz 10 years ago
parent
commit
fffe203d53
2 changed files with 89 additions and 1 deletions
  1. 65
    1
      filesystem.cpp
  2. 24
    0
      filesystem.hpp

+ 65
- 1
filesystem.cpp View File

namespace CodeDweller { namespace CodeDweller {
#ifdef _WIN32
char const FilePath::DirectorySeparator = '\\';
#else
char const FilePath::DirectorySeparator = '/';
#endif
std::string FilePath::join(std::initializer_list<std::string> components) {
std::string path;
for (auto &component : components) {
if (!path.empty() &&
path.back() != FilePath::DirectorySeparator) {
path += FilePath::DirectorySeparator;
}
path += component;
}
if (!path.empty() &&
path.back() == FilePath::DirectorySeparator) {
path.pop_back();
}
return path;
}
FileReference::FileReference(std::string fileName) : FileReference::FileReference(std::string fileName) :
name(fileName), name(fileName),
modTimestamp(0), modTimestamp(0),
bool (*dirFilter)(std::string)) : bool (*dirFilter)(std::string)) :
name(dirName), name(dirName),
filter(dirFilter) { filter(dirFilter) {
refresh(); refresh();
} }
// Clear any entries in this object. // Clear any entries in this object.
this->clear(); this->clear();
#ifdef _WIN32
HANDLE hDirList;
WIN32_FIND_DATA dirListData;
std::string searchString = FilePath::join({name, "*"});
hDirList = FindFirstFile(searchString.c_str(), &dirListData);
if (INVALID_HANDLE_VALUE == hDirList) {
throw std::runtime_error("Error getting file list for \"" + name +
"\": " + FileReference::getErrorText());
}
std::string tempName;
while (INVALID_HANDLE_VALUE != hDirList) {
tempName = FilePath::join({name, dirListData.cFileName});
if ( (0 == filter) || (*filter)(dirListData.cFileName)) {
emplace_back(tempName);
}
if (!FindNextFile(hDirList, &dirListData)) {
FindClose(hDirList);
hDirList = INVALID_HANDLE_VALUE;
}
}
#else
// Get new list. // Get new list.
struct dirent **entries; struct dirent **entries;
while (nEntries--) { while (nEntries--) {
std::string tempName; std::string tempName;
tempName = name + "/" + entries[nEntries]->d_name;
tempName = FilePath::join({name, entries[nEntries]->d_name});
if ( (0 == filter) || (*filter)(entries[nEntries]->d_name)) { if ( (0 == filter) || (*filter)(entries[nEntries]->d_name)) {
emplace_back(tempName); emplace_back(tempName);
} }
free(entries[nEntries]); free(entries[nEntries]);
} }
free(entries); free(entries);
#endif
} }
} }

+ 24
- 0
filesystem.hpp View File



#include <string> #include <string>
#include <vector> #include <vector>
#include <initializer_list>


namespace CodeDweller { namespace CodeDweller {


/** Abstracts OS specifics for manipulating file paths. */
class FilePath {

public:

/// Directory separator character.
static char const DirectorySeparator;

/** Join path components.

This method joins a veriable number of std::string inputs to
form a path.

@param[in] component... is one or more components of a
path (i.e. directory names or a file name).

@returns the path consisting of all the pathComponent inputs.

*/
static std::string join(std::initializer_list<std::string> components);

};

/** Abstracts OS specifics for identifying a file and provides /** Abstracts OS specifics for identifying a file and provides
access to meta data. access to meta data.
*/ */

Loading…
Cancel
Save