#include "StdAfx.h" #include "CommandLineInfoEx.h" CCommandLineInfoEx::CCommandLineInfoEx(void) : m_bRunasAdmin(false) , m_bChildProcess(false) , m_nShellCommand(Nothing) , m_bResume(false) , m_bVisable(false) , m_bStartPdf(false) { } CCommandLineInfoEx::~CCommandLineInfoEx(void) { } bool CCommandLineInfoEx::IsRunasAdmin() { return m_bRunasAdmin; } bool CCommandLineInfoEx::IsChildProcess() { return m_bChildProcess; } bool CCommandLineInfoEx::GetWindowHandle(wstring& strWindowHandle) { strWindowHandle = m_strWindow; if (strWindowHandle.length() == 0) { return false; } return true; } bool CCommandLineInfoEx::HasCommand(int nCmd) { return (m_nShellCommand == nCmd); } void CCommandLineInfoEx::ParseCommandLine(CCommandLineInfoEx& rCmdInfo) { for (int i = 0; i < __argc; i++) { LPCTSTR pszParam = __targv[i]; BOOL bFlag = FALSE; BOOL bLast = ((i + 1) == __argc); if (pszParam[0] == '-' || pszParam[0] == '/') { // remove flag specifier bFlag = TRUE; ++pszParam; } rCmdInfo.ParseParam(pszParam, bFlag, bLast); } } void CCommandLineInfoEx::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast) { if (bFlag) { const CStringA strParam(pszParam); ParseParamFlag(strParam.GetString()); } else ParseParamNotFlag(pszParam); ParseLast(bLast); } #ifdef UNICODE void CCommandLineInfoEx::ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast) { if (bFlag) ParseParamFlag(pszParam); else ParseParamNotFlag(pszParam); ParseLast(bLast); } #endif // UNICODE void CCommandLineInfoEx::ParseParamFlag(const char* pszParam) { if (lstrcmpA(pszParam, "child") == 0) { m_bChildProcess = true; } else if (lstrcmpA(pszParam, "wf") == 0) { m_nShellCommand = WriteFlashCookie; } else if (lstrcmpA(pszParam, "df") == 0) { m_nShellCommand = DelFlashCookie; } else if (lstrcmpA(pszParam, "") == 0) { m_nShellCommand = Nothing; } } void CCommandLineInfoEx::ParseParamNotFlag(const TCHAR* pszParam) { if (_tcscmp(pszParam, _T("runas")) == 0) { m_bRunasAdmin = true; } else if (ParseWindow(pszParam, m_strWindow)) { } else if (ParseSharePath(pszParam, m_strSharePath)) { } else if (ParseResume(pszParam, m_bResume)) { } else if (ParseVisable(pszParam, m_bVisable)) { } else if (ParseStartPdf(pszParam, m_bStartPdf)) { } else if (ParseRect(pszParam, m_rcRect)) { } } #ifdef UNICODE void CCommandLineInfoEx::ParseParamNotFlag(const char* pszParam) { if (_stricmp(pszParam, "runas") == 0) { m_bRunasAdmin = true; } } #endif void CCommandLineInfoEx::ParseLast(BOOL bLast) { } BOOL CCommandLineInfoEx::ParseWindow(wstring strUrl, wstring& strWindow) { const wstring strKey = _T("window="); int nPos = strUrl.find(strKey); if (nPos < 0) { return FALSE; } nPos += strKey.length(); if (nPos >= (int)strUrl.length()) { return FALSE; } strWindow = strUrl.substr(nPos); return TRUE; } BOOL CCommandLineInfoEx::ParseSharePath(wstring strUrl, wstring& strPath) { const wstring strKey = _T("sharepath="); int nPos = strUrl.find(strKey); if (nPos < 0) { return FALSE; } nPos += strKey.length(); if (nPos >= (int)strUrl.length()) { return FALSE; } strPath = strUrl.substr(nPos); return TRUE; } BOOL CCommandLineInfoEx::ParseResume(wstring strUrl, bool& bResume) { const wstring strKey = _T("resume="); int nPos = strUrl.find(strKey); if (nPos < 0) { return FALSE; } nPos += strKey.length(); if (nPos >= (int)strUrl.length()) { return FALSE; } wstring strResume = strUrl.substr(nPos); if (strResume == L"1") { bResume = true; } else { bResume = false; } return TRUE; } BOOL CCommandLineInfoEx::ParseVisable(wstring strUrl, bool& bVisable) { const wstring strKey = _T("visable="); int nPos = strUrl.find(strKey); if (nPos < 0) { return FALSE; } nPos += strKey.length(); if (nPos >= (int)strUrl.length()) { return FALSE; } wstring strVisable = strUrl.substr(nPos); if (strVisable == L"1") { bVisable = true; } else { bVisable = false; } return TRUE; } BOOL CCommandLineInfoEx::ParseStartPdf(wstring strUrl, bool& bStartPdf) { const wstring strKey = _T("startpdf="); int nPos = strUrl.find(strKey); if (nPos < 0) { return FALSE; } nPos += strKey.length(); if (nPos >= (int)strUrl.length()) { return FALSE; } wstring strStartPdf = strUrl.substr(nPos); if (strStartPdf == L"1") { bStartPdf = true; } else { bStartPdf = false; } return TRUE; } BOOL CCommandLineInfoEx::ParseRect(wstring strUrl, RECT& rcRect) { const wstring strKey = _T("rect="); int nPos = strUrl.find(strKey); if (nPos < 0) { return FALSE; } nPos += strKey.length(); if (nPos >= (int)strUrl.length()) { return FALSE; } wstring strRect = strUrl.substr(nPos); std::vector vctRect; if (split(strRect, L",", vctRect) && vctRect.size() == 4) { rcRect.left = _wtoi(vctRect[0].c_str()); rcRect.top = _wtoi(vctRect[1].c_str()); rcRect.right = _wtoi(vctRect[2].c_str()); rcRect.bottom = _wtoi(vctRect[3].c_str()); } return TRUE; } typedef basic_string::size_type S_T; bool CCommandLineInfoEx::split(const std::wstring& src, std::wstring delimit, std::vector &v, std::wstring null_subst) { if (src.empty() || delimit.empty()) return false; S_T deli_len = delimit.size(); long index = -1, last_search_position = 0; while ((index = src.find(delimit,last_search_position)) != -1) { if (index == last_search_position) v.push_back(null_subst); else v.push_back(src.substr(last_search_position, index-last_search_position)); last_search_position = index + deli_len; } std::wstring last_one = src.substr(last_search_position); v.push_back(last_one.empty() ? null_subst : last_one); return true; }