dcbase.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "pch.h"
  2. ////////////////////////////////////////////////////////////////////////////////
  3. //
  4. // ZFile
  5. //
  6. ////////////////////////////////////////////////////////////////////////////////
  7. ZFile::ZFile(const PathString& strPath, DWORD how) :
  8. m_p(NULL)
  9. {
  10. WCHAR wszPath[MAX_PATH];
  11. mbstowcs(wszPath, (LPCSTR)strPath, MAX_PATH);
  12. if (INVALID_HANDLE_VALUE == (m_handle = CreateFileForMapping( wszPath, GENERIC_READ, FILE_SHARE_READ, NULL,
  13. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)))
  14. {
  15. ZString str = "Failed to open file: " + strPath + " (code = " + ZString((int)GetLastError()) + ")\r\n";
  16. debugf(str);
  17. }
  18. else
  19. {
  20. ZString str = "Opened file: " + strPath + "\r\n";
  21. debugf(str);
  22. }
  23. }
  24. ZFile::~ZFile()
  25. {
  26. if (m_p) {
  27. UnmapViewOfFile(m_p);
  28. CloseHandle(m_hfileMapping);
  29. }
  30. }
  31. bool ZFile::IsValid()
  32. {
  33. return m_handle != (HANDLE)-1;
  34. }
  35. DWORD ZFile::Read(void* p, DWORD length)
  36. {
  37. DWORD cbActual;
  38. ReadFile(m_handle, p, length, (LPDWORD)&cbActual, NULL);
  39. return cbActual;
  40. }
  41. DWORD ZFile::Write(void* p, DWORD length)
  42. {
  43. DWORD cbActual;
  44. WriteFile(m_handle, p, length, (LPDWORD)&cbActual, NULL);
  45. ZAssert(cbActual == length);
  46. return cbActual;
  47. }
  48. bool ZFile::Write(const ZString& str)
  49. {
  50. return (Write((void*)(PCC)str, str.GetLength()) != 0);
  51. }
  52. bool ZFile::WriteString(const ZString& str)
  53. {
  54. return (Write((void*)(PCC)str, str.GetLength() + 1) != 0);
  55. }
  56. bool ZFile::Write(DWORD value)
  57. {
  58. return (Write(&value, 4) != 0);
  59. }
  60. bool ZFile::Write(int value)
  61. {
  62. return (Write(&value, 4) != 0);
  63. }
  64. bool ZFile::Write(float value)
  65. {
  66. return (Write(&value, 4) != 0);
  67. }
  68. int ZFile::GetLength()
  69. {
  70. return GetFileSize(m_handle, NULL);
  71. }
  72. void* ZFile::GetPointer()
  73. {
  74. if (m_p != NULL) {
  75. return m_p;
  76. }
  77. m_hfileMapping = CreateFileMapping(m_handle, 0, PAGE_READONLY, 0, 0, NULL);
  78. if (m_hfileMapping == (HANDLE)-1) {
  79. ZError("Can't map file");
  80. return NULL;
  81. }
  82. m_p = MapViewOfFile(m_hfileMapping, FILE_MAP_READ, 0, 0, 0);
  83. return m_p;
  84. }
  85. /*
  86. void* m_p;
  87. char* m_pRead;
  88. DWORD m_dwSize;
  89. DWORD m_dwRead;
  90. ////////////////////////////////////////////////////////////////////////////////
  91. //
  92. // ZFile
  93. //
  94. ////////////////////////////////////////////////////////////////////////////////
  95. ZFile::ZFile(const PathString& strPath, DWORD how) :
  96. m_p(NULL),
  97. m_pRead(NULL),
  98. m_dwSize(0),
  99. m_dwRead(0)
  100. {
  101. HRSRC hrsrc;
  102. HGLOBAL hglobal;
  103. TCHAR outbuf[256];
  104. ZString strFile = strPath;
  105. if (!strPath.GetExtension().IsEmpty())
  106. strFile = strFile.LeftOf(strPath.GetExtension().GetLength()+1) + strPath.GetExtension();
  107. WCHAR wszFile[MAX_PATH];
  108. mbstowcs(wszFile, (LPCSTR)strFile, MAX_PATH);
  109. // Handles obtained through FindResource and LoadResource do not have
  110. // to be freed or released. This is done automatically.
  111. hrsrc = FindResource (GetModuleHandle(NULL), wszFile, RT_RCDATA);
  112. if (hrsrc == NULL)
  113. {
  114. wsprintf (outbuf, TEXT("ERROR: Could not find resource \"%s\".\r\n"), wszFile);
  115. OutputDebugString (outbuf);
  116. return;
  117. }
  118. m_dwSize = SizeofResource(GetModuleHandle(NULL), hrsrc);
  119. // Load the resource.
  120. wsprintf (outbuf, TEXT("Loading the resource \"%s\".\r\n"), wszFile);
  121. OutputDebugString (outbuf);
  122. hglobal = LoadResource (GetModuleHandle(NULL), hrsrc);
  123. if (hglobal == NULL)
  124. {
  125. wsprintf (outbuf, TEXT("ERROR: Could not load the resource \"%s\".\r\n"), wszFile);
  126. OutputDebugString (outbuf);
  127. return;
  128. }
  129. // Get pointer to raw data in resource.
  130. m_p = LockResource (hglobal);
  131. m_pRead = (char*)m_p;
  132. }
  133. ZFile::~ZFile()
  134. {
  135. }
  136. bool ZFile::IsValid()
  137. {
  138. return m_p != NULL;
  139. }
  140. DWORD ZFile::Read(void* p, DWORD length)
  141. {
  142. DWORD cbActual = length < (m_dwSize - m_dwRead) ? length : (m_dwSize - m_dwRead);
  143. if (p)
  144. {
  145. memcpy(p, m_pRead, cbActual);
  146. m_dwRead += cbActual;
  147. m_pRead += cbActual;
  148. }
  149. return cbActual;
  150. }
  151. DWORD ZFile::Write(void* p, DWORD length)
  152. {
  153. return 0;
  154. }
  155. bool ZFile::Write(const ZString& str)
  156. {
  157. return 0;
  158. }
  159. bool ZFile::WriteString(const ZString& str)
  160. {
  161. return 0;
  162. }
  163. bool ZFile::Write(DWORD value)
  164. {
  165. return 0;
  166. }
  167. bool ZFile::Write(int value)
  168. {
  169. return 0;
  170. }
  171. bool ZFile::Write(float value)
  172. {
  173. return 0;
  174. }
  175. int ZFile::GetLength()
  176. {
  177. return (int)m_dwSize;
  178. }
  179. void* ZFile::GetPointer()
  180. {
  181. return m_p;
  182. }
  183. */