{ Copyright (C) <2005> chmspecialparser.pas This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. } { See the file COPYING.LCL, included in this distribution, for details about the copyright. } unit ChmSpecialParser; {$mode objfpc}{$H+} //{$INLINING ON} interface uses Classes, Forms, SysUtils, Controls, ComCtrls; type TContentTreeNode = class(TTreeNode) private fUrl: String; public property Url:String read fUrl write fUrl; end; TContentNode = record Name: String; Url: String; LineCount: Integer; end; TIndexItem = class(TListITem) private fUrl: String; public property Url:String read fUrl write fUrl; end; { TContentsFiller } TContentsFiller = class(TObject) private fTreeView: TTreeView; fStream: TStream; fText: TStringList; fBranchCount: DWord; fStop: PBoolean; fULList: Array of Integer; procedure CustomCreateContentTreeItem(Sender: TCustomTreeView; var ATreeNode: TTreenode); function AddULTree(StartLine, EndLine: Integer; ParentNode: TTreeNode): Boolean; function GetULEnd(StartLine: Integer): Integer; function GetLIData(StartLine: Integer): TContentNode; procedure FillULIndices; public constructor Create(ATreeView: TTreeView; AStream: TStream; StopBoolean: PBoolean); procedure DoFill; //inline; end; { TIndexFiller } TIndexFiller = class(TObject) private fListView: TListView; fStream: TStream; fText: TStringList; function GetLIEnd(StartLine: Integer): Integer; function GetNextLI(StartLine: Integer): Integer; function AddLIObjects(StartLine: Integer): Integer; function LineHasLI(ALine: Integer): Boolean; public constructor Create(AListView: TListView; AStream: TStream); procedure DoFill; end; implementation function FixURL(URL: String):String; var X: LongInt; begin X := Pos('%20', Url); while X > 0 do begin Delete(Url, X, 3); Insert(' ', Url, X); X := Pos('%20', Url); end; Result := Url; end; { TContentsFiller } procedure TContentsFiller.CustomCreateContentTreeItem(Sender: TCustomTreeView; var ATreeNode: TTreenode); begin ATreeNode := TContentTreeNode.Create(TTreeView(Sender).Items); end; function TContentsFiller.AddULTree(StartLine, EndLine: Integer; ParentNode: TTreeNode): Boolean; var X: LongInt; TreeNode: TContentTreeNode = nil; NodeInfo: TContentNode; ULEnd: Integer; begin Result := True; Inc(fBranchCount); X := StartLine-1; while X < EndLine do begin Inc(X); if Pos('
  • ', fText.Strings[X]) > 0 then begin NodeInfo := GetLIData(X); TreeNode := TContentTreeNode(fTreeView.Items.AddChild(ParentNode, NodeInfo.Name)); TreeNode.Url := NodeInfo.Url; Inc(X, NodeInfo.LineCount); end; if (X <> StartLine) and (fULList[X-1] < fULList[X]) then begin ULEnd := GetULEnd(X); if not AddULTree(X, ULEnd, TreeNode) then exit(False); Inc(X, ULEnd-X); end; end; if fBranchCount mod 200 = 0 then begin Application.ProcessMessages; if fStop^ = True then Exit(False); end; end; function TContentsFiller.GetULEnd(StartLine: Integer): Integer; var X: LongInt; begin for X := StartLine+1 to fText.Count-1 do begin if fULList[X] < fULList[StartLine] then begin Result := X; Exit; end; end; end; function TContentsFiller.GetLIData(StartLine: Integer): TContentNode; var X: Integer; NameCount: Integer = 0; fLength: Integer; fPos: Integer; Line: String; begin FillChar(Result, SizeOf(Result), 0); for X := StartLine to fText.Count-1 do begin Line := fText.Strings[X]; fPos := Pos('', UpperCase(Line)) > 0 then begin Result.LineCount := X-StartLine; Break; end; end; end; Result.Url := FixURL(Result.Url); end; procedure TContentsFiller.FillULIndices; var ULDepth: Integer = 0; X: Integer; begin for X := 0 to fText.Count-1 do begin if Pos('', fText.Strings[X]) > 0 then Dec(ULDepth); fULList[X] := ULDepth; end; end; constructor TContentsFiller.Create(ATreeView: TTreeView; AStream: TStream; StopBoolean: PBoolean); begin inherited Create; fTreeView := ATreeView; fStream := AStream; fStop := StopBoolean; end; procedure TContentsFiller.DoFill; var OrigEvent: TTVCustomCreateNodeEvent; X: Integer; begin OrigEvent := fTreeView.OnCustomCreateItem; fTreeView.OnCustomCreateItem := @CustomCreateContentTreeItem; fText := TStringList.Create; //TMemoryStream(fStream).SaveToFile('/tmp/contents.txt'); fStream.Position := 0; fTreeView.BeginUpdate; fTreeView.Items.Clear; fText.LoadFromStream(fStream); SetLength(fULList, fText.Count); FillULIndices; for X := 0 to fText.Count-1 do begin if Pos('