Browse Source

Fixed some range errors and other error in chm writing
Fixed a bug where reusing a stream would keep the size of the largest file added! Making the chm much bigger than it should be

git-svn-id: trunk@20412 -

andrew 13 years ago
parent
commit
ffa54e73d9

+ 15 - 5
packages/chm/src/chmfiftimain.pas

@@ -135,7 +135,7 @@ type
     constructor Create(AStream: TStream; AFreeStreamOnDestroy: Boolean);
     constructor Create(AStream: TStream; AFreeStreamOnDestroy: Boolean);
     destructor  Destroy; override;
     destructor  Destroy; override;
     procedure   DumpData(AFoundDataEvent: TChmSearchReaderFoundDataEvent);
     procedure   DumpData(AFoundDataEvent: TChmSearchReaderFoundDataEvent);
-    function    LookupWord(AWord: String; out ATitleHits: TChmWLCTopicArray): TChmWLCTopicArray;
+    function    LookupWord(AWord: String; out ATitleHits: TChmWLCTopicArray; AStartsWith: Boolean = True): TChmWLCTopicArray;
     property    FileIsValid: Boolean read FFileIsValid;
     property    FileIsValid: Boolean read FFileIsValid;
   end;
   end;
 
 
@@ -534,7 +534,11 @@ begin
 
 
   FBlockStream.WriteByte(Length(NewWord)+1);
   FBlockStream.WriteByte(Length(NewWord)+1);
   FBlockStream.WriteByte(Offset);
   FBlockStream.WriteByte(Offset);
-  FBlockStream.Write(NewWord[1], Length(Trim(NewWord)));
+
+  // length can be 0 if it is the same word as the last. there is a word entry each for title and content
+  if Length(NewWord) > 0 then
+    FBlockStream.Write(NewWord[1], Length(Trim(NewWord)));
+
   FBlockStream.WriteByte(Ord(AWord.IsTitle));
   FBlockStream.WriteByte(Ord(AWord.IsTitle));
   WriteCompressedIntegerBE(FBlockStream, AWord.DocumentCount);
   WriteCompressedIntegerBE(FBlockStream, AWord.DocumentCount);
   FBlockStream.WriteDWord(NtoLE(DWord(FWriteStream.Position)));
   FBlockStream.WriteDWord(NtoLE(DWord(FWriteStream.Position)));
@@ -996,7 +1000,7 @@ begin
   until False; //FStream.Position - FActiveNodeStart >= FIFTI_NODE_SIZE - FActiveNodeFreeSpace
   until False; //FStream.Position - FActiveNodeStart >= FIFTI_NODE_SIZE - FActiveNodeFreeSpace
 end;
 end;
 
 
-function TChmSearchReader.LookupWord(AWord: String; out ATitleHits: TChmWLCTopicArray): TChmWLCTopicArray;
+function TChmSearchReader.LookupWord(AWord: String; out ATitleHits: TChmWLCTopicArray; AStartsWith: Boolean = True): TChmWLCTopicArray;
 var
 var
   LastWord: String;
   LastWord: String;
   NewWord: String;
   NewWord: String;
@@ -1020,7 +1024,7 @@ begin
      if ReadIndexNodeEntry(LastWord, NewWord, NewNodePosition) <> False then
      if ReadIndexNodeEntry(LastWord, NewWord, NewNodePosition) <> False then
      begin
      begin
        //WriteLn('Found Index Entry: ', NewWord, ' Comparing to ', AWord);
        //WriteLn('Found Index Entry: ', NewWord, ' Comparing to ', AWord);
-       if ChmCompareText(NewWord, AWord) >= 0 then
+       if  ChmCompareText(NewWord, AWord) >= 0 then
        begin
        begin
          LastWord := '';
          LastWord := '';
          Dec(NodeLevel);
          Dec(NodeLevel);
@@ -1038,7 +1042,13 @@ begin
   begin
   begin
     //WriteLn('Found Leaf Entry: ', NewWord, ' Comparing to ', AWord);
     //WriteLn('Found Leaf Entry: ', NewWord, ' Comparing to ', AWord);
     LastWord := NewWord;
     LastWord := NewWord;
-    CompareResult := ChmCompareText(AWord, NewWord);
+    if Length(NewWord) < Length(AWord) then
+      continue;
+
+    if AStartsWith then //it only has to start with the searched term
+      CompareResult := ChmCompareText(AWord, Copy(NewWord, 1, Length(AWord)))
+    else // it must match exactly
+      CompareResult := ChmCompareText(AWord, NewWord);
     if CompareResult < 0 then
     if CompareResult < 0 then
       Exit;
       Exit;
     if CompareResult = 0 then
     if CompareResult = 0 then

+ 3 - 2
packages/chm/src/chmfilewriter.pas

@@ -209,6 +209,7 @@ begin
   FSpareString.Free;
   FSpareString.Free;
   FTotalFileList.FreeAndClear;
   FTotalFileList.FreeAndClear;
   FTotalFileList.Free;
   FTotalFileList.Free;
+  fAllowedExtensions.Free;
   inherited Destroy;
   inherited Destroy;
 end;
 end;
 
 
@@ -1052,8 +1053,8 @@ begin
   Writer.FullTextSearch := MakeSearchable;
   Writer.FullTextSearch := MakeSearchable;
   Writer.HasBinaryTOC := MakeBinaryTOC;
   Writer.HasBinaryTOC := MakeBinaryTOC;
   Writer.HasBinaryIndex := MakeBinaryIndex;
   Writer.HasBinaryIndex := MakeBinaryIndex;
-  Writer.IndexName := IndexFileName;
-  Writer.TocName   := TableOfContentsFileName;
+  Writer.IndexName := ExtractFileName(IndexFileName);
+  Writer.TocName   := ExtractFileName(TableOfContentsFileName);
   Writer.ReadmeMessage := ReadmeMessage;
   Writer.ReadmeMessage := ReadmeMessage;
   for i:=0 to files.count-1 do
   for i:=0 to files.count-1 do
     begin
     begin

+ 1 - 0
packages/chm/src/chmwriter.pas

@@ -647,6 +647,7 @@ begin
     then begin
     then begin
       // the current file has been read. move to the next file in the list
       // the current file has been read. move to the next file in the list
       FCurrentStream.Position := 0;
       FCurrentStream.Position := 0;
+      FCurrentStream.Size:=0;
       Inc(FCurrentIndex);
       Inc(FCurrentIndex);
       ForceExit := OnGetFileData(FFileNames[FCurrentIndex], FileEntry.Path, FileEntry.Name, FCurrentStream);
       ForceExit := OnGetFileData(FFileNames[FCurrentIndex], FileEntry.Path, FileEntry.Name, FCurrentStream);
       FileEntry.DecompressedSize := FCurrentStream.Size;
       FileEntry.DecompressedSize := FCurrentStream.Size;

+ 4 - 1
packages/chm/src/paslzxcomp.pas

@@ -421,7 +421,10 @@ begin
 	Inc(pathlength);
 	Inc(pathlength);
       end;
       end;
       leaves[i].code := cur_code;
       leaves[i].code := cur_code;
-      Inc(cur_code);
+      {$PUSH}
+      {$R-}
+      Inc(cur_code); // range error but i = 0 so it's harmless
+      {$POP}
     end;
     end;
 //#endif
 //#endif