// MBAMDBFun.cpp : implementation file // #include "stdafx.h" #include "mbamdb.h" #include "MBAMDBFun.h" #include "MBAMDBDef.h" // CMBAMDBFun CMBAMDBFun::CMBAMDBFun() { } CMBAMDBFun::~CMBAMDBFun() { } // CMBAMDBFun member functions CString CMBAMDBFun::GetDefaultIniFile() { CString strFileName; strFileName.Format( _T("%s\\%s"), CBaseCommFun::GetAMDataDir( ), INI_AMDEFAULT_CONFIGFILE ); return strFileName; } // CMBAMDBFun member functions CString CMBAMDBFun::GetDBReplaceStr( CString strData ) { CString strValue; strValue = strData; strValue.Replace( _T("'"), _T("''") ); return strValue; } CString CMBAMDBFun::FormatDBTIMEToStr( COleDateTime dtData ) { CString strDate; strDate = dtData.Format(_T("%Y-%m-%d %H:%M:%S")); return strDate; } void CMBAMDBFun::WriteRowToFile(CString strFileName, CString strContent ) { CFile cfile; CFileException ex; if (!cfile.Open(strFileName, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate, &ex)) { // complain if an error happened // no need to delete the ex object TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); TRACE("Couldn't open source ", szError); return; } #ifdef _UNICODE if ( cfile.GetLength( ) == 0 ) { WORD unicode = 0xFEFF; //UNICODE±àÂëÎļþÍ· cfile.Write( &unicode, 2 ); } #endif cfile.SeekToEnd(); cfile.Write( strContent.GetBuffer( strContent.GetLength( ) ), strContent.GetLength( ) * sizeof(TCHAR) ); strContent.ReleaseBuffer( ); cfile.Close(); } void CMBAMDBFun::WriteDBErrToFile( CString strErrInfo ) { CString strWorkDir = CBaseCommFun::GetModulePath( ); CString strErrPath = strWorkDir + _T("\\Log\\AMDBLog.txt"); CHighTime dtCurTime = CHighTime::GetPresentTime( true ); CString strInfo; strInfo.Format( _T("\r\n%s:%s"), dtCurTime.Format( _T("%Y-%m-%d %H:%M:%S") ), strErrInfo ); CMBAMDBFun::WriteRowToFile( strErrPath, strInfo ); } int CMBAMDBFun::GetStringLen(CString strText) { LPSTR pszA = NULL; LPSTR pszB = NULL; char chRes; CStringA strChinese = ""; #ifdef UNICODE DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, strText.GetBuffer(0), -1, NULL, 0, NULL, FALSE); pszA = (LPSTR) CoTaskMemAlloc(dwNum+1); if ( !pszA) return strText.GetLength(); ZeroMemory(pszA,dwNum+1); WideCharToMultiByte(CP_OEMCP, NULL, strText.GetBuffer(0), -1, pszA, dwNum, NULL, FALSE); strChinese.Format(("%s"),pszA); CoTaskMemFree(pszA); pszA = NULL; #else strChinese = strText; #endif return strChinese.GetLength(); } CString CMBAMDBFun::GetSubjectFromFormatText( CString strFormatText) { CString strSubject; COIXMLParser xmlParser; IXMLDOMElementPtr ptrBody; IXMLDOMNodePtr ptrNode,ptrNext; CString strNodeName,strNodeValue,strAttrValue; CString strMsgItem; if(strFormatText.IsEmpty()) { strSubject = strFormatText; return strSubject; } if( !xmlParser.ParseXMLStr(strFormatText, ptrBody) ) { strSubject = strFormatText; return strSubject; } strSubject = _T(""); ptrBody->get_firstChild( &ptrNode); while(ptrNode) { COIXMLParser::GetNodeContent(ptrNode, strNodeName, strNodeValue); if(strNodeName == _T("T")) { strMsgItem = strNodeValue; strMsgItem.Replace( _T("]]>"), _T("]]>") ); strSubject += strMsgItem; } else if(strNodeName == _T("I")) { #ifdef AFX_TARG_CHS strSubject += _T("[ͼƬ]") ; #else strSubject += _T("[Image]") ; #endif } else if(strNodeName == _T("F")) { CString strShortcut; if( COIXMLParser::GetNodeAttrValue(ptrNode, _T("ID"),strShortcut )) { strSubject += strShortcut ; } } ptrNode->get_nextSibling( &ptrNext); ptrNode = ptrNext; } strSubject.TrimRight(); if ( !strSubject.IsEmpty() ) { int nPos; nPos = strSubject.FindOneOf( _T("\r\n\t") ); if ( nPos >= 0 ) { strSubject = strSubject.Left( nPos ); } } return strSubject; } CString CMBAMDBFun::GetRelSubject( CString strSubject, long nMaxLen ) { int nIndex, nCount; bool bIsFirstChs = false; // ºº×Ö bool bFindFirstChs = false; TCHAR fChar; CString strData; CString strTempSubject; int nTempMaxLen; int nPosIndex; int nLen = CMBAMDBFun::GetStringLen(strSubject); if ( nLen <= nMaxLen || nLen <= 4 ) return strSubject; nCount = strSubject.GetLength(); nTempMaxLen = (nMaxLen- 3); nPosIndex = nTempMaxLen/2; strTempSubject = strSubject.Left(nTempMaxLen/2); nLen = CMBAMDBFun::GetStringLen(strTempSubject); for ( nIndex = nPosIndex; nIndex < nCount; nIndex++ ) { fChar = strSubject.GetAt( nIndex ); if ( fChar >= 0x7e ) //??ĿǰÅжÏ×Ö·ûÊDz»ÊÇË«×Ö½Ú×Ö·ûµÄ { nLen+=2; } else nLen+=1; if ( nLen > nTempMaxLen ) { break; } nPosIndex++; } strData = strSubject.Left(nPosIndex) + _T("..."); return strData; } int CMBAMDBFun::FindStringInArray( CString strData, CStringArray &asData, bool bCheckCompare ) { int nNum,nCount; nCount = asData.GetSize(); if(bCheckCompare) { for( nNum = 0 ; nNum < nCount ; nNum++ ) { if( asData.GetAt( nNum ) == strData ) return nNum; } } else { for( nNum = 0 ; nNum < nCount ; nNum++ ) { if( asData.GetAt( nNum ).CompareNoCase( strData) == 0 ) return nNum; } } return -1; } //========================================================= // ·ÖÎö²ÎÊý´®£º¡¡a = data1 & b = data2 & c=data3 // ÆäÖÐ strApart Ϊ·Ö¸ô·û£¬ÈçÉÏÃæµÄ×Ö·û´®Öзָô·ûΪ£º& // ±äÁ¿Ãû²»Çø·Ö´óСд //========================================================= BOOL CMBAMDBFun::GetParam( CString strStuff, CString strParamName , CString &strParam , CString strApart) { CString strTemp; CString strItem; int nPos; if( strStuff.IsEmpty() ) return false; strParam.Empty(); while( GetElementItem( strStuff , strItem , strApart ) ) { nPos = strItem.Find( _T("=") ); if( nPos <= 0 ) continue; strTemp = strItem.Left( nPos ); strTemp.TrimLeft(' '); strTemp.TrimRight(' '); if( strTemp.CompareNoCase( strParamName ) == 0 ) { strTemp = strItem.Mid( nPos + 1 ); strTemp.TrimLeft(' '); strTemp.TrimRight(' '); strTemp.TrimLeft('\"'); strTemp.TrimRight('\"'); strParam = strTemp; return true; } } return false; } //================================================== // ·ÖÎö²ÎÊý´®£º¡¡µÃµ½²ÎÊýÁбí //================================================== bool CMBAMDBFun::GetParam( CString strStuff, CStringArray &asVarName , CStringArray &asValue , CString strApart) { CString strTemp; CString strItem; CString strVarName,strValue; int nPos; asVarName.RemoveAll(); asValue.RemoveAll(); if( strStuff.IsEmpty() ) return true; while( GetElementItem( strStuff , strItem , strApart ) ) { nPos = strItem.Find( _T("=") ); if( nPos <= 0 ) continue; strTemp = strItem.Left( nPos ); strTemp.TrimLeft(' '); strTemp.TrimRight(' '); strVarName = strTemp; strTemp = strItem.Mid( nPos + 1 ); strTemp.TrimLeft(' '); strTemp.TrimRight(' '); strTemp.TrimLeft('\"'); strTemp.TrimRight('\"'); strTemp.TrimLeft('\''); strTemp.TrimRight('\''); strValue = strTemp; asVarName.Add( strVarName ); asValue.Add( strValue ); } return true; } int CMBAMDBFun::GetElementCount(CString strStuff,CString strApart) { int nCount = 0; CString strTemp,strTempItem; strTemp = strStuff; while( GetElementItem(strTemp, strTempItem,strApart) ) nCount++; return nCount; } //--------------------------------------------------------------------------- // µ¥Êý¾Ý void CMBAMDBFun::AddElementItem(CString &strStuff, CString strItem, CString strApart) { if( strStuff.GetLength() > 0 ) strStuff = strStuff + strApart + strItem; else strStuff = strItem; } void CMBAMDBFun::DelElementItem(CString &strStuff, CString strItem,CString strApart) { CString strTemp,strTempItem; CString strReturn; if( strItem == _T("") ) return; strReturn = _T(""); strTemp = strStuff; while( GetElementItem(strTemp, strTempItem,strApart) ) { if( strTempItem == strItem ) { if( strReturn == _T("") ) strStuff = strTemp; else strStuff = strReturn + strApart + strTemp; return; } else if( strReturn == _T("") ) strReturn = strTempItem; else strReturn = strReturn + strApart + strTempItem; } strStuff = strReturn; } BOOL CMBAMDBFun::GetElementItem(CString &strStuff, CString &strItem,CString strApart) { if( strStuff.GetLength() == 0 ) { strItem = _T(""); return FALSE; } int pos = strStuff.Find(strApart); if( pos >= 0 ) { strItem = strStuff.Left(pos); strStuff = strStuff.Right( strStuff.GetLength() - pos - strApart.GetLength() ); } else { strItem = strStuff; strStuff = _T(""); } return TRUE; } BOOL CMBAMDBFun::ExistElementItem(CString strStuff, CString strItem,CString strApart) { CString strTemp,strTempItem; strTemp = strStuff; while( GetElementItem(strTemp, strTempItem,strApart) ) { if( strTempItem == strItem ) return true; } return false; } ///////////////////////////////////////////////////////////////////////////// // Ë«Êý¾Ý ------------------------------------------------------------------ void CMBAMDBFun::AddElementItem(CString &strStuff, CString strItem1, CString strItem2, CString strApart) { if( strStuff.GetLength() > 0 ) strStuff = strStuff + strApart + strItem1 + strApart + strItem2; else strStuff = strItem1 + strApart + strItem2; } BOOL CMBAMDBFun::GetElementItem(CString &strStuff, CString &strItem1,CString &strItem2, CString strApart) { int pos; CString strItem; CString strPrompt; if( strStuff.GetLength() == 0 ) return FALSE; pos = strStuff.Find(strApart); if( pos >= 0 ) { strItem1 = strStuff.Left(pos); strStuff = strStuff.Right( strStuff.GetLength() - pos - strApart.GetLength() ); } else return FALSE; pos = strStuff.Find(strApart); if( pos >= 0 ) { strItem2 = strStuff.Left(pos); strStuff = strStuff.Right( strStuff.GetLength() - pos - strApart.GetLength() ); } else { strItem2 = strStuff; strStuff = _T(""); } strItem1.TrimLeft(); strItem1.TrimRight(); strItem2.TrimLeft(); strItem2.TrimRight(); return TRUE; } BOOL CMBAMDBFun::ExistElementItem(CString strStuff, CString strItem1, CString strItem2, CString strApart) { CString strTempItem1,strTempItem2; CString strTemp; BOOL bItem1,bItem2; strTemp = strStuff; while( GetElementItem(strTemp,strTempItem1, strTempItem2,strApart) ) { bItem1 = true; bItem2 = true; if( strItem1.GetLength() > 0 ) if( strTempItem1 != strItem1 ) bItem1 = false; if( strItem2.GetLength() > 0 ) if( strTempItem2 != strItem2 ) bItem2 = false; if( bItem1 && bItem2) return true; } return false; } BOOL CMBAMDBFun::GetElementItemByIndex(CString strStuff, int nIndex, CString &strItem , CString strApart ) { int nTemp; CString strString; CString strTemp; strString = strStuff; nTemp = 0; strItem = _T(""); while( GetElementItem( strString, strTemp, strApart ) ) { if( nTemp == nIndex ) { strItem = strTemp; return true; } nTemp++; } return false; } CString CMBAMDBFun::GetWorkDir() { CString strWorkDir; CRegKey reg; CString strSubKey; TCHAR szValue[256] = { 0 }; DWORD dwReaded; strSubKey.Format( _T("%s"), REG_SUBKEY ); if ( ERROR_SUCCESS == reg.Open( HKEY_LOCAL_MACHINE, strSubKey, KEY_READ ) ) { dwReaded = 255; if ( ERROR_SUCCESS == reg.QueryValue( szValue, _T( "WorkDir" ), &dwReaded ) && dwReaded > 0 ) { strWorkDir = szValue; strWorkDir.TrimRight(); strWorkDir.TrimRight( _T('\\') ); } reg.Close(); } if ( strWorkDir.IsEmpty() ) strWorkDir = GetModulePath( ); return strWorkDir; } CString CMBAMDBFun::GetModulePath() { CString strFileName; GetModuleFileName( NULL, strFileName.GetBuffer( MAX_PATH ), MAX_PATH ); strFileName.ReleaseBuffer(); return GetPathName( strFileName ); } CString CMBAMDBFun::GetPathName( CString strFileName ) { int nPos; CString strPathName; if ( strFileName.IsEmpty() ) return _T( "" ); nPos = strFileName.ReverseFind( '\\' ); if ( nPos < 0 ) return _T( "" ); strPathName = strFileName.Left( nPos ); return strPathName; } CString CMBAMDBFun::GetFileName( CString strFilePathName ) { int nPos; CString strFileName; if ( strFilePathName.IsEmpty() ) return _T( "" ); nPos = strFilePathName.ReverseFind( '\\' ); if ( nPos < 0 ) return strFilePathName; strFileName = strFilePathName.Right( strFilePathName.GetLength( ) - nPos - 1 ); return strFileName; } BOOL CMBAMDBFun::ExistPath( CString strPathName ) { CFileFind fileFind; BOOL bExistPath = FALSE; if ( strPathName.IsEmpty() ) return FALSE; if ( fileFind.FindFile( strPathName ) ) { BOOL bFind; do { bFind = fileFind.FindNextFile(); if ( fileFind.IsDirectory() ) { bExistPath = TRUE; break; } } while ( bFind ); fileFind.Close(); } return bExistPath; } BOOL CMBAMDBFun::ExistFile( CString strFileName ) { CFileFind fileFind; BOOL bExistFile = false; if ( strFileName.IsEmpty() ) return false; if ( fileFind.FindFile( strFileName ) ) { BOOL bFind; do { bFind = fileFind.FindNextFile(); if ( fileFind.IsDirectory() || fileFind.IsDots() ) continue; bExistFile = true; break; } while ( bFind ); fileFind.Close(); } return bExistFile; }