MsrParamFileMgr.cpp 4.5 KB

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