home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / pas / ktools / source / oftext.pas < prev    next >
Pascal/Delphi Source File  |  1994-10-31  |  3KB  |  138 lines

  1. Unit OFText;
  2. { Objet fichier texte dérivé de TBinFile }
  3. { KB Avril-Mai 1994 }
  4. {
  5.  Cet objet permet les déplacements ligne à ligne vers le haut et vers le
  6. bas dans un fichier texte.
  7. }
  8.  
  9. {$IFDEF debug}
  10.  {$A+,B-,D+,E-,F-,I+,L+,N-,R+,S+,V-,W+,X+}
  11. {$ELSE}
  12.  {$A+,B-,D-,E-,F-,I+,L-,N-,R-,S-,V-,W+,X+}
  13. {$ENDIF}
  14.  
  15. INTERFACE
  16.  
  17. Uses Dos,OBinFile;
  18.  
  19. Const
  20.  MaxLigne = 255;
  21.  
  22. Type
  23.   PTextFile=^TTextFile;
  24.   TTextFile=object(TBinFile)
  25.    Constructor Init(NomDeFichier:PathStr;ABufSize:Word);
  26.    { ouverture du fichier et attribution mémoire }
  27.    { La fermeture et la libération mémoire se font par la méthode
  28.      Done héritée de TBinFile. }
  29.    Function GetLine:String;
  30.    { renvoie la ligne courante }
  31.    Procedure GoToPrevLine;
  32.    { fixe la position courante au début de la ligne précédente }
  33.    Procedure GoToNextLine;
  34.    { fixe la position courante au début de la ligne suivante }
  35.    Procedure Append(S:String);
  36.    { ajoute S et un retour chariot à la fin du fichier }
  37.    Function Search(S:String):Boolean;
  38.    { cherche la chaine S et place le pointeur de fichier sur le début
  39.      de la ligne }
  40.    End;
  41.  
  42. IMPLEMENTATION
  43.  
  44. { Objet TTextFile }
  45.  
  46. Constructor TTextFile.Init(NomDeFichier:PathStr;ABufSize:Word);
  47. Begin
  48.  TBinFile.Init(NomDeFichier,ABufSize);
  49. End;
  50.  
  51. Function TTextFile.GetLine:String;
  52. { Lit la ligne en cours et place le pointeur au début de la ligne suivante }
  53. Var S:String;
  54.     l,p:Integer;
  55. Begin
  56.  if EndOfFile
  57.     then begin
  58.           GetLine:='';
  59.           exit;
  60.          end;
  61.  if DataFileSize-DataFilePosit>MaxLigne
  62.     then l:=MaxLigne
  63.     else l:=DataFileSize-DataFilePosit;
  64.  S[0]:=chr(l);
  65.  ReadVar(S[1],l);
  66.  p:=pos(#13#10,S);
  67.  if p<>0
  68.     then begin
  69.           SetFilePosit(DataFilePosit-l+p+1);
  70.           S[0]:=chr(p-1);
  71.          end;
  72.  GetLine:=S;
  73. End;
  74.  
  75. Procedure TTextFile.GoToNextLine;
  76. { Place le pointeur au début de la ligne suivante }
  77. Var S:String;
  78. Begin
  79.  S:=GetLine;
  80. End;
  81.  
  82. Procedure TTextFile.GoToPrevLine;
  83. { Place le pointeur au début de la ligne précédente }
  84. Var car:byte;
  85. Begin
  86.  if DataFilePosit=0 then exit;
  87.  SetFilePosit(DataFilePosit-3);
  88.  car:=ReadByte;
  89.  while (DataFilePosit>1) and (car<>10) and (car<>13) do
  90.   begin
  91.    SetFilePosit(DataFilePosit-3);
  92.    car:=ReadByte;
  93.   end;
  94.  if car=13
  95.     then SetFilePosit(DataFilePosit+1);
  96.  if DataFilePosit=1 then SetFilePosit(0);
  97. End;
  98.  
  99. Procedure TTextFile.Append(S:String);
  100. Begin
  101.  S:=S+#13#10;
  102.  SetEndPosit;
  103.  WriteVar(S[1],length(S));
  104. End;
  105.  
  106. Function TTextFile.Search(S:String):Boolean;
  107. { cherche la chaine S et place le pointeur de fichier sur le début
  108.   de la ligne }
  109.  
  110.  Procedure UpStr(Var Str:String);
  111.  Var i:Byte;
  112.  Begin
  113.   for i:=1 to length(Str) do
  114.    Str[i]:=UpCase(Str[i]);
  115.  End;
  116.  
  117. Var W:String;
  118. Begin
  119.  Search:=false;
  120.  if S='' then exit;
  121.  UpStr(S);
  122.  while not EndOfFile do
  123.   begin
  124.    W:=GetLine;
  125.    UpStr(W);
  126.    if pos(S,W)<>0
  127.       then begin
  128.             GoToPrevLine;
  129.             Search:=true;
  130.             exit;
  131.            end;
  132.   end;
  133. End;
  134.  
  135. END.
  136.  
  137. {                          Fin du fichier OFTEXT.PAS                        }
  138.