Annotation of parser3/src/main/pa_dir.C, revision 1.23

1.1       paf         1: /** @file
                      2:        Parser: directory scanning for different OS-es.
                      3: 
1.21      moko        4:        Copyright (c) 2000-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.11      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.12      paf         6: */
1.1       paf         7: 
1.9       paf         8: #include "pa_common.h"
1.1       paf         9: #include "pa_dir.h"
                     10: 
1.23    ! moko       11: volatile const char * IDENT_PA_DIR_C="$Id: pa_dir.C,v 1.22 2013/07/21 14:45:32 moko Exp $" IDENT_PA_DIR_H;
1.21      moko       12: 
1.1       paf        13: #ifdef WIN32
                     14: 
1.22      moko       15: #define TICKS_PER_SECOND 10000000ULL
                     16: #define EPOCH_DIFFERENCE 11644473600ULL
                     17: time_t filetime_to_timet(FILETIME const& ft){
                     18:        ULARGE_INTEGER ull;
                     19:        ull.LowPart = ft.dwLowDateTime;
                     20:        ull.HighPart = ft.dwHighDateTime;
                     21:        long long secs=(ull.QuadPart / TICKS_PER_SECOND - EPOCH_DIFFERENCE);
                     22:        time_t t =(time_t)secs;
                     23:        return (secs == (long long)t) ? t : 0;
                     24: }
                     25: 
1.16      paf        26: bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) {
1.1       paf        27:        char mask[MAXPATH];
                     28:        snprintf(mask, MAXPATH, "%s/*.*", _pathname);
                     29: 
                     30:        _ffblk->handle=FindFirstFile(mask, (_WIN32_FIND_DATAA *)_ffblk);
                     31:        return _ffblk->handle==INVALID_HANDLE_VALUE;
                     32: }
                     33: 
                     34: bool findnext(struct ffblk *_ffblk) {
1.22      moko       35:        return !FindNextFile(_ffblk->handle, (_WIN32_FIND_DATAA *)_ffblk);
                     36: }
1.1       paf        37: 
                     38: void findclose(struct ffblk *_ffblk) {
                     39:        FindClose(_ffblk->handle);
                     40: }
                     41: 
1.22      moko       42: bool ffblk::is_dir() {
                     43:        return (ff_attrib & FILE_ATTRIBUTE_DIRECTORY) != 0;
                     44: }
                     45: 
                     46: double ffblk::size() {
                     47:        ULARGE_INTEGER ull;
                     48:        ull.LowPart = nFileSizeLow;
                     49:        ull.HighPart = nFileSizeHigh;
                     50:        return (double)ull.QuadPart;
                     51: }
                     52: 
                     53: time_t ffblk::c_timestamp() {
                     54:        return filetime_to_timet(ftCreationTime);
                     55: }
                     56: 
                     57: time_t ffblk::m_timestamp() {
                     58:        return filetime_to_timet(ftLastWriteTime);
                     59: }
                     60: 
                     61: time_t ffblk::a_timestamp() {
                     62:        return filetime_to_timet(ftLastAccessTime);
                     63: }
                     64: 
1.1       paf        65: #else
                     66: 
1.18      paf        67: bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) {
1.23    ! moko       68:        _ffblk->filePath=_pathname;
1.1       paf        69:        if(!(_ffblk->dir=opendir(_ffblk->filePath)))
                     70:         return true;
                     71: 
                     72:        return findnext(_ffblk);
                     73: }
                     74: 
                     75: bool findnext(struct ffblk *_ffblk) {
                     76:     while(true) {
                     77:         struct dirent *entry=readdir(_ffblk->dir);
                     78:         if(!entry)
                     79:             return true;
                     80: 
1.23    ! moko       81:                strncpy(_ffblk->ff_name, entry->d_name, sizeof(_ffblk->ff_name)-1);
        !            82:                _ffblk->ff_name[sizeof(_ffblk->ff_name)-1]=0;
1.1       paf        83:                
1.22      moko       84: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
                     85:                // http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
                     86:                _ffblk->_d_type=entry->d_type;
                     87: #endif
1.1       paf        88:                return false;
                     89:     }
                     90: }
                     91: 
                     92: void findclose(struct ffblk *_ffblk) {
                     93:        closedir(_ffblk->dir);
                     94: }
                     95: 
1.22      moko       96: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
                     97: void ffblk::stat_file() {
                     98: #else
                     99: void ffblk::real_stat_file() {
                    100: #endif
                    101:        char fileSpec[MAXPATH];
                    102:        snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name);
                    103:        
                    104:        if(stat(fileSpec, &_st) != 0) {
                    105:                memset(&_st,0,sizeof(_st));
                    106:        }
                    107: }
                    108: 
                    109: bool ffblk::is_dir() {
                    110: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
                    111:        return (_d_type & DT_DIR) != 0;
                    112: #else
                    113:        real_stat_file();
                    114:        return S_ISDIR(_st.st_mode) != 0;
                    115: #endif
                    116: }
                    117: 
                    118: double ffblk::size() {
                    119:        return (double)_st.st_size;
                    120: }
                    121: 
                    122: time_t ffblk::c_timestamp() {
                    123:        return _st.st_ctime;
                    124: }
                    125: 
                    126: time_t ffblk::m_timestamp() {
                    127:        return _st.st_mtime;
                    128: }
                    129: 
                    130: time_t ffblk::a_timestamp() {
                    131:        return _st.st_atime;
                    132: }
                    133: 
1.1       paf       134: #endif

E-mail: