OSDN Git Service

Shell Extension for Windows 11 or later (5)
[winmerge-jp/winmerge-jp.git] / Src / DirTravel.cpp
1 /** 
2  * @file  DirTravel.cpp
3  *
4  * @brief Implementation file for Directory traversal functions.
5  *
6  */
7
8 #include "pch.h"
9 #include "DirTravel.h"
10 #include <algorithm>
11 #include <Poco/DirectoryIterator.h>
12 #include <Poco/Timestamp.h>
13 #include <windows.h>
14 #include "TFile.h"
15 #include "UnicodeString.h"
16 #include "DirItem.h"
17 #include "unicoder.h"
18 #include "paths.h"
19 #include "Win_VersionHelper.h"
20 #include "DebugNew.h"
21
22 using Poco::DirectoryIterator;
23 using Poco::Timestamp;
24
25 static void LoadFiles(const String& sDir, DirItemArray * dirs, DirItemArray * files);
26 static void Sort(DirItemArray * dirs, bool casesensitive);
27
28 /**
29  * @brief Load arrays with all directories & files in specified dir
30  */
31 void LoadAndSortFiles(const String& sDir, DirItemArray * dirs, DirItemArray * files, bool casesensitive)
32 {
33         LoadFiles(sDir, dirs, files);
34         Sort(dirs, casesensitive);
35         Sort(files, casesensitive);
36 }
37
38 /**
39  * @brief Find file and sub-folder names from given folder.
40  * This function saves all file and sub-folder names in given folder to arrays.
41  * We use 64-bit version of stat() to get times since find doesn't return
42  * valid times for very old files (around year 1970). Even stat() seems to
43  * give negative time values but we can live with that. Those around 1970
44  * times can happen when file is created so that it doesn't get valid
45  * creation or modification dates.
46  * @param [in] sDir Base folder for files and subfolders.
47  * @param [in, out] dirs Array where subfolder names are stored.
48  * @param [in, out] files Array where file names are stored.
49  */
50 static void LoadFiles(const String& sDir, DirItemArray * dirs, DirItemArray * files)
51 {
52         boost::flyweight<String> dir(sDir);
53 #if 0
54         DirectoryIterator it(ucr::toUTF8(sDir));
55         DirectoryIterator end;
56
57         for (; it != end; ++it)
58         {
59                 bool bIsDirectory = it->isDirectory();
60                 if (bIsDirectory)
61                         continue;
62
63                 DirItem ent;
64                 ent.ctime = it->created();
65                 if (ent.ctime < 0)
66                         ent.ctime = 0;
67                 ent.mtime = it->getLastModified();
68                 if (ent.mtime < 0)
69                         ent.mtime = 0;
70                 ent.size = it->getSize();
71                 ent.path = dir;
72                 ent.filename = ucr::toTString(it.name());
73                 ent.flags.attributes = GetFileAttributes(ucr::toTString(it.name()).c_str());            
74                 (bIsDirectory ? dirs : files)->push_back(ent);
75         }
76
77 #else
78         String sPattern = paths::ConcatPath(sDir, _T("*.*"));
79
80         WIN32_FIND_DATA ff;
81         HANDLE h;
82         if (IsWin7_OrGreater()) // (also 'Windows Server 2008 R2' and greater) for FindExInfoBasic and FIND_FIRST_EX_LARGE_FETCH
83                 h = FindFirstFileEx(TFile(sPattern).wpath().c_str(), FindExInfoBasic, &ff, FindExSearchNameMatch, nullptr, FIND_FIRST_EX_LARGE_FETCH);
84         else
85                 h = FindFirstFile(TFile(sPattern).wpath().c_str(), &ff);
86         if (h != INVALID_HANDLE_VALUE)
87         {
88                 do
89                 {
90                         bool bIsDirectory = (ff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;
91                         if (bIsDirectory && _tcsstr(_T(".."), ff.cFileName))
92                                 continue;
93
94                         DirItem ent;
95
96                         // Save filetimes as seconds since January 1, 1970
97                         // Note that times can be < 0 if they are around that 1970..
98                         // Anyway that is not sensible case for normal files so we can
99                         // just use zero for their time.
100                         ent.ctime = Timestamp::fromFileTimeNP(ff.ftCreationTime.dwLowDateTime, ff.ftCreationTime.dwHighDateTime);
101                         if (ent.ctime < 0)
102                                 ent.ctime = 0;
103                         ent.mtime = Timestamp::fromFileTimeNP(ff.ftLastWriteTime.dwLowDateTime, ff.ftLastWriteTime.dwHighDateTime);
104                         if (ent.mtime < 0)
105                                 ent.mtime = 0;
106
107                         if (ff.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
108                                 ent.size = DirItem::FILE_SIZE_NONE;  // No size for directories
109                         else
110                         {
111                                 ent.size = ((int64_t)ff.nFileSizeHigh << 32) + ff.nFileSizeLow;
112                         }
113
114                         ent.path = dir;
115                         ent.filename = ff.cFileName;
116                         ent.flags.attributes = ff.dwFileAttributes;
117                         
118                         (bIsDirectory ? dirs : files)->push_back(ent);
119                 } while (FindNextFile(h, &ff));
120                 FindClose(h);
121         }
122
123 #endif
124 }
125
126 static inline int collate(const String &str1, const String &str2)
127 {
128         return _tcscoll(str1.c_str(), str2.c_str());
129 }
130
131 static inline int collate_ignore_case(const String &str1, const String &str2)
132 {
133         return _tcsicoll(str1.c_str(), str2.c_str());
134 }
135
136
137 template<int (*compfunc)(const TCHAR *, const TCHAR *)>
138 struct StringComparer
139 {
140         bool operator()(const DirItem &elem1, const DirItem &elem2)
141         {
142                 return compfunc(elem1.filename.get().c_str(), elem2.filename.get().c_str()) < 0;
143         }
144 };
145
146 /**
147  * @brief sort specified array
148  */
149 static void Sort(DirItemArray * dirs, bool casesensitive)
150 {
151         if (casesensitive)
152         std::sort(dirs->begin(), dirs->end(), StringComparer<_tcscoll>());
153         else
154                 std::sort(dirs->begin(), dirs->end(), StringComparer<_tcsicoll>());
155 }
156
157 /**
158  * @brief  Compare (NLS aware) two strings, either case-sensitive or
159  * case-insensitive as caller specifies
160  */
161 int collstr(const String & s1, const String & s2, bool casesensitive)
162 {
163         if (casesensitive)
164                 return collate(s1, s2);
165         else
166                 return collate_ignore_case(s1, s2);
167 }