Sfoglia il codice sorgente

Implemented and tested DirectoryReference.


git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@63 d34b734f-a00e-4b39-a726-e4eeb87269ab
adeniz_1
adeniz 10 anni fa
parent
commit
8a041cd3d0
2 ha cambiato i file con 83 aggiunte e 2 eliminazioni
  1. 37
    0
      filesystem.cpp
  2. 46
    2
      filesystem.hpp

+ 37
- 0
filesystem.cpp Vedi File

@@ -26,9 +26,11 @@
#else
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <cstdlib>
#include <cstring>
#include <cerrno>
@@ -183,4 +185,39 @@ namespace CodeDweller {
#endif
}
DirectoryReference::DirectoryReference(std::string dirName,
bool (*dirFilter)(std::string)) :
name(dirName),
filter(dirFilter) {
refresh();
}
void DirectoryReference::refresh() {
// Clear any entries in this object.
this->clear();
// Get new list.
struct dirent **entries;
int nEntries = scandir(name.c_str(), &entries, 0, 0);
if (nEntries < 0) {
throw std::runtime_error("Error getting file list for \"" + name +
"\": " + FileReference::getErrorText());
}
// Create the FileReference objects.
while (nEntries--) {
std::string tempName;
tempName = name + "/" + entries[nEntries]->d_name;
if ( (0 == filter) || (*filter)(entries[nEntries]->d_name)) {
emplace_back(tempName);
}
free(entries[nEntries]);
}
free(entries);
}
}

+ 46
- 2
filesystem.hpp Vedi File

@@ -30,6 +30,7 @@
#include <ctime>

#include <string>
#include <vector>

namespace CodeDweller {

@@ -88,6 +89,7 @@ namespace CodeDweller {

This method updates the FileReference object with the current
data on the file system, except for the full path.

*/
void refresh();

@@ -113,14 +115,14 @@ namespace CodeDweller {
*/
bool isDirectory() const;

private:

/// Return text for the most recent error.
//
// \returns Human-readable description of the most recent error.
//
static std::string getErrorText();

private:

/// Reset all members but the name. */
void reset();

@@ -144,6 +146,48 @@ namespace CodeDweller {

};

/** Abstracts OS specifics for scanning a directory and provides
access to meta data.
*/

class DirectoryReference : public std::vector<FileReference> {

public:

/** Initialize by scanning the directory.

The constructor updates the DirectoryReference object with the
listing of the specified directory, filtered by the specified
function.

@param[in] dirName is the name of the directory.

@param[in] filter is the function used to filter the directory
listing. It inputs the name of the file, and returns true if
the file is is to be included in the listing, and false
otherwise.

*/
DirectoryReference(std::string dirName, bool (*dirFilter)(std::string) = 0);

/** Refreshes the DirectoryReference object.

This method updates the DirectoryReference object with the
current listing on the file system.

*/
void refresh();

private:

/** Directory name provided to constructor. */
std::string name;

/** Filter function. */
bool (*filter)(std::string);

};

}

#endif // FILESYSTEM_HPP

Loading…
Annulla
Salva