Add base::file_size() function

This commit is contained in:
David Capello 2014-08-22 01:12:12 -03:00
parent 483878140a
commit e8d4388061
3 changed files with 15 additions and 0 deletions

View File

@ -15,6 +15,8 @@ namespace base {
bool is_file(const std::string& path);
bool is_directory(const std::string& path);
size_t file_size(const std::string& path);
void delete_file(const std::string& path);
bool has_readonly_attr(const std::string& path);

View File

@ -42,6 +42,12 @@ void make_directory(const std::string& path)
}
}
size_t file_size(const std::string& path)
{
struct stat sts;
return (stat(path.c_str(), &sts) == 0) ? sts.st_size: 0;
}
void delete_file(const std::string& path)
{
int result = unlink(path.c_str());

View File

@ -6,6 +6,7 @@
#include <stdexcept>
#include <windows.h>
#include <sys/stat.h>
#include "base/string.h"
#include "base/win32_exception.h"
@ -30,6 +31,12 @@ bool is_directory(const std::string& path)
((attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY));
}
size_t file_size(const std::string& path)
{
struct _stat sts;
return (_wstat(from_utf8(path).c_str(), &sts) == 0) ? sts.st_size: 0;
}
void delete_file(const std::string& path)
{
BOOL result = ::DeleteFile(from_utf8(path).c_str());