Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.9 KiB

  1. //--------------------------------------------------------------------
  2. // CmdArgs - implementation
  3. // Copyright (C) Microsoft Corporation, 1999
  4. //
  5. // Created by: Louis Thomas (louisth), 10-4-99
  6. //
  7. // stuff to deal with command line arguments
  8. //
  9. #include "pch.h"
  10. //--------------------------------------------------------------------
  11. bool CheckNextArg(CmdArgs * pca, WCHAR * wszTag, WCHAR ** pwszParam) {
  12. // make sure there are more arguments to look at
  13. if (pca->nNextArg==pca->nArgs) {
  14. return false;
  15. }
  16. WCHAR * wszArg=pca->rgwszArgs[pca->nNextArg];
  17. // our args must always start with a switch char
  18. if (L'/'!=wszArg[0] && L'-'!=wszArg[0]) {
  19. return false;
  20. }
  21. wszArg++;
  22. WCHAR * pwchColon=NULL;
  23. // if it is supposed to have a parameter, make sure it does
  24. if (NULL!=pwszParam) {
  25. pwchColon=wcschr(wszArg, L':');
  26. if (NULL==pwchColon) {
  27. return false;
  28. }
  29. *pwchColon=L'\0';
  30. }
  31. // is this the one we're looking for?
  32. if (0!=_wcsicmp(wszTag, wszArg)) {
  33. // no.
  34. // put colon back if there was one
  35. if (NULL!=pwchColon) {
  36. *pwchColon=L':';
  37. }
  38. return false;
  39. } else {
  40. // yes.
  41. // put colon back, and point at the parameter, if necessary
  42. if (NULL!=pwszParam) {
  43. *pwchColon=L':';
  44. *pwszParam=pwchColon+1;
  45. }
  46. pca->nNextArg++;
  47. return true;
  48. }
  49. }
  50. //--------------------------------------------------------------------
  51. bool FindArg(CmdArgs * pca, WCHAR * wszTag, WCHAR ** pwszParam, unsigned int * pnIndex) {
  52. unsigned int nOrigNextArg=pca->nNextArg;
  53. bool bFound=false;
  54. // check each arg to see if it matches
  55. unsigned int nIndex;
  56. for (nIndex=nOrigNextArg; nIndex<pca->nArgs; nIndex++) {
  57. pca->nNextArg=nIndex;
  58. if (CheckNextArg(pca, wszTag, pwszParam)) {
  59. *pnIndex=nIndex;
  60. bFound=true;
  61. break;
  62. }
  63. }
  64. pca->nNextArg=nOrigNextArg;
  65. return bFound;
  66. }
  67. //--------------------------------------------------------------------
  68. void MarkArgUsed(CmdArgs * pca, unsigned int nIndex) {
  69. if (nIndex<pca->nNextArg || nIndex>=pca->nArgs) {
  70. return;
  71. }
  72. for (; nIndex>pca->nNextArg; nIndex--) {
  73. WCHAR * wszTemp=pca->rgwszArgs[nIndex];
  74. pca->rgwszArgs[nIndex]=pca->rgwszArgs[nIndex-1];
  75. pca->rgwszArgs[nIndex-1]=wszTemp;
  76. }
  77. pca->nNextArg++;
  78. }
  79. //--------------------------------------------------------------------
  80. HRESULT VerifyAllArgsUsed(CmdArgs * pca) {
  81. HRESULT hr;
  82. if (pca->nArgs!=pca->nNextArg) {
  83. LocalizedWPrintfCR(IDS_W32TM_ERRORGENERAL_UNEXPECTED_PARAMS);
  84. for(; pca->nArgs!=pca->nNextArg; pca->nNextArg++) {
  85. wprintf(L" %s", pca->rgwszArgs[pca->nNextArg]);
  86. }
  87. wprintf(L"\n");
  88. hr=E_INVALIDARG;
  89. _JumpError(hr, error, "command line parsing");
  90. }
  91. hr=S_OK;
  92. error:
  93. return hr;
  94. }