// PartSTDFileMnr.cpp : implementation file // #include "stdafx.h" #include "OTSModel.h" #include "STDXMLFileMnr.h" #include "OTSFileSys.h" #include "OTSHelper.h" #include "STDFileMgr.h" namespace OTSMODEL { using namespace OTSDATA; // CSTDXMLFileMnr // constructor CSTDXMLFileMnr::CSTDXMLFileMnr() { // initialization Init(); } // copy constructor CSTDXMLFileMnr::CSTDXMLFileMnr(const CSTDXMLFileMnr& a_oSource) { // can't copy itself if (&a_oSource == this) { return; } // copy data over Duplicate(a_oSource); } // copy constructor CSTDXMLFileMnr::CSTDXMLFileMnr(CSTDXMLFileMnr* a_poSource) { // can't copy itself if (a_poSource == this) { return; } // copy data over Duplicate(*a_poSource); } // =operator CSTDXMLFileMnr& CSTDXMLFileMnr::operator=(const CSTDXMLFileMnr& a_oSource) { // cleanup Cleanup(); // copy the class data over Duplicate(a_oSource); // return class return *this; } // ==operator BOOL CSTDXMLFileMnr::operator==(const CSTDXMLFileMnr& a_oSource) { // members return *(m_poPartSTDData.get()) == *(a_oSource.m_poPartSTDData.get()); } // destructor CSTDXMLFileMnr::~CSTDXMLFileMnr() { // cleanup Cleanup(); } // CSTDXMLFileMnr member functions // public // Load/Save BOOL CSTDXMLFileMnr::Load(CString a_strPathName /*= _T("")*/, BOOL a_bClear /*= TRUE*/) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // clear all data if necessary if (a_bClear) { Init(); } // check file pathname a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { // file open dialog CFileDialog dlg(TRUE, STD_FILE_EXT, NULL, OFN_FILEMUSTEXIST, STD_FILE_FILTER); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname a_strPathName = dlg.GetPathName(); } // create a new STD data file point CPartSTDDataPtr poPartSTDData = CPartSTDDataPtr(new CPartSTDData()); // file version tinyxml2::XMLDocument doc; doc.LoadFile(a_strPathName);//载入xml文件 tinyxml2::XMLElement *rootNode; rootNode = doc.FirstChildElement(RootClassName); poPartSTDData->Serialize(false, &doc, rootNode); CString strFileVersion = poPartSTDData->GetFileVersion(); DWORD nFileVersion = COTSHelper::GetVersionFromString(strFileVersion); if (nFileVersion == 0) { // invalid file LogErrorTrace(__FILE__, __LINE__, _T("Load: invalid particle std data file %s"), a_strPathName); return FALSE; } m_poPartSTDData = poPartSTDData; // file pathname m_strPathName = a_strPathName; // set file modify flag to FALSE m_bModify = FALSE; // ok, return TRUE return TRUE; } BOOL CSTDXMLFileMnr::Save(CString a_strPathName /*= _T("")*/, CString a_LibraryName) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // check file pathname a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { // file save as dialog CFileDialog dlg(FALSE, STD_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, STD_FILE_FILTER); TCHAR _szPath[MAX_PATH + 1] = { 0 }; GetModuleFileName(NULL, _szPath, MAX_PATH); (_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名,只获得路径字串 CString strPath; for (int n = 0; _szPath[n]; n++) { if (_szPath[n] != _T('\\')) { strPath += _szPath[n]; } else { strPath += _T("\\"); } } CString Suffix = "Config\\ProData"; CString FinalPath = strPath + Suffix; dlg.m_ofn.lpstrInitialDir = FinalPath; TCHAR szBuffer[MAX_PATH] = { 0 }; CString dd = a_LibraryName; //"Sample1"; _tcscpy(szBuffer, dd); dlg.m_ofn.lpstrFile = szBuffer; dlg.m_ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname a_strPathName = dlg.GetPathName(); } else { if (a_strPathName == ".\\Config\\ProData\\") { // file open dialog CFileDialog dlg(FALSE, STD_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, TEXTAPTF_FILE_FILTER); TCHAR _szPath[MAX_PATH + 1] = { 0 }; GetModuleFileName(NULL, _szPath, MAX_PATH); (_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名,只获得路径字串 CString strPath; for (int n = 0; _szPath[n]; n++) { if (_szPath[n] != _T('\\')) { strPath += _szPath[n]; } else { strPath += _T("\\"); } } CString Suffix = "Config\\ProData"; CString FinalPath = strPath + Suffix; dlg.m_ofn.lpstrInitialDir = FinalPath; TCHAR szBuffer[MAX_PATH] = { 0 }; CString dd = a_LibraryName; //"Sample1"; _tcscpy(szBuffer, dd); dlg.m_ofn.lpstrFile = szBuffer; dlg.m_ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname a_strPathName = dlg.GetPathName(); } else { CFileDialog dlg(FALSE, STD_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, TEXTAPTF_FILE_FILTER); dlg.m_ofn.lpstrInitialDir = a_strPathName; TCHAR szBuffer[MAX_PATH] = { 0 }; CString dd = a_LibraryName; //"Sample1"; _tcscpy(szBuffer, dd); dlg.m_ofn.lpstrFile = szBuffer; dlg.m_ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname a_strPathName = dlg.GetPathName(); } } // create the file tinyxml2::XMLDocument doc; doc.LoadFile(a_strPathName);//载入xml文件 doc.Clear(); tinyxml2::XMLDeclaration* declaration = doc.NewDeclaration();//添加xml文件头申明 doc.InsertFirstChild(declaration); tinyxml2::XMLElement *rootNode; rootNode = doc.NewElement(RootClassName); doc.InsertEndChild(rootNode); m_poPartSTDData->Serialize(true, &doc, rootNode); int result = doc.SaveFile(a_strPathName); // file pathname m_strPathName = a_strPathName; // set file modify flag to FALSE m_bModify = FALSE; // ok, return TRUE return TRUE; } // elements list CElementsList& CSTDXMLFileMnr::GetElementsList() { return m_poPartSTDData->GetElementsList(); } void CSTDXMLFileMnr::SetElementsList(CElementsList& a_listElements, BOOL a_bClear) { m_poPartSTDData->SetElementsList(a_listElements, a_bClear); } CElementPtr CSTDXMLFileMnr::GetElementByIndex(int a_nIndex) { // element CElementPtr poElement = nullptr; // get elements list of particle analysis standard data CElementsList& listElements = m_poPartSTDData->GetElementsList(); // get index related item if (a_nIndex >= 0 && a_nIndex < (int)listElements.size()) { poElement = listElements[a_nIndex]; } // return element return poElement; } BOOL CSTDXMLFileMnr::DeleteElementByIndex(int a_nIndex) { // get elements list of particle analysis standard data CElementsList& listElements = m_poPartSTDData->GetElementsList(); if (a_nIndex < 0 || a_nIndex >(int)listElements.size()) { // invalid index LogErrorTrace(__FILE__, __LINE__, _T("DeleteElementByIndex: invalid index.")); return FALSE; } // remove the element listElements.erase(listElements.begin() + a_nIndex); // ok, return TRUE return TRUE; } BOOL CSTDXMLFileMnr::AddElement(CElementPtr a_poElement) { // input check ASSERT(a_poElement); if (!a_poElement) { // invalid element pointer LogErrorTrace(__FILE__, __LINE__, _T("AddElement: invalid input element pointer.")); return FALSE; } // get elements list of particle analysis standard data CElementsList& listElements = m_poPartSTDData->GetElementsList(); // make sure the element item is not already in the list int nAtomNum = a_poElement->GetAtomNum(); auto itr = std::find_if(listElements.begin(), listElements.end(), [nAtomNum](CElementPtr& poElement) { return poElement->GetAtomNum() == nAtomNum; }); if (itr != listElements.end()) { // the element is already in the list LogErrorTrace(__FILE__, __LINE__, _T("AddElement: the element item is already in the list.")); return FALSE; } // add the item into the list listElements.push_back(a_poElement); // ok, return TRUE return TRUE; } //std items list CSTDItemsList& CSTDXMLFileMnr::GetSTDItemsList() { return m_poPartSTDData->GetSTDItemsList(); } void CSTDXMLFileMnr::SetSTDItemsList(CSTDItemsList& a_listSTDItems, BOOL a_bClear /*= TRUE*/) { // set std items list of the particle analysis standard data m_poPartSTDData->SetSTDItemsList(a_listSTDItems, a_bClear); } CSTDItemPtr CSTDXMLFileMnr::GetSTDItemByIndex(int a_nIndex) { // std item CSTDItemPtr poSDTItem = nullptr; // get std items list CSTDItemsList& listSTDItems = GetSTDItemsList(); // get index related item if (a_nIndex >= 0 && a_nIndex < (int)listSTDItems.size()) { poSDTItem = listSTDItems[a_nIndex]; } // return std item return poSDTItem; } CSTDItemPtr CSTDXMLFileMnr::GetSTDItemById(int a_nId) { // safety check ASSERT(m_poPartSTDData); if (!m_poPartSTDData) { // invalid m_poPartSTDData pointer LogErrorTrace(__FILE__, __LINE__, _T("GetSTDItemById: invalid m_poPartSTDData pointer.")); return nullptr; } // return std item return m_poPartSTDData->GetSTDItemById(a_nId); } BOOL CSTDXMLFileMnr::DeleteSTDItemByIndex(int a_nIndex) { // get std items list CSTDItemsList& listSTDItems = GetSTDItemsList(); if (a_nIndex < 0 || a_nIndex >(int)listSTDItems.size()) { // invalid index LogErrorTrace(__FILE__, __LINE__, _T("DeleteSTDItemByIndex: invalid index.")); return FALSE; } // remove the std item listSTDItems.erase(listSTDItems.begin() + a_nIndex); // ok, return TRUE return TRUE; } BOOL CSTDXMLFileMnr::MoveSTDItemDown(int a_nIndex) { // get std items list CSTDItemsList& listSTDItems = GetSTDItemsList(); if (a_nIndex < 0 || a_nIndex >(int)listSTDItems.size()) { // invalid index LogErrorTrace(__FILE__, __LINE__, _T("MoveSTDItemDown: invalid index.")); return FALSE; } else if (a_nIndex == (int)listSTDItems.size() - 1) { // last item can't be move down LogErrorTrace(__FILE__, __LINE__, _T("MoveSTDItemDown: last item, can't be moved down.")); return FALSE; } // change the position the the two related items std::swap(listSTDItems[a_nIndex], listSTDItems[a_nIndex + 1]); std::string a = ""; // ok, return TRUE return TRUE; } BOOL CSTDXMLFileMnr::MoveSTDItemUp(int a_nIndex) { // get std items list CSTDItemsList& listSTDItems = GetSTDItemsList(); if (a_nIndex < 0 || a_nIndex >(int)listSTDItems.size()) { // invalid index LogErrorTrace(__FILE__, __LINE__, _T("MoveSTDItemUp: invalid index.")); return FALSE; } else if (a_nIndex == 0) { // first item can't be move up LogErrorTrace(__FILE__, __LINE__, _T("MoveSTDItemDown: first item, can't be moved up.")); return FALSE; } // change the position the the two related items std::swap(listSTDItems[a_nIndex], listSTDItems[a_nIndex - 1]); // ok, return TRUE return TRUE; } BOOL CSTDXMLFileMnr::AddSTDItem(CSTDItemPtr a_poSTDItem) { // input check ASSERT(a_poSTDItem); if (!a_poSTDItem) { // invalid std item pointer LogErrorTrace(__FILE__, __LINE__, _T("AddSTDItem: invalid input std item pointer.")); return FALSE; } // get std items list CSTDItemsList& listSTDItems = GetSTDItemsList(); // make sure there is no a same name item in the list CString strName = a_poSTDItem->GetName(); auto itr = std::find_if(listSTDItems.begin(), listSTDItems.end(), [strName](CSTDItemPtr& poSTDItem) { return poSTDItem->GetName().CompareNoCase(strName) == 0; }); if (itr != listSTDItems.end()) { // same name std item already in the list LogErrorTrace(__FILE__, __LINE__, _T("AddSTDItem: same name std item already in the list. %s"), strName); return FALSE; } // add the item into the list listSTDItems.push_back(a_poSTDItem); // ok, return TRUE return TRUE; } BOOL CSTDXMLFileMnr::InsertSTDItem(int a_nIndex, CSTDItemPtr a_poSTDItem) { // input check ASSERT(a_poSTDItem); if (!a_poSTDItem) { // invalid std item pointer LogErrorTrace(__FILE__, __LINE__, _T("InsertSTDItem: invalid input std item pointer.")); return FALSE; } // get std items list CSTDItemsList& listSTDItems = GetSTDItemsList(); // check index value if (a_nIndex < 0 || a_nIndex >(int)listSTDItems.size()) { // invalid index LogErrorTrace(__FILE__, __LINE__, _T("InsertSTDItem: invalid index.")); return FALSE; } // make sure there is no a same name item in the list CString strName = a_poSTDItem->GetName(); auto itr = std::find_if(listSTDItems.begin(), listSTDItems.end(), [strName](CSTDItemPtr& poSTDItem) { return poSTDItem->GetName().CompareNoCase(strName) == 0; }); if (itr != listSTDItems.end()) { // same name std item already in the list LogErrorTrace(__FILE__, __LINE__, _T("InsertSTDItem: same name std item already in the list. %s"), strName); return FALSE; } // insert the std item listSTDItems.insert(listSTDItems.begin() + a_nIndex, a_poSTDItem); // ok, return TRUE return TRUE; } BOOL CSTDXMLFileMnr::EditSTDItem(int a_nIndex, CSTDItemPtr a_poSTDItem) { // input check ASSERT(a_poSTDItem); if (!a_poSTDItem) { // invalid std item pointer LogErrorTrace(__FILE__, __LINE__, _T("EditSTDItem: invalid input std item pointer.")); return FALSE; } // get std items list CSTDItemsList& listSTDItems = GetSTDItemsList(); // check index value if (a_nIndex < 0 || a_nIndex >(int)listSTDItems.size()) { // invalid index LogErrorTrace(__FILE__, __LINE__, _T("EditSTDItem: invalid index.")); return FALSE; } // make sure there is no a same name item in the list CString strName = a_poSTDItem->GetName(); if (a_nIndex > 0) { auto itr = std::find_if(listSTDItems.begin(), listSTDItems.begin() + a_nIndex - 1, [strName](CSTDItemPtr& poSTDItem) { return poSTDItem->GetName().CompareNoCase(strName) == 0; }); if (itr != listSTDItems.end()) { // same name std item already in the list LogErrorTrace(__FILE__, __LINE__, _T("EditSTDItem: same name std item already in the list. %s"), strName); return FALSE; } } else if (a_nIndex != (int)listSTDItems.size() - 1) { auto itr = std::find_if(listSTDItems.begin() + a_nIndex + 1, listSTDItems.end(), [strName](CSTDItemPtr& poSTDItem) { return poSTDItem->GetName().CompareNoCase(strName) == 0; }); if (itr != listSTDItems.end()) { // same name std item already in the list LogErrorTrace(__FILE__, __LINE__, _T("EditSTDItem: same name std item already in the list. %s"), strName); return FALSE; } } // replace the std item *(listSTDItems[a_nIndex].get()) = *(a_poSTDItem.get()); // ok, return TRUE return TRUE; } void CSTDXMLFileMnr::SetPartSTDData(CPartSTDDataPtr a_pPartSTDData) { ASSERT(a_pPartSTDData); if (!a_pPartSTDData) { LogErrorTrace(__FILE__, __LINE__, _T("SetPartSTDData: invalid pointer.")); return; } // library name m_poPartSTDData->SetName(a_pPartSTDData->GetName()); CElementsList plistCurrentElement; plistCurrentElement.clear(); // elements list CElementsList plistElements = a_pPartSTDData->GetElementsList(); for (auto pElement : plistElements) { ASSERT(pElement); if (!pElement) { LogErrorTrace(__FILE__, __LINE__, _T("SetPartSTDData: invalid element pointer.")); return; } CElementPtr pCurrentElement = CElementPtr(new CElement(*pElement.get())); plistCurrentElement.push_back(pCurrentElement); } m_poPartSTDData->SetElementsList(plistCurrentElement, TRUE); CSTDItemsList plistCurrentSTDItem; plistCurrentSTDItem.clear(); CSTDItemsList plistSTDItems = a_pPartSTDData->GetSTDItemsList(); for (auto pSTDItem : plistSTDItems) { ASSERT(pSTDItem); if (!pSTDItem) { LogErrorTrace(__FILE__, __LINE__, _T("SetPartSTDData: invalid STD Item pointer.")); return; } CSTDItemPtr pCurrentSTDItem = CSTDItemPtr(new CSTDItem(*pSTDItem.get())); plistCurrentSTDItem.push_back(pCurrentSTDItem); } m_poPartSTDData->SetSTDItemsList(plistCurrentSTDItem, TRUE); } // protected // cleanup void CSTDXMLFileMnr::Cleanup() { // need to do nothing at the moment } // initialization void CSTDXMLFileMnr::Init() { // initialization m_strPathName = _T(""); m_bModify = FALSE; m_poPartSTDData = CPartSTDDataPtr(new CPartSTDData()); //m_poSTDFileMgr = CSTDFileMgrPtr(new CSTDFileMgr()); } // duplication void CSTDXMLFileMnr::Duplicate(const CSTDXMLFileMnr& a_oSource) { // initialization Init(); // copy data over m_strPathName = a_oSource.m_strPathName; m_bModify = a_oSource.m_bModify; m_poPartSTDData = CPartSTDDataPtr(new CPartSTDData(*a_oSource.m_poPartSTDData.get())); //m_poSTDFileMgr = CSTDFileMgrPtr(new CSTDFileMgr(*a_oSource.m_poSTDFileMgr.get())); } /*void CSTDXMLFileMnr::SetDBSTDFileMgr(CSTDFileMgrPtr a_pSTDFileMgr) { ASSERT(a_pSTDFileMgr); if (!a_pSTDFileMgr) { LogErrorTrace(__FILE__, __LINE__, _T("SetSTDFileMgr: invalid pointer.")); return; } m_poSTDFileMgr = CSTDFileMgrPtr(new CSTDFileMgr(*a_pSTDFileMgr.get())); }*/ }