--- parser3/src/main/pa_dir.C 2002/08/01 11:41:18 1.13 +++ parser3/src/main/pa_dir.C 2016/12/28 22:50:07 1.27 @@ -1,18 +1,29 @@ /** @file Parser: directory scanning for different OS-es. - Copyright (c) 2000,2001, 2002 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2000-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char* IDENT_DIR_C="$Date: 2002/08/01 11:41:18 $"; - #include "pa_common.h" #include "pa_dir.h" -#ifdef WIN32 +volatile const char * IDENT_PA_DIR_C="$Id: pa_dir.C,v 1.27 2016/12/28 22:50:07 moko Exp $" IDENT_PA_DIR_H; + +#ifdef _MSC_VER + +#define TICKS_PER_SECOND 10000000ULL +#define EPOCH_DIFFERENCE 11644473600ULL +time_t filetime_to_timet(FILETIME const& ft){ + ULARGE_INTEGER ull; + ull.LowPart = ft.dwLowDateTime; + ull.HighPart = ft.dwHighDateTime; + long long secs=(ull.QuadPart / TICKS_PER_SECOND - EPOCH_DIFFERENCE); + time_t t =(time_t)secs; + return (secs == (long long)t) ? t : 0; +} -bool findfirst(const char *_pathname, struct ffblk *_ffblk, int _attrib) { +bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) { char mask[MAXPATH]; snprintf(mask, MAXPATH, "%s/*.*", _pathname); @@ -21,16 +32,40 @@ bool findfirst(const char *_pathname, st } bool findnext(struct ffblk *_ffblk) { - return !FindNextFile(_ffblk->handle, (_WIN32_FIND_DATAA *)_ffblk);} + return !FindNextFile(_ffblk->handle, (_WIN32_FIND_DATAA *)_ffblk); +} void findclose(struct ffblk *_ffblk) { FindClose(_ffblk->handle); } +bool ffblk::is_dir(bool) { + return (ff_attrib & FILE_ATTRIBUTE_DIRECTORY) != 0; +} + +double ffblk::size() { + ULARGE_INTEGER ull; + ull.LowPart = nFileSizeLow; + ull.HighPart = nFileSizeHigh; + return (double)ull.QuadPart; +} + +time_t ffblk::c_timestamp() { + return filetime_to_timet(ftCreationTime); +} + +time_t ffblk::m_timestamp() { + return filetime_to_timet(ftLastWriteTime); +} + +time_t ffblk::a_timestamp() { + return filetime_to_timet(ftLastAccessTime); +} + #else -bool findfirst(const char *_pathname, struct ffblk *_ffblk, int _attrib) { - strncpy(_ffblk->filePath, _pathname, MAXPATH-1); _ffblk->filePath[MAXPATH-1]=0; +bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) { + _ffblk->filePath=_pathname; if(!(_ffblk->dir=opendir(_ffblk->filePath))) return true; @@ -43,18 +78,13 @@ bool findnext(struct ffblk *_ffblk) { if(!entry) return true; - int maxsize=sizeof(_ffblk->ff_name)-1; - strncpy(_ffblk->ff_name, entry->d_name, maxsize-1); _ffblk->ff_name[maxsize]=0; - - char fileSpec[MAXPATH]; - snprintf(fileSpec, MAXPATH, "%s/%s", - _ffblk->filePath, - _ffblk->ff_name); + strncpy(_ffblk->ff_name, entry->d_name, sizeof(_ffblk->ff_name)-1); + _ffblk->ff_name[sizeof(_ffblk->ff_name)-1]=0; - struct stat st; - - _ffblk->ff_attrib = - stat(fileSpec, &st) < 0 ? 0/*would fail later*/ : st.st_mode; +#ifdef HAVE_STRUCT_DIRENT_D_TYPE + // http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html + _ffblk->_d_type=entry->d_type; +#endif return false; } } @@ -63,4 +93,38 @@ void findclose(struct ffblk *_ffblk) { closedir(_ffblk->dir); } +void ffblk::stat_file() { + char fileSpec[MAXPATH]; + snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name); + + if(pa_stat(fileSpec, &_st) != 0) { + memset(&_st,0,sizeof(_st)); + } +} + +bool ffblk::is_dir(bool stat) { +#ifdef HAVE_STRUCT_DIRENT_D_TYPE + if(!stat && _d_type != DT_UNKNOWN) + return _d_type == DT_DIR; +#endif + stat_file(); + return S_ISDIR(_st.st_mode) != 0; +} + +double ffblk::size() { + return (double)_st.st_size; +} + +time_t ffblk::c_timestamp() { + return _st.st_ctime; +} + +time_t ffblk::m_timestamp() { + return _st.st_mtime; +} + +time_t ffblk::a_timestamp() { + return _st.st_atime; +} + #endif