fastfile.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. //----------------------------------------------------------------------------
  5. // Global Fast File Code.
  6. //
  7. #include "heap.h"
  8. #include "fastfile.h"
  9. #include <ctype.h>
  10. long ffLastError = 0;
  11. #define NO_ERR 0
  12. //-----------------------------------------------------------------------------------
  13. bool FastFileInit (char *fname)
  14. {
  15. if (numFastFiles == maxFastFiles)
  16. {
  17. ffLastError = -1;
  18. return FALSE;
  19. }
  20. //-----------------------------------------------------------------------------
  21. //-- Open this fast file, add it to the list O pointers and return TRUE if OK!
  22. fastFiles[numFastFiles] = new FastFile;
  23. long result = fastFiles[numFastFiles]->open(fname);
  24. if (result == FASTFILE_VERSION)
  25. {
  26. ffLastError = result;
  27. return FALSE;
  28. }
  29. numFastFiles++;
  30. return TRUE;
  31. }
  32. //-----------------------------------------------------------------------------------
  33. void FastFileFini (void)
  34. {
  35. if (fastFiles)
  36. {
  37. long currentFastFile = 0;
  38. while (currentFastFile < maxFastFiles)
  39. {
  40. if (fastFiles[currentFastFile])
  41. {
  42. fastFiles[currentFastFile]->close();
  43. delete fastFiles[currentFastFile];
  44. fastFiles[currentFastFile] = NULL;
  45. }
  46. currentFastFile++;
  47. }
  48. }
  49. free(fastFiles);
  50. fastFiles = NULL;
  51. numFastFiles= 0;
  52. }
  53. //-----------------------------------------------------------------------------------
  54. FastFile *FastFileFind (char *fname, long &fastFileHandle)
  55. {
  56. if (fastFiles)
  57. {
  58. DWORD thisHash = elfHash(fname);
  59. long currentFastFile = 0;
  60. long tempHandle = -1;
  61. while (currentFastFile < numFastFiles)
  62. {
  63. tempHandle = fastFiles[currentFastFile]->openFast(thisHash,fname);
  64. if (tempHandle != -1)
  65. {
  66. fastFileHandle = tempHandle;
  67. return fastFiles[currentFastFile];
  68. }
  69. currentFastFile++;
  70. }
  71. }
  72. return NULL;
  73. }
  74. //------------------------------------------------------------------
  75. DWORD elfHash (char *name)
  76. {
  77. unsigned long h = 0, g;
  78. while ( *name )
  79. {
  80. h = ( h << 4 ) + *name++;
  81. if ( g = h & 0xF0000000 )
  82. h ^= g >> 24;
  83. h &= ~g;
  84. }
  85. return h;
  86. }
  87. //-----------------------------------------------------------------------------------