| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- // 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()));
- }*/
- }
|