other-licenses/7zstub/src/Windows/FileFind.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/Windows/FileFind.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,365 @@
     1.4 +// Windows/FileFind.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "FileFind.h"
     1.9 +#ifndef _UNICODE
    1.10 +#include "../Common/StringConvert.h"
    1.11 +#endif
    1.12 +
    1.13 +#ifndef _UNICODE
    1.14 +extern bool g_IsNT;
    1.15 +#endif
    1.16 +
    1.17 +namespace NWindows {
    1.18 +namespace NFile {
    1.19 +namespace NFind {
    1.20 +
    1.21 +static const TCHAR kDot = TEXT('.');
    1.22 +
    1.23 +bool CFileInfo::IsDots() const
    1.24 +{ 
    1.25 +  if (!IsDirectory() || Name.IsEmpty())
    1.26 +    return false;
    1.27 +  if (Name[0] != kDot)
    1.28 +    return false;
    1.29 +  return Name.Length() == 1 || (Name[1] == kDot && Name.Length() == 2);
    1.30 +}
    1.31 +
    1.32 +#ifndef _UNICODE
    1.33 +bool CFileInfoW::IsDots() const
    1.34 +{ 
    1.35 +  if (!IsDirectory() || Name.IsEmpty())
    1.36 +    return false;
    1.37 +  if (Name[0] != kDot)
    1.38 +    return false;
    1.39 +  return Name.Length() == 1 || (Name[1] == kDot && Name.Length() == 2);
    1.40 +}
    1.41 +#endif
    1.42 +
    1.43 +static void ConvertWIN32_FIND_DATA_To_FileInfo(
    1.44 +    const WIN32_FIND_DATA &findData,
    1.45 +    CFileInfo &fileInfo)
    1.46 +{
    1.47 +  fileInfo.Attributes = findData.dwFileAttributes; 
    1.48 +  fileInfo.CreationTime = findData.ftCreationTime;  
    1.49 +  fileInfo.LastAccessTime = findData.ftLastAccessTime; 
    1.50 +  fileInfo.LastWriteTime = findData.ftLastWriteTime;
    1.51 +  fileInfo.Size  = (((UInt64)findData.nFileSizeHigh) << 32) + findData.nFileSizeLow; 
    1.52 +  fileInfo.Name = findData.cFileName;
    1.53 +  #ifndef _WIN32_WCE
    1.54 +  fileInfo.ReparseTag = findData.dwReserved0;
    1.55 +  #else
    1.56 +  fileInfo.ObjectID = findData.dwOID;
    1.57 +  #endif
    1.58 +}
    1.59 +
    1.60 +#ifndef _UNICODE
    1.61 +
    1.62 +static inline UINT GetCurrentCodePage() { return ::AreFileApisANSI() ? CP_ACP : CP_OEMCP; } 
    1.63 +
    1.64 +static void ConvertWIN32_FIND_DATA_To_FileInfo(
    1.65 +    const WIN32_FIND_DATAW &findData,
    1.66 +    CFileInfoW &fileInfo)
    1.67 +{
    1.68 +  fileInfo.Attributes = findData.dwFileAttributes; 
    1.69 +  fileInfo.CreationTime = findData.ftCreationTime;  
    1.70 +  fileInfo.LastAccessTime = findData.ftLastAccessTime; 
    1.71 +  fileInfo.LastWriteTime = findData.ftLastWriteTime;
    1.72 +  fileInfo.Size  = (((UInt64)findData.nFileSizeHigh) << 32) + findData.nFileSizeLow; 
    1.73 +  fileInfo.Name = findData.cFileName;
    1.74 +  #ifndef _WIN32_WCE
    1.75 +  fileInfo.ReparseTag = findData.dwReserved0;
    1.76 +  #else
    1.77 +  fileInfo.ObjectID = findData.dwOID;
    1.78 +  #endif
    1.79 +}
    1.80 +
    1.81 +static void ConvertWIN32_FIND_DATA_To_FileInfo(
    1.82 +    const WIN32_FIND_DATA &findData,
    1.83 +    CFileInfoW &fileInfo)
    1.84 +{
    1.85 +  fileInfo.Attributes = findData.dwFileAttributes; 
    1.86 +  fileInfo.CreationTime = findData.ftCreationTime;  
    1.87 +  fileInfo.LastAccessTime = findData.ftLastAccessTime; 
    1.88 +  fileInfo.LastWriteTime = findData.ftLastWriteTime;
    1.89 +  fileInfo.Size  = (((UInt64)findData.nFileSizeHigh) << 32) + findData.nFileSizeLow; 
    1.90 +  fileInfo.Name = GetUnicodeString(findData.cFileName, GetCurrentCodePage());
    1.91 +  #ifndef _WIN32_WCE
    1.92 +  fileInfo.ReparseTag = findData.dwReserved0;
    1.93 +  #else
    1.94 +  fileInfo.ObjectID = findData.dwOID;
    1.95 +  #endif
    1.96 +}
    1.97 +#endif
    1.98 +  
    1.99 +////////////////////////////////
   1.100 +// CFindFile
   1.101 +
   1.102 +bool CFindFile::Close()
   1.103 +{
   1.104 +  if(!_handleAllocated)
   1.105 +    return true;
   1.106 +  bool result = BOOLToBool(::FindClose(_handle));
   1.107 +  _handleAllocated = !result;
   1.108 +  return result;
   1.109 +}
   1.110 +           
   1.111 +bool CFindFile::FindFirst(LPCTSTR wildcard, CFileInfo &fileInfo)
   1.112 +{
   1.113 +  Close();
   1.114 +  WIN32_FIND_DATA findData;
   1.115 +  _handle = ::FindFirstFile(wildcard, &findData);
   1.116 +  if (_handleAllocated = (_handle != INVALID_HANDLE_VALUE))
   1.117 +    ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
   1.118 +  return _handleAllocated;
   1.119 +}
   1.120 +
   1.121 +#ifndef _UNICODE
   1.122 +bool CFindFile::FindFirst(LPCWSTR wildcard, CFileInfoW &fileInfo)
   1.123 +{
   1.124 +  Close();
   1.125 +  if (g_IsNT)
   1.126 +  {
   1.127 +    WIN32_FIND_DATAW findData;
   1.128 +    _handle = ::FindFirstFileW(wildcard, &findData);
   1.129 +    if (_handleAllocated = (_handle != INVALID_HANDLE_VALUE))
   1.130 +      ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
   1.131 +  }
   1.132 +  else
   1.133 +  {
   1.134 +    WIN32_FIND_DATAA findData;
   1.135 +    _handle = ::FindFirstFileA(UnicodeStringToMultiByte(wildcard, 
   1.136 +        GetCurrentCodePage()), &findData);
   1.137 +    if (_handleAllocated = (_handle != INVALID_HANDLE_VALUE))
   1.138 +      ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
   1.139 +  }
   1.140 +  return _handleAllocated;
   1.141 +}
   1.142 +#endif
   1.143 +
   1.144 +bool CFindFile::FindNext(CFileInfo &fileInfo)
   1.145 +{
   1.146 +  WIN32_FIND_DATA findData;
   1.147 +  bool result = BOOLToBool(::FindNextFile(_handle, &findData));
   1.148 +  if (result)
   1.149 +    ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
   1.150 +  return result;
   1.151 +}
   1.152 +
   1.153 +#ifndef _UNICODE
   1.154 +bool CFindFile::FindNext(CFileInfoW &fileInfo)
   1.155 +{
   1.156 +  if (g_IsNT)
   1.157 +  {
   1.158 +    WIN32_FIND_DATAW findData;
   1.159 +    if (!::FindNextFileW(_handle, &findData))
   1.160 +      return false;
   1.161 +    ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
   1.162 +  }
   1.163 +  else
   1.164 +  {
   1.165 +    WIN32_FIND_DATAA findData;
   1.166 +    if (!::FindNextFileA(_handle, &findData))
   1.167 +      return false;
   1.168 +    ConvertWIN32_FIND_DATA_To_FileInfo(findData, fileInfo);
   1.169 +  }
   1.170 +  return true;
   1.171 +}
   1.172 +#endif
   1.173 +
   1.174 +bool FindFile(LPCTSTR wildcard, CFileInfo &fileInfo)
   1.175 +{
   1.176 +  CFindFile finder;
   1.177 +  return finder.FindFirst(wildcard, fileInfo);
   1.178 +}
   1.179 +
   1.180 +#ifndef _UNICODE
   1.181 +bool FindFile(LPCWSTR wildcard, CFileInfoW &fileInfo)
   1.182 +{
   1.183 +  CFindFile finder;
   1.184 +  return finder.FindFirst(wildcard, fileInfo);
   1.185 +}
   1.186 +#endif
   1.187 +
   1.188 +bool DoesFileExist(LPCTSTR name)
   1.189 +{
   1.190 +  CFileInfo fileInfo;
   1.191 +  return FindFile(name, fileInfo);
   1.192 +}
   1.193 +
   1.194 +#ifndef _UNICODE
   1.195 +bool DoesFileExist(LPCWSTR name)
   1.196 +{
   1.197 +  CFileInfoW fileInfo;
   1.198 +  return FindFile(name, fileInfo);
   1.199 +}
   1.200 +#endif
   1.201 +
   1.202 +/////////////////////////////////////
   1.203 +// CEnumerator
   1.204 +
   1.205 +bool CEnumerator::NextAny(CFileInfo &fileInfo)
   1.206 +{
   1.207 +  if(_findFile.IsHandleAllocated())
   1.208 +    return _findFile.FindNext(fileInfo);
   1.209 +  else
   1.210 +    return _findFile.FindFirst(_wildcard, fileInfo);
   1.211 +}
   1.212 +
   1.213 +bool CEnumerator::Next(CFileInfo &fileInfo)
   1.214 +{
   1.215 +  while(true)
   1.216 +  {
   1.217 +    if(!NextAny(fileInfo))
   1.218 +      return false;
   1.219 +    if(!fileInfo.IsDots())
   1.220 +      return true;
   1.221 +  }
   1.222 +}
   1.223 +
   1.224 +bool CEnumerator::Next(CFileInfo &fileInfo, bool &found)
   1.225 +{
   1.226 +  if (Next(fileInfo))
   1.227 +  {
   1.228 +    found = true;
   1.229 +    return true;
   1.230 +  }
   1.231 +  found = false;
   1.232 +  return (::GetLastError() == ERROR_NO_MORE_FILES);
   1.233 +}
   1.234 +
   1.235 +#ifndef _UNICODE
   1.236 +bool CEnumeratorW::NextAny(CFileInfoW &fileInfo)
   1.237 +{
   1.238 +  if(_findFile.IsHandleAllocated())
   1.239 +    return _findFile.FindNext(fileInfo);
   1.240 +  else
   1.241 +    return _findFile.FindFirst(_wildcard, fileInfo);
   1.242 +}
   1.243 +
   1.244 +bool CEnumeratorW::Next(CFileInfoW &fileInfo)
   1.245 +{
   1.246 +  while(true)
   1.247 +  {
   1.248 +    if(!NextAny(fileInfo))
   1.249 +      return false;
   1.250 +    if(!fileInfo.IsDots())
   1.251 +      return true;
   1.252 +  }
   1.253 +}
   1.254 +
   1.255 +bool CEnumeratorW::Next(CFileInfoW &fileInfo, bool &found)
   1.256 +{
   1.257 +  if (Next(fileInfo))
   1.258 +  {
   1.259 +    found = true;
   1.260 +    return true;
   1.261 +  }
   1.262 +  found = false;
   1.263 +  return (::GetLastError() == ERROR_NO_MORE_FILES);
   1.264 +}
   1.265 +
   1.266 +#endif
   1.267 +
   1.268 +////////////////////////////////
   1.269 +// CFindChangeNotification
   1.270 +
   1.271 +bool CFindChangeNotification::Close()
   1.272 +{
   1.273 +  if(_handle == INVALID_HANDLE_VALUE || _handle == 0)
   1.274 +    return true;
   1.275 +  bool result = BOOLToBool(::FindCloseChangeNotification(_handle));
   1.276 +  if (result)
   1.277 +    _handle = INVALID_HANDLE_VALUE;
   1.278 +  return result;
   1.279 +}
   1.280 +           
   1.281 +HANDLE CFindChangeNotification::FindFirst(LPCTSTR pathName, bool watchSubtree, 
   1.282 +    DWORD notifyFilter)
   1.283 +{
   1.284 +  _handle = ::FindFirstChangeNotification(pathName, 
   1.285 +      BoolToBOOL(watchSubtree), notifyFilter);
   1.286 +  return _handle;
   1.287 +}
   1.288 +
   1.289 +#ifndef _UNICODE
   1.290 +HANDLE CFindChangeNotification::FindFirst(LPCWSTR pathName, bool watchSubtree, 
   1.291 +    DWORD notifyFilter)
   1.292 +{
   1.293 +  if (g_IsNT)
   1.294 +    return (_handle = ::FindFirstChangeNotificationW(pathName, BoolToBOOL(watchSubtree), notifyFilter));
   1.295 +  return FindFirst(UnicodeStringToMultiByte(pathName, GetCurrentCodePage()), watchSubtree, notifyFilter);
   1.296 +}
   1.297 +#endif
   1.298 +
   1.299 +#ifndef _WIN32_WCE
   1.300 +bool MyGetLogicalDriveStrings(CSysStringVector &driveStrings)
   1.301 +{
   1.302 +  driveStrings.Clear();
   1.303 +  UINT32 size = GetLogicalDriveStrings(0, NULL); 
   1.304 +  if(size == 0)
   1.305 +    return false;
   1.306 +  CSysString buffer;
   1.307 +  UINT32 newSize = GetLogicalDriveStrings(size, buffer.GetBuffer(size)); 
   1.308 +  if(newSize == 0)
   1.309 +    return false;
   1.310 +  if(newSize > size)
   1.311 +    return false;
   1.312 +  CSysString string;
   1.313 +  for(UINT32 i = 0; i < newSize; i++)
   1.314 +  {
   1.315 +    TCHAR c = buffer[i];
   1.316 +    if(c == TEXT('\0'))
   1.317 +    {
   1.318 +      driveStrings.Add(string);
   1.319 +      string.Empty();
   1.320 +    }
   1.321 +    else
   1.322 +      string += c;
   1.323 +  }
   1.324 +  if(!string.IsEmpty())
   1.325 +    return false;
   1.326 +  return true;
   1.327 +}
   1.328 +
   1.329 +#ifndef _UNICODE
   1.330 +bool MyGetLogicalDriveStrings(UStringVector &driveStrings)
   1.331 +{
   1.332 +  driveStrings.Clear();
   1.333 +  if (g_IsNT)
   1.334 +  {
   1.335 +    UINT32 size = GetLogicalDriveStringsW(0, NULL); 
   1.336 +    if (size == 0)
   1.337 +      return false;
   1.338 +    UString buffer;
   1.339 +    UINT32 newSize = GetLogicalDriveStringsW(size, buffer.GetBuffer(size)); 
   1.340 +    if(newSize == 0)
   1.341 +      return false;
   1.342 +    if(newSize > size)
   1.343 +      return false;
   1.344 +    UString string;
   1.345 +    for(UINT32 i = 0; i < newSize; i++)
   1.346 +    {
   1.347 +      WCHAR c = buffer[i];
   1.348 +      if(c == L'\0')
   1.349 +      {
   1.350 +        driveStrings.Add(string);
   1.351 +        string.Empty();
   1.352 +      }
   1.353 +      else
   1.354 +        string += c;
   1.355 +    }
   1.356 +    return string.IsEmpty();
   1.357 +  }
   1.358 +  CSysStringVector driveStringsA;
   1.359 +  bool res = MyGetLogicalDriveStrings(driveStringsA);
   1.360 +  for (int i = 0; i < driveStringsA.Size(); i++)
   1.361 +    driveStrings.Add(GetUnicodeString(driveStringsA[i]));
   1.362 +  return res;
   1.363 +}
   1.364 +#endif
   1.365 +
   1.366 +#endif
   1.367 +
   1.368 +}}}

mercurial