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