123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- #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<std::wstring> 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<char>::size_type S_T;
- bool CCommandLineInfoEx::split(const std::wstring& src, std::wstring delimit, std::vector<std::wstring> &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;
- }
|