Source code of Windows XP (NT5)
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.

234 lines
5.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 2000.
  5. //
  6. // File: brdoc.hxx
  7. //
  8. // Contents:
  9. //
  10. // History: 15 Aug 1996 DLee Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #pragma once
  14. class ChunkOffset
  15. {
  16. public:
  17. void SetChunkId (ULONG id) { _idChunk = id; }
  18. void SetOffset (ULONG off) { _offset = off; }
  19. ULONG ChunkId () const { return _idChunk; }
  20. ULONG Offset () const { return _offset; }
  21. private:
  22. ULONG _idChunk;
  23. ULONG _offset;
  24. };
  25. class Position
  26. {
  27. public:
  28. Position () { Init (); }
  29. Position ( int para, int begOff, int len )
  30. : _para (para), _begOff(begOff), _endOff (_begOff + len) {}
  31. void Init ()
  32. {
  33. _para = 0;
  34. _begOff = 0;
  35. _endOff = 0;
  36. }
  37. int Para () const { return _para; }
  38. int BegOff () const { return _begOff; }
  39. int EndOff () const { return _endOff; }
  40. int Len () const { return _endOff - _begOff; }
  41. void SetLen (int len) { _endOff = _begOff + len; }
  42. void IncBegOff () { _begOff++; }
  43. void NewLine ()
  44. {
  45. _begOff = 0;
  46. _para++;
  47. }
  48. int Compare( const Position& pos) const;
  49. private:
  50. int _para;
  51. int _begOff;
  52. int _endOff;
  53. };
  54. class Hit
  55. {
  56. public:
  57. Hit( const Position * aPos, unsigned cPos );
  58. ~Hit();
  59. unsigned Count() const { return _cPos; }
  60. const Position& GetPos (int i) const
  61. {
  62. Win4Assert ( i < int(_cPos) );
  63. return _aPos[i];
  64. }
  65. private:
  66. Position * _aPos;
  67. unsigned _cPos;
  68. };
  69. class ParaLine
  70. {
  71. public:
  72. ParaLine() : next(0) {}
  73. int offEnd;
  74. ParaLine* next;
  75. };
  76. class Document
  77. {
  78. friend class HitIter;
  79. public:
  80. Document ();
  81. Document (WCHAR const* filename, LONG rank, BOOL fDelete );
  82. ~Document();
  83. void Free();
  84. //
  85. // Initialization methods
  86. //
  87. SCODE Init(ISearchQueryHits *pSearch);
  88. BOOL ConvertPositionsToHit( LONG rank );
  89. //
  90. // Viewing methods
  91. //
  92. BOOL GetLine(int nPara, int off, int& cwc, WCHAR* buf);
  93. void GetWord(int nPara, int offSrc, int cwcSrc, WCHAR* buf);
  94. const WCHAR* Filename() const { return _filename;}
  95. int MaxParaLen () const { return _maxParaLen; }
  96. int Paras () const {return _cPara; }
  97. WCHAR* Buffer () const { return _buffer; }
  98. WCHAR* BufEnd () const { return _bufEnd; }
  99. LONG Rank () const { return _rank; }
  100. BOOL IsInit () const { return _isInit; }
  101. unsigned Hits () const { return _cHit; }
  102. ChunkOffset OffsetToChunkOffset ( ULONG offset ) const
  103. {
  104. for (int i = 0; i < _chunkCount; i++)
  105. if (offset < _chunk[i].Offset())
  106. break;
  107. Win4Assert ( i > 0 );
  108. i -= 1;
  109. ChunkOffset off;
  110. off.SetChunkId (_chunk[i].ChunkId());
  111. off.SetOffset (offset - _chunk[i].Offset());
  112. return off;
  113. }
  114. ParaLine const * GetParaLine() const { return _aParaLine; }
  115. private:
  116. void AllocBuffer( WCHAR const * pwcPath );
  117. void BindToFilter( WCHAR const * pwcPath );
  118. void ReadFile ();
  119. void BreakParas();
  120. void BreakLines();
  121. WCHAR * EatPara( WCHAR * pCur );
  122. Position RegionToPos ( FILTERREGION& region );
  123. ISearchQueryHits* _pSearch;
  124. WCHAR * _filename;
  125. LONG _rank;
  126. WCHAR * _buffer;
  127. ULONG _bufLen;
  128. WCHAR * _bufEnd;
  129. BOOL _isInit;
  130. CDynArrayInPlace<Hit *> _aHit;
  131. unsigned _cHit;
  132. IFilter* _pFilter;
  133. unsigned * _aParaOffset; // array of offsets of paragraphs
  134. int _cPara;
  135. int _maxParaLen;
  136. int _chunkCount;
  137. CDynArrayInPlace<ChunkOffset> _chunk;
  138. ParaLine* _aParaLine;
  139. BOOL _fDelete;
  140. };
  141. class HitIter
  142. {
  143. public:
  144. HitIter () : _pDoc (0), _cHit(0), _iHit(0), _iRestoreHit(0) {}
  145. void Init (const Document* pDoc)
  146. {
  147. _pDoc = pDoc;
  148. _cHit = pDoc->Hits();
  149. _iHit = 0;
  150. _iRestoreHit = 0;
  151. }
  152. BOOL FirstHit();
  153. BOOL PrevHit();
  154. BOOL NextHit();
  155. void SaveHit() { _iRestoreHit = _iHit; }
  156. void RestoreHit() { _iHit = _iRestoreHit; }
  157. BOOL isSavedCurrent() { return _iRestoreHit == _iHit; }
  158. BOOL isLastHit() { return(_cHit == 0 || _iHit == _cHit - 1); }
  159. BOOL isFirstHit() { return(_iHit == 0); }
  160. int GetPositionCount () const;
  161. Position GetPosition ( int i ) const;
  162. int HitCount() { return _cHit; }
  163. int CurrentHit() { return _iHit; }
  164. int ParaCount() { return _pDoc->Paras(); }
  165. private:
  166. const Document* _pDoc;
  167. unsigned _cHit;
  168. unsigned _iHit;
  169. unsigned _iRestoreHit;
  170. };
  171. inline BOOL HitIter::PrevHit()
  172. {
  173. if ( _cHit!= 0 && _iHit > 0 )
  174. {
  175. _iHit--;
  176. return TRUE;
  177. }
  178. return FALSE;
  179. }
  180. inline BOOL HitIter::FirstHit()
  181. {
  182. if ( _cHit != 0 )
  183. {
  184. _iHit = 0;
  185. return TRUE;
  186. }
  187. return FALSE;
  188. }
  189. inline BOOL HitIter::NextHit()
  190. {
  191. if ( _cHit != 0 && _iHit + 1 < _cHit)
  192. {
  193. _iHit++;
  194. return TRUE;
  195. }
  196. return FALSE;
  197. }