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.

323 lines
6.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995.
  5. //
  6. // File: DBColumn.cxx
  7. //
  8. // Contents: C++ wrappers for OLE-DB columns and sort keys
  9. //
  10. // Classes: CDbColumns
  11. // CDbSortKey
  12. // CDbSortSet
  13. //
  14. // History: 20 Jul 1995 AlanW Created
  15. //
  16. //--------------------------------------------------------------------------
  17. #include <pch.cxx>
  18. #pragma hdrstop
  19. //
  20. // Methods for CDbColumns
  21. //
  22. CDbColumns::CDbColumns( unsigned size )
  23. : _size( size ),
  24. _cCol( 0 ),
  25. _aCol( 0 )
  26. {
  27. if ( _size > 0 )
  28. {
  29. _aCol = (CDbColId *)CoTaskMemAlloc( _size * sizeof(CDbColId) );
  30. if ( 0 == _aCol )
  31. {
  32. _size = 0;
  33. }
  34. else
  35. memset( _aCol, DBKIND_GUID_PROPID, _size * sizeof( CDbColId ) );
  36. }
  37. }
  38. CDbColumns::CDbColumns( CDbColumns const & src )
  39. : _size( src._cCol ),
  40. _cCol( 0 )
  41. {
  42. if ( _size > 0 )
  43. {
  44. _aCol = (CDbColId *)CoTaskMemAlloc( _size * sizeof( CDbColId ) );
  45. if ( 0 != _aCol )
  46. {
  47. memset( _aCol, DBKIND_GUID_PROPID, _size * sizeof( CDbColId ) );
  48. while( _cCol < src._cCol )
  49. {
  50. if ( !Add( src.Get( _cCol ), _cCol ) )
  51. break;
  52. }
  53. }
  54. else
  55. {
  56. _size = 0;
  57. }
  58. }
  59. }
  60. CDbColumns::~CDbColumns()
  61. {
  62. if ( _size > 0 )
  63. {
  64. for ( unsigned i = 0; i < _cCol; i++ )
  65. _aCol[i].CDbColId::~CDbColId();
  66. if ( _aCol )
  67. CoTaskMemFree( _aCol );
  68. }
  69. }
  70. #if 0 // NOTE: Marshall & Unmarshall not needed at this time.
  71. void CDbColumns::Marshall( PSerStream & stm ) const
  72. {
  73. stm.PutULong( _cCol );
  74. for ( unsigned i = 0; i < _cCol; i++ )
  75. {
  76. _aCol[i].Marshall( stm );
  77. }
  78. }
  79. CDbColumns::CDbColumns( PDeSerStream & stm )
  80. {
  81. _size = stm.GetULong();
  82. if ( _size > 0 )
  83. {
  84. _aCol = (CDbColId *)CoTaskMemAlloc( _size * sizeof( CDbColId ) );
  85. if ( 0 != _aCol )
  86. {
  87. for( _cCol = 0; _cCol < _size; _cCol++ )
  88. {
  89. CDbColId ps(stm);
  90. Add( ps, _cCol );
  91. }
  92. }
  93. else
  94. {
  95. _size = 0;
  96. }
  97. }
  98. }
  99. #endif // 0 - Marshall and Unmarshall not needed now.
  100. BOOL CDbColumns::Add( CDbColId const & Property, unsigned pos )
  101. {
  102. while ( pos >= _size )
  103. {
  104. unsigned cNew = (_size > 0) ? (_size * 2) : 1;
  105. CDbColId * aNew =
  106. (CDbColId *)CoTaskMemAlloc( cNew * sizeof( CDbColId ) );
  107. if ( 0 == aNew )
  108. return FALSE;
  109. memcpy( aNew, _aCol, _cCol * sizeof( CDbColId ) );
  110. memset( aNew + _cCol, DBKIND_GUID_PROPID, (cNew - _cCol) * sizeof( CDbColId ) );
  111. if ( 0 != _aCol )
  112. CoTaskMemFree( _aCol );
  113. _aCol = aNew;
  114. _size = cNew;
  115. }
  116. _aCol[pos] = Property;
  117. if ( pos >= _cCol )
  118. _cCol = pos + 1;
  119. return( TRUE );
  120. }
  121. void CDbColumns::Remove( unsigned pos )
  122. {
  123. if ( pos < _cCol )
  124. {
  125. _aCol[pos].CDbColId::~CDbColId();
  126. _cCol--;
  127. RtlMoveMemory( _aCol + pos,
  128. _aCol + pos + 1,
  129. (_cCol - pos) * sizeof( CDbColId ) );
  130. }
  131. }
  132. //
  133. // Methods for CDbSortSet
  134. //
  135. CDbSortSet::CDbSortSet( unsigned size )
  136. : _size( size ),
  137. _csk( 0 ),
  138. _ask( 0 )
  139. {
  140. if ( _size > 0 )
  141. {
  142. _ask = (CDbSortKey *)CoTaskMemAlloc( _size * sizeof( CDbSortKey ) );
  143. if ( _ask == 0 )
  144. {
  145. _size = 0;
  146. }
  147. else
  148. {
  149. memset( _ask, DBKIND_GUID_PROPID, _size * sizeof( CDbSortKey ) );
  150. }
  151. }
  152. }
  153. CDbSortSet::CDbSortSet( CDbSortSet const & src )
  154. : _size( src._csk ),
  155. _csk( 0 ),
  156. _ask( 0 )
  157. {
  158. if ( _size > 0 )
  159. {
  160. _ask = (CDbSortKey *)CoTaskMemAlloc( _size * sizeof( CDbSortKey ) );
  161. if ( 0 != _ask )
  162. {
  163. memset( _ask, DBKIND_GUID_PROPID, _size * sizeof( CDbSortKey ) );
  164. while( _csk < src._csk )
  165. {
  166. if ( !Add( src.Get( _csk ), _csk ) )
  167. {
  168. break;
  169. }
  170. }
  171. }
  172. else
  173. {
  174. _size = 0;
  175. }
  176. }
  177. }
  178. #if 0 // NOTE: Marshall & Unmarshall not needed at this time.
  179. void CDbSortKey::Marshall( PSerStream & stm ) const
  180. {
  181. //
  182. // NOTE: Order is important!
  183. //
  184. _property.Marshall( stm );
  185. stm.PutULong( _dwOrder );
  186. }
  187. CDbSortKey::CDbSortKey( PDeSerStream & stm )
  188. : _property( stm ),
  189. _dwOrder( stm.GetULong() )
  190. {
  191. }
  192. #endif // 0 - Marshall and Unmarshall not needed now.
  193. CDbSortSet::~CDbSortSet()
  194. {
  195. if ( _size > 0 )
  196. {
  197. for ( unsigned i = 0; i < _csk; i++ )
  198. {
  199. _ask[i].GetProperty().CDbColId::~CDbColId();
  200. }
  201. if ( _ask )
  202. {
  203. CoTaskMemFree( _ask );
  204. }
  205. }
  206. }
  207. #if 0 // NOTE: Marshall & Unmarshall not needed at this time.
  208. void CDbSortSet::Marshall( PSerStream & stm ) const
  209. {
  210. stm.PutULong( _csk );
  211. for ( unsigned i = 0; i < _csk; i++ )
  212. {
  213. _ask[i].Marshall( stm );
  214. }
  215. }
  216. CDbSortSet::CDbSortSet( PDeSerStream & stm )
  217. : _csk( stm.GetULong() ),
  218. _size( _csk )
  219. {
  220. _ask = (CDbSortKey *)CoTaskMemAlloc( _csk * sizeof( CDbSortKey ) );
  221. for ( unsigned i = 0; i < _csk; i++ )
  222. {
  223. CDbSortKey sk(stm);
  224. Add( sk, i );
  225. }
  226. }
  227. #endif // 0 - Marshall and Unmarshall not needed now.
  228. BOOL
  229. CDbSortSet::Add( CDbSortKey const & sk, unsigned pos )
  230. {
  231. while ( pos >= _size )
  232. {
  233. unsigned cNew = (_size > 0) ? (_size * 2) : 1;
  234. CDbSortKey * aNew = (CDbSortKey *)CoTaskMemAlloc( cNew * sizeof( CDbSortKey ) );
  235. if ( 0 == aNew )
  236. return FALSE;
  237. memcpy( aNew, _ask, _csk * sizeof( CDbSortKey ) );
  238. memset( aNew + _csk, DBKIND_GUID_PROPID, (cNew - _csk) * sizeof( CDbSortKey ) );
  239. CoTaskMemFree( _ask );
  240. _ask = aNew;
  241. _size = cNew;
  242. }
  243. _ask[pos] = sk;
  244. if ( !_ask[pos].IsValid() )
  245. return FALSE;
  246. if ( pos >= _csk )
  247. _csk = pos + 1;
  248. return TRUE;
  249. }
  250. BOOL CDbSortSet::Add( CDbColId const & property, ULONG dwOrder, unsigned pos )
  251. {
  252. CDbSortKey sk( property, dwOrder );
  253. if ( !sk.IsValid() )
  254. return FALSE;
  255. return Add( sk, pos );
  256. }
  257. void CDbSortSet::Remove( unsigned pos )
  258. {
  259. if ( pos < _csk )
  260. {
  261. _ask[pos].GetProperty().CDbColId::~CDbColId();
  262. _csk--;
  263. RtlMoveMemory( _ask + pos,
  264. _ask + pos + 1,
  265. (_csk - pos) * sizeof( CDbSortKey ) );
  266. }
  267. }