ODIR.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename : ODIR.CPP
  21. //Description : Object Directory
  22. #include <string.h>
  23. #include <windows.h>
  24. #include <ODIR.h>
  25. //----------- Define static function ------------//
  26. static int sort_file_function( const void *a, const void *b );
  27. //------- Begin of function Directory::Directory -------//
  28. Directory::Directory() : DynArray( sizeof(FileInfo), 20 )
  29. {
  30. }
  31. //-------- End of function Directory::Directory -------//
  32. //------- Begin of function Directory::read -------//
  33. //
  34. // Read in the file list of the specified file spec.
  35. //
  36. // <char*> fileSpec = the file spec of the directory
  37. // [int] sortName = sort the file list by file name
  38. // (default : 0)
  39. //
  40. // return : <int> the no. of files matched the file spec.
  41. //
  42. int Directory::read(char *fileSpec, int sortName)
  43. {
  44. FileInfo fileInfo;
  45. WIN32_FIND_DATA findData;
  46. //----------- get the file list -------------//
  47. HANDLE findHandle = FindFirstFile( fileSpec, &findData );
  48. while(findHandle!=INVALID_HANDLE_VALUE)
  49. {
  50. m.extract_file_name( fileInfo.name, findData.cFileName ); // get the file name only from a full path string
  51. fileInfo.size = findData.nFileSizeLow;
  52. fileInfo.time = findData.ftLastWriteTime;
  53. linkin( &fileInfo );
  54. if( !FindNextFile( findHandle, &findData ) )
  55. break;
  56. }
  57. FindClose(findHandle);
  58. //------ the file list by file name ---------//
  59. if( sortName )
  60. quick_sort( sort_file_function );
  61. return size(); // DynArray::size()
  62. }
  63. //-------- End of function Directory::read -------//
  64. //------ Begin of function sort_file_function ------//
  65. //
  66. static int sort_file_function( const void *a, const void *b )
  67. {
  68. return strcmpi( ((FileInfo*)a)->name, ((FileInfo*)b)->name );
  69. }
  70. //------- End of function sort_file_function ------//