From fef9128d148d83d784eadaf504a076a4352e69c5 Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Wed, 23 Dec 2015 00:29:14 -0500 Subject: [PATCH] Combined file info functions --- src/openalpr/support/filesystem.cpp | 72 +++++++++++++++++------------ src/openalpr/support/filesystem.h | 9 +++- 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/openalpr/support/filesystem.cpp b/src/openalpr/support/filesystem.cpp index 8d46c85..b9febfd 100644 --- a/src/openalpr/support/filesystem.cpp +++ b/src/openalpr/support/filesystem.cpp @@ -1,4 +1,6 @@ #include "filesystem.h" +#include +#include namespace alpr { @@ -146,41 +148,51 @@ namespace alpr #ifdef 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(char *path, mode_t mode) { return 0; } - bool makePath(char* path, mode_t mode) { return true; } - int64_t getFileCreationTime(std::string filename) { return 0; } + static int makeDir(const char *path, mode_t mode) { return 0; } + bool makePath(const char* path, mode_t mode) { + std::stringstream pathstream; + 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 - int64_t getFileSize(std::string filename) + FileInfo getFileInfo(std::string filename) { - struct stat stat_buf; - int rc = stat(filename.c_str(), &stat_buf); - //return rc == 0 ? stat_buf.st_size : -1; + FileInfo response; + + 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 - if (rc == 0) - return 512 * stat_buf.st_blocks; - - return -1; - } - - int64_t getFileCreationTime(std::string filename) - { - struct stat stat_buf; - int rc = stat(filename.c_str(), &stat_buf); - - if (rc != 0) - return 0; - -#if defined(__APPLE__) - 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); -#endif - - return (int64_t) milliseconds; + // 512 bytes is the standard block size + if (rc == 0) + { + #if defined(__APPLE__) + 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); + #endif + response.size = 512 * stat_buf.st_blocks; + response.creation_time = (int64_t) milliseconds; + } + else + { + response.creation_time = 0; + response.size = 0; + } + + return response; } static int makeDir(const char *path, mode_t mode) diff --git a/src/openalpr/support/filesystem.h b/src/openalpr/support/filesystem.h index 09b2e54..8278bde 100644 --- a/src/openalpr/support/filesystem.h +++ b/src/openalpr/support/filesystem.h @@ -25,14 +25,19 @@ typedef int mode_t; namespace alpr { + struct FileInfo + { + int64_t size; + int64_t creation_time; + }; + bool startsWith(std::string const &fullString, std::string const &prefix); bool hasEnding (std::string const &fullString, std::string const &ending); bool hasEndingInsensitive(const std::string& fullString, const std::string& ending); std::string filenameWithoutExtension(std::string filename); - int64_t getFileSize(std::string filename); - int64_t getFileCreationTime(std::string filename); + FileInfo getFileInfo(std::string filename); bool DirectoryExists( const char* pzPath ); bool fileExists( const char* pzPath );