Add agi::util::Remove for deleting files

This commit is contained in:
Thomas Goyne 2013-01-03 12:59:07 -08:00
parent cec5f2256c
commit e8344ab0c7
4 changed files with 47 additions and 14 deletions

View File

@ -31,36 +31,41 @@ namespace agi {
TypeDir ///< Directory TypeDir ///< Directory
}; };
// Calculate midpoint from a list of Integers /// Clamp `b` to the range [`a`,`c`]
template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); } template<typename T> inline T mid(T a, T b, T c) { return std::max(a, std::min(b, c)); }
// Get the parent directory of a path. /// Get the parent directory of a path.
// @param path Path to process. /// @param path Path to process.
const std::string DirName(const std::string& path); const std::string DirName(const std::string& path);
// Rename a file. /// Rename a file.
// @param from Source. /// @param from Source.
// @param to Destination. /// @param to Destination.
void Rename(const std::string& from, const std::string& to); void Rename(const std::string& from, const std::string& to);
// Get time sutable for logging mechinisms. /// Delete a file
// @param tv timeval /// @param path Path to file to delete
/// @throws agi::FileNotAccessibleError if file exists but could not be deleted
void Remove(std::string const& path);
/// Get time suitable for logging mechanisms.
/// @param tv timeval
void time_log(agi_timeval &tv); void time_log(agi_timeval &tv);
// Make all alphabetic characters lowercase. /// Make all alphabetic characters lowercase.
// @param str Input string /// @param str Input string
void str_lower(std::string &str); void str_lower(std::string &str);
// Convert a string to Integer. /// Convert a string to Integer.
// @param str Input string /// @param str Input string
int strtoi(std::string const& str); int strtoi(std::string const& str);
bool try_parse(std::string const& str, double *out); bool try_parse(std::string const& str, double *out);
bool try_parse(std::string const& str, int *out); bool try_parse(std::string const& str, int *out);
/// Check for amount of free space on a Path. /// Check for amount of free space on a Path.
// @param path[in] Path to check /// @param path[in] Path to check
// @param type PathType (default is TypeDir) /// @param type PathType (default is TypeDir)
uint64_t freespace(std::string const& path, PathType type=TypeDir); uint64_t freespace(std::string const& path, PathType type=TypeDir);
struct delete_ptr { struct delete_ptr {

View File

@ -60,6 +60,11 @@ void Rename(const std::string& from, const std::string& to) {
rename(from.c_str(), to.c_str()); rename(from.c_str(), to.c_str());
} }
void Remove(std::string const& path) {
if (!remove(path.c_str()) && errno != ENOENT)
throw agi::FileNotAccessibleError("Can not remove file: " + path);
}
void time_log(timeval &tv) { void time_log(timeval &tv) {
gettimeofday(&tv, (struct timezone *)NULL); gettimeofday(&tv, (struct timezone *)NULL);
} }

View File

@ -57,6 +57,14 @@ void Rename(const std::string& from, const std::string& to) {
throw agi::FileNotAccessibleError("Can not overwrite file: " + ErrorString(GetLastError())); throw agi::FileNotAccessibleError("Can not overwrite file: " + ErrorString(GetLastError()));
} }
void Remove(std::string const& path) {
if (!DeleteFile(ConvertW(path).c_str())) {
DWORD err = GetLastError();
if (err != ERROR_FILE_NOT_FOUND)
throw agi::FileNotAccessibleError("Can not remove file: " + ErrorString(err));
}
}
std::string ErrorString(DWORD error) { std::string ErrorString(DWORD error) {
LPWSTR lpstr = nullptr; LPWSTR lpstr = nullptr;

View File

@ -62,6 +62,21 @@ TEST(lagi_util, UtilRenameExNotFound) {
EXPECT_THROW(util::Rename("./data/nonexistent", ""), FileNotFoundError); EXPECT_THROW(util::Rename("./data/nonexistent", ""), FileNotFoundError);
} }
TEST(lagi_util, RemoveExisting) {
std::ofstream("./data/file_to_remove");
EXPECT_NO_THROW(util::Remove("./data/file_to_remove"));
std::ifstream check("./data/file_to_remove");
EXPECT_FALSE(check.good());
}
TEST(lagi_util, RemoveNonExisting) {
EXPECT_NO_THROW(util::Remove("./data/nonexistent"));
}
TEST(lagi_util, RemoveReadOnly) {
EXPECT_THROW(util::Remove("./data/file_read_only"), FileNotAccessibleError);
}
TEST(lagi_util, Utilstr_lower) { TEST(lagi_util, Utilstr_lower) {
std::string str("-!ABCDEFGHIJKLMNOPQRSTUVWXYZ123"); std::string str("-!ABCDEFGHIJKLMNOPQRSTUVWXYZ123");
util::str_lower(str); util::str_lower(str);