Annotation of parser3/src/main/pa_dir.C, revision 1.31
1.1 paf 1: /** @file
2: Parser: directory scanning for different OS-es.
3:
1.30 moko 4: Copyright (c) 2000-2020 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.31 ! moko 13: volatile const char * IDENT_PA_DIR_C="$Id: pa_dir.C,v 1.30 2020/12/15 17:10:36 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.31 ! moko 93: pa_strncpy(_ffblk->ff_name, entry->d_name, sizeof(_ffblk->ff_name));
! 94:
1.22 moko 95: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
96: // http://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html
97: _ffblk->_d_type=entry->d_type;
98: #endif
1.1 paf 99: return false;
100: }
101: }
102:
103: void findclose(struct ffblk *_ffblk) {
104: closedir(_ffblk->dir);
105: }
106:
1.22 moko 107: void ffblk::stat_file() {
108: char fileSpec[MAXPATH];
109: snprintf(fileSpec, MAXPATH, "%s/%s", filePath, ff_name);
110:
1.27 moko 111: if(pa_stat(fileSpec, &_st) != 0) {
1.22 moko 112: memset(&_st,0,sizeof(_st));
113: }
114: }
115:
1.25 moko 116: bool ffblk::is_dir(bool stat) {
1.22 moko 117: #ifdef HAVE_STRUCT_DIRENT_D_TYPE
1.25 moko 118: if(!stat && _d_type != DT_UNKNOWN)
119: return _d_type == DT_DIR;
120: #endif
121: stat_file();
1.22 moko 122: return S_ISDIR(_st.st_mode) != 0;
123: }
124:
125: double ffblk::size() {
126: return (double)_st.st_size;
127: }
128:
129: time_t ffblk::c_timestamp() {
130: return _st.st_ctime;
131: }
132:
133: time_t ffblk::m_timestamp() {
134: return _st.st_mtime;
135: }
136:
137: time_t ffblk::a_timestamp() {
138: return _st.st_atime;
139: }
140:
1.1 paf 141: #endif
E-mail: