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

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.25    ! moko       11: volatile const char * IDENT_PA_DIR_C="$Id: pa_dir.C,v 1.24 2013/07/22 20:55:54 moko Exp $" IDENT_PA_DIR_H;
1.21      moko       12: 
1.24      moko       13: #ifdef _MSC_VER
1.1       paf        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.25    ! moko       42: bool ffblk::is_dir(bool) {
1.22      moko       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: void ffblk::stat_file() {
                     97:        char fileSpec[MAXPATH];
                     98:        snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name);
                     99:        
                    100:        if(stat(fileSpec, &_st) != 0) {
                    101:                memset(&_st,0,sizeof(_st));
                    102:        }
                    103: }
                    104: 
1.25    ! moko      105: bool ffblk::is_dir(bool stat) {
1.22      moko      106: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
1.25    ! moko      107:        if(!stat && _d_type != DT_UNKNOWN)
        !           108:                return _d_type == DT_DIR;
        !           109: #endif
        !           110:        stat_file();
1.22      moko      111:        return S_ISDIR(_st.st_mode) != 0;
                    112: }
                    113: 
                    114: double ffblk::size() {
                    115:        return (double)_st.st_size;
                    116: }
                    117: 
                    118: time_t ffblk::c_timestamp() {
                    119:        return _st.st_ctime;
                    120: }
                    121: 
                    122: time_t ffblk::m_timestamp() {
                    123:        return _st.st_mtime;
                    124: }
                    125: 
                    126: time_t ffblk::a_timestamp() {
                    127:        return _st.st_atime;
                    128: }
                    129: 
1.1       paf       130: #endif

E-mail: