FileHelper.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /******************************************************************************
  2. |* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  3. |* ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  4. |* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  5. |* PARTICULAR PURPOSE.
  6. |*
  7. |* Copyright 1995-2005 Nero AG. All Rights Reserved.
  8. |*-----------------------------------------------------------------------------
  9. |* PROJECT: Nero Plugin Manager Example
  10. |*
  11. |* FILE: FileHelper.cpp
  12. |*
  13. |* PURPOSE: Implementation of helper functions for file access
  14. ******************************************************************************/
  15. #include "stdafx.h"
  16. #include "FileHelper.h"
  17. #ifdef _DEBUG
  18. # define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22. // Returns the current position in the file.
  23. DWORD GetFilePointer(HANDLE hFile)
  24. {
  25. ASSERT(hFile != INVALID_HANDLE_VALUE);
  26. return SetFilePointer(hFile, 0, 0, FILE_CURRENT);
  27. }
  28. // Reads a DWORD from a file, if it's not possible throws an exception. This
  29. // allows to scan the file with multiple read without checking after every read.
  30. void ReadDWORD(void* pObject, HANDLE hFile)
  31. {
  32. if(!(pObject && hFile))
  33. {
  34. ASSERT(FALSE);
  35. throw FALSE;
  36. }
  37. DWORD dwRead = 0;
  38. if (!(ReadFile(hFile, pObject, sizeof(DWORD), &dwRead, NULL) &&
  39. dwRead == sizeof(DWORD)))
  40. {
  41. throw FALSE;
  42. }
  43. }
  44. // Reads a WORD from a file, if it's not possible throws an exception. This
  45. // allows to scan the file with multiple read without checking after every read.
  46. void ReadWORD(void* pObject, HANDLE hFile)
  47. {
  48. if(!(pObject && hFile))
  49. {
  50. ASSERT(FALSE);
  51. throw FALSE;
  52. }
  53. DWORD dwRead = 0;
  54. if (!(ReadFile(hFile, pObject, sizeof(WORD), &dwRead, NULL) &&
  55. dwRead == sizeof(WORD)))
  56. {
  57. throw FALSE;
  58. }
  59. }
  60. // Reads a DWORD from the file and aligns it to a 2-byte border since the chunk
  61. // length must be aligned to word size.
  62. DWORD ReadChunkLen(HANDLE hFile)
  63. {
  64. ASSERT(hFile);
  65. DWORD dwLen = 0;
  66. ReadDWORD(&dwLen, hFile);
  67. if (dwLen % 2)
  68. {
  69. dwLen++;
  70. }
  71. return dwLen;
  72. }
  73. // Writes a byte in the file.
  74. void WriteZeroByte(HANDLE hFile)
  75. {
  76. if(hFile)
  77. {
  78. BYTE bt = 0;
  79. DWORD dwWritten = 0;
  80. WriteFile(hFile, &bt, 1, &dwWritten, NULL);
  81. }
  82. else
  83. {
  84. ASSERT(FALSE);
  85. }
  86. }