Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.2 KiB

  1. /***********************************************************************************************\
  2. * FILE: CraneFl.c
  3. *
  4. * Code to load crane data ( QHEAD and QNODE ) from a file.
  5. \***********************************************************************************************/
  6. #include <stdio.h>
  7. #include "common.h"
  8. #include "crane.h"
  9. #include "cranep.h"
  10. // Load crane data from a file.
  11. BOOL CraneLoadFile(
  12. LOCRUN_INFO *pLocRunInfo,
  13. CRANE_LOAD_INFO *pLoadInfo,
  14. wchar_t *pwchPathName
  15. ) {
  16. HANDLE hFile, hMap;
  17. BYTE *pByte;
  18. wchar_t aPath[128];
  19. // Generate path to file.
  20. FormatPath(aPath, pwchPathName, (wchar_t *)0, (wchar_t *)0, (wchar_t *)0, L"cart.dat");
  21. hFile = CreateMappingCall(
  22. aPath,
  23. GENERIC_READ,
  24. FILE_SHARE_READ,
  25. NULL,
  26. OPEN_EXISTING,
  27. FILE_ATTRIBUTE_NORMAL,
  28. NULL
  29. );
  30. if (hFile == INVALID_HANDLE_VALUE)
  31. {
  32. goto error1;
  33. }
  34. // Create a mapping handle
  35. hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  36. if (hMap == NULL)
  37. {
  38. goto error2;
  39. }
  40. // Map the entire file starting at the first byte
  41. pByte = (BYTE *) MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
  42. if ( !pByte )
  43. {
  44. goto error3;
  45. }
  46. if ( !CraneLoadFromPointer(pLocRunInfo, gapqhList, gapqnList, pByte) )
  47. {
  48. goto error4;
  49. }
  50. // Save away the pointers so we can close up cleanly latter
  51. pLoadInfo->pLoadInfo1 = hFile;
  52. pLoadInfo->pLoadInfo2 = hMap;
  53. pLoadInfo->pLoadInfo3 = pByte;
  54. return TRUE;
  55. // Error handling
  56. error4:
  57. UnmapViewOfFile(pByte);
  58. pByte = NULL;
  59. error3:
  60. CloseHandle(hMap);
  61. hMap = INVALID_HANDLE_VALUE;
  62. error2:
  63. CloseHandle(hFile);
  64. hFile = INVALID_HANDLE_VALUE;
  65. error1:
  66. pLoadInfo->pLoadInfo1 = INVALID_HANDLE_VALUE;
  67. pLoadInfo->pLoadInfo2 = INVALID_HANDLE_VALUE;
  68. pLoadInfo->pLoadInfo3 = INVALID_HANDLE_VALUE;
  69. return FALSE;
  70. }
  71. BOOL CraneUnLoadFile(CRANE_LOAD_INFO *pInfo)
  72. {
  73. if (pInfo->pLoadInfo1 == INVALID_HANDLE_VALUE ||
  74. pInfo->pLoadInfo2 == INVALID_HANDLE_VALUE ||
  75. pInfo->pLoadInfo3 == INVALID_HANDLE_VALUE) {
  76. return FALSE;
  77. }
  78. UnmapViewOfFile(pInfo->pLoadInfo3);
  79. CloseHandle(pInfo->pLoadInfo2);
  80. CloseHandle(pInfo->pLoadInfo1);
  81. pInfo->pLoadInfo1 = INVALID_HANDLE_VALUE;
  82. pInfo->pLoadInfo2 = INVALID_HANDLE_VALUE;
  83. pInfo->pLoadInfo3 = INVALID_HANDLE_VALUE;
  84. return TRUE;
  85. }