Leaked source code of windows server 2003
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.

288 lines
6.1 KiB

  1. /*++
  2. Copyright (C) 1998-2001 Microsoft Corporation
  3. Module Name:
  4. DataSrc.CPP
  5. Abstract:
  6. Implements DataSrc objects.
  7. History:
  8. a-davj 21-Dec-99 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include "DataSrc.h"
  12. #include <wbemcli.h>
  13. #include <malloc.h>
  14. #include <autoptr.h>
  15. #define HR_LASTERR MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, GetLastError() )
  16. FileDataSrc::FileDataSrc(TCHAR * pFileName)
  17. {
  18. m_fp = NULL;
  19. m_pFileName = NULL;
  20. m_iFilePos = -1; // forces a read
  21. m_iToFar = 0;
  22. if(pFileName == NULL)
  23. {
  24. m_iStatus = WBEM_E_INVALID_PARAMETER;
  25. return;
  26. }
  27. DWORD dwLen = lstrlen(pFileName) + 1;
  28. wmilib::auto_buffer<WCHAR> pTmpFileName( new TCHAR[dwLen]);
  29. if(NULL == pTmpFileName.get())
  30. {
  31. m_iStatus = WBEM_E_OUT_OF_MEMORY;
  32. return;
  33. }
  34. StringCchCopyW(pTmpFileName.get(), dwLen, pFileName);
  35. m_fp = _wfopen( pFileName, L"rb");
  36. OnDeleteIf<FILE *,int(__cdecl *)(FILE *),fclose> closeme(m_fp);
  37. if(m_fp == NULL)
  38. {
  39. m_iStatus = WBEM_E_OUT_OF_MEMORY;
  40. return;
  41. }
  42. // calculate the file size. Note that the number is the size in unicode words, not bytes.
  43. int ret = fseek(m_fp, 0, SEEK_END);
  44. if(ret) throw GenericException(ret);
  45. m_iSize = ftell(m_fp)/2; // add a bit extra for ending space and null NULL
  46. ret = fseek(m_fp, 0, SEEK_SET);
  47. if( ret ) throw GenericException(ret);
  48. m_iPos = -1;
  49. closeme.dismiss();
  50. m_pFileName = pTmpFileName.release();
  51. }
  52. FileDataSrc::~FileDataSrc()
  53. {
  54. if(m_fp)
  55. fclose(m_fp);
  56. DeleteFile(m_pFileName);
  57. delete m_pFileName;
  58. }
  59. wchar_t FileDataSrc::GetAt(int nOffset)
  60. {
  61. wchar_t tRet;
  62. int iPos = m_iPos + nOffset;
  63. if(iPos >= m_iFilePos && iPos < m_iToFar)
  64. return m_Buff[iPos - m_iFilePos];
  65. Move(nOffset);
  66. tRet = m_Buff[m_iPos - m_iFilePos];
  67. Move(-nOffset);
  68. return tRet;
  69. }
  70. void FileDataSrc::Move(int n)
  71. {
  72. m_iPos += n;
  73. // if m_iPos is in range, then all is well
  74. if(m_iPos >= m_iFilePos && m_iPos < m_iToFar && m_iFilePos >= 0)
  75. return;
  76. // if m_iPos is not even in the file, bail out
  77. if(m_iPos >= 0 && m_iPos < m_iSize)
  78. UpdateBuffer();
  79. return;
  80. }
  81. int FileDataSrc::MoveToPos(int n)
  82. {
  83. m_iPos = n;
  84. // if m_iPos is in range, then all is well
  85. if(m_iPos >= m_iFilePos && m_iPos < m_iToFar && m_iFilePos >= 0)
  86. return -1;
  87. // if m_iPos is not even in the file, bail out
  88. if(m_iPos >= 0 && m_iPos < m_iSize)
  89. UpdateBuffer();
  90. return n;
  91. }
  92. void FileDataSrc::UpdateBuffer()
  93. {
  94. int numRead, ret;
  95. // buffer needs to be read. Determine the starting and ending points
  96. m_iFilePos = m_iPos - 1000;
  97. if(m_iFilePos < 0)
  98. m_iFilePos = 0;
  99. int iReadLen = 10000;
  100. if(iReadLen + m_iFilePos > m_iSize)
  101. iReadLen = m_iSize - m_iFilePos;
  102. ret = fseek(m_fp, 2*m_iFilePos, SEEK_SET);
  103. if(ret)
  104. throw GenericException(ret);
  105. numRead = fread(m_Buff, 2, iReadLen, m_fp);
  106. if(numRead != iReadLen)
  107. throw GenericException(ferror( m_fp ));
  108. m_iToFar = m_iFilePos + iReadLen;
  109. return;
  110. }
  111. int FileDataSrc::MoveToStart()
  112. {
  113. int ret = fseek(m_fp, 0, SEEK_SET);
  114. if( ret )
  115. throw GenericException(ret);
  116. m_iPos = -1;
  117. return 0;
  118. }
  119. #ifdef USE_MMF_APPROACH
  120. FileDataSrc1::FileDataSrc1(TCHAR * pFileName)
  121. {
  122. m_pFileName = NULL;
  123. m_hFile = INVALID_HANDLE_VALUE;
  124. m_hFileMapSrc = NULL;
  125. m_pData = NULL;
  126. if(pFileName == NULL)
  127. {
  128. m_iStatus = WBEM_E_INVALID_PARAMETER;
  129. return;
  130. }
  131. DWORD dwLen = lstrlen(pFileName) + 1;
  132. m_pFileName = new TCHAR[dwLen];
  133. if(m_pFileName == NULL)
  134. {
  135. m_iStatus = WBEM_E_OUT_OF_MEMORY;
  136. return;
  137. }
  138. StringCchCopyW(m_pFileName, dwLen, pFileName);
  139. m_hFile = CreateFile(pFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
  140. if (INVALID_HANDLE_VALUE == m_hFile)
  141. {
  142. m_iStatus = HR_LASTERR;
  143. return;
  144. }
  145. DWORD dwSize = GetFileSize(m_hFile,NULL);
  146. m_hFileMapSrc = CreateFileMapping(m_hFile,
  147. NULL,
  148. PAGE_READONLY,
  149. 0,0, // the entire file
  150. NULL);
  151. if (NULL == m_hFileMapSrc)
  152. {
  153. m_iStatus = HR_LASTERR;
  154. return;
  155. }
  156. m_pData = (WCHAR *)MapViewOfFile(m_hFileMapSrc,FILE_MAP_READ,0,0,0);
  157. if (NULL == m_pData)
  158. {
  159. m_iStatus = HR_LASTERR;
  160. return;
  161. }
  162. m_iSize = dwSize/sizeof(WCHAR);
  163. MoveToStart();
  164. }
  165. FileDataSrc1::~FileDataSrc1()
  166. {
  167. if (m_pData) UnmapViewOfFile(m_pData);
  168. if (m_hFileMapSrc) CloseHandle(m_hFileMapSrc);
  169. if (INVALID_HANDLE_VALUE != m_hFile) CloseHandle(m_hFile);
  170. if (m_pFileName) DeleteFile(m_pFileName);
  171. delete m_pFileName;
  172. }
  173. wchar_t FileDataSrc1::GetAt(int nOffset)
  174. {
  175. int iPos = m_iPos + nOffset;
  176. if(iPos < 0 || iPos > m_iSize)
  177. return -1;
  178. else
  179. return m_pData[iPos];
  180. }
  181. void FileDataSrc1::Move(int n)
  182. {
  183. m_iPos += n;
  184. return;
  185. }
  186. int FileDataSrc1::MoveToPos(int n)
  187. {
  188. if(n < 0 || n > m_iSize)
  189. return -1;
  190. else
  191. return m_iPos = n;
  192. }
  193. int FileDataSrc1::MoveToStart()
  194. {
  195. m_iPos = -1;
  196. return 0;
  197. }
  198. #endif
  199. BufferDataSrc::BufferDataSrc(long lSize, char * pMemSrc)
  200. {
  201. m_Data = new WCHAR[lSize+1];
  202. if(m_Data)
  203. {
  204. memset(m_Data, 0, (lSize+1) * sizeof(WCHAR));
  205. m_iSize = mbstowcs(m_Data, pMemSrc, lSize);
  206. }
  207. MoveToStart();
  208. }
  209. BufferDataSrc::~BufferDataSrc()
  210. {
  211. delete [] m_Data;
  212. }
  213. wchar_t BufferDataSrc::GetAt(int nOffset)
  214. {
  215. int iPos = m_iPos + nOffset;
  216. if(iPos < 0 || iPos > m_iSize)
  217. return -1;
  218. return m_Data[m_iPos + nOffset];
  219. }
  220. void BufferDataSrc::Move(int n)
  221. {
  222. m_iPos += n;
  223. }
  224. int BufferDataSrc::MoveToStart()
  225. {
  226. m_iPos = -1;
  227. return 0;
  228. }