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.

163 lines
3.6 KiB

  1. /* Copyright (c) 1994, Microsoft Corporation, all rights reserved
  2. **
  3. ** parambuf.c
  4. ** Double-NUL terminated buffer of "key=value" parameter routines.
  5. **
  6. ** 03/14/94 Steve Cobb
  7. */
  8. #include <windows.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #define INCL_PARAMBUF
  12. #include "ppputil.h"
  13. VOID
  14. AddFlagToParamBuf(
  15. IN CHAR* pszzBuf,
  16. IN CHAR* pszKey,
  17. IN BOOL fValue )
  18. /* Add a "key=value" entry with key 'pszKey' and value 'fValue' to
  19. ** double-NUL terminated buffer of "key=value"s 'pszzBuf'.
  20. */
  21. {
  22. AddStringToParamBuf( pszzBuf, pszKey, (fValue) ? "1" : "0" );
  23. }
  24. VOID
  25. AddLongToParamBuf(
  26. IN CHAR* pszzBuf,
  27. IN CHAR* pszKey,
  28. IN LONG lValue )
  29. /* Add a "key=value" entry with key 'pszKey' and value 'lValue' to
  30. ** double-NUL terminated buffer of "key=value"s 'pszzBuf'.
  31. */
  32. {
  33. CHAR szNum[ 33 + 1 ];
  34. _ltoa( lValue, szNum, 10 );
  35. AddStringToParamBuf( pszzBuf, pszKey, szNum );
  36. }
  37. VOID
  38. AddStringToParamBuf(
  39. IN CHAR* pszzBuf,
  40. IN CHAR* pszKey,
  41. IN CHAR* pszValue )
  42. /* Add a "key=value" entry with key 'pszKey' and value 'pszValue' to
  43. ** double-NUL terminated buffer of "key=value"s 'pszzBuf'.
  44. */
  45. {
  46. CHAR* psz;
  47. INT cb;
  48. for (psz = pszzBuf; (cb = strlen( psz )) > 0; psz += cb + 1)
  49. ;
  50. if (!pszValue)
  51. pszValue = "";
  52. strcpy( psz, pszKey );
  53. strcat( psz, "=" );
  54. strcat( psz, pszValue );
  55. psz[ strlen( psz ) + 1 ] = '\0';
  56. }
  57. VOID
  58. ClearParamBuf(
  59. IN OUT CHAR* pszzBuf )
  60. /* Clears double-NUL terminated buffer of "key=value"s 'pszzBuf'.
  61. */
  62. {
  63. pszzBuf[ 0 ] = pszzBuf[ 1 ] = '\0';
  64. }
  65. BOOL
  66. FindFlagInParamBuf(
  67. IN CHAR* pszzBuf,
  68. IN CHAR* pszKey,
  69. IN BOOL* pfValue )
  70. /* Loads caller's 'pfValue' with the flag value associated with the key
  71. ** 'pszKey' in double-NUL terminated buffer of "key=value"s 'pszzBuf'.
  72. **
  73. ** Returns true if the parameter was found, false otherwise.
  74. */
  75. {
  76. CHAR szBuf[ 2 ];
  77. if (FindStringInParamBuf( pszzBuf, pszKey, szBuf, 2 ))
  78. {
  79. *pfValue = (szBuf[ 0 ] == '1');
  80. return TRUE;
  81. }
  82. return FALSE;
  83. }
  84. BOOL
  85. FindLongInParamBuf(
  86. IN CHAR* pszzBuf,
  87. IN CHAR* pszKey,
  88. IN LONG* plValue )
  89. /* Loads caller's 'plValue' with the long value associated with the key
  90. ** 'pszKey' in double-NUL terminated buffer of "key=value"s 'pszzBuf'.
  91. **
  92. ** Returns true if the parameter was found, false otherwise.
  93. */
  94. {
  95. CHAR szBuf[ 33 + 1 ];
  96. if (FindStringInParamBuf( pszzBuf, pszKey, szBuf, 33 ))
  97. {
  98. *plValue = atol( szBuf );
  99. return TRUE;
  100. }
  101. return FALSE;
  102. }
  103. BOOL
  104. FindStringInParamBuf(
  105. IN CHAR* pszzBuf,
  106. IN CHAR* pszKey,
  107. IN CHAR* pchValueBuf,
  108. IN DWORD cbValueBuf )
  109. /* Loads caller's 'pchValueBuf' with the value associated with the key
  110. ** 'pszKey' in double-NUL terminated buffer of "key=value"s 'pszzBuf'.
  111. ** The string is truncated at 'cbValueBuf' if necessary.
  112. **
  113. ** Returns true if the parameter was found, false otherwise.
  114. */
  115. {
  116. INT cbSearchKey = strlen( pszKey );
  117. CHAR* psz;
  118. INT cb;
  119. for (psz = pszzBuf; (cb = strlen( psz )) > 0; psz += cb + 1)
  120. {
  121. CHAR* pszKeyEnd = strchr( psz, '=' );
  122. INT cbKey = (pszKeyEnd) ? (LONG)(pszKeyEnd - psz) : 0;
  123. if (cbKey == cbSearchKey && _strnicmp( psz, pszKey, cbKey ) == 0)
  124. {
  125. strncpy( pchValueBuf, pszKeyEnd + 1, cbValueBuf );
  126. return TRUE;
  127. }
  128. }
  129. return FALSE;
  130. }