Combined file info functions

This commit is contained in:
Matt Hill
2015-12-23 00:29:14 -05:00
parent 175e77c8ae
commit fef9128d14
2 changed files with 49 additions and 32 deletions

View File

@@ -1,4 +1,6 @@
#include "filesystem.h" #include "filesystem.h"
#include <sstream>
#include <algorithm>
namespace alpr namespace alpr
{ {
@@ -146,41 +148,51 @@ namespace alpr
#ifdef WINDOWS #ifdef WINDOWS
// Stub out these functions on Windows. They're used for the daemon anyway, which isn't supported on Windows. // Stub out these functions on Windows. They're used for the daemon anyway, which isn't supported on Windows.
int64_t getFileSize(std::string filename) { return 0; } static int makeDir(const char *path, mode_t mode) { return 0; }
static int makeDir(char *path, mode_t mode) { return 0; } bool makePath(const char* path, mode_t mode) {
bool makePath(char* path, mode_t mode) { return true; } std::stringstream pathstream;
int64_t getFileCreationTime(std::string filename) { return 0; } pathstream << "mkdir " << path;
std::string candidate_path = pathstream.str();
std::replace(candidate_path.begin(), candidate_path.end(), '/', '\\');
system(candidate_path.c_str());
return true;
}
FileInfo getFileInfo(std::string filename) {
FileInfo response;
response.creation_time = 0;
response.size = 0;
return response;
}
#else #else
int64_t getFileSize(std::string filename) FileInfo getFileInfo(std::string filename)
{ {
struct stat stat_buf; FileInfo response;
int rc = stat(filename.c_str(), &stat_buf);
//return rc == 0 ? stat_buf.st_size : -1; struct stat stat_buf;
int rc = stat(filename.c_str(), &stat_buf);
//return rc == 0 ? stat_buf.st_size : -1;
// 512 bytes is the standard block size // 512 bytes is the standard block size
if (rc == 0) if (rc == 0)
return 512 * stat_buf.st_blocks; {
#if defined(__APPLE__)
return -1; double milliseconds = (stat_buf.st_ctimespec.tv_sec * 1000) + (((double) stat_buf.st_ctimespec.tv_nsec) / 1000000.0);
} #else
double milliseconds = (stat_buf.st_ctim.tv_sec * 1000) + (((double) stat_buf.st_ctim.tv_nsec) / 1000000.0);
int64_t getFileCreationTime(std::string filename) #endif
{ response.size = 512 * stat_buf.st_blocks;
struct stat stat_buf; response.creation_time = (int64_t) milliseconds;
int rc = stat(filename.c_str(), &stat_buf); }
else
if (rc != 0) {
return 0; response.creation_time = 0;
response.size = 0;
#if defined(__APPLE__) }
double milliseconds = (stat_buf.st_ctimespec.tv_sec * 1000) + (((double) stat_buf.st_ctimespec.tv_nsec) / 1000000.0);
#else return response;
double milliseconds = (stat_buf.st_ctim.tv_sec * 1000) + (((double) stat_buf.st_ctim.tv_nsec) / 1000000.0);
#endif
return (int64_t) milliseconds;
} }
static int makeDir(const char *path, mode_t mode) static int makeDir(const char *path, mode_t mode)

View File

@@ -25,14 +25,19 @@ typedef int mode_t;
namespace alpr namespace alpr
{ {
struct FileInfo
{
int64_t size;
int64_t creation_time;
};
bool startsWith(std::string const &fullString, std::string const &prefix); bool startsWith(std::string const &fullString, std::string const &prefix);
bool hasEnding (std::string const &fullString, std::string const &ending); bool hasEnding (std::string const &fullString, std::string const &ending);
bool hasEndingInsensitive(const std::string& fullString, const std::string& ending); bool hasEndingInsensitive(const std::string& fullString, const std::string& ending);
std::string filenameWithoutExtension(std::string filename); std::string filenameWithoutExtension(std::string filename);
int64_t getFileSize(std::string filename); FileInfo getFileInfo(std::string filename);
int64_t getFileCreationTime(std::string filename);
bool DirectoryExists( const char* pzPath ); bool DirectoryExists( const char* pzPath );
bool fileExists( const char* pzPath ); bool fileExists( const char* pzPath );