Leaked source code of windows server 2003
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.

269 lines
6.8 KiB

  1. /******************************Module*Header***********************************\
  2. *
  3. * *******************
  4. * * GDI SAMPLE CODE *
  5. * *******************
  6. *
  7. * Module Name: extparse.cxx
  8. *
  9. * Contains all the token parser functions
  10. *
  11. * Copyright (C) 1994-1998 3Dlabs Inc. Ltd. All rights reserved.
  12. * Copyright (C) 1995-1999 Microsoft Corporation. All rights reserved.
  13. ******************************************************************************/
  14. #include "dbgext.hxx"
  15. /**********************************Public*Routine******************************\
  16. *
  17. * Command line parsing routines
  18. *
  19. * This routine should return an array of char* 's in the idx parameter with the
  20. * beginning of each token in the array.
  21. * It also returns the number of tokens found.
  22. *
  23. ******************************************************************************/
  24. int
  25. iParseTokenizer(char* pcCmdStr,
  26. char** ppcTok)
  27. {
  28. char* pcSeps = " \t\n"; // white space separators
  29. char* pcToken = strtok(pcCmdStr, pcSeps); // get the first token
  30. int iTokCount = 0; // the token count
  31. while ( pcToken )
  32. {
  33. ppcTok[iTokCount++] = pcToken;
  34. pcToken = strtok(NULL, pcSeps);
  35. }
  36. return iTokCount;
  37. }// iParseTokenizer()
  38. /**********************************Public*Routine******************************\
  39. *
  40. * This routine finds the token specified in srchtok
  41. * and returns the index into tok.
  42. * A return value of -1 is used if the token is not found.
  43. *
  44. * Generally we use the case insensitive version (iParseiFindToken)
  45. * but occasionally we need the case sensitive version (iParseFindToken).
  46. *
  47. ******************************************************************************/
  48. int
  49. iParseFindToken(char** ppcTok,
  50. int iTok,
  51. char* pcSrchTok)
  52. {
  53. for ( int iTemp = 0; iTemp < iTok; ++iTemp )
  54. {
  55. if ( strcmp(ppcTok[iTemp], pcSrchTok) == 0 )
  56. {
  57. break;
  58. }
  59. }
  60. if ( iTemp >= iTok )
  61. {
  62. return -1;
  63. }
  64. return iTemp;
  65. }// iParseFindToken()
  66. /**********************************Public*Routine******************************\
  67. *
  68. * Case insensitive version of iParseFindToken
  69. *
  70. ******************************************************************************/
  71. int
  72. iParseiFindToken(char** ppcTok,
  73. int iTok,
  74. char* pcSrchTok)
  75. {
  76. for ( int iTemp = 0; iTemp < iTok; ++iTemp )
  77. {
  78. if ( _strnicmp(ppcTok[iTemp], pcSrchTok, strlen(pcSrchTok)) == 0 )
  79. {
  80. break;
  81. }
  82. }
  83. if ( iTemp >= iTok )
  84. {
  85. return -1;
  86. }
  87. return iTemp;
  88. }// iParseiFindToken()
  89. /**********************************Public*Routine******************************\
  90. *
  91. * Verifies that the given token at tok[iTokPos] is a switch
  92. * and contains the switch value cSwitch.
  93. *
  94. * Both case sensitive and insensitive versions.
  95. *
  96. ******************************************************************************/
  97. int
  98. iParseiIsSwitch(char** ppcTok,
  99. int iTokPos,
  100. char cSwitch)
  101. {
  102. if ( iTokPos < 0 )
  103. {
  104. return 0;
  105. }
  106. char* pcTemp = ppcTok[iTokPos];
  107. if ( (pcTemp[0] == '-' ) || ( pcTemp[0] == '/' ) )
  108. {
  109. //
  110. // Is a switch.
  111. //
  112. for ( pcTemp++; *pcTemp; pcTemp++ )
  113. {
  114. if ( toupper(*pcTemp) == toupper(cSwitch) )
  115. {
  116. return 1;
  117. }
  118. }
  119. }
  120. return 0;
  121. }// iParseiIsSwitch()
  122. int
  123. iParseIsSwitch(char** ppcTok,
  124. int iTokPos,
  125. char cSwitch)
  126. {
  127. if ( iTokPos < 0 )
  128. {
  129. return 0;
  130. }
  131. char* pcTemp = ppcTok[iTokPos];
  132. if ( (pcTemp[0] == '-') || (pcTemp[0] == '/') )
  133. {
  134. //
  135. // Is a switch.
  136. //
  137. for ( pcTemp++; *pcTemp; pcTemp++ )
  138. {
  139. if ( *pcTemp == cSwitch )
  140. {
  141. return 1;
  142. }// search each char
  143. }
  144. }
  145. return 0;
  146. }// iParseIsSwitch()
  147. /**********************************Public*Routine******************************\
  148. *
  149. * Finds a switch in a given list of tokens.
  150. * of the form -xxx(cSwitch)xxx or /xxx(cSwitch)xxx
  151. * example:
  152. * searching for 'a' in -jklabw returns true.
  153. *
  154. * Again both case sensitive and insensitive versions are needed.
  155. *
  156. ******************************************************************************/
  157. int
  158. iParseFindSwitch(char** ppcTok,
  159. int iTok,
  160. char cSwitch)
  161. {
  162. //
  163. // Search each token
  164. //
  165. for ( int iTemp = 0; iTemp < iTok; ++iTemp )
  166. {
  167. if ( iParseIsSwitch(ppcTok, iTemp, cSwitch) )
  168. {
  169. return iTemp;
  170. }// found it? return position.
  171. }
  172. return -1;
  173. }// iParseIsSwitch()
  174. int
  175. iParseiFindSwitch(char** ppcTok,
  176. int iTok,
  177. char cSwitch)
  178. {
  179. for ( int iTemp = 0; iTemp < iTok; ++iTemp )
  180. {
  181. if ( iParseIsSwitch(ppcTok, iTemp, cSwitch) )
  182. {
  183. return iTemp;
  184. }//found it? return position.
  185. }
  186. return -1;
  187. }// iParseIsSwitch()
  188. /**********************************Public*Routine******************************\
  189. *
  190. * Find the first non-switch token starting from position start
  191. * Will find token at position start
  192. *
  193. ******************************************************************************/
  194. int
  195. iParseFindNonSwitch(char** ppcTok,
  196. int iTok,
  197. int iStart)
  198. {
  199. for (int iTemp = iStart; iTemp < iTok; ++iTemp )
  200. {
  201. if ( (ppcTok[iTemp][0]!='-')&&(ppcTok[iTemp][0]!='/') )
  202. {
  203. break;
  204. }
  205. }
  206. if ( iTemp >= iTok )
  207. {
  208. return -1;
  209. }
  210. return iTemp;
  211. }// iParseFindNonSwitch()
  212. /**********************************Public*Routine******************************\
  213. *
  214. * Case insensitive token comparer.
  215. * returns 1 if pcChk == ppcTok[iTokPos] otherwise returns 0
  216. *
  217. * Pay careful attention to the length specifier in the _strnicmp
  218. *
  219. ******************************************************************************/
  220. int
  221. iParseiIsToken(char** ppcTok,
  222. int iTokPos,
  223. char* pcChk)
  224. {
  225. if ( iTokPos < 0 )
  226. {
  227. return 0;
  228. }
  229. return(_strnicmp(ppcTok[iTokPos], pcChk, strlen(pcChk)) == 0);
  230. }// iParseiIsToken()
  231. int
  232. iParseIsToken(char** ppcTok,
  233. int iTokPos,
  234. char* pcChk)
  235. {
  236. if ( iTokPos < 0 )
  237. {
  238. return 0;
  239. }
  240. return(strcmp(ppcTok[iTokPos], pcChk) == 0);
  241. }// iParseIsToken()