MsrParamFileMgr.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // MsrParamFileMgr.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MsrParamFileMgr.h"
  5. namespace OTSMODEL {
  6. using namespace OTSDATA;
  7. // CMsrParamFileMrg
  8. // constructor
  9. CMsrParamFileMrg::CMsrParamFileMrg()
  10. {
  11. // initialization
  12. void Init();
  13. }
  14. // destructor
  15. CMsrParamFileMrg::~CMsrParamFileMrg()
  16. {
  17. // cleanup
  18. Cleanup();
  19. }
  20. // CMsrParamFileMrg member functions
  21. // public
  22. // serialization
  23. void CMsrParamFileMrg::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  24. {
  25. xmls::xInt xMrsParamFileMark;
  26. xmls::xString xMrsOaramFileVersion;
  27. xmls::Slo slo;
  28. slo.Register("MrsParamFileMark", &xMrsParamFileMark);
  29. slo.Register("MrsParamFileVersion", &xMrsOaramFileVersion);
  30. slo.Register("MsrParams", m_poMsrParams.get());
  31. if (isStoring)
  32. {
  33. xMrsParamFileMark = MRS_PARAM_FILE_MARK;
  34. xMrsOaramFileVersion = MRS_PARAM_FILE_VERSION;
  35. slo.Serialize(true, classDoc, rootNode);
  36. }
  37. else
  38. {
  39. slo.Serialize(false, classDoc, rootNode);
  40. }
  41. }
  42. // Load/Save
  43. BOOL CMsrParamFileMrg::Load(CString a_strPathName /*= _T("")*/, BOOL a_bClear /*= TRUE*/)
  44. {
  45. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  46. // clear all data if necessary
  47. if (a_bClear)
  48. {
  49. Init();
  50. }
  51. // check file pathname
  52. a_strPathName.Trim();
  53. if (a_strPathName.IsEmpty())
  54. {
  55. // file open dialog
  56. CFileDialog dlg(TRUE, MESUREMENT_PARAM_FILE_EXT,NULL, OFN_FILEMUSTEXIST, MESUREMENT_PARAM_FILE_FILTER);
  57. TCHAR _szPath[MAX_PATH + 1] = { 0 };
  58. GetModuleFileName(NULL, _szPath, MAX_PATH);
  59. (_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名,只获得路径字串
  60. CString strPath;
  61. for (int n = 0; _szPath[n]; n++) {
  62. if (_szPath[n] != _T('\\')) {
  63. strPath += _szPath[n];
  64. }
  65. else {
  66. strPath += _T("\\");
  67. }
  68. }
  69. CString Suffix = "Config\\ProData";
  70. CString FinalPath = strPath + Suffix;
  71. dlg.m_ofn.lpstrInitialDir = FinalPath;
  72. if (dlg.DoModal() != IDOK)
  73. {
  74. return FALSE;
  75. }
  76. // get file pathname
  77. a_strPathName = dlg.GetPathName();
  78. }
  79. // open the particle analysis standard file
  80. tinyxml2::XMLDocument doc;
  81. doc.LoadFile(a_strPathName);//载入xml文件
  82. tinyxml2::XMLElement *rootNode;
  83. rootNode = doc.FirstChildElement(RootClassName);
  84. Serialize(false, &doc, rootNode);
  85. // file pathname
  86. m_strPathName = a_strPathName;
  87. // ok, return TRUE
  88. return TRUE;
  89. }
  90. BOOL CMsrParamFileMrg::Save(CString a_strPathName /*= _T("")*/)
  91. {
  92. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  93. // check file pathname
  94. a_strPathName.Trim();
  95. if (a_strPathName.IsEmpty())
  96. {
  97. // file save as dialog
  98. CFileDialog dlg(FALSE, MESUREMENT_PARAM_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, MESUREMENT_PARAM_FILE_FILTER);
  99. TCHAR _szPath[MAX_PATH + 1] = { 0 };
  100. GetModuleFileName(NULL, _szPath, MAX_PATH);
  101. (_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名,只获得路径字串
  102. CString strPath;
  103. for (int n = 0; _szPath[n]; n++) {
  104. if (_szPath[n] != _T('\\')) {
  105. strPath += _szPath[n];
  106. }
  107. else {
  108. strPath += _T("\\");
  109. }
  110. }
  111. CString Suffix = "Config\\ProData";
  112. CString FinalPath = strPath + Suffix;
  113. dlg.m_ofn.lpstrInitialDir = FinalPath;
  114. if (dlg.DoModal() != IDOK)
  115. {
  116. return FALSE;
  117. }
  118. // get file pathname
  119. a_strPathName = dlg.GetPathName();
  120. }
  121. // create the file
  122. tinyxml2::XMLDocument doc;
  123. doc.LoadFile(a_strPathName);//载入xml文件
  124. doc.Clear();
  125. tinyxml2::XMLDeclaration* declaration = doc.NewDeclaration();//添加xml文件头申明
  126. doc.InsertFirstChild(declaration);
  127. tinyxml2::XMLElement *rootNode;
  128. rootNode = doc.NewElement(RootClassName);
  129. doc.InsertEndChild(rootNode);
  130. Serialize(true, &doc, rootNode);
  131. int result = doc.SaveFile(a_strPathName);
  132. // file pathname
  133. m_strPathName = a_strPathName;
  134. // ok, return TRUE
  135. return TRUE;
  136. }
  137. void CMsrParamFileMrg::SetMsrParamFile(CMsrParamsPtr a_poMsrParams)
  138. {
  139. ASSERT(a_poMsrParams);
  140. if (!a_poMsrParams)
  141. {
  142. LogErrorTrace(__FILE__, __LINE__, _T("SetMsrParamFile::invalid param pointer"));
  143. return;
  144. }
  145. m_poMsrParams = CMsrParamsPtr(new CMsrParams(*a_poMsrParams.get()));
  146. }
  147. // protected
  148. // cleanup
  149. void CMsrParamFileMrg::Cleanup()
  150. {
  151. // need to do nothing at the moment
  152. }
  153. // initialization
  154. void CMsrParamFileMrg::Init()
  155. {
  156. // initialization
  157. m_poMsrParams = CMsrParamsPtr(new CMsrParams());
  158. }
  159. // duplication
  160. void CMsrParamFileMrg::Duplicate(const CMsrParamFileMrg& a_oSource)
  161. {
  162. // initialization
  163. Init();
  164. // copy data over
  165. m_poMsrParams = CMsrParamsPtr(new CMsrParams(a_oSource.m_poMsrParams.get()));
  166. }
  167. }