PartSizeFileMgr.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. #include "stdafx.h"
  2. #include "PartSizeFileMgr.h"
  3. #include "OTSFileSys.h"
  4. #include "OTSHelper.h"
  5. namespace OTSMODEL {
  6. using namespace OTSDATA;
  7. // CPartSTDFileMnr
  8. // constructor
  9. CPartSizeFileMgr::CPartSizeFileMgr()
  10. {
  11. // initialization
  12. Init();
  13. }
  14. // copy constructor
  15. CPartSizeFileMgr::CPartSizeFileMgr(const CPartSizeFileMgr& a_oSource)
  16. {
  17. // can't copy itself
  18. if (&a_oSource == this)
  19. {
  20. return;
  21. }
  22. // copy data over
  23. Duplicate(a_oSource);
  24. }
  25. // copy constructor
  26. CPartSizeFileMgr::CPartSizeFileMgr(CPartSizeFileMgr* a_poSource)
  27. {
  28. // can't copy itself
  29. if (a_poSource == this)
  30. {
  31. return;
  32. }
  33. // copy data over
  34. Duplicate(*a_poSource);
  35. }
  36. // =operator
  37. CPartSizeFileMgr& CPartSizeFileMgr::operator=(const CPartSizeFileMgr& a_oSource)
  38. {
  39. // cleanup
  40. Cleanup();
  41. // copy the class data over
  42. Duplicate(a_oSource);
  43. // return class
  44. return *this;
  45. }
  46. // ==operator
  47. BOOL CPartSizeFileMgr::operator==(const CPartSizeFileMgr& a_oSource)
  48. {
  49. // members
  50. return *(m_poPartSizeFile.get()) == *(a_oSource.m_poPartSizeFile.get());
  51. }
  52. // destructor
  53. CPartSizeFileMgr::~CPartSizeFileMgr()
  54. {
  55. // cleanup
  56. Cleanup();
  57. }
  58. // public
  59. //LoadAll/SaveAll
  60. BOOL CPartSizeFileMgr::LoadAll(CString a_strPathName /*= _T("")*/, BOOL a_bClear /*= TRUE*/)
  61. {
  62. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  63. // clear all data if necessary
  64. if (a_bClear)
  65. {
  66. Init();
  67. }
  68. // check file pathname
  69. a_strPathName.Trim();
  70. if (a_strPathName.IsEmpty())
  71. {
  72. // file open dialog
  73. CFileDialog dlg(TRUE, PART_SIZE_FILE_EXT, NULL, OFN_FILEMUSTEXIST, TEXTAPTF_FILE_FILTER);
  74. if (dlg.DoModal() != IDOK)
  75. {
  76. return FALSE;
  77. }
  78. // get file pathname
  79. a_strPathName = dlg.GetPathName();
  80. }
  81. int nLength = a_strPathName.GetLength();
  82. int nSeatt = a_strPathName.Find(TEXTFILE_FILE_EXT);
  83. int nSeatp = a_strPathName.Find(PART_SIZE_FILE_EXT);
  84. if (nSeatt > 0 && ((nLength - nSeatt) == FILE_SUFFIX_NUMBER))
  85. {
  86. LoadPartSizeFromTextFile(a_strPathName);
  87. }
  88. if (nSeatp > 0 && ((nLength - nSeatp) == FILE_SUFFIX_NUMBER))
  89. {
  90. Load(a_strPathName, a_bClear);
  91. }
  92. return true;
  93. }
  94. BOOL CPartSizeFileMgr::SaveAll(CString a_strPathName /*= _T("")*/, CString a_SizeName)
  95. {
  96. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  97. // check file pathname
  98. a_strPathName.Trim();
  99. if (a_strPathName.IsEmpty())
  100. {
  101. // file open dialog
  102. CFileDialog dlg(FALSE, PART_SIZE_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, TEXTAPTF_FILE_FILTER);
  103. //CFileDialog dlg(TRUE, PART_SIZE_FILE_EXT, NULL, OFN_FILEMUSTEXIST, TEXTAPTF_FILE_FILTER);
  104. TCHAR _szPath[MAX_PATH + 1] = {0};
  105. GetModuleFileName(NULL,_szPath,MAX_PATH);
  106. (_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名,只获得路径字串
  107. CString strPath;
  108. for (int n = 0; _szPath[n];n++) {
  109. if (_szPath[n] != _T('\\')) {
  110. strPath += _szPath[n];
  111. }
  112. else {
  113. strPath += _T("\\");
  114. }
  115. }
  116. CString Suffix = "Config\\ProData";
  117. CString FinalPath = strPath + Suffix;
  118. dlg.m_ofn.lpstrInitialDir = FinalPath;
  119. TCHAR szBuffer[MAX_PATH] = { 0 };
  120. CString dd = a_SizeName; //"Sample1";
  121. _tcscpy(szBuffer, dd);
  122. dlg.m_ofn.lpstrFile = szBuffer;
  123. dlg.m_ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
  124. if (dlg.DoModal() != IDOK)
  125. {
  126. return FALSE;
  127. }
  128. // get file pathname
  129. a_strPathName = dlg.GetPathName();
  130. }
  131. else {
  132. if (a_strPathName==".\\Config\\") {
  133. // file open dialog
  134. CFileDialog dlg(FALSE, PART_SIZE_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, TEXTAPTF_FILE_FILTER);
  135. TCHAR _szPath[MAX_PATH + 1] = { 0 };
  136. GetModuleFileName(NULL, _szPath, MAX_PATH);
  137. (_tcsrchr(_szPath, _T('\\')))[1] = 0;//删除文件名,只获得路径字串
  138. CString strPath;
  139. for (int n = 0; _szPath[n]; n++) {
  140. if (_szPath[n] != _T('\\')) {
  141. strPath += _szPath[n];
  142. }
  143. else {
  144. strPath += _T("\\");
  145. }
  146. }
  147. CString Suffix = "Config\\ProData";
  148. CString FinalPath = strPath + Suffix;
  149. dlg.m_ofn.lpstrInitialDir = FinalPath;
  150. if (dlg.DoModal() != IDOK)
  151. {
  152. return FALSE;
  153. }
  154. // get file pathname
  155. a_strPathName = dlg.GetPathName();
  156. }
  157. else {
  158. CFileDialog dlg(FALSE, PART_SIZE_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, TEXTAPTF_FILE_FILTER);
  159. dlg.m_ofn.lpstrInitialDir = a_strPathName;
  160. TCHAR szBuffer[MAX_PATH] = { 0 };
  161. CString dd = a_SizeName; //"Sample1";
  162. _tcscpy(szBuffer, dd);
  163. dlg.m_ofn.lpstrFile = szBuffer;
  164. dlg.m_ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
  165. if (dlg.DoModal() != IDOK)
  166. {
  167. return FALSE;
  168. }
  169. // get file pathname
  170. a_strPathName = dlg.GetPathName();
  171. }
  172. }
  173. int nLength = a_strPathName.GetLength();
  174. int nSeatt = a_strPathName.Find(TEXTFILE_FILE_EXT);
  175. int nSeatp = a_strPathName.Find(PART_SIZE_FILE_EXT);
  176. if (nSeatt > 0 && ((nLength - nSeatt) == FILE_SUFFIX_NUMBER))
  177. {
  178. if (SavePartSizeTextFile(a_strPathName))
  179. {
  180. SetPathName(a_strPathName);
  181. }
  182. }
  183. else if (nSeatp > 0 && ((nLength - nSeatp) == FILE_SUFFIX_NUMBER))
  184. {
  185. if (Save(a_strPathName))
  186. {
  187. SetPathName(a_strPathName);
  188. }
  189. }
  190. return true;
  191. }
  192. // Load/Save
  193. BOOL CPartSizeFileMgr::Load(CString a_strPathName /*= _T("")*/, BOOL a_bClear /*= TRUE*/)
  194. {
  195. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  196. // clear all data if necessary
  197. if (a_bClear)
  198. {
  199. Init();
  200. }
  201. // check file pathname
  202. a_strPathName.Trim();
  203. if (a_strPathName.IsEmpty())
  204. {
  205. // file open dialog
  206. CFileDialog dlg(TRUE, PART_SIZE_FILE_EXT, NULL, OFN_FILEMUSTEXIST, PART_SIZE_FILE_FILTER);
  207. if (dlg.DoModal() != IDOK)
  208. {
  209. return FALSE;
  210. }
  211. // get file pathname
  212. a_strPathName = dlg.GetPathName();
  213. }
  214. // open the particle analysis standard file
  215. //CFile hFile;
  216. //CFileException ex;
  217. //if (!hFile.Open(a_strPathName, CFile::modeRead, &ex))
  218. //{
  219. // // failed to open the file
  220. // TCHAR szCause[255];
  221. // ex.GetErrorMessage(szCause, 255);
  222. // LogErrorTrace(__FILE__, __LINE__, _T("Load: can't open file %s. error: %s"), a_strPathName, szCause);
  223. // return FALSE;
  224. //}
  225. // create a new part size file point
  226. CPartSizeFilePtr poPartSizeFile = CPartSizeFilePtr(new CPartSizeFile());
  227. //// create a loading archive
  228. //CArchive ar(&hFile, CArchive::load);
  229. //poPartSizeFile->Serialize(ar);
  230. //// close the file
  231. //ar.Close();
  232. tinyxml2::XMLDocument doc;
  233. doc.LoadFile(a_strPathName);//载入xml文件
  234. tinyxml2::XMLElement *rootNode;
  235. rootNode = doc.FirstChildElement(RootClassName);
  236. poPartSizeFile->Serialize(false, &doc, rootNode);
  237. // file version
  238. CString strFileVersion = poPartSizeFile->GetVersion();
  239. DWORD nFileVersion = COTSHelper::GetVersionFromString(strFileVersion);
  240. if (nFileVersion == 0)
  241. {// invalid file
  242. LogErrorTrace(__FILE__, __LINE__, _T("Load: invalid particle size level file %s"), a_strPathName);
  243. return FALSE;
  244. }
  245. m_poPartSizeFile = poPartSizeFile;
  246. // file pathname
  247. m_strPathName = a_strPathName;
  248. // set file modify flag to FALSE
  249. m_bModify = FALSE;
  250. // ok, return TRUE
  251. return TRUE;
  252. }
  253. BOOL CPartSizeFileMgr::Save(CString a_strPathName /*= _T("")*/)
  254. {
  255. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  256. // check file pathname
  257. a_strPathName.Trim();
  258. if (a_strPathName.IsEmpty())
  259. {
  260. // file save as dialog
  261. CFileDialog dlg(FALSE, PART_SIZE_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, PART_SIZE_FILE_FILTER);
  262. if (dlg.DoModal() != IDOK)
  263. {
  264. return FALSE;
  265. }
  266. // get file pathname
  267. a_strPathName = dlg.GetPathName();
  268. }
  269. // create the file
  270. //CFile hFile;
  271. //CFileException ex;
  272. //if (!hFile.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &ex))
  273. //{
  274. // // failed to open file
  275. // TCHAR szCause[255];
  276. // ex.GetErrorMessage(szCause, 255);
  277. // LogErrorTrace(__FILE__, __LINE__, _T("Save: failed to create file. %s. error: %s"), a_strPathName, szCause);
  278. // return FALSE;
  279. //}
  280. // create a store archive
  281. //CArchive ar(&hFile, CArchive::store);
  282. // file serialization (store)
  283. //m_poPartSizeFile->Serialize(ar);
  284. //// close the file
  285. //ar.Close();
  286. //--------xml serialize-------
  287. tinyxml2::XMLDocument doc;
  288. doc.LoadFile(a_strPathName);//载入xml文件
  289. doc.Clear();
  290. tinyxml2::XMLDeclaration* declaration = doc.NewDeclaration();//添加xml文件头申明
  291. doc.InsertFirstChild(declaration);
  292. tinyxml2::XMLElement *rootNode;
  293. rootNode = doc.NewElement(RootClassName);
  294. doc.InsertEndChild(rootNode);
  295. m_poPartSizeFile->Serialize(true, &doc, rootNode);
  296. int result = doc.SaveFile(a_strPathName);
  297. //-----------------------------
  298. // file pathname
  299. m_strPathName = a_strPathName;
  300. // set file modify flag to FALSE
  301. m_bModify = FALSE;
  302. // ok, return TRUE
  303. return TRUE;
  304. }
  305. // load/save a part size from a text file
  306. BOOL CPartSizeFileMgr::LoadPartSizeFromTextFile(CString a_strFilePathName/* = _T("")*/)
  307. {
  308. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  309. // check file pathname
  310. a_strFilePathName.Trim();
  311. if (a_strFilePathName.IsEmpty())
  312. {
  313. // open file dialog
  314. CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, TEXTFILE_FILTER);
  315. if (dlg.DoModal() != IDOK)
  316. {
  317. // user didn't click OK button, return FALSE
  318. LogTrace(__FILE__, __LINE__, _T("LoadStageFromTextFile: user canceled on file open dialog."));
  319. return FALSE;
  320. }
  321. else
  322. {
  323. a_strFilePathName = dlg.GetPathName();
  324. }
  325. }
  326. // load string lines from the file
  327. std::vector<CString > listLineStr = COTSHelper::LoadTextFileToCStingList(a_strFilePathName);
  328. CString strName = _T("");
  329. CString strVersion = PART_SIZE_FILE_VERSION;
  330. CString strLine = listLineStr[0];
  331. // split the string line with ":"
  332. std::vector<CString > listStr = COTSHelper::SplitString(strLine, FILE_TITLE_SPLIT);
  333. // jump over the string if it is invalid
  334. // it should have a title string and value string
  335. if ((int)listStr.size() != TEXTFILE_ITEM_COLUMN_NUMBER)
  336. {
  337. LogErrorTrace(__FILE__, __LINE__, _T("LoadPartSizeFromTextFile: There is no name."));
  338. return FALSE;
  339. }
  340. CString strTitle = listStr[0];
  341. CString strValue = listStr[1];
  342. // get part size component
  343. for (int i = (int)PART_SIZE_ITEMS::MIN; i <= (int)PART_SIZE_ITEMS::MAX; ++i)
  344. {
  345. // match title?
  346. CString strFileItemTitle;
  347. strFileItemTitle.LoadString(IDS_PART_SIZE_FILE_TITLE_FIRST + i);
  348. if (strTitle.CompareNoCase(strFileItemTitle) == 0)
  349. {
  350. // jump over if name is set
  351. if (!strTitle.IsEmpty())
  352. {
  353. // jump over if value string is empty
  354. strValue.Trim();
  355. if (!strValue.IsEmpty())
  356. {
  357. strName = strValue;
  358. }
  359. }
  360. }
  361. }
  362. CPartSizeList listSizes;
  363. listSizes.clear();
  364. int nSize = (int)listLineStr.size();
  365. if (nSize < 2)
  366. {
  367. LogErrorTrace(__FILE__, __LINE__, _T("LoadPartSizeFromTextFile: There is no particle size."));
  368. return FALSE;
  369. }
  370. for (int i = 1; i< nSize; i++)
  371. {
  372. CString strLine = listLineStr[i];
  373. double size = atof(strLine);
  374. listSizes.push_back(size);
  375. }
  376. m_poPartSizeFile->SetSizes(listSizes);
  377. m_poPartSizeFile->ChangDoubleToItem();
  378. // set stage components
  379. m_poPartSizeFile->SetName(strName);
  380. m_poPartSizeFile->SetVersion(strVersion);
  381. // ok, return TRUE
  382. return TRUE;
  383. }
  384. BOOL CPartSizeFileMgr::SavePartSizeTextFile(CString a_strFilePathName/* = _T("")*/)
  385. {
  386. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  387. // check file pathname
  388. a_strFilePathName.Trim();
  389. if (a_strFilePathName.IsEmpty())
  390. {
  391. // file pathname is empty
  392. // open a file save as dialog
  393. CFileDialog dlg(FALSE, TEXTFILE_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, TEXTFILE_FILTER);
  394. if (dlg.DoModal() != IDOK)
  395. {
  396. // user didn't click OK button, return FALSE
  397. LogTrace(__FILE__, __LINE__, _T("SaveStageIntoTextFile: user canceled on file open dialog."));
  398. return FALSE;
  399. }
  400. // get file pathname
  401. a_strFilePathName = dlg.GetPathName();
  402. }
  403. else
  404. {
  405. // get path name string
  406. CString strFilePath = COTSHelper::GetFolderName(a_strFilePathName);
  407. // check the path name string
  408. strFilePath.Trim();
  409. if (!strFilePath.IsEmpty())
  410. {
  411. // check if file folder exists
  412. if (!COTSFileSys::Exists(strFilePath))
  413. {
  414. // create the file folder
  415. if (!COTSFileSys::CreateFolder(strFilePath))
  416. {
  417. // failed to create the file folder
  418. LogErrorTrace(__FILE__, __LINE__, _T("SavePartSizeTextFile: failed to create file folder. %s"), strFilePath);
  419. return FALSE;
  420. }
  421. }
  422. }
  423. }
  424. // create the file
  425. CStdioFile file;
  426. CFileException ex;
  427. if (!file.Open(a_strFilePathName, CFile::modeCreate | CFile::modeWrite, &ex))
  428. {
  429. // failed to open file
  430. LogErrorTrace(__FILE__, __LINE__, _T("SaveStageIntoTextFile: failed to create file. %s"), a_strFilePathName);
  431. return FALSE;
  432. }
  433. // get part size text body
  434. CString strStageTextBody = GetPartSizeTextBody(m_poPartSizeFile);
  435. file.WriteString(strStageTextBody);
  436. file.Close();
  437. // ok, return TRUE
  438. return TRUE;
  439. }
  440. // part size items list
  441. void CPartSizeFileMgr::SetPartSizeItemList(std::vector<double>& a_listPartSizeItems, BOOL a_bClear/* = TRUE*/)
  442. {
  443. ASSERT(m_poPartSizeFile);
  444. if (!m_poPartSizeFile)
  445. {
  446. LogErrorTrace(__FILE__, __LINE__, _T("SetPartSizeItemList: empty part size file."));
  447. return;
  448. }
  449. m_poPartSizeFile->SetSizes(a_listPartSizeItems, a_bClear);
  450. }
  451. CPartSizeItemPtr CPartSizeFileMgr::GetPartSizeItemByIndex(int a_nIndex)
  452. {
  453. CPartSizeItemList listPartSize = m_poPartSizeFile->GetPartSizeList();
  454. if (a_nIndex < 0 || a_nIndex >(int)listPartSize.size())
  455. {
  456. LogErrorTrace(__FILE__, __LINE__, _T("GetPartSizeItemList: invalid index."));
  457. return nullptr;
  458. }
  459. CPartSizeItemPtr pPartSizeItem = CPartSizeItemPtr(new CPartSizeItem(*(listPartSize[a_nIndex].get())));
  460. return pPartSizeItem;
  461. }
  462. CPartSizeItemPtr CPartSizeFileMgr::GetPartSizeItemByName(CString a_strName)
  463. {
  464. CPartSizeItemList listPartSize = m_poPartSizeFile->GetPartSizeList();
  465. // make sure there is no a same name item in the list
  466. auto itr = std::find_if(listPartSize.begin(), listPartSize.end(), [a_strName](CPartSizeItemPtr& poPartSizeItem) { return poPartSizeItem->GetName().CompareNoCase(a_strName) == 0; });
  467. if (itr != listPartSize.end())
  468. {
  469. // same name std item already in the list
  470. LogErrorTrace(__FILE__, __LINE__, _T("GetPartSizeItemByName: can't find the same name in the list. name is %s"), a_strName);
  471. return FALSE;
  472. }
  473. CPartSizeItemPtr pPartSizeItem = *itr;
  474. return CPartSizeItemPtr(new CPartSizeItem(*(pPartSizeItem.get())));;
  475. }
  476. //delete as index
  477. BOOL CPartSizeFileMgr::DeletePartSizeItemByIndex(int a_nIndex)
  478. {
  479. CPartSizeList listPartSize = m_poPartSizeFile->GetSizes();
  480. if (a_nIndex >= 0 && a_nIndex <(int)listPartSize.size())
  481. {
  482. listPartSize.erase(listPartSize.begin() + a_nIndex);
  483. }
  484. SetPartSizeItemList(listPartSize);
  485. m_poPartSizeFile->ChangDoubleToItem();
  486. return TRUE;
  487. }
  488. BOOL CPartSizeFileMgr::DeletePartSizeItemByPart(double d_Part)
  489. {
  490. CPartSizeList listPartSize = m_poPartSizeFile->GetSizes();
  491. // find the name matching sample
  492. auto itr = std::find_if(listPartSize.begin(), listPartSize.end(), [d_Part](double p) { return p == d_Part; });
  493. if (itr != listPartSize.end())
  494. {
  495. listPartSize.erase(itr);
  496. }
  497. SetPartSizeItemList(listPartSize);
  498. m_poPartSizeFile->ChangDoubleToItem();
  499. return TRUE;
  500. }
  501. BOOL CPartSizeFileMgr::AddPartSizeItem(double a_poPartSizeItem)
  502. {
  503. ASSERT(m_poPartSizeFile);
  504. if (!m_poPartSizeFile)
  505. {
  506. LogErrorTrace(__FILE__, __LINE__, _T("AddPartSizeItem: empty part size file."));
  507. return FALSE;
  508. }
  509. CPartSizeList listPartSize = m_poPartSizeFile->GetSizes();
  510. //check if in the list already
  511. auto itr = std::find_if(listPartSize.begin(), listPartSize.end(), [a_poPartSizeItem](double poPartSize) { return (poPartSize == a_poPartSizeItem); });
  512. if (itr != listPartSize.end())
  513. {
  514. // same name std item already in the list
  515. LogErrorTrace(__FILE__, __LINE__, _T("AddPartSizeItem: the double %f value has already in the list. "), a_poPartSizeItem);
  516. return TRUE;
  517. }
  518. listPartSize.push_back(a_poPartSizeItem);
  519. sort(listPartSize.begin(), listPartSize.end());
  520. SetPartSizeItemList(listPartSize);
  521. m_poPartSizeFile->ChangDoubleToItem();
  522. return TRUE;
  523. }
  524. BOOL CPartSizeFileMgr::EditPartSizeItem(int a_nIndex, CPartSizeItemPtr a_poPartSizeItem)
  525. {
  526. // input check
  527. ASSERT(a_poPartSizeItem);
  528. if (!a_poPartSizeItem)
  529. {
  530. // invalid std item pointer
  531. LogErrorTrace(__FILE__, __LINE__, _T("EdiPartSizeItem: invalid input std item pointer."));
  532. return FALSE;
  533. }
  534. // get part size items list
  535. CPartSizeItemList& listPartSizeItems = m_poPartSizeFile->GetPartSizeList();
  536. // check index value
  537. if (a_nIndex < 0 || a_nIndex >(int)listPartSizeItems.size())
  538. {
  539. // invalid index
  540. LogErrorTrace(__FILE__, __LINE__, _T("EdiPartSizeItem: invalid index."));
  541. return FALSE;
  542. }
  543. // replace the part size item
  544. *(listPartSizeItems[a_nIndex].get()) = *(a_poPartSizeItem.get());
  545. // ok, return TRUE
  546. return TRUE;
  547. }
  548. void CPartSizeFileMgr::SetPartSizeFile(CPartSizeFilePtr a_pPartSizeFile)
  549. {
  550. ASSERT(a_pPartSizeFile);
  551. if (!a_pPartSizeFile)
  552. {
  553. LogErrorTrace(__FILE__, __LINE__, _T("SetPartSizeFile: empty part size file pointer."));
  554. return;
  555. }
  556. m_poPartSizeFile = CPartSizeFilePtr(new CPartSizeFile(*a_pPartSizeFile.get()));
  557. }
  558. // protected
  559. // cleanup
  560. void CPartSizeFileMgr::Cleanup()
  561. {
  562. // need to do nothing at the moment
  563. }
  564. // initialization
  565. void CPartSizeFileMgr::Init()
  566. {
  567. // initialization
  568. m_strPathName = _T("");
  569. m_bModify = FALSE;
  570. m_poPartSizeFile = CPartSizeFilePtr(new CPartSizeFile());
  571. }
  572. // duplication
  573. void CPartSizeFileMgr::Duplicate(const CPartSizeFileMgr& a_oSource)
  574. {
  575. // initialization
  576. Init();
  577. // copy data over
  578. m_strPathName = a_oSource.m_strPathName;
  579. m_bModify = a_oSource.m_bModify;
  580. m_poPartSizeFile = CPartSizeFilePtr(new CPartSizeFile(a_oSource.m_poPartSizeFile.get()));
  581. }
  582. // get part size
  583. CPartSizeItemPtr CPartSizeFileMgr::GetLevel(CString a_strValue)
  584. {
  585. // part size item
  586. CPartSizeItemPtr pPartSize = nullptr;
  587. // check value string
  588. a_strValue.Trim();
  589. if (a_strValue.IsEmpty())
  590. {
  591. return pPartSize;
  592. }
  593. // split the value string
  594. std::vector<CString > listStr = COTSHelper::SplitString(a_strValue, FILE_VALUE_SPLIT);
  595. // check vector size
  596. if (listStr.size() != PART_SIZE_ITEM_NUMBER)
  597. {
  598. return pPartSize;
  599. }
  600. // get name
  601. CString strName = listStr[0];
  602. // remove the name from the list
  603. listStr.erase(listStr.begin());
  604. CDoubleRangePtr poSizeLevel = CDoubleRangePtr(new CDoubleRange());
  605. // Max
  606. int nMax;
  607. CString strValue = listStr[0];
  608. if (!COTSHelper::StringToInt(strValue, nMax))
  609. {
  610. return pPartSize;
  611. }
  612. poSizeLevel->SetEnd(nMax);
  613. // Min
  614. int nMin;
  615. strValue = listStr[1];
  616. if (!COTSHelper::StringToInt(strValue, nMin))
  617. {
  618. return pPartSize;
  619. }
  620. poSizeLevel->SetStart(nMin);
  621. // check the level
  622. if (!poSizeLevel)
  623. {
  624. // failed to get domain
  625. return pPartSize;
  626. }
  627. pPartSize = CPartSizeItemPtr(new CPartSizeItem());
  628. // create part size
  629. pPartSize->SetName(strName);
  630. pPartSize->SetSizeLevel(poSizeLevel);
  631. // return the hole pointer
  632. return pPartSize;
  633. }
  634. // get part size level
  635. CString CPartSizeFileMgr::GetPartSizeTextBody(CPartSizeFilePtr a_pPartSizeFile)
  636. {
  637. // part size text body
  638. CString strPartSizeTextBody = _T("");
  639. // check input
  640. ASSERT(a_pPartSizeFile);
  641. if (!a_pPartSizeFile)
  642. {
  643. // invalid input
  644. LogErrorTrace(__FILE__, __LINE__, _T("GetPartSizeTextBody: invalid input stage pointer."));
  645. return strPartSizeTextBody;
  646. }
  647. // name
  648. CString strTitle;
  649. strTitle.LoadString(IDS_PART_SIZE_FILE_TITLE_FIRST);
  650. strPartSizeTextBody += strTitle + FILE_TITLE_SPLIT + _T(" ") + a_pPartSizeFile->GetName() + LINE_END;
  651. CPartSizeList listSizes = a_pPartSizeFile->GetSizes();
  652. for (size_t i=0;i<listSizes.size();i++)
  653. {
  654. CString strValue;
  655. strValue.Format(_T("%f"), listSizes[i]);
  656. strPartSizeTextBody += strValue + LINE_END;
  657. }
  658. // return part size text body
  659. return strPartSizeTextBody;
  660. }
  661. }