Pārlūkot izejas kodu

Tested on Windows 7.


git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@62 d34b734f-a00e-4b39-a726-e4eeb87269ab
adeniz_1
adeniz pirms 9 gadiem
vecāks
revīzija
629deb2397
2 mainītis faili ar 102 papildinājumiem un 44 dzēšanām
  1. 64
    28
      filesystem.cpp
  2. 38
    16
      filesystem.hpp

+ 64
- 28
filesystem.cpp Parādīt failu

@@ -20,13 +20,22 @@
// Place, Suite 330, Boston, MA 02111-1307 USA
//==============================================================================
#include <sys/types.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <cstring>
#include <cerrno>
#endif
#include <sys/stat.h>
#include <stdexcept>
#include "filesystem.hpp"
@@ -46,45 +55,23 @@ namespace CodeDweller {
void FileReference::refresh() {
// Load path if necessary.
if (path.empty()) {
char *realPath = realpath(name.c_str(), NULL);
if (NULL == realPath) {
// Nothing to do if the file doesn't exist.
if (ENOENT == errno) {
return;
}
// Something went wrong.
throw std::runtime_error("Error checking file \"" + name + "\": " +
getErrorText());
}
path.assign(realPath);
free(realPath);
}
reset();
// Load info.
struct stat statBuffer;
int status = stat(path.c_str(), &statBuffer);
int status = stat(name.c_str(), &statBuffer);
if (-1 == status) {
// File no longer exists.
if (ENOENT == errno) {
reset();
return;
}
// Something went wrong.
throw std::runtime_error("Error updating status of file \"" +
path + "\": " + getErrorText());
name + "\": " + getErrorText());
}
@@ -100,6 +87,7 @@ namespace CodeDweller {
size_bytes = 0;
fileExists = false;
fileIsDirectory = false;
path.clear();
}
time_t FileReference::ModTimestamp() const {
@@ -110,7 +98,55 @@ namespace CodeDweller {
return size_bytes;
}
std::string FileReference::FullPath() const {
std::string FileReference::FullPath() {
if (!path.empty()) {
return path;
}
if (!fileExists) {
return "";
}
#ifdef _WIN32
// Get the size of the full path name.
DWORD nTchars = GetFullPathName(name.c_str(), 0, NULL, NULL);
if (0 == nTchars) {
throw std::runtime_error("Error getting full path length for \"" + name
+ "\": " + getErrorText());
}
size_t bufSize = nTchars * sizeof(TCHAR);
TCHAR fullPath[bufSize];
nTchars = GetFullPathName(name.c_str(), bufSize, fullPath, NULL);
if (0 == nTchars) {
throw std::runtime_error("Error getting full path for \"" + name
+ "\": " + getErrorText());
}
path.assign(fullPath);
#else
char *realPath = realpath(name.c_str(), NULL);
if (NULL == realPath) {
// Nothing to do if the file doesn't exist.
if (ENOENT == errno) {
reset();
return "";
}
// Something went wrong.
throw std::runtime_error("Error checking file \"" + name + "\": " +
getErrorText());
}
path.assign(realPath);
free(realPath);
#endif
return path;
}

+ 38
- 16
filesystem.hpp Parādīt failu

@@ -42,41 +42,61 @@ namespace CodeDweller {

/** Initialize with the name of the file.

The constructor updates the FileReference object with the file
status except for the full path.

@param[in] fileName is the name of the file.

*/
FileReference(std::string fileName);

/** Return timestamp.

@returns the epoch modification time for file when
FileReference was created or last refreshed, or zero if the
file doesn't exist.
@returns the epoch modification time of the file if the file
existed when the FileReference object was created, or the last
time refersh() was called (whichever is more recent), 0
otherwise.

*/
time_t ModTimestamp() const;

/** Get the file size.

@returns the size of file when FileReference was created or
last refreshed, or 0 if the file doesn't exist.
@returns the size of the file if the file existed when the
FileReference object was created, or the last time refersh()
was called (whichever is more recent), 0 otherwise.

*/
size_t Size() const;

/** Get full path.
/** Get full path if the file exists.

This method access the filesystem to get the full path of the
file.

@returns a fully referenced path to the file.
@warning This method might fail under Windows in a
multi-threaded application if the current working directory of
the application is being changed by a thread while this method
is invoked.

@returns a fully referenced path to the file if the file
exists. Otherwise, "" is returned.
*/
std:: string FullPath() const;
std:: string FullPath();

/** Refreshes the FileReference object.

/** Refreshes the FileReference object with the current data on the file
system.
This method updates the FileReference object with the current
data on the file system, except for the full path.
*/
void refresh();

/** Check if the file exists.

@returns true if the file reference name exists in the file
system, false otherwise.
@returns true if the file reference name existed in the file
system when the FileReference object was created, or the last
time refersh() was called (whichever is more recent), false
otherwise.

@throws std::runtime_error if an error occurs.

@@ -85,9 +105,11 @@ namespace CodeDweller {

/** Check if the file is a directory.

@returns true if the name provided on construction resulted in
finding a directory not a file, and false otherwise or if the
file doesn't exist.
@returns true if the file reference name existed and was a
directory in the file system when the FileReference object was
created, or the last time refersh() was called (whichever is
more recent), false otherwise.

*/
bool isDirectory() const;

@@ -99,7 +121,7 @@ namespace CodeDweller {
//
static std::string getErrorText();

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

/** Name provided to constructor. */

Notiek ielāde…
Atcelt
Saglabāt