CommandLineInfoEx.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "StdAfx.h"
  2. #include "CommandLineInfoEx.h"
  3. CCommandLineInfoEx::CCommandLineInfoEx(void)
  4. : m_bRunasAdmin(false)
  5. , m_bChildProcess(false)
  6. , m_nShellCommand(Nothing)
  7. , m_bResume(false)
  8. , m_bVisable(false)
  9. , m_bStartPdf(false)
  10. {
  11. }
  12. CCommandLineInfoEx::~CCommandLineInfoEx(void)
  13. {
  14. }
  15. bool CCommandLineInfoEx::IsRunasAdmin()
  16. {
  17. return m_bRunasAdmin;
  18. }
  19. bool CCommandLineInfoEx::IsChildProcess()
  20. {
  21. return m_bChildProcess;
  22. }
  23. bool CCommandLineInfoEx::GetWindowHandle(wstring& strWindowHandle)
  24. {
  25. strWindowHandle = m_strWindow;
  26. if (strWindowHandle.length() == 0)
  27. {
  28. return false;
  29. }
  30. return true;
  31. }
  32. bool CCommandLineInfoEx::HasCommand(int nCmd)
  33. {
  34. return (m_nShellCommand == nCmd);
  35. }
  36. void CCommandLineInfoEx::ParseCommandLine(CCommandLineInfoEx& rCmdInfo)
  37. {
  38. for (int i = 0; i < __argc; i++)
  39. {
  40. LPCTSTR pszParam = __targv[i];
  41. BOOL bFlag = FALSE;
  42. BOOL bLast = ((i + 1) == __argc);
  43. if (pszParam[0] == '-' || pszParam[0] == '/')
  44. {
  45. // remove flag specifier
  46. bFlag = TRUE;
  47. ++pszParam;
  48. }
  49. rCmdInfo.ParseParam(pszParam, bFlag, bLast);
  50. }
  51. }
  52. void CCommandLineInfoEx::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
  53. {
  54. if (bFlag)
  55. {
  56. const CStringA strParam(pszParam);
  57. ParseParamFlag(strParam.GetString());
  58. }
  59. else
  60. ParseParamNotFlag(pszParam);
  61. ParseLast(bLast);
  62. }
  63. #ifdef UNICODE
  64. void CCommandLineInfoEx::ParseParam(const char* pszParam, BOOL bFlag, BOOL bLast)
  65. {
  66. if (bFlag)
  67. ParseParamFlag(pszParam);
  68. else
  69. ParseParamNotFlag(pszParam);
  70. ParseLast(bLast);
  71. }
  72. #endif // UNICODE
  73. void CCommandLineInfoEx::ParseParamFlag(const char* pszParam)
  74. {
  75. if (lstrcmpA(pszParam, "child") == 0)
  76. {
  77. m_bChildProcess = true;
  78. }
  79. else if (lstrcmpA(pszParam, "wf") == 0)
  80. {
  81. m_nShellCommand = WriteFlashCookie;
  82. }
  83. else if (lstrcmpA(pszParam, "df") == 0)
  84. {
  85. m_nShellCommand = DelFlashCookie;
  86. }
  87. else if (lstrcmpA(pszParam, "") == 0)
  88. {
  89. m_nShellCommand = Nothing;
  90. }
  91. }
  92. void CCommandLineInfoEx::ParseParamNotFlag(const TCHAR* pszParam)
  93. {
  94. if (_tcscmp(pszParam, _T("runas")) == 0)
  95. {
  96. m_bRunasAdmin = true;
  97. }
  98. else if (ParseWindow(pszParam, m_strWindow))
  99. {
  100. }
  101. else if (ParseSharePath(pszParam, m_strSharePath))
  102. {
  103. }
  104. else if (ParseResume(pszParam, m_bResume))
  105. {
  106. }
  107. else if (ParseVisable(pszParam, m_bVisable))
  108. {
  109. }
  110. else if (ParseStartPdf(pszParam, m_bStartPdf))
  111. {
  112. }
  113. else if (ParseRect(pszParam, m_rcRect))
  114. {
  115. }
  116. }
  117. #ifdef UNICODE
  118. void CCommandLineInfoEx::ParseParamNotFlag(const char* pszParam)
  119. {
  120. if (_stricmp(pszParam, "runas") == 0)
  121. {
  122. m_bRunasAdmin = true;
  123. }
  124. }
  125. #endif
  126. void CCommandLineInfoEx::ParseLast(BOOL bLast)
  127. {
  128. }
  129. BOOL CCommandLineInfoEx::ParseWindow(wstring strUrl, wstring& strWindow)
  130. {
  131. const wstring strKey = _T("window=");
  132. int nPos = strUrl.find(strKey);
  133. if (nPos < 0)
  134. {
  135. return FALSE;
  136. }
  137. nPos += strKey.length();
  138. if (nPos >= (int)strUrl.length())
  139. {
  140. return FALSE;
  141. }
  142. strWindow = strUrl.substr(nPos);
  143. return TRUE;
  144. }
  145. BOOL CCommandLineInfoEx::ParseSharePath(wstring strUrl, wstring& strPath)
  146. {
  147. const wstring strKey = _T("sharepath=");
  148. int nPos = strUrl.find(strKey);
  149. if (nPos < 0)
  150. {
  151. return FALSE;
  152. }
  153. nPos += strKey.length();
  154. if (nPos >= (int)strUrl.length())
  155. {
  156. return FALSE;
  157. }
  158. strPath = strUrl.substr(nPos);
  159. return TRUE;
  160. }
  161. BOOL CCommandLineInfoEx::ParseResume(wstring strUrl, bool& bResume)
  162. {
  163. const wstring strKey = _T("resume=");
  164. int nPos = strUrl.find(strKey);
  165. if (nPos < 0)
  166. {
  167. return FALSE;
  168. }
  169. nPos += strKey.length();
  170. if (nPos >= (int)strUrl.length())
  171. {
  172. return FALSE;
  173. }
  174. wstring strResume = strUrl.substr(nPos);
  175. if (strResume == L"1")
  176. {
  177. bResume = true;
  178. }
  179. else
  180. {
  181. bResume = false;
  182. }
  183. return TRUE;
  184. }
  185. BOOL CCommandLineInfoEx::ParseVisable(wstring strUrl, bool& bVisable)
  186. {
  187. const wstring strKey = _T("visable=");
  188. int nPos = strUrl.find(strKey);
  189. if (nPos < 0)
  190. {
  191. return FALSE;
  192. }
  193. nPos += strKey.length();
  194. if (nPos >= (int)strUrl.length())
  195. {
  196. return FALSE;
  197. }
  198. wstring strVisable = strUrl.substr(nPos);
  199. if (strVisable == L"1")
  200. {
  201. bVisable = true;
  202. }
  203. else
  204. {
  205. bVisable = false;
  206. }
  207. return TRUE;
  208. }
  209. BOOL CCommandLineInfoEx::ParseStartPdf(wstring strUrl, bool& bStartPdf)
  210. {
  211. const wstring strKey = _T("startpdf=");
  212. int nPos = strUrl.find(strKey);
  213. if (nPos < 0)
  214. {
  215. return FALSE;
  216. }
  217. nPos += strKey.length();
  218. if (nPos >= (int)strUrl.length())
  219. {
  220. return FALSE;
  221. }
  222. wstring strStartPdf = strUrl.substr(nPos);
  223. if (strStartPdf == L"1")
  224. {
  225. bStartPdf = true;
  226. }
  227. else
  228. {
  229. bStartPdf = false;
  230. }
  231. return TRUE;
  232. }
  233. BOOL CCommandLineInfoEx::ParseRect(wstring strUrl, RECT& rcRect)
  234. {
  235. const wstring strKey = _T("rect=");
  236. int nPos = strUrl.find(strKey);
  237. if (nPos < 0)
  238. {
  239. return FALSE;
  240. }
  241. nPos += strKey.length();
  242. if (nPos >= (int)strUrl.length())
  243. {
  244. return FALSE;
  245. }
  246. wstring strRect = strUrl.substr(nPos);
  247. std::vector<std::wstring> vctRect;
  248. if (split(strRect, L",", vctRect) && vctRect.size() == 4)
  249. {
  250. rcRect.left = _wtoi(vctRect[0].c_str());
  251. rcRect.top = _wtoi(vctRect[1].c_str());
  252. rcRect.right = _wtoi(vctRect[2].c_str());
  253. rcRect.bottom = _wtoi(vctRect[3].c_str());
  254. }
  255. return TRUE;
  256. }
  257. typedef basic_string<char>::size_type S_T;
  258. bool CCommandLineInfoEx::split(const std::wstring& src, std::wstring delimit, std::vector<std::wstring> &v, std::wstring null_subst)
  259. {
  260. if (src.empty() || delimit.empty())
  261. return false;
  262. S_T deli_len = delimit.size();
  263. long index = -1, last_search_position = 0;
  264. while ((index = src.find(delimit,last_search_position)) != -1)
  265. {
  266. if (index == last_search_position)
  267. v.push_back(null_subst);
  268. else
  269. v.push_back(src.substr(last_search_position, index-last_search_position));
  270. last_search_position = index + deli_len;
  271. }
  272. std::wstring last_one = src.substr(last_search_position);
  273. v.push_back(last_one.empty() ? null_subst : last_one);
  274. return true;
  275. }