Pārlūkot izejas kodu

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 pirms 9 gadiem
vecāks
revīzija
fffe203d53
2 mainītis faili ar 89 papildinājumiem un 1 dzēšanām
  1. 65
    1
      filesystem.cpp
  2. 24
    0
      filesystem.hpp

+ 65
- 1
filesystem.cpp Parādīt failu

@@ -44,6 +44,36 @@
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) :
name(fileName),
modTimestamp(0),
@@ -189,6 +219,7 @@ namespace CodeDweller {
bool (*dirFilter)(std::string)) :
name(dirName),
filter(dirFilter) {
refresh();
}
@@ -197,6 +228,36 @@ namespace CodeDweller {
// Clear any entries in this object.
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.
struct dirent **entries;
@@ -211,13 +272,16 @@ namespace CodeDweller {
while (nEntries--) {
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)) {
emplace_back(tempName);
}
free(entries[nEntries]);
}
free(entries);
#endif
}
}

+ 24
- 0
filesystem.hpp Parādīt failu

@@ -31,9 +31,33 @@

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

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
access to meta data.
*/

Notiek ielāde…
Atcelt
Saglabāt