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.

195 lines
3.8 KiB

  1. //-----------------------------------------------------------------------------------------
  2. #pragma once
  3. #include <assert.h>
  4. #include <shlwapi.h>
  5. #include "strlist.h"
  6. #define CA_PROPERTY_LEN 100
  7. #define CA_VALUE_LEN 100
  8. //-----------------------------------------------------------------------------------------
  9. // define a list that will hold MSI property/value pairs
  10. //
  11. class CPropertyBag
  12. {
  13. private:
  14. CStrList list;
  15. public:
  16. CPropertyBag( void )
  17. {
  18. }
  19. //---------------------------------------------------------
  20. void Add( LPCTSTR szProperty, LPCTSTR szValue )
  21. {
  22. list.AddValue ( szProperty, szValue );
  23. }
  24. //---------------------------------------------------------
  25. void Add( LPCTSTR szProperty, DWORD dwValue )
  26. {
  27. TCHAR szValue[ 100 ];
  28. _stprintf( szValue, TEXT( "%d" ), dwValue );
  29. Add( szProperty, szValue );
  30. }
  31. //---------------------------------------------------------
  32. void Delete( LPCTSTR szProperty )
  33. {
  34. list.RemoveByKey ( szProperty );
  35. }
  36. //---------------------------------------------------------
  37. LPTSTR ConcatValuePairs (LPCTSTR separator, LPTSTR outBuf)
  38. {
  39. if (!outBuf)
  40. return NULL;
  41. list.ConcatKeyValues ( separator, outBuf );
  42. return outBuf;
  43. }
  44. //---------------------------------------------------------
  45. void Clear( void )
  46. {
  47. list.RemoveAll ();
  48. }
  49. //---------------------------------------------------------
  50. LPCTSTR GetString( LPCTSTR szProperty, LPTSTR buf )
  51. {
  52. return list.Lookup (szProperty, buf);
  53. }
  54. //---------------------------------------------------------
  55. DWORD GetValue( LPCTSTR szProperty )
  56. {
  57. TCHAR buf [256];
  58. if ( list.Lookup (szProperty, buf) )
  59. {
  60. DWORD numRes = _ttoi( buf );
  61. return numRes;
  62. }
  63. else
  64. return (DWORD)-1;
  65. }
  66. //---------------------------------------------------------
  67. bool Parse( LPTSTR szPropertyString, DWORD dwStrLen )
  68. {
  69. // property1=value1;property2=value2;
  70. assert( szPropertyString );
  71. assert( _tcslen(szPropertyString) > 0 );
  72. if( NULL == szPropertyString || 0 == _tcslen( szPropertyString ) )
  73. {
  74. return false;
  75. }
  76. //
  77. // trim space, commas and semicolons
  78. //
  79. StrTrim( szPropertyString, TEXT( " ;," ) );
  80. //
  81. // add a semicolon to the end
  82. //
  83. if( _tcslen( szPropertyString ) < dwStrLen - 1)
  84. _tcscat( szPropertyString, TEXT( ";" ) );
  85. else
  86. {
  87. assert( false );
  88. return false;
  89. }
  90. // parse out the pairs
  91. PTCHAR pProperty = szPropertyString;
  92. PTCHAR pValue = NULL;
  93. TCHAR szProperty[ 100 ];
  94. TCHAR szValue[ 100 ];
  95. while( *pProperty )
  96. {
  97. //
  98. // the value starts 1 char after the next "="
  99. //
  100. pValue = _tcschr(pProperty, TEXT('='));
  101. if( NULL == pValue )
  102. {
  103. assert( false );
  104. return false;
  105. }
  106. //
  107. // make sure the property value was not blank
  108. //
  109. if( pProperty == pValue )
  110. {
  111. assert( false );
  112. return false;
  113. }
  114. //
  115. // put a NULL there to mark the end of the Property
  116. //
  117. *pValue = NULL;
  118. //
  119. // the value starts after the "="
  120. //
  121. pValue++;
  122. //
  123. // capture the Property
  124. //
  125. _tcsncpy( szProperty, pProperty, sizeof( szProperty ) / sizeof( TCHAR ) );
  126. //
  127. // move the property pointer ahead to the next ";"
  128. //
  129. //
  130. pProperty = _tcschr(pValue, TEXT(';'));
  131. if( NULL == pProperty )
  132. {
  133. assert( false );
  134. return false;
  135. }
  136. //
  137. // null it out to mark the end of the previous value
  138. //
  139. *pProperty = NULL;
  140. //
  141. // set over the null to the start of the next property (or the end of the string)
  142. //
  143. pProperty++;
  144. //
  145. // capture the value
  146. //
  147. _tcsncpy( szValue, pValue, sizeof( szValue ) / sizeof( TCHAR ) );
  148. Add( szProperty, szValue );
  149. }
  150. return true;
  151. }
  152. };