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.

166 lines
2.9 KiB

  1. // Persistor.cpp: implementation of the CPersistor class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include <string>
  6. #include <iosfwd>
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace std;
  10. #include <WTYPES.H>
  11. #include "t_string.h"
  12. #include "Persistor.h"
  13. #include "StructureWapperHelpers.h"
  14. #ifdef _UNICODE
  15. static TCHAR g_tcBeginFile[] = {0xfeff};
  16. static TCHAR g_atcNL[] = {0x0d, 0x0a, 0x00};
  17. #endif
  18. //////////////////////////////////////////////////////////////////////
  19. // Construction/Destruction
  20. //////////////////////////////////////////////////////////////////////
  21. CPersistor::CPersistor
  22. (const char *pszFilename, int nMode, bool bLoading)
  23. {
  24. m_sFilename = pszFilename;
  25. m_nMode = nMode;
  26. m_bLoading = bLoading;
  27. m_pfsFile = NULL;
  28. m_bFirst = true;
  29. m_pfsFile = NULL;
  30. }
  31. CPersistor::~CPersistor()
  32. {
  33. Close();
  34. }
  35. HRESULT CPersistor::Close()
  36. {
  37. if (m_pfsFile && m_pfsFile->is_open())
  38. {
  39. #ifdef _UNICODE
  40. if (!m_bLoading)
  41. {
  42. }
  43. #endif
  44. m_pfsFile->flush();
  45. m_pfsFile->close();
  46. delete m_pfsFile;
  47. m_pfsFile = NULL;
  48. }
  49. else if (m_pfsFile)
  50. {
  51. delete m_pfsFile;
  52. m_pfsFile = NULL;
  53. }
  54. return S_OK;
  55. }
  56. HRESULT CPersistor::Open()
  57. {
  58. m_pfsFile = NULL;
  59. m_pfsFile = new t_fstream
  60. (m_sFilename.c_str(),m_nMode | ios_base::binary);
  61. if (m_pfsFile && m_pfsFile->fail())
  62. {
  63. return HRESULT_FROM_WIN32(GetLastError());
  64. }
  65. else
  66. {
  67. #ifdef _UNICODE
  68. if (m_bFirst && !m_bLoading)
  69. {
  70. // To Do: Need to write out here the UNICODE string.
  71. PutALine(*m_pfsFile, g_tcBeginFile, 1);
  72. }
  73. else if (m_bFirst)
  74. {
  75. // Need to skip over the UNICODE string.
  76. fpos_t p = m_pfsFile->tellp();
  77. if (p == (fpos_t) 0)
  78. {
  79. TCHAR tc;
  80. tc = Stream().peek();
  81. // Need to make sure that the file is unicode.
  82. if (tc != 0xff)
  83. {
  84. m_pfsFile ->close();
  85. delete m_pfsFile;
  86. m_pfsFile = NULL;
  87. return HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION);
  88. }
  89. TCHAR t;
  90. GetAChar(Stream(), t);
  91. }
  92. }
  93. #else
  94. if (m_bFirst && m_bLoading)
  95. {
  96. // Need to make sure that the file is not unicode.
  97. int tc;
  98. tc = Stream().peek();
  99. if (tc == 0xff)
  100. {
  101. m_pfsFile ->close();
  102. delete m_pfsFile;
  103. m_pfsFile = NULL;
  104. return HRESULT_FROM_WIN32(ERROR_NO_UNICODE_TRANSLATION);
  105. }
  106. }
  107. #endif
  108. m_bFirst = false;
  109. return S_OK;
  110. }
  111. }
  112. //
  113. HRESULT CPersistor::OpenLog(bool bAppend)
  114. {
  115. m_pfsFile = NULL;
  116. m_pfsFile = new t_fstream
  117. (m_sFilename.c_str(),m_nMode | ios_base::binary | (bAppend ? ios::app : 0));
  118. if (m_pfsFile && m_pfsFile->fail())
  119. {
  120. return HRESULT_FROM_WIN32(GetLastError());
  121. }
  122. else
  123. {
  124. #ifdef _UNICODE
  125. //m_pfsFile->seekp(ios::end);
  126. //fpos_t p = m_pfsFile->tellp();
  127. //if (p == (fpos_t) 0)
  128. //{
  129. PutALine(*m_pfsFile, g_tcBeginFile, 1);
  130. //}
  131. #endif
  132. return S_OK;
  133. }
  134. }