Directory.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  24. #if (0)
  25. #include "Common/Directory.h"
  26. //-------------------------------------------------------------------------------------------------
  27. static void TimetToFileTime( time_t t, FILETIME& ft )
  28. {
  29. LONGLONG ll = Int32x32To64(t, 10000000) + 116444736000000000;
  30. ft.dwLowDateTime = (DWORD) ll;
  31. ft.dwHighDateTime = ll >>32;
  32. }
  33. static time_t FileTimeToTimet( const FILETIME& ft )
  34. {
  35. LONGLONG ll = (ft.dwHighDateTime << 32) + ft.dwLowDateTime - 116444736000000000;
  36. ll /= 10000000;
  37. return (time_t)ll;
  38. }
  39. //-------------------------------------------------------------------------------------------------
  40. void FileInfo::set( const WIN32_FIND_DATA& info )
  41. {
  42. filename = info.cFileName;
  43. creationTime = FileTimeToTimet(info.ftCreationTime);
  44. accessTime = FileTimeToTimet(info.ftLastAccessTime);
  45. modTime = FileTimeToTimet(info.ftLastWriteTime);
  46. attributes = info.dwFileAttributes;
  47. filesize = info.nFileSizeLow;
  48. //DEBUG_LOG(("FileInfo::set(): fname=%s, size=%d\n", filename.str(), filesize));
  49. }
  50. //-------------------------------------------------------------------------------------------------
  51. Directory::Directory( const AsciiString& dirPath ) : m_dirPath(dirPath)
  52. {
  53. WIN32_FIND_DATA item; // search item
  54. HANDLE hFile; // handle for search resources
  55. char currDir[ MAX_PATH ];
  56. // sanity
  57. if( m_dirPath.isEmpty() )
  58. {
  59. DEBUG_LOG(( "Empty dirname\n"));
  60. return;
  61. }
  62. // save the current directory
  63. GetCurrentDirectory( MAX_PATH, currDir );
  64. // switch into the directory provided
  65. if( SetCurrentDirectory( m_dirPath.str() ) == 0 )
  66. {
  67. DEBUG_LOG(( "Can't set directory '%s'\n", m_dirPath.str() ));
  68. return;
  69. }
  70. // go through each item in the output directory
  71. bool done = false;
  72. hFile = FindFirstFile( "*", &item);
  73. if( hFile == INVALID_HANDLE_VALUE )
  74. {
  75. DEBUG_LOG(( "Can't search directory '%s'\n", m_dirPath.str() ));
  76. done = true;
  77. }
  78. FileInfo info;
  79. while (!done)
  80. {
  81. // if this is a subdirectory keep the name around till the end
  82. if( BitTest( item.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY ) )
  83. {
  84. if ( strcmp( item.cFileName, "." ) && strcmp( item.cFileName, ".." ) )
  85. {
  86. info.set(item);
  87. m_subdirs.insert( info );
  88. }
  89. }
  90. else
  91. {
  92. info.set(item);
  93. m_files.insert( info );
  94. }
  95. if ( FindNextFile( hFile, &item ) == 0 )
  96. {
  97. done = true;
  98. }
  99. }
  100. // close search
  101. FindClose( hFile );
  102. // restore the working directory to what it was when we started here
  103. SetCurrentDirectory( currDir );
  104. }
  105. FileInfoSet* Directory::getFiles( void )
  106. {
  107. return &m_files;
  108. }
  109. FileInfoSet* Directory::getSubdirs( void )
  110. {
  111. return &m_subdirs;
  112. }
  113. #endif