123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- #pragma once
- // OTSFileSys.cpp : implementation file
- //
- #include "stdafx.h"
- #include "OTSFileSys.h"
- #include "OTSHelper.h"
- #include "COTSUtilityDllFunExport.h"
- // COTSFileSys
- namespace OTSTools {
- //using namespace NSLogTools;
- // COTSFileSys member functions
- // private functions:
- const CString STR_COFIGPATH = _T("Config");
- const CString STR_APPNAME_OTSINCA = _T("OTSIncA");
- const CString STR_APPNAME_OTSPARTA = _T("OTSPartA");
- const CString STR_SYSTEM_DATA = _T("SysData");
- const CString STR_PROG_DATA = _T("ProData");
- const CString STR_MEASURE_PREFERENCE_FILE_NAME = _T("OTSProgMgrParam.pmf");
- const CString STR_REPORT_PREFERENCE_FILE_NAME = _T("OTSReportMgrParam.rpf");
- //SySSTDData.db
- const CString SYS_STD_LIB_FILE_NAME = _T("IncASTDData.db");
- const CString SYS_PartSTD_LIB_FILE_NAME = _T("PartASTDData.db");
- const CString STR_LOG = _T("Log");
- const CString TEXTFILE_FILE_EXT = _T(".txt");
- const CString TEXTFILE_FILTER = _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||");
- const CString TEXTAPTF_FILE_FILTER = _T("Text Files (*.txt)|*.txt|(*.psf)|*.psf|All Files (*.*)|*.*||");
- const CString OTS_TEXT_FILE_COMMENT = _T("//");
- const CString LINE_END = _T("\r\n");
- const CString FILE_TITLE_SPLIT = _T(":");
- const CString FILE_VALUE_SPLIT = _T(",");
- const int TEXTFILE_ITEM_COLUMN_NUMBER = 2;
- const int FILE_SUFFIX_NUMBER = 4;
- COTSFileSys::COTSFileSys()
- {
- }
- COTSFileSys::~COTSFileSys()
- {
- }
- BOOL HasAttribute(LPCTSTR a_strFolder, DWORD a_nAttribute)
- {
- DWORD flags = GetFileAttributes(a_strFolder);
- return (flags != INVALID_FILE_ATTRIBUTES) &&
- (flags & a_nAttribute);
- }
- // check if the file exists or not
- BOOL COTSFileSys::Exists(LPCTSTR a_sPath)
- {
- return ::PathFileExists(a_sPath) == TRUE;
- }
- // check if the given string is valid file name or not
- BOOL COTSFileSys::IsValidFileName(LPCTSTR a_sFileName)
- {
- CString strFileName = a_sFileName;
- const CString INVALIDFILENAMECHAR(_T("\\/:*?\"<>"));
- return strFileName.FindOneOf(INVALIDFILENAMECHAR) == -1;
- }
- // copy a file
- BOOL COTSFileSys::CopyAFile(LPCTSTR a_strSourceFile, LPCTSTR a_strTargetFile, const BOOL a_bOverwrite /*= FALSE*/)
- {
- // make sure the two file name string are not empty
- ASSERT(a_strSourceFile);
- ASSERT(a_strTargetFile);
- if (!a_strSourceFile || !a_strTargetFile)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CopyAFile: invalid file name"));
- return FALSE;
- }
- // make sure the source file exist
- if (!Exists(a_strSourceFile))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CopyAFile: source file doesn't exist"));
- return FALSE;
- }
- // quit if the target file exists and can't be overwritten
- if (!a_bOverwrite && Exists(a_strTargetFile))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CopyAFile: target file exists and can't be overwritten"));
- return FALSE;
- }
- // copy file
- if (!::CopyFile(a_strSourceFile, a_strTargetFile, !a_bOverwrite))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CopyAFile: copy(%s) file to %s failed.error : %s"), a_strSourceFile, a_strTargetFile, COTSHelper::GetSystemErrorString(GetLastError()));
- return FALSE;
- }
- // ok, return TRUE
- return TRUE;
- }
- // move a file
- BOOL COTSFileSys::MoveAFile(LPCTSTR a_strSourceFile, LPCTSTR a_strTargetFile, const BOOL a_bOverwrite /*= TRUE*/)
- {
- // make sure the two file name string are not empty
- ASSERT(a_strSourceFile);
- ASSERT(a_strTargetFile);
- if (!a_strSourceFile || !a_strTargetFile)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: invalid file name"));
- return FALSE;
- }
- // make sure the source file exist
- if (!Exists(a_strSourceFile))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: source file doesn't exist"));
- return FALSE;
- }
- // the target exists?
- if (Exists(a_strTargetFile))
- {
- // quit if the target file can't be overwritten
- if(!a_bOverwrite)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: target file exists and can't be overwritten"));
- return FALSE;
- }
- if (DeleteAFile(a_strTargetFile))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: can't delete the exist file %s"), a_strTargetFile);
- return FALSE;
- }
- }
- if (!::MoveFile(a_strSourceFile, a_strTargetFile))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: move(%s) file to %s failed.error : %s"), a_strSourceFile, a_strTargetFile, COTSHelper::GetSystemErrorString(GetLastError()));
- return FALSE;
- }
- // ok, return TRUE
- return TRUE;
- }
- // delete a file.
- BOOL COTSFileSys::DeleteAFile(LPCTSTR a_strFileName)
- {
- // make sure the file name string is not empty
- ASSERT(a_strFileName);
- if (!a_strFileName)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAFile: invalid file name"));
- return FALSE;
- }
- // return TRUE if the file is not exist
- if (!Exists(a_strFileName))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAFile: %s file does not exist."), a_strFileName);
- return TRUE;
- }
- // quit if the file is read-only
- if (!IsReadOnly(a_strFileName))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAFile: %s file is readonly."), a_strFileName);
- return FALSE;
- }
- if (!::DeleteFile(a_strFileName))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAFile: delete (%s) file failed. error: %s"), a_strFileName, COTSHelper::GetSystemErrorString(GetLastError()));
- }
- // ok, return TRUE
- return TRUE;
- }
- // delete all files in the folder
- BOOL COTSFileSys::DeleteAllFiles(LPCTSTR a_strFolder, LPCTSTR a_sFilter /*= nullptr*/)
- {
- // make sure the folder name string are not empty
- ASSERT(a_strFolder);
- if (!a_strFolder)
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: invalid folder name"));
- return FALSE;
- }
- // return TRUE if the folder is not exist
- if (!Exists(a_strFolder))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: %s folder does not exist."), a_strFolder);
- return TRUE;
- }
- // make sure this is a folder
- if (!IsFolder(a_strFolder))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: %s is not a folder."), a_strFolder);
- return FALSE;
- }
- // is the folder read-only?
- if (!IsReadOnly(a_strFolder))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: %s folder is readonly."), a_strFolder);
- return FALSE;
- }
- // get all files in the folder
- CFileFind searchFile;
- CString filePattern(a_strFolder);
- if (filePattern)
- {
- filePattern += _T("\\");
- filePattern += a_sFilter;
- }
- else
- {
- filePattern += _T("\\*.*");
- }
- BOOL ret = searchFile.FindFile(filePattern);
- // delete all files in the folder
- while (ret)
- {
- // get a file from the folder
- ret = searchFile.FindNextFile();
- // make sure that this is a real file, jump over if is not
- if (searchFile.IsDots() || searchFile.IsDirectory())
- {
- continue;
- }
- // delete the file
- if (!DeleteAFile(searchFile.GetFilePath()))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: delete (%s) file failed."), searchFile.GetFilePath());
- return FALSE;
- }
- }
- searchFile.Close();
- // ok, return TRUE
- return TRUE;
- }
- // creates a folder.
- BOOL COTSFileSys::CreateFolder(LPCTSTR a_strFolder)
- {
- // make sure the folder name string are not empty
- CString strFolder = a_strFolder;
- strFolder.Trim();
- if (strFolder.IsEmpty())
- {
- LogErrorTrace(__FILE__, __LINE__, "CreateFolder: invalid folder name is empty.");
- return FALSE;
- }
- // if the folder exist?
- if (Exists(strFolder))
- {
- // is a real folder?
- if (IsFolder(strFolder))
- {
- return TRUE;
- }
- }
- // create folder
- // remove back slash if there
- CString strParentFolder = strFolder;
- strParentFolder.Trim(_T("\\"));
- // find last back slash position
- int nBackSlashPos = strParentFolder.ReverseFind(_T('\\'));
- if (nBackSlashPos > 0)
- {
- // get the folder name
- CString strFolderName = strParentFolder.Right(strParentFolder.GetLength() - nBackSlashPos - 1);
- // separate the folder string
- strParentFolder = strParentFolder.Left(nBackSlashPos);
- if (!IsValidFileName(strFolderName))
- {
- LogErrorTrace(__FILE__, __LINE__, "CreateFolder: invalid folder or file name %s.", strFolderName);
- return FALSE;
- }
- // try to create folder from the base folder
- if (!CreateFolder(strParentFolder))
- {
- LogErrorTrace(__FILE__, __LINE__, "CreateFolder: failed to create %s folder.", strParentFolder);
- return FALSE;
- }
- }
- // create the folder
- BOOL bRet = ::CreateDirectory(strFolder, NULL) == TRUE;
- // return folder create result
- return bRet;
- }
- // deletes a folder and contents recursively.
- BOOL COTSFileSys::DeleteFolder(LPCTSTR a_strFolder)
- {
- // make sure the folder name string are not empty
- CString strFolder = a_strFolder;
- strFolder.Trim();
- if (strFolder.IsEmpty())
- {
- LogErrorTrace(__FILE__, __LINE__, _T("CreateFolder: invalid folder name is empty."));
- return FALSE;
- }
- // quit if the folder is not exist
- if (!Exists(strFolder))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: %s folder is not exist."), strFolder);
- return TRUE;
- }
- // make sure it is a folder
- if (!IsFolder(strFolder))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: %s is a folder."), strFolder);
- return FALSE;
- }
- // readonly?
- if (!IsReadOnly(strFolder))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: %s folder is readonly."), strFolder);
- return FALSE;
- }
- // remove every thing in the folder
- CFileFind fileSearch;
- CString filePattern(strFolder);
- filePattern += _T("\\*.*");
- BOOL ret = fileSearch.FindFile(filePattern);
- while (ret)
- {
- // get a file or a folder
- ret = fileSearch.FindNextFile();
- // jump over if this is a dots item
- if (fileSearch.IsDots())
- {
- continue;
- }
- // is a sub folder
- if (fileSearch.IsDirectory())
- {
- // delete the sub folder
- if (!DeleteFolder(fileSearch.GetFilePath()))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: delete %s sub folder failed."), fileSearch.GetFilePath());
- return FALSE;
- }
- }
- // or a file, delete the file
- else if (!DeleteAFile(fileSearch.GetFilePath()))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: delete %s file failed."), fileSearch.GetFilePath());
- return FALSE;
- }
- }
- // delete the folderstrFolder
- if (!::RemoveDirectory(a_strFolder))
- {
- LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: remove directory %s failed."), strFolder);
- return FALSE;
- }
- // ok return TRUE
- return TRUE;
- }
- // check if this is an existing folder
- BOOL COTSFileSys::IsFolder(LPCTSTR a_strFolder)
- {
- if (::PathIsDirectory(a_strFolder))
- {
- return HasAttribute(a_strFolder, FILE_ATTRIBUTE_DIRECTORY);
- }
- return FALSE;
- }
- // check if the file or folder is read-only
- BOOL COTSFileSys::IsReadOnly(LPCTSTR a_strPathName)
- {
- return HasAttribute(a_strPathName, FILE_ATTRIBUTE_READONLY);
- }
- // sets the read-only flag for a file or a folder.
- BOOL COTSFileSys::SetReadOnly(LPCTSTR a_strPathName, BOOL a_bReadOnly /*=TRUE*/)
- {
- DWORD flags = GetFileAttributes(a_strPathName);
- if (a_bReadOnly)
- {
- flags |= FILE_ATTRIBUTE_READONLY;
- }
- else
- {
- flags &= ~FILE_ATTRIBUTE_READONLY;
- }
- return ::SetFileAttributes(a_strPathName, flags) != 0;
- }
- // get system common data folder pathname
- // return "" if failed
- CString COTSFileSys::GetOSCommonDataPathName()
- {
- CString strPathName= _T(".\\");
-
-
- return strPathName;
- }
- // get company system data path
- CString COTSFileSys::GetSysDataPathName()
- {
- // get common data pathname string
- CString strCommonDataPathName = COTSFileSys::GetOSCommonDataPathName();
- if (strCommonDataPathName.IsEmpty())
- {
- // failed to get common data pathname string
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackSysDataPathName: failed to common data pathname string."));
- return _T("");
- }
- // company system data pathname
- // e.g. "c:\ProgramData\OPTON\SysData\"
- CString strCmpSysDataPath = strCommonDataPathName + STR_COFIGPATH + _T("\\") + STR_SYSTEM_DATA + _T("\\");
- // return company system data pathname
- return strCmpSysDataPath;
- }
- // get company log pathname
- CString COTSFileSys::GetLogPathName()
- {
- // get common data pathname string
- CString strCommonDataPathName = COTSFileSys::GetOSCommonDataPathName();
- if (strCommonDataPathName.IsEmpty())
- {
- // failed to get company system data folder string
- LogErrorTrace(__FILE__, __LINE__, _T("GetCompayLogPathName: failed to common data pathname string."));
- return _T("");
- }
- // software package log path
- // e.g. "c:\ProgramData\Log\"
- CString strCompanyLogPathName = strCommonDataPathName + _T("\\") + STR_LOG + _T("\\");
- // return software package log path
- return strCompanyLogPathName;
- }
- // get software pack system data path
- CString COTSFileSys::GetOTSPackSysDataPathName(OTS_SOFT_PACKAGE_ID a_nPackId)//deprecated,since we have build one new solution for the particle system.
- {
- // get app package name
- CString strAppPackageName(_T(""));
- switch (a_nPackId)
- {
- case OTS_SOFT_PACKAGE_ID::OTSIncA:
- strAppPackageName = STR_APPNAME_OTSINCA;
- break;
- case OTS_SOFT_PACKAGE_ID::OTSPartA:
- strAppPackageName = STR_APPNAME_OTSINCA;
- break;
- default:
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackSysDataPathName: invalid software package id."));
- return _T("");
- }
- // get common data pathname string
- CString strCommonDataPathName = COTSFileSys::GetOSCommonDataPathName();
- if (strCommonDataPathName.IsEmpty())
- {
- // can't common data path
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackSysDataPathName: failed to common data path."));
- return _T("");
- }
- // software package system data pathname
- // e.g. "c:\ProgramData\OPTON\OTSIncA\SysData\"
- //CString strOTSSysDataPathName = strCommonDataPathName + STR_COFIGPATH + _T("\\") + strAppPackageName + _T("\\") + STR_SYSTEM_DATA + _T("\\");
- CString strOTSSysDataPathName = strCommonDataPathName + STR_COFIGPATH + _T("\\") + STR_SYSTEM_DATA + _T("\\");
- // return software package system data path
- return strOTSSysDataPathName;
- }
- // get software pack program data path
- CString COTSFileSys::GetOTSPackProgDataPathName(OTS_SOFT_PACKAGE_ID a_nPackId)
- {
- // get app package name
- CString strAppPackageName(_T(""));
- switch (a_nPackId)
- {
- case OTS_SOFT_PACKAGE_ID::OTSIncA:
- strAppPackageName = STR_APPNAME_OTSINCA;
- break;
- case OTS_SOFT_PACKAGE_ID::OTSPartA:
- strAppPackageName = STR_APPNAME_OTSINCA;
- break;
- default:
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackProgDataPathName: invalid software package id."));
- return _T("");
- }
- // get common data pathname string
- CString strCommonDataPathName = COTSFileSys::GetOSCommonDataPathName();
- if (strCommonDataPathName.IsEmpty())
- {
- // can't common data path
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackProgDataPathName: failed to common data path."));
- return _T("");
- }
- // software package program data pathname
-
- CString strOTSProDataPathName = strCommonDataPathName + STR_COFIGPATH + _T("\\") + STR_PROG_DATA + _T("\\");
- // return software package program data path
- return strOTSProDataPathName;
- }
- // get software pack preference file path name
- CString COTSFileSys::GetOTSPackMeasurePrefFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId)
- {
- // get software package system data pathname
- CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_nPackId);
- // check if software package system data pathname is right
- if (strOTSPackSysDataPathName.IsEmpty())
- {
- // failed to get software package system data pathname
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string."));
- return _T("");
- }
- // software package project manager file pathname
- // i.e. "c:\ProgramData\OPTON\OTSIncA\SysData\OTSProgMgrParam.pmf"
- CString strOTSPackProgMgrPathName = strOTSPackSysDataPathName + STR_MEASURE_PREFERENCE_FILE_NAME;
- // return software package license file pathname
- return strOTSPackProgMgrPathName;
- }
- // get software pack preference file path name
- CString COTSFileSys::GetOTSPackReportPrefFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId)
- {
- // get software package system data pathname
- CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_nPackId);
- // check if software package system data pathname is right
- if (strOTSPackSysDataPathName.IsEmpty())
- {
- // failed to get software package system data pathname
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string."));
- return _T("");
- }
- // software package project manager file pathname
- // i.e. "c:\ProgramData\OPTON\OTSIncA\SysData\OTSReportMgrParam.rp"
- CString strOTSPackProgMgrPathName = strOTSPackSysDataPathName + STR_REPORT_PREFERENCE_FILE_NAME;
- // return software package license file pathname
- return strOTSPackProgMgrPathName;
- }
- CString COTSFileSys::GetOTSPackSysSTDLibFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId)
- {
- // get software package system data pathname
- CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_nPackId);
- // check if software package system data pathname is right
- if (strOTSPackSysDataPathName.IsEmpty())
- {
- // failed to get software package system data pathname
- LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string."));
- return _T("");
- }
- CString strOTSPackSysSTDlibPathName;
- if(a_nPackId== OTS_SOFT_PACKAGE_ID::OTSIncA)
- { // software package project manager file pathname
- strOTSPackSysSTDlibPathName = strOTSPackSysDataPathName + SYS_STD_LIB_FILE_NAME;
-
- }
- else
- {
- strOTSPackSysSTDlibPathName = strOTSPackSysDataPathName + SYS_PartSTD_LIB_FILE_NAME;
- }
-
- // return software package license file pathname
- return strOTSPackSysSTDlibPathName;
- }
- }
|