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.

214 lines
6.0 KiB

  1. /******************************************************************************
  2. Source File: Glue.CPP
  3. This file contains the functions needed to make the GPD parser code work on
  4. both platforms, as well as stubs to support function I do not need to supply.
  5. Copyright (c) 1997 by Microsoft Corporation, All Rights Reserved.
  6. A Pretty Penny Enterprises Production
  7. Change History:
  8. 03/28/1997 Bob_Kjelgaard@Prodigy.Net Created it
  9. ******************************************************************************/
  10. #include "StdAfx.H"
  11. #include "ProjNode.H"
  12. #include "Resource.H"
  13. #include "GPDFile.H"
  14. // I'll use a class to guarantee memory never leaks from here.
  15. class CMaps {
  16. CObArray m_coaMaps;
  17. public:
  18. CMaps() { m_coaMaps.Add(NULL); }
  19. ~CMaps() {
  20. for (int i = 0; i < m_coaMaps.GetSize(); i++)
  21. if (m_coaMaps[i])
  22. delete m_coaMaps[i];
  23. }
  24. unsigned Count() { return (unsigned)m_coaMaps.GetSize(); }
  25. unsigned HandleFor(CByteArray* cba) {
  26. for (unsigned u = 1; u < Count(); u++) {
  27. if (!m_coaMaps[u]) {
  28. m_coaMaps[u] = cba;
  29. return u;
  30. }
  31. }
  32. m_coaMaps.Add(cba);
  33. return Count();
  34. }
  35. void Free(unsigned u) {
  36. if (!u || u >= Count() || !m_coaMaps[u])
  37. return;
  38. delete m_coaMaps[u];
  39. m_coaMaps[u] = NULL;
  40. }
  41. };
  42. static CMaps scmAll;
  43. extern "C" unsigned MapFileIntoMemory(LPCWSTR pstrFile, PVOID *ppvData,
  44. PDWORD pdwSize) {
  45. if (!pstrFile || !ppvData || !pdwSize)
  46. return 0;
  47. CFile cfMap;
  48. CByteArray* pcbaMap = new CByteArray;
  49. CString csFile(pstrFile);
  50. if (!pcbaMap)
  51. return 0;
  52. if (!cfMap.Open(csFile, CFile::modeRead | CFile::shareDenyWrite))
  53. return 0;
  54. try {
  55. pcbaMap -> SetSize(*pdwSize = cfMap.GetLength());
  56. cfMap.Read(pcbaMap -> GetData(), cfMap.GetLength());
  57. *ppvData = pcbaMap -> GetData();
  58. return scmAll.HandleFor(pcbaMap);
  59. }
  60. catch (CException *pce) {
  61. pce -> ReportError();
  62. pce -> Delete();
  63. }
  64. return 0;
  65. }
  66. extern "C" void UnmapFileFromMemory(unsigned uFile) {
  67. scmAll.Free(uFile);
  68. }
  69. // This one is just a stub to make the whole thing work for us- we don't use
  70. // the checksum- it's there for the end product to tell if a GPD file has
  71. // been altered since it was converted lase.
  72. extern "C" DWORD ComputeCrc32Checksum(PBYTE pbuf,DWORD dwCount,
  73. DWORD dwChecksum) {
  74. return dwCount ^ dwChecksum ^ (PtrToUlong(pbuf));
  75. }
  76. // The next two variables are used to control how and when GPD parsing/conversion
  77. // log messages are saved.
  78. // pcsaLog Ptr to string array to load with messages
  79. // bEnableLogging True iff logging is enabled
  80. static CStringArray* pcsaLog = NULL ;
  81. static bool bEnableLogging = false ;
  82. /******************************************************************************
  83. CModelData::SetLog
  84. Prepare to log parse/conversion error and warning messages.
  85. ******************************************************************************/
  86. void CModelData::SetLog()
  87. {
  88. m_csaConvertLog.RemoveAll() ;
  89. pcsaLog = &m_csaConvertLog ;
  90. bEnableLogging = true ;
  91. }
  92. /******************************************************************************
  93. CModelData::EndLog
  94. Turn off parse/conversion error and warning message logging.
  95. ******************************************************************************/
  96. void CModelData::EndLog()
  97. {
  98. pcsaLog = NULL ;
  99. bEnableLogging = false ;
  100. }
  101. /******************************************************************************
  102. DebugPrint
  103. This routine is called to log the parsing/conversion error and warning
  104. messages.
  105. ******************************************************************************/
  106. extern "C" void DebugPrint(LPCTSTR pstrFormat, ...)
  107. {
  108. CString csOutput;
  109. va_list ap;
  110. // Don't do anything if logging is not enabled.
  111. if (!bEnableLogging)
  112. return ;
  113. va_start(ap, pstrFormat);
  114. vsprintf(csOutput.GetBuffer(1000), pstrFormat, ap);
  115. va_end(ap);
  116. csOutput.ReleaseBuffer();
  117. csOutput.TrimLeft();
  118. CStringArray& csaError = *pcsaLog;
  119. // If this routine is being called when we're NOT recording problems with a
  120. // GPD, display the message when debugging.
  121. if (pcsaLog == NULL) {
  122. CString csmsg ;
  123. csmsg.LoadString(IDS_XXXUnexpectedCPError) ;
  124. csmsg += csOutput ;
  125. AfxMessageBox(csmsg, MB_ICONEXCLAMATION) ;
  126. //#ifdef _DEBUG
  127. // afxDump << csOutput ;
  128. // if (csOutput.Right(1) != _T("\n"))
  129. // afxDump << _T("\n") ;
  130. //#endif
  131. return ;
  132. } ;
  133. if (!csaError.GetSize()) {
  134. csaError.Add(csOutput);
  135. return;
  136. }
  137. if (csaError[-1 + csaError.GetSize()].Find(_T('\n')) >= 0) {
  138. csaError[-1 + csaError.GetSize()].TrimRight();
  139. pcsaLog -> Add(csOutput);
  140. }
  141. else
  142. csaError[-1 + csaError.GetSize()] += csOutput;
  143. }
  144. /******************************************************************************
  145. MDSCreateFileW
  146. I implement a version of this API here which calls the ANSI API, so I can
  147. compile the parser code with UNICODE on, but still run the resulting binary
  148. on Win95.
  149. ******************************************************************************/
  150. extern "C" HANDLE MDSCreateFileW(LPCWSTR lpstrFile, DWORD dwDesiredAccess,
  151. DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpsa,
  152. DWORD dwCreateFlags, DWORD dwfAttributes,
  153. HANDLE hTemplateFile) {
  154. CString csFile(lpstrFile); // Let CString conversions do the hard work!
  155. return CreateFile(csFile, dwDesiredAccess, dwShareMode, lpsa,
  156. dwCreateFlags, dwfAttributes, hTemplateFile);
  157. }
  158.