Ignore:
Timestamp:
Jun 1, 2009, 2:42:16 PM (16 years ago)
Author:
RBRi
Message:

index is a real object now
support for env variables to make glossary simulation work

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/NewView/HelpFile.pas

    r252 r342  
    2222
    2323type
     24
     25  TIndexEntry = class
     26  private
     27    name: String;
     28    topic: TTopic;
     29    flags: uint8;
     30  public
     31    CONSTRUCTOR Create(aName: String; aTopic: TTopic; aFlags: uint8);
     32    DESTRUCTOR Destroy; override;
     33
     34    PROPERTY getTopic: TTopic read topic;
     35    FUNCTION getLabel: String;
     36    FUNCTION isGlobal: boolean;
     37    FUNCTION getLevel: integer;
     38  end;
     39
     40
     41  TIndex = class
     42  private
     43    entries: TStringList;
     44    // labels: TStringList;
     45  public
     46    CONSTRUCTOR Create;
     47    DESTRUCTOR Destroy; override;
     48
     49    FUNCTION Count: longint;
     50    FUNCTION GetLabels: TStringList;
     51    FUNCTION GetTopic(aPos: longint): TTopic;
     52    PROCEDURE Add(anIndexEntry: TIndexEntry);
     53  end;
     54
     55
    2456  THelpFile = class
    2557  protected
     
    3769    _Dictionary: TList; // pointers to strings.
    3870
    39     _Index: TStringList;
     71    _Index: TIndex;
    4072
    4173    _SearchTable: TSearchTable;
     
    82114    function GetDictionaryWordPtr( Index: longint ): pstring;
    83115
    84     function GetIndexEntryPtr( Index: longint ): pstring;
    85116    function GetHighlightWords: UInt32ArrayPointer;
    86117
     
    94125
    95126  public
    96     constructor Create( const FileName: string );
     127    constructor Create( const aFileName: string );
    97128
    98129    destructor Destroy; override;
    99130
    100     function GetIndex: TStringList;
     131    function GetIndex: TIndex;
    101132
    102133    property Title: string read _Title;
     
    104135    property TopicList: TList read _Topics;
    105136    property TopicCount: longint read GetTopicCount;
    106     property Index: TStringList read GetIndex;
    107     property IndexEntryPtr[ index: longint ]: pstring read GetIndexEntryPtr;
     137    property Index: TIndex read GetIndex;
    108138    property Filename: string read _FileName;
    109139
     
    170200  FileErrorInUse: string;
    171201  FileErrorInvalidHeader: string;
     202
     203
     204  // -----------
     205  // TIndexEntry
     206  // -----------
     207
     208  CONSTRUCTOR TIndexEntry.Create(aName: String; aTopic: TTopic; aFlags: uint8);
     209  begin
     210    LogEvent(LogObjConstDest, 'TIndexEntry.Create');
     211    name := aName;
     212    topic := aTopic;
     213    flags := aFlags;
     214  end;
     215
     216
     217  DESTRUCTOR TIndexEntry.Destroy;
     218  begin
     219    LogEvent(LogObjConstDest, 'TIndexEntry.Destroy');
     220    topic := nil;
     221    // inherited Destroy;
     222  end;
     223
     224
     225  FUNCTION TIndexEntry.getLabel: String;
     226  begin
     227    result := name;
     228
     229    // index level check (level 1 or 2)
     230    if (getLevel) > 1 then
     231    begin
     232      result := '- ' + result;
     233    end;
     234
     235    if isGlobal then
     236    begin
     237      result := result + ' (g)';
     238    end;
     239  end;
     240
     241
     242  FUNCTION TIndexEntry.isGlobal: boolean;
     243  begin
     244    result := (flags and 64) > 0
     245  end;
     246
     247
     248  FUNCTION TIndexEntry.getLevel: integer;
     249  begin
     250    result := 1;
     251
     252    // index level check (level 1 or 2)
     253    if (flags and 2 ) > 0 then
     254    begin
     255      result := 2;
     256    end;
     257  end;
     258
     259
     260
     261
     262  // -----------
     263  // TIndex
     264  // -----------
     265  CONSTRUCTOR TIndex.Create;
     266  begin
     267    inherited Create;
     268
     269    entries := TStringList.Create;
     270    // labels := nil; // lazy
     271  end;
     272
     273
     274  DESTRUCTOR TIndex.Destroy;
     275  var
     276    i : longint;
     277    tmpEntry : TIndexEntry;
     278  begin
     279    LogEvent(LogObjConstDest, 'TIndex.Destroy (size:' + IntToStr(entries.Count) + ')');
     280
     281    for i := 0 to entries.Count - 1 do
     282    begin
     283      tmpEntry := TIndexEntry(entries.Objects[i]);
     284      if tmpEntry <> nil then
     285      begin
     286        tmpEntry.Destroy;
     287        entries.Objects[i] := nil;
     288      end;
     289    end;
     290    entries.Destroy;
     291
     292    inherited Destroy;
     293  end;
     294
     295
     296  FUNCTION TIndex.Count: longint;
     297  begin
     298    result := entries.Count;
     299  end;
     300
     301
     302  FUNCTION TIndex.GetLabels: TStringList;
     303  begin
     304    result := entries;
     305  end;
     306
     307
     308  FUNCTION TIndex.GetTopic(aPos: longint): TTopic;
     309  begin
     310    result := TTopic(entries.Objects[aPos]);
     311  end;
     312
     313
     314  PROCEDURE TIndex.add(anIndexEntry: TIndexEntry);
     315  begin
     316    // LogEvent(LogDebug, 'TIndex.add(' + aName + ', ' + anIndexEntry.ClassName);
     317    entries.AddObject(anIndexEntry.getLabel, anIndexEntry);
     318  end;
     319
     320
     321
    172322
    173323Procedure OnLanguageEvent( Language: TLanguageFile;
     
    221371end;
    222372
    223 constructor THelpFile.Create( const FileName: string );
    224 begin
    225   LogEvent(LogParse, 'Helpfile Load: ' + FileName);
    226 
    227   _FileName := FileName;
     373
     374constructor THelpFile.Create(const aFileName: string);
     375begin
     376  LogEvent(LogObjConstDest, 'THelpFile.Create (file:' + aFileName + ')');
     377  LogEvent(LogParse, 'Helpfile Load: ' + aFileName);
     378
     379  _FileName := aFileName;
    228380
    229381  InitMembers;
     
    248400end;
    249401
     402
    250403destructor THelpFile.Destroy;
    251404begin
     405  LogEvent(LogObjConstDest, 'THelpFile.Destroy');
    252406  DeallocateMemory( _pHeader );
    253407  DeallocateMemory( _pExtendedHeader );
     
    262416  DeallocateMemory( _pHighlightWords );
    263417
     418  // index entries are pointing to topics
     419  // so let us clean them first
     420  if Assigned( _Index ) then
     421  begin
     422    _Index.Destroy;
     423  end;
     424
    264425  if Assigned( _Topics ) then
     426  begin
    265427    DestroyListAndObjects( _Topics );
    266 
    267   if Assigned( _Index ) then
    268     _Index.Destroy;
     428  end;
    269429
    270430  _Dictionary.Free;
     
    453613end;
    454614
    455 function THelpFile.GetIndex: TStringList;
     615
     616function THelpFile.GetIndex: TIndex;
    456617begin
    457618  if _Index = nil then
     
    477638  pEnd: pointer;
    478639  pIndexData: pointer;
     640
     641  tmpIndexEntry: TIndexEntry;
    479642begin
    480643  LogEvent(LogParse, 'Read index');
    481644
    482   _Index := TStringList.Create;
     645  _Index := TIndex.Create;
    483646
    484647  if _pHeader^.nindex = 0 then
     
    504667
    505668    GetMemString( p, EntryText, IndexTitleLen );
    506     if ( pEntryHeader^.flags and 2 ) > 0 then
    507       EntryText := '- ' + EntryText;
     669
    508670    if pEntryHeader^.TOCIndex < _Topics.Count then
    509       _Index.AddObject( EntryText, _Topics[ pEntryHeader^.TOCIndex ] )
     671    begin
     672      tmpIndexEntry := TIndexEntry.Create(EntryText, _Topics[pEntryHeader^.TOCIndex], pEntryHeader^.flags);
     673      _Index.Add(tmpIndexEntry);
     674    end
    510675    else
    511676//      raise EHelpFileException.Create( 'Error reading help file index - out of range topic reference' );
     
    666831end;
    667832
     833
     834// TODO move to index class
    668835function THelpFile.FindTopicByIndexStartsWith( const SearchText: string ): TTopic;
    669836var
    670837  i: longint;
    671   tmpIndex: String;
     838  tmpLabel: String;
    672839begin
    673840  result := nil;
    674841  GetIndex; // make sure it's read
     842
    675843  for i := 0 to _Index.Count - 1 do
    676844  begin
    677     tmpIndex := _Index.ValuePtrs[i]^;
    678     if StrStartsWithIgnoringCase(tmpIndex, SearchText) then
     845    tmpLabel := _Index.GetLabels.ValuePtrs[i]^;
     846    if StrStartsWithIgnoringCase(tmpLabel, SearchText) then
    679847    begin
    680848      // found
    681       result := TTopic( Index.Objects[ i ] );
     849      result := Index.getTopic(i);
    682850      exit;
    683851    end;
     
    685853end;
    686854
    687 function THelpFile.FindTopicByIndexContains( const SearchText: string ): TTopic;
     855
     856function THelpFile.FindTopicByIndexContains(const SearchText: string): TTopic;
    688857var
    689858  i: longint;
     859  tmpLabel: String;
    690860begin
    691861  result := nil;
    692862  GetIndex; // make sure it's read
     863
    693864  for i := 0 to _Index.Count - 1 do
    694865  begin
    695     if CaseInsensitivePos( SearchText, _Index.ValuePtrs[ i ] ^ ) > 0 then
     866    tmpLabel := _Index.GetLabels.ValuePtrs[i]^;
     867    if CaseInsensitivePos(SearchText, tmpLabel) > 0 then
    696868    begin
    697869      // found
    698       result := TTopic( Index.Objects[ i ] );
     870      result := Index.getTopic(i);
    699871      exit;
    700872    end;
    701873  end;
    702874end;
     875
    703876
    704877function THelpFile.FindTopicByTitleStartsWith( const SearchText: string ): TTopic;
     
    9461119begin
    9471120  Result := pstring( _Dictionary[ Index ] );
    948 end;
    949 
    950 function THelpFile.GetIndexEntryPtr( Index: longint ): pstring;
    951 begin
    952   if _Index = nil then
    953     ReadIndex;
    954   Result := _Index.ValuePtrs[ Index ];
    9551121end;
    9561122
Note: See TracChangeset for help on using the changeset viewer.