| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- /**************************************************************************************************
- 版本所有(C), 2020-2022, 北京普达迪泰
- ***************************************************************************************************
- 文 件 名: CINI.cpp
- 功能描述: .ini配置文件操作类
- 版 本 号: 1.00初版
- 作 者: HYF
- 生成日期; 2021-12-20
- 最近修改:
- 函数列表:
- 修改历史:
- 1.日 期 :2021-12-20
- 作 者 : HYF
- 修改内容 : 创建文件
- **************************************************************************************************/
- #include "pch.h"
- #include "CINI.h"
- CINI::CINI()
- {
- CString szPath = Utils::GetLocalUrlDir();
- szPath.Append("/");
- szPath.Append(g_szConfigFile);
- m_bDebug = FALSE;
- #ifdef _UNICODE
- //wprintf(m_cFileName,_T("%s"),FileName);
- memset(m_cFileName, 0, sizeof(TCHAR)*BUFF_MAX);
- memcpy(m_cFileName, FileName, sizeof(TCHAR)*_tcslen(FileName));
- #else
- sprintf_s(m_cFileName, _T("%s"), szPath.GetBuffer());
- #endif
- }
- CINI::~CINI()
- {
- RemoveAll();
- }
- BOOL CINI::ReadIniNodes(CStringList *li, TCHAR *FileName)
- {
- RemoveAll();
- FILE *m_fp;
- long filelen = 0, tempoffe;
- CString t;
- NodeData *m_nodedata = NULL;
- #ifdef _UNICODE
- if (FileName != NULL)
- wcscpy(m_cFileName, FileName);
- m_fp = _wfopen(m_cFileName, _T("r"));
- if (m_fp == NULL) return false;
- if (li != NULL)
- li->RemoveAll();
- fseek(m_fp, 0, SEEK_END);
- filelen = ftell(m_fp);
- fseek(m_fp, 0, SEEK_SET);
- while (ftell(m_fp) < filelen)
- {
- fgetws(m_cReadBuff, BUFF_MAX, m_fp);
- tempoffe = ftell(m_fp);
- t.Format(_T("%s"), m_cReadBuff);
- t.TrimLeft();
- t.MakeUpper();
- t.Remove(10);
- t.Remove(13);
- t.Remove(9);
- swprintf(m_cReadBuff, _T("%s"), t.GetBuffer(t.GetLength()));
- t.Delete(t.GetLength() - 1);
- t.Delete(0);
- if (m_cReadBuff[0] == ';')
- continue;
- if (t.GetLength() > 0)
- {
- if (m_cReadBuff[0] == '[')
- {
- if (li != NULL)
- li->AddTail(t);
- m_nodedata = new NodeData;
- m_nodedata->m_nIndex = m_pNodeList.GetCount() + 1;
- m_nodedata->m_SubCount = 0;
- m_nodedata->size = wcslen(m_cReadBuff);
- m_nodedata->strNode = new TCHAR[m_nodedata->size];
- m_nodedata->m_ByteOffe = tempoffe;
- wsprintf(m_nodedata->strNode, _T("%s"), m_cReadBuff);
- m_pNodeList.InsertAfter(m_pNodeList.GetTailPosition(), m_nodedata);
- }
- }
- }
- _fcloseall();
- #else
- if (FileName != NULL) sprintf_s(m_cFileName, "%s", FileName);
- if (m_cFileName[0] == NULL)
- {
- MessageBox(GetFocus(), "m_cFileName = NULL", "CINI", 0);
- return false;
- }
- m_fp = fopen(m_cFileName, "r");
- if (m_fp == NULL)
- {
- MessageBox(GetFocus(), "m_fp=fopen(m_cFileName,r)==NULL", "CINI", 0);
- return false;
- }
- if (li != NULL)
- if (li->GetCount() > 0)
- li->RemoveAll();
- fseek(m_fp, 0, SEEK_END);
- filelen = ftell(m_fp);
- fseek(m_fp, 0, SEEK_SET);
- while (ftell(m_fp) < filelen)
- {
- fgets(m_cReadBuff, BUFF_MAX, m_fp);
- tempoffe = ftell(m_fp);
- t.Format("%s", m_cReadBuff);
- t.MakeUpper();
- t.Remove(10);
- t.Remove(13);
- t.Remove(9);
- sprintf_s(m_cReadBuff, "%s", t.GetBuffer(t.GetLength()));
- t.Delete(t.GetLength() - 1);
- t.Delete(0);
- t.TrimLeft();
- if (m_cReadBuff[0] == ';')
- continue;
- if (t.GetLength() > 0)
- {
- if (m_cReadBuff[0] == '[')
- {
- if (li != NULL)
- li->AddTail(t);
- m_nodedata = new NodeData;
- m_nodedata->m_nIndex = m_pNodeList.GetCount() + 1;
- m_nodedata->m_SubCount = 0;
- m_nodedata->size = strlen(m_cReadBuff);
- m_nodedata->strNode = new char[m_nodedata->size];
- m_nodedata->m_ByteOffe = tempoffe;
- sprintf(m_nodedata->strNode, "%s", m_cReadBuff);
- m_pNodeList.InsertAfter(m_pNodeList.GetTailPosition(), m_nodedata);
- }
- }
- }
- fclose(m_fp);
- #endif
- return true;
- }
- BOOL CINI::ReadIniNodeSubData(LPCTSTR Nodestr, CStringList *li)
- {
- if (m_bDebug)MessageBox(NULL, _T("Start"), _T("ReadIniNodeSubData"), 0);
- if (m_cFileName[0] == NULL)
- return false;
- if (m_bDebug)MessageBox(NULL, _T("Start2"), _T("ReadIniNodeSubData"), 0);
- FILE *m_fp;
- long filelen = 0, tempoffe;
- CString t;
- NodeData *m_nodedata = NULL;
- if (m_bDebug)MessageBox(NULL, _T("Start3"), _T("ReadIniNodeSubData"), 0);
- if (Nodestr != NULL)
- {
- m_nodedata = GetNode(Nodestr);
- if (m_nodedata == NULL)
- {
- return false;
- }
- tempoffe = m_nodedata->m_ByteOffe;
- }
- else
- return false;
- if (m_bDebug)MessageBox(NULL, _T("Start4"), _T("ReadIniNodeSubData"), 0);
- #ifdef _UNICODE
- m_fp = _wfopen(m_cFileName, _T("r"));
- if (m_fp == NULL)
- return false;
- fseek(m_fp, 0, SEEK_END);
- filelen = ftell(m_fp);
- fseek(m_fp, tempoffe, SEEK_SET);
- while (ftell(m_fp) < filelen)
- {
- tempoffe = ftell(m_fp);
- fgetws(m_cReadBuff, BUFF_MAX, m_fp);
- t.Format(_T("%s"), m_cReadBuff);
- t.MakeUpper();
- t.Remove(10);
- t.Remove(13);
- t.TrimLeft();
- wsprintf(m_cReadBuff, _T("%s"), t.GetBuffer(t.GetLength()));
- if (m_cReadBuff[0] == ';')
- continue;
- if (t.GetLength() > 0)
- {
- if (m_cReadBuff[0] != '[')
- {
- if (li != NULL)
- li->AddTail(t);
- m_nodedata = new NodeData;
- m_nodedata->m_nIndex = m_pNodeList.GetCount() + 1;
- m_nodedata->m_SubCount = 0;
- m_nodedata->size = wcslen(m_cReadBuff);
- m_nodedata->strNode = new TCHAR[m_nodedata->size];
- m_nodedata->m_ByteOffe = tempoffe;
- wsprintf(m_nodedata->strNode, _T("%s"), m_cReadBuff);
- m_pNodeList.InsertAfter(m_pNodeList.GetTailPosition(), m_nodedata);
- }
- else
- break;
- }
- }
- fclose(m_fp);
- #else
- if (m_bDebug)MessageBox(NULL, "Start5", "ReadIniNodeSubData", 0);
- if (li != NULL) li->RemoveAll();
- m_fp = fopen(m_cFileName, "r");
- if (m_fp == NULL) return false;
- if (m_bDebug)MessageBox(NULL, "Start6", "ReadIniNodeSubData", 0);
- fseek(m_fp, 0, SEEK_END);
- filelen = ftell(m_fp);
- fseek(m_fp, tempoffe, SEEK_SET);
- if (m_bDebug)MessageBox(NULL, "Start7", "ReadIniNodeSubData", 0);
- while (ftell(m_fp) < filelen)
- {
- if (m_bDebug)MessageBox(NULL, "Start81", "ReadIniNodeSubData", 0);
- tempoffe = ftell(m_fp);
- fgets(m_cReadBuff, BUFF_MAX, m_fp);
- t.Format("%s", m_cReadBuff);
- if (t.GetLength() > 0)
- {
- t.MakeUpper();
- t.Remove(10);
- t.Remove(13);
- t.TrimLeft();
- }
- if (m_bDebug)MessageBox(NULL, m_cReadBuff, "ReadIniNodeSubData:82", 0);
- sprintf_s(m_cReadBuff, "%s", t.GetBuffer(t.GetLength()));
- if (m_cReadBuff[0] == ';') continue;
- if (t.GetLength() > 0)
- {
- if (m_cReadBuff[0] != '[')
- {
- if (li != NULL)
- {
- if (li != NULL)
- li->AddTail(t);
- m_nodedata = new NodeData;
- m_nodedata->m_nIndex = m_pNodeList.GetCount() + 1;
- m_nodedata->m_SubCount = 0;
- m_nodedata->size = strlen(m_cReadBuff);
- m_nodedata->strNode = new char[m_nodedata->size];
- m_nodedata->m_ByteOffe = tempoffe;
- sprintf(m_nodedata->strNode, "%s", m_cReadBuff);
- m_pNodeList.InsertAfter(m_pNodeList.GetTailPosition(), m_nodedata);
- }
- }
- else
- break;
- }
- if (m_bDebug)MessageBox(NULL, "Start83", "ReadIniNodeSubData", 0);
- }
- fclose(m_fp);
- #endif
- return true;
- }
- int CINI::GetNodeSubCount(LPCTSTR str)
- {
- NodeData *pnd = GetNode(str);
- if (pnd != NULL)
- return pnd->m_SubCount;
- return 0;
- }
- BOOL CINI::WriteStrVal(LPCTSTR Node, LPCTSTR Key, TCHAR* Value)
- {
- return WritePrivateProfileString(Node, Key, Value, m_cFileName);
- }
- BOOL CINI::WriteIntVal(LPCTSTR Node, LPCTSTR Key, int n)
- {
- CString wbuff;
- wbuff.Format(_T("%d"), n);
- return WritePrivateProfileString(Node, Key, (LPCTSTR)wbuff, m_cFileName);
- }
- BOOL CINI::DeleteKey(LPCTSTR Node, LPCTSTR Key)
- {
- return WritePrivateProfileString(Node, Key, NULL, m_cFileName);
- }
- BOOL CINI::DeleteNode(LPCTSTR Node)
- {
- return WritePrivateProfileString(Node, NULL, NULL, m_cFileName);
- }
- void CINI::SetFileName(LPCTSTR Filename)
- {
- #ifdef _UNICODE
- wcscpy(m_cFileName, Filename);
- #else
- strcpy(m_cFileName, Filename);
- #endif
- }
- void CINI::RemoveAll()
- {
- if (m_pNodeList.GetCount() <= 0)
- return;
- TCHAR *d;
- for (POSITION pos = m_pNodeList.GetHeadPosition(); pos != NULL;)
- {
- NodeData* ndtemp = (NodeData*)m_pNodeList.GetNext(pos);
- if (ndtemp != NULL)
- {
- d = ndtemp->strNode;
- if (d != NULL)
- {
- //delete[] d;
- ndtemp->strNode = NULL;
- delete ndtemp;
- ndtemp = NULL;
- }
- }
- }
- m_pNodeList.RemoveAll();
- }
- NodeData *CINI::GetNode(LPCTSTR findc)
- {
- if (m_pNodeList.GetCount() <= 0)
- return NULL;
- TCHAR findBuff[512];
- for (POSITION pos = m_pNodeList.GetHeadPosition(); pos != NULL;)
- {
- NodeData *ndtemp = (NodeData*)m_pNodeList.GetNext(pos);
- if (ndtemp != NULL)
- {
- #ifdef _UNICODE
- wsprintf(findBuff, _T("%s"), ndtemp->strNode);
- if (wcscmp(findc, findBuff) == 0)
- return ndtemp;
- #else
- sprintf_s(findBuff, _T("%s"), ndtemp->strNode);
- // MessageBox(GetFocus(),findBuff,findc,0);
- if (strcmp(findc, findBuff) == 0)
- return ndtemp;
- #endif
- }
- }
- return NULL;
- }
- float CINI::ReadFloatVal(LPCTSTR strHead, LPCTSTR strKey, const float idefault)
- {
- TCHAR buff[512];
- if (ReadStrVal(strHead, strKey, buff, 512))
- //return (float)atof(buff);
- return (float)_ttof(buff);
- else
- {
- WriteFloatVal(strHead, strKey, idefault);
- return idefault;
- }
- }
- double CINI::ReadDoubleVal(LPCTSTR strHead, LPCTSTR strKey, const double idefault)
- {
- TCHAR buff[512];
- if (ReadStrVal(strHead, strKey, buff, 512))
- return (double)_ttof(buff);
- else
- {
- _stprintf(buff, _T("%lf"), idefault);
- WriteDoubleVal(strHead, strKey, idefault);
- return idefault;
- }
- }
- BOOL CINI::GetInitFlag()
- {
- if (_tcslen(m_cFileName) == 0) return FALSE;
- else return TRUE;
- }
- BOOL CINI::ReadStrVal(LPCTSTR strHead, LPCTSTR strKey, CString &buff)
- {
- TCHAR buffx[2048];
- if (ReadStrVal(strHead, strKey, buffx, 2048))
- {
- buff.Format(_T("%s"), buffx);
- return TRUE;
- }
- else
- {
- _stprintf(buffx, _T("%s"), buff);
- WriteStrVal(strHead, strKey, buffx);
- return FALSE;
- }
- }
- BOOL CINI::ReadStrVal(LPCTSTR strHead, LPCTSTR strKey, TCHAR *buff, int size)
- {
- if (m_cFileName[0] == _T('\0')) return FALSE;
- return GetPrivateProfileString(strHead, strKey, _T(""), buff, size, m_cFileName);
- }
- UINT CINI::ReadIntVal(LPCTSTR strHead, LPCTSTR strKey, const UINT idefault)
- {
- TCHAR buff[512];
- if (ReadStrVal(strHead, strKey, buff, 512))
- return (UINT)_ttol(buff);
- else
- {
- WriteIntVal(strHead, strKey, idefault);
- return idefault;
- }
- }
- BOOL CINI::GetSubValueFromStr(LPCTSTR str, CString &strDest, int index)
- {
- CString strTemp, strTemp2;
- int ni = 0;
- strTemp = str;
- strTemp.Remove(10);
- strTemp.Remove(13);
- strTemp.Remove(9);
- strTemp.Remove(' ');
- int nStart = strTemp.Find(_T('='), 0);
- if (nStart > -1) strTemp.SetAt(nStart, _T(','));
- strDest = _T("");
- //MessageBox(NULL,strTemp,"CINI",0);
- while (strTemp.GetLength() > 0)
- {
- if (ni == index)
- {
- strDest += strTemp.GetAt(0);
- strTemp.Delete(0);
- if (strTemp.GetLength() == 0)
- {
- if (strDest.GetLength() > 0)
- return TRUE;
- }
- else
- if (strTemp.GetAt(0) == _T(',')) return TRUE;
- }
- else
- {
- if (strTemp.GetAt(0) == _T(',')) ni++;
- strTemp.Delete(0);
- }
- }
- if (strDest.GetLength() == 0) return FALSE;
- else
- return TRUE;
- }
- BOOL CINI::GetSubValueFromStr(LPCTSTR str, int &iDest, int index)
- {
- CString strTemp;
- if (GetSubValueFromStr(str, strTemp, index))
- {
- iDest = _ttoi(strTemp);
- return TRUE;
- }
- return FALSE;
- }
- BOOL CINI::GetSubValueFromStr(LPCTSTR str, double &fDest, int index)
- {
- CString strTemp;
- if (GetSubValueFromStr(str, strTemp, index))
- {
- fDest = _ttof(strTemp);
- return TRUE;
- }
- return FALSE;
- }
- BOOL CINI::GetSubValueFromStrEx(LPCTSTR str, CString &strDest, int index)
- {
- CString strTemp, strTemp2;
- int ni = 0;
- strTemp = str;
- strTemp.Remove(10);
- strTemp.Remove(13);
- strTemp.TrimLeft();
- strTemp.TrimRight();
- BOOL bdot = FALSE;
- strDest = "";
- int i = 0;
- while (i < strTemp.GetLength())
- {
- if (strTemp.GetAt(i) == _T('='))
- {
- if (!bdot)
- strTemp.SetAt(i, _T(','));
- else strTemp.Delete(i);
- bdot = TRUE;
- }
- else if (strTemp.GetAt(i) == 9)
- {
- if (!bdot)
- strTemp.SetAt(i, _T(','));
- else strTemp.Delete(i);
- bdot = TRUE;
- }
- else if (strTemp.GetAt(i) == _T(' '))
- {
- if (!bdot)
- strTemp.SetAt(i, _T(','));
- else strTemp.Delete(i);
- bdot = TRUE;
- }
- else if (strTemp.GetAt(i) == _T(','))
- {
- if (bdot)
- strTemp.Delete(i);
- bdot = TRUE;
- }
- else bdot = FALSE;
- i++;
- }
- while (strTemp.GetLength() > 0)
- {
- if (ni == index)
- {
- strDest += strTemp.GetAt(0);
- strTemp.Delete(0);
- if (strTemp.GetLength() == 0)
- {
- if (strDest.GetLength() > 0)
- return TRUE;
- }
- else
- if (strTemp.GetAt(0) == _T(',')) return TRUE;
- }
- else
- {
- if (strTemp.GetAt(0) == _T(',')) ni++;
- strTemp.Delete(0);
- }
- }
- if (strDest.GetLength() == 0) return FALSE;
- else
- return TRUE;
- }
- BOOL CINI::GetSubValueFromStrEx(LPCTSTR str, int &iDest, int index)
- {
- CString strTemp;
- if (GetSubValueFromStrEx(str, strTemp, index))
- {
- iDest = _ttoi(strTemp);
- return TRUE;
- }
- return FALSE;
- }
- BOOL CINI::GetSubValueFromStrEx(LPCTSTR str, double &fDest, int index)
- {
- CString strTemp;
- if (GetSubValueFromStrEx(str, strTemp, index))
- {
- fDest = _ttof(strTemp);
- return TRUE;
- }
- return FALSE;
- }
- BOOL CINI::WriteDoubleVal(LPCTSTR Node, LPCTSTR Key, double n)
- {
- CString wbuff;
- wbuff.Format(_T("%f"), n);
- return WritePrivateProfileString(Node, Key, (LPCTSTR)wbuff, m_cFileName);
- }
- BOOL CINI::WriteFloatVal(LPCTSTR Node, LPCTSTR Key, float n)
- {
- CString wbuff;
- wbuff.Format(_T("%f"), n);
- return WritePrivateProfileString(Node, Key, (LPCTSTR)wbuff, m_cFileName);
- }
- BOOL CINI::WriteStrVal(LPCTSTR strHead, LPCTSTR strKey, LPCTSTR strValue)
- {
- BOOL rb;
- CString str = strValue;
- TCHAR *cbuff = new TCHAR[str.GetLength() + 1];
- _stprintf(cbuff, _T("%s"), strValue);
- rb = WriteStrVal(strHead, strKey, cbuff);
- delete[]cbuff;
- cbuff = NULL;
- return rb;
- }
|