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

1.1       paf         1: /** @file
                      2:        Parser: directory scanning for different OS-es.
                      3: 
1.26      moko        4:        Copyright (c) 2000-2015 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"
1.28    ! moko       10: #include "pa_request.h"
        !            11: #include "pa_convert_utf.h"
1.1       paf        12: 
1.28    ! moko       13: 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;
1.21      moko       14: 
1.24      moko       15: #ifdef _MSC_VER
1.1       paf        16: 
1.28    ! moko       17: const UTF16* pa_utf16_encode(const char* in, Charset& source_charset);
        !            18: const char* pa_utf16_decode(const UTF16* in, Charset& asked_charset);
        !            19: 
1.22      moko       20: #define TICKS_PER_SECOND 10000000ULL
                     21: #define EPOCH_DIFFERENCE 11644473600ULL
1.28    ! moko       22: 
1.22      moko       23: time_t filetime_to_timet(FILETIME const& ft){
                     24:        ULARGE_INTEGER ull;
                     25:        ull.LowPart = ft.dwLowDateTime;
                     26:        ull.HighPart = ft.dwHighDateTime;
                     27:        long long secs=(ull.QuadPart / TICKS_PER_SECOND - EPOCH_DIFFERENCE);
                     28:        time_t t =(time_t)secs;
                     29:        return (secs == (long long)t) ? t : 0;
                     30: }
                     31: 
1.16      paf        32: bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) {
1.1       paf        33:        char mask[MAXPATH];
                     34:        snprintf(mask, MAXPATH, "%s/*.*", _pathname);
                     35: 
1.28    ! moko       36:        const UTF16* utf16mask=pa_utf16_encode(mask, pa_thread_request().charsets.source());
        !            37: 
        !            38:        _ffblk->handle=FindFirstFileW((const wchar_t *)utf16mask, &_ffblk->stat);
1.1       paf        39:        return _ffblk->handle==INVALID_HANDLE_VALUE;
                     40: }
                     41: 
                     42: bool findnext(struct ffblk *_ffblk) {
1.28    ! moko       43:        return !FindNextFileW(_ffblk->handle, &_ffblk->stat);
1.22      moko       44: }
1.1       paf        45: 
                     46: void findclose(struct ffblk *_ffblk) {
                     47:        FindClose(_ffblk->handle);
                     48: }
                     49: 
1.28    ! moko       50: const char *ffblk::name() {
        !            51:        return pa_utf16_decode((const UTF16*)stat.cFileName, pa_thread_request().charsets.source());
        !            52: }
        !            53: 
1.25      moko       54: bool ffblk::is_dir(bool) {
1.28    ! moko       55:        return (stat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
1.22      moko       56: }
                     57: 
                     58: double ffblk::size() {
                     59:        ULARGE_INTEGER ull;
1.28    ! moko       60:        ull.LowPart = stat.nFileSizeLow;
        !            61:        ull.HighPart = stat.nFileSizeHigh;
1.22      moko       62:        return (double)ull.QuadPart;
                     63: }
                     64: 
                     65: time_t ffblk::c_timestamp() {
1.28    ! moko       66:        return filetime_to_timet(stat.ftCreationTime);
1.22      moko       67: }
                     68: 
                     69: time_t ffblk::m_timestamp() {
1.28    ! moko       70:        return filetime_to_timet(stat.ftLastWriteTime);
1.22      moko       71: }
                     72: 
                     73: time_t ffblk::a_timestamp() {
1.28    ! moko       74:        return filetime_to_timet(stat.ftLastAccessTime);
1.22      moko       75: }
                     76: 
1.1       paf        77: #else
                     78: 
1.18      paf        79: bool findfirst(const char* _pathname, struct ffblk *_ffblk, int /*_attrib*/) {
1.23      moko       80:        _ffblk->filePath=_pathname;
1.1       paf        81:        if(!(_ffblk->dir=opendir(_ffblk->filePath)))
1.28    ! moko       82:                return true;
1.1       paf        83: 
                     84:        return findnext(_ffblk);
                     85: }
                     86: 
                     87: bool findnext(struct ffblk *_ffblk) {
1.28    ! moko       88:        while(true) {
        !            89:                struct dirent *entry=readdir(_ffblk->dir);
        !            90:                if(!entry)
        !            91:                        return true;
1.1       paf        92: 
1.23      moko       93:                strncpy(_ffblk->ff_name, entry->d_name, sizeof(_ffblk->ff_name)-1);
                     94:                _ffblk->ff_name[sizeof(_ffblk->ff_name)-1]=0;
1.1       paf        95:                
1.22      moko       96: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
                     97:                // http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
                     98:                _ffblk->_d_type=entry->d_type;
                     99: #endif
1.1       paf       100:                return false;
                    101:     }
                    102: }
                    103: 
                    104: void findclose(struct ffblk *_ffblk) {
                    105:        closedir(_ffblk->dir);
                    106: }
                    107: 
1.22      moko      108: void ffblk::stat_file() {
                    109:        char fileSpec[MAXPATH];
                    110:        snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name);
                    111:        
1.27      moko      112:        if(pa_stat(fileSpec, &_st) != 0) {
1.22      moko      113:                memset(&_st,0,sizeof(_st));
                    114:        }
                    115: }
                    116: 
1.25      moko      117: bool ffblk::is_dir(bool stat) {
1.22      moko      118: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
1.25      moko      119:        if(!stat && _d_type != DT_UNKNOWN)
                    120:                return _d_type == DT_DIR;
                    121: #endif
                    122:        stat_file();
1.22      moko      123:        return S_ISDIR(_st.st_mode) != 0;
                    124: }
                    125: 
                    126: double ffblk::size() {
                    127:        return (double)_st.st_size;
                    128: }
                    129: 
                    130: time_t ffblk::c_timestamp() {
                    131:        return _st.st_ctime;
                    132: }
                    133: 
                    134: time_t ffblk::m_timestamp() {
                    135:        return _st.st_mtime;
                    136: }
                    137: 
                    138: time_t ffblk::a_timestamp() {
                    139:        return _st.st_atime;
                    140: }
                    141: 
1.1       paf       142: #endif

E-mail: