Source code of Windows XP (NT5)
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.

171 lines
3.0 KiB

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