void CSVString( const TStringVector &sv, TString &string, char adelimiter ) { TString word; int i; int n = sv.size(); int totallength = 0; // int pos = 0; char *pos; if( sv.size()==0 ) { string = "\"\""; return; } for( unsigned int i=0; i<n; i++ ) { word = CSVString( sv[i], adelimiter ); totallength += word.Length(); } if( n>0 ) totallength += n-1; string.SetLength( totallength ); pos = string.c_str(); for( unsigned int i=0; i<n; i++ ) { word = CSVString( sv[i], adelimiter ); strcpy( pos, word.c_str() ); pos += word.Length(); if( i<(n-1) ) { *pos = adelimiter; pos++; } } }
// D: convert a string list of attribute values into a STRING2STRING // representation STRING2STRING StringToS2SHash(string sString, string sSeparator, string sEquals) { // extract the pairs TStringVector vsPairs = PartitionString(sString, (char *)sSeparator.c_str()); // form the hash STRING2STRING s2s; for(unsigned int i = 0; i < vsPairs.size(); i++) { string sAttr, sValue; SplitOnFirst(vsPairs[i], (char *)sEquals.c_str(), sAttr, sValue); s2s.insert(STRING2STRING::value_type(Trim(sAttr), Trim(sValue))); } // finally, return the constructed hash return s2s; }
bool CComboboxImp::SetListItems( const TStringVector& items ) {GUCE_TRACE; if ( NULL != m_combobox ) { m_combobox->resetList(); for ( UInt32 i=0; i<items.size(); ++i ) { m_combobox->addItem( new ListItem( items[ i ].C_String() ) ); } return true; } return false; }
BOOL Shell::RefershPartitions() { for (int i = 0; i < 26; i++) { m_partitions[i].bValid = FALSE; } CommData request; request.SetMsgID(MSGID_DISKS); CommData commData; if (! AskAndWaitForReply(request, commData)) { return FALSE; } DECLARE_STR_PARAM(result); TStringVector partitionVector; splitByChar(result.c_str(), partitionVector, ':'); BOOL bFoundOne = FALSE; TStringVector::iterator partitionIter = partitionVector.begin(); for (; partitionIter != partitionVector.end(); partitionIter++) { tstring& partitionStr = *partitionIter; TStringVector infoVector; splitByChar(partitionStr.c_str(), infoVector, '|'); if (infoVector.size() != 4) continue; //partition(str)|drivertype(uint)|totalbytes(uint64)|freebytes(uint64): if (infoVector[0].size() == 0) continue; TCHAR partition = infoVector[0][0]; int index = 0; if (partition >= 'a' && partition <= 'z') index = partition - 'a'; else if (partition >= 'A' && partition <= 'Z') index = partition - 'A'; else continue; if (1 != _stscanf_s(infoVector[1].c_str(), _T("%u"), &m_partitions[index].drivertype)) continue; if (1 != _stscanf_s(infoVector[2].c_str(), _T("%I64u"), &m_partitions[index].totalBytes)) continue; if (1 != _stscanf_s(infoVector[3].c_str(), _T("%I64u"), &m_partitions[index].freeBytes)) continue; m_partitions[index].curPath = ('A' + index); m_partitions[index].curPath += _T(":\\"); m_partitions[index].bValid = TRUE; bFoundOne = TRUE; } return bFoundOne; }
BOOL CommandManager::Execute( LPCTSTR cmdline, tstring& replyText ) { //分割并整理命令行的各个部分 TStringVector parts; splitByChar(cmdline, parts, ' '); TStringVector::iterator iter = parts.begin(); while (iter != parts.end()) { tstring& part = *iter; trim(part); if (part.size() == 0) { iter = parts.erase(iter); } else { iter++; } } //检查是否有可用的部分 if (parts.size() == 0) { replyText = _T("invalid command."); return FALSE; } //查找可用的命令 tstring& cmdname = parts[0]; makeLower(cmdname); CommandMap::iterator cmdIter = m_cmdMap.find(cmdname); if (cmdIter == m_cmdMap.end()) { replyText = _T("no such command."); return FALSE; } //执行命令 ICmd* pCmd = cmdIter->second; BOOL bExecuteOK = pCmd->Execute(parts, replyText, m_env); return bExecuteOK; }
bool CPing::Start( const TStringVector& remoteHosts , const Int32 maxPings /* = 0 */ , const UInt32 bytesToSend /* = 32 */ , const UInt32 timeout /* = 1000 */ , const UInt32 minimalPingDelta /* = 500 */ ) {GUCEF_TRACE; if ( !m_isActive && ( remoteHosts.size() > 0 ) ) { m_remoteHosts = remoteHosts; m_maxPings = maxPings; m_timeout = timeout; m_bytesToSend = bytesToSend; m_minimalPingDelta = minimalPingDelta; CPingTaskConsumer::CPingTaskData taskData( remoteHosts, maxPings, bytesToSend, timeout, minimalPingDelta ); return CORE::CCoreGlobal::Instance()->GetTaskManager().StartTask( m_pingTaskConsumer , &taskData ); } return false; }
BOOL Shell::ExecuteCommand( LPCTSTR cmdlinestr, tstring& reply ) { //分割并整理命令行的各个部分 tstring cmdline = cmdlinestr; trim(cmdline, ' '); trim(cmdline, '\n'); trim(cmdline, '\r'); TStringVector parts; splitByChar(cmdline.c_str(), parts, ' '); //清理无效参数 TStringVector::iterator iter = parts.begin(); while (iter != parts.end()) { tstring& part = *iter; trim(part); if (part.size() == 0) { iter = parts.erase(iter); } else { iter++; } } //检查是否有可用的部分 if (parts.size() == 0) { reply = _T(""); return TRUE; } //查找可用的命令 tstring& cmdname = parts[0]; makeLower(cmdname); if (cmdname == _T("dir")) { return Execute_Dir(parts, reply); } else if (cmdname == _T("cd")) { return Execute_Cd(parts, reply); } else if (cmdname == _T("disks")) { return Execute_Disks(parts, reply); } //将命令发到客户端去执行 CommData commandCommData; commandCommData.SetMsgID(MSGID_EXECUTE_CMDLINE); commandCommData.SetData(_T("cmdline"), cmdline.c_str()); CommData replyCommData; if (! AskAndWaitForReply(commandCommData, replyCommData)) { reply = _T("等待客户端回应超时"); return FALSE; } else { tstring result; replyCommData.GetStrData(_T("result"), result); if (result == _T("OK")) { reply = s2ws(std::string((LPCSTR)(LPBYTE)replyCommData.GetByteData(), replyCommData.GetByteData().Size())); return TRUE; } else { reply = result; return FALSE; } } }
BOOL Shell::Execute_Cd( const TStringVector& cmdparts, tstring& reply ) { if (cmdparts.size() == 1) { reply = m_currentPath; return TRUE; } else if (cmdparts.size() > 1) { tstring target = cmdparts[1]; if (target.size() == 2 && target[1] == ':') { //切换当前分区 int index = 0; TCHAR partition = target[0]; if (partition >= 'a' && partition <= 'z') index = partition - 'a'; else if (partition >= 'A' && partition <= 'Z') index = partition - 'A'; else { reply = _T("invalid partition"); return FALSE; } if (! m_partitions[index].bValid) { reply = _T("invalid partition"); return FALSE; } m_currentPath = m_partitions[index].curPath; reply = m_currentPath; return TRUE; } else { tstring testPath; if (target.size() > 2 && target[1] == ':') { //绝对路径 testPath = target; } else { //相对路径 testPath = m_currentPath; testPath += '\\'; testPath += target; MakeAbsolutePath(testPath.c_str(), testPath); } BOOL bExists = FALSE; BOOL bDir = FALSE; if (FileExistsInClient(testPath.c_str(), bExists, bDir)) { if (bExists && bDir) { SetCurrentPath(testPath.c_str()); reply = m_currentPath; return TRUE; } else { reply = _T("invalid filepath"); return FALSE; } } else { reply = _T("等待客户端回应超时"); return FALSE; } } } return FALSE; }
BOOL Shell::Execute_Dir( const TStringVector& cmdparts, tstring& reply ) { tstring findstr = _T("*"); if (cmdparts.size() > 1) { findstr = cmdparts[1]; } findstr = m_currentPath + _T("\\") + findstr; MakeAbsolutePath(findstr.c_str(), findstr); if (findstr.find('*') == tstring::npos && findstr.find('?') == tstring::npos) { trim(findstr, '\\'); findstr += _T("\\*"); } CommData request; request.SetMsgID(MSGID_LIST_FILES); request.SetData(_T("findstr"), findstr.c_str()); CommData commData; if (! AskAndWaitForReply(request, commData)) { reply = _T("客户端响应超时"); return FALSE; } DECLARE_STR_PARAM(result); // result str 目录内容 filename(str)|attr(dword)|filesize(uint64)|lastWriteTime(uint64 filetime): tostringstream toss; TStringVector fileParts; splitByChar(result.c_str(), fileParts, ':'); TStringVector::iterator fileIter = fileParts.begin(); for (; fileIter != fileParts.end(); fileIter++) { tstring& fileInfo = *fileIter; TStringVector attrParts; splitByChar(fileInfo.c_str(), attrParts, '|'); if (attrParts.size() != 4) continue; tstring& filename = attrParts[0]; DWORD dwAttrs = 0; ULARGE_INTEGER filesize = {0}; ULARGE_INTEGER lastWriteTime = {0}; if (1 != _stscanf_s(attrParts[1].c_str(), _T("%u"), &dwAttrs)) continue; if (1 != _stscanf_s(attrParts[2].c_str(), _T("%I64u"), &filesize.QuadPart)) continue; if (1 != _stscanf_s(attrParts[3].c_str(), _T("%I64u"), &lastWriteTime.QuadPart)) continue; SYSTEMTIME systime; FILETIME ftLastWriteTime; ftLastWriteTime.dwLowDateTime = lastWriteTime.LowPart; ftLastWriteTime.dwHighDateTime = lastWriteTime.HighPart; FILETIME ftLocalLastWriteTime; FileTimeToLocalFileTime(&ftLastWriteTime, &ftLocalLastWriteTime); FileTimeToSystemTime(&ftLocalLastWriteTime, &systime); BOOL bDir = (dwAttrs & FILE_ATTRIBUTE_DIRECTORY); TCHAR line[MAX_PATH * 2] = {0}; _stprintf_s(line, _T("%04d/%02d/%02d %02d:%02d:%02d %20s %s\r\n"), systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute, systime.wSecond, (bDir ? _T("<DIR> ") : FormatSizeWithUnit(filesize.QuadPart).c_str()), filename.c_str()); toss << line; } reply = toss.str(); return TRUE; }
void DecodeCSVString( TStringVector &strings, const TString& astring, char adelimiter ) { TString str; str.SetLength( astring.Length() ); strings.clear(); if( astring.Length()==0 ) return; enum { normal, parenthesis } state; state = normal; const char *beg = astring.c_str(); const char *c = beg; char *destbeg = str.c_str(); char *dest = destbeg; while( *c ) { switch( state ) { case normal: switch( *c ) { case '"': state = parenthesis; c++; break; default: if( *c == adelimiter ) { strings.push_back( TString( destbeg, dest-destbeg ) ); dest = destbeg; } else { *dest = *c; dest++; } c++; } break; case parenthesis: if( *c == '"' ) { c++; state = normal; } else { *dest = *c; c++; dest++; } break; default: strings.clear(); /* TODO : Nen� upln� jasn�, zda v p��pad� failu smazat to, co se dosud ud�lalo... */ throw TException( "Unknown error in DecodeCSVString." ); } } strings.push_back( TString( destbeg, dest-destbeg ) ); unsigned int nstrings = strings.size(); unsigned int ui; for( ui=0; ui<nstrings; ui++ ) ReplaceHexFormWithOrdinals( strings[ui] ); }