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.

198 lines
4.3 KiB

  1. /*****************************************************************************
  2. *
  3. * Util.c
  4. *
  5. * Copyright (c) 1996 Microsoft Corporation. All Rights Reserved.
  6. *
  7. * Abstract:
  8. *
  9. * Misc helper functions.
  10. *
  11. * Contents:
  12. *
  13. *
  14. *
  15. *****************************************************************************/
  16. #include "pch.h"
  17. #define DbgFl DbgFlUtil
  18. /*****************************************************************************
  19. *
  20. * @doc INTERNAL
  21. *
  22. * @func PV | pvFindResource |
  23. *
  24. * Handy wrapper that finds and loads a resource.
  25. *
  26. * @parm IN HINSTANCE | hinst |
  27. *
  28. * Module instance handle.
  29. *
  30. * @parm DWORD | id |
  31. *
  32. * Resource identifier.
  33. *
  34. * @parm LPCTSTR | rt |
  35. *
  36. * Resource type.
  37. *
  38. * @returns
  39. *
  40. * Pointer to resource, or 0.
  41. *
  42. *****************************************************************************/
  43. PV EXTERNAL
  44. pvFindResource(HINSTANCE hinst, DWORD id, LPCTSTR rt)
  45. {
  46. HANDLE hrsrc;
  47. PV pv = NULL;
  48. hrsrc = FindResource(hinst, (LPTSTR)ULongToPtr(id), rt);
  49. if (hrsrc) {
  50. pv = LoadResource(hinst, hrsrc);
  51. } else {
  52. pv = 0;
  53. }
  54. return pv;
  55. }
  56. #ifndef UNICODE
  57. /*****************************************************************************
  58. *
  59. * @doc INTERNAL
  60. *
  61. * @func UINT | LoadStringW |
  62. *
  63. * Implementation of LoadStringW for platforms on which Unicode is
  64. * not supported. Does exactly what LoadStringW would've done
  65. * if it existed.
  66. *
  67. * @parm IN HINSTANCE | hinst |
  68. *
  69. * Module instance handle.
  70. *
  71. * @parm UINT | ids |
  72. *
  73. * String id number.
  74. *
  75. * @parm LPWSTR | pwsz |
  76. *
  77. * UNICODE output buffer.
  78. *
  79. * @parm UINT | cwch |
  80. *
  81. * Size of UNICODE output buffer.
  82. *
  83. * @returns
  84. *
  85. * Number of characters copied, not including terminating null.
  86. *
  87. * @comm
  88. *
  89. * Since the string is stored in the resource as UNICODE,
  90. * we just pull it out ourselves. If we go through
  91. * <f LoadStringA>, we may end up losing characters due
  92. * to character set translation.
  93. *
  94. *****************************************************************************/
  95. int EXTERNAL
  96. LoadStringW(HINSTANCE hinst, UINT ids, LPWSTR pwsz, int cwch)
  97. {
  98. PWCHAR pwch;
  99. AssertF(cwch);
  100. ScrambleBuf(pwsz, cbCwch(cwch));
  101. /*
  102. * String tables are broken up into "bundles" of 16 strings each.
  103. */
  104. pwch = pvFindResource(hinst, 1 + ids / 16, RT_STRING);
  105. if (pwch) {
  106. /*
  107. * Now skip over the strings in the resource until we
  108. * hit the one we want. Each entry is a counted string,
  109. * just like Pascal.
  110. */
  111. for (ids %= 16; ids; ids--) {
  112. pwch += *pwch + 1;
  113. }
  114. cwch = min(*pwch, cwch - 1);
  115. memcpy(pwsz, pwch+1, cbCwch(cwch)); /* Copy the goo */
  116. } else {
  117. cwch = 0;
  118. }
  119. pwsz[cwch] = TEXT('\0'); /* Terminate the string */
  120. return cwch;
  121. }
  122. #endif
  123. /*****************************************************************************
  124. *
  125. * @doc INTERNAL
  126. *
  127. * @func Parse command line
  128. *
  129. * @parm | |
  130. *
  131. *****************************************************************************/
  132. HRESULT
  133. ParseCommandLine(
  134. LPSTR lpszCmdLine,
  135. UINT *pargc,
  136. LPTSTR *argv
  137. )
  138. {
  139. LPSTR pszT = lpszCmdLine;
  140. *pargc=0;
  141. //
  142. // Get to first parameter in command line.
  143. //
  144. while (*pszT && ((*pszT != '-') && (*pszT != '/')) ) {
  145. pszT++;
  146. }
  147. //
  148. // Parse options from command line
  149. //
  150. while (*pszT) {
  151. // Skip white spaces
  152. while (*pszT && *pszT <= ' ') {
  153. pszT++;
  154. }
  155. if (!*pszT)
  156. break;
  157. if ('-' == *pszT || '/' == *pszT) {
  158. pszT++;
  159. if (!*pszT)
  160. break;
  161. argv[*pargc] = pszT;
  162. (*pargc)++;
  163. }
  164. // Skip till space
  165. while (*pszT && *pszT > ' ') {
  166. pszT++;
  167. }
  168. if (!*pszT)
  169. break;
  170. // Got next argument
  171. *pszT++='\0';
  172. }
  173. return TRUE;
  174. }