home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / directry / pff / pdir.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-08-18  |  10.2 KB  |  445 lines

  1. Unit PDir;
  2.  
  3. (*
  4.  
  5.    Palcic Directory Routines
  6.    Copyright (C) 1989, Matthew J. Palcic
  7.    Requires Turbo Pascal 5.5 or higher
  8.  
  9.    v1.0, 18 Aug 89 - Original release.
  10.  
  11. *)
  12.  
  13.  
  14. INTERFACE
  15.  
  16. uses Dos,Objects;
  17.  
  18. (*------------------------------------------------------------------------*)
  19.  
  20. TYPE
  21.  
  22.   AttrType = Byte;
  23.   FileStr = String[12];
  24.  
  25.   BaseEntryPtr = ^BaseEntry;
  26.   BaseEntry = object(Node)
  27.     Attr: AttrType;
  28.     Time: Longint;
  29.     Size: Longint;
  30.     Name: FileStr;
  31.     constructor Init;
  32.     destructor Done; virtual;
  33.     procedure ConvertRec(S:SearchRec);
  34.     function FileName: FileStr; virtual;
  35.     function FileExt: ExtStr; virtual;
  36.     function FullName: PathStr; virtual;
  37.     function FileTime: Longint; virtual;
  38.     function FileAttr: AttrType; virtual;
  39.     function FileSize: Longint; virtual;
  40.     function IsDirectory: Boolean;
  41.     constructor Load(var S: Stream);
  42.     procedure Store(var S: Stream); virtual;
  43.     end;
  44.  
  45.   FileEntryPtr = ^FileEntry;
  46.   FileEntry = object(BaseEntry)
  47.     constructor Init;
  48.     destructor Done; virtual;
  49.     procedure ForceExt(E:ExtStr);
  50.     procedure ChangeName(P:PathStr); virtual;
  51.      (* Change the name in memory *)
  52.     procedure ChangePath(P:PathStr); virtual;
  53.     procedure ChangeTime(T:Longint); virtual;
  54.     procedure ChangeAttr(A:AttrType); virtual;
  55.     procedure Erase; virtual;
  56.     function Rename(NewName:PathStr): Boolean; virtual;
  57.      (* Physically rename file on disk, returns False if Rename fails *)
  58.     function ResetTime: Boolean;
  59.     function ResetAttr: Boolean;
  60.     function SetTime(T:Longint): Boolean; virtual;
  61.     function SetAttr(A:AttrType): Boolean; virtual;
  62.     constructor Load(var S: Stream);
  63.     procedure Store(var S: Stream); virtual;
  64.     end;
  65.  
  66.   DirEntryPtr = ^DirEntry;
  67.   DirEntry = object(FileEntry)
  68.     DirEntries: List;
  69.     constructor Init;
  70.     constructor Clear;
  71.     destructor Done; virtual;
  72.     procedure FindFiles(FileSpec: FileStr; Attrib: AttrType);
  73.     procedure FindDirectories(FileSpec: FileStr; Attrib: AttrType);
  74.     constructor Load(var S: Stream);
  75.     procedure Store(var S: Stream); virtual;
  76.     end;
  77.  
  78.   DirStream = object(DosStream)
  79.     procedure RegisterTypes; virtual;
  80.     end;
  81.  
  82. function ExtensionPos(FName : PathStr): Word;
  83. function CurDir: PathStr;
  84. function ReadString(var S: Stream): String;
  85. procedure WriteString(var S: Stream; Str: String);
  86.  
  87. (*------------------------------------------------------------------------*)
  88.  
  89. IMPLEMENTATION
  90.  
  91.   (*--------------------------------------------------------------------*)
  92.   (* Methods for BaseEntry                                               *)
  93.   (*--------------------------------------------------------------------*)
  94.  
  95.   constructor BaseEntry.Init;
  96.     begin
  97.     end;
  98.  
  99.   destructor BaseEntry.Done;
  100.     begin
  101.     end;
  102.  
  103.   procedure BaseEntry.ConvertRec;
  104.     begin
  105.     Name := S.Name;
  106.     Size := S.Size;
  107.     Time := S.Time;
  108.     Attr := S.Attr;
  109.     end;
  110.  
  111.   function BaseEntry.FileName;
  112.     begin
  113.     FileName := Name;
  114.     end;
  115.  
  116.   function BaseEntry.FullName;
  117.     begin
  118.     FullName := Name;
  119.     end;
  120.  
  121.   function BaseEntry.FileExt;
  122.     var
  123.       ep: word;
  124.     begin
  125.     ep := ExtensionPos(Name);
  126.     if ep > 0 then
  127.       FileExt := Copy(Name, Succ(ep), 3)
  128.     else
  129.       FileExt[0] := #0;
  130.   end;
  131.  
  132.  
  133.   function BaseEntry.FileAttr;
  134.     begin
  135.     FileAttr := Attr;
  136.     end;
  137.  
  138.   function BaseEntry.FileSize;
  139.     begin
  140.     FileSize := Size;
  141.     end;
  142.  
  143.   function BaseEntry.FileTime;
  144.     begin
  145.     FileTime := Time;
  146.     end;
  147.  
  148.   function BaseEntry.IsDirectory;
  149.     begin
  150.     IsDirectory := (FileAttr and Dos.Directory) = Dos.Directory;
  151.     end;
  152.  
  153.   constructor BaseEntry.Load;
  154.     begin
  155.     S.Read(Attr,SizeOf(Attr));
  156.     S.Read(Time,SizeOf(Time));
  157.     S.Read(Size,SizeOf(Size));
  158.     Name := ReadString(S);
  159.     end;
  160.  
  161.   procedure BaseEntry.Store;
  162.     begin
  163.     S.Write(Attr,SizeOf(Attr));
  164.     S.Write(Time,SizeOf(Time));
  165.     S.Write(Size,SizeOf(Size));
  166.     WriteString(S,Name);
  167.     end;
  168.  
  169.   (*--------------------------------------------------------------------*)
  170.   (* Methods for FileEntry                                              *)
  171.   (*--------------------------------------------------------------------*)
  172.  
  173.   constructor FileEntry.Init;
  174.     begin
  175.     BaseEntry.Init; (* Call ancestor's Init *)
  176.     Name := '';
  177.     Size := 0;
  178.     Time := $210000; (* Jan. 1 1980, 12:00a *)
  179.     Attr := $00;  (* ReadOnly  = $01;
  180.                      Hidden    = $02;
  181.                      SysFile   = $04;
  182.                      VolumeID  = $08;
  183.                      Directory = $10;
  184.                      Archive   = $20;
  185.                      AnyFile   = $3F; *)
  186.     end;
  187.  
  188.   destructor FileEntry.Done;
  189.     begin
  190.     BaseEntry.Done;
  191.     end;
  192.  
  193.   function FileEntry.Rename;
  194.     var
  195.       F: File;
  196.     begin
  197.     Assign(F,FullName);
  198.     System.Rename(F,NewName); (* Explicit call to 'System.Rename' avoid
  199.                                  calling method 'FileEntry.Rename' *)
  200.     if IOResult = 0 then
  201.       begin
  202.       ChangePath(NewName);
  203.       Rename := True;
  204.       end
  205.     else
  206.       Rename := False;
  207.     end;
  208.  
  209.   procedure FileEntry.ForceExt;
  210.     var
  211.       ep: Word;
  212.       TempBool: Boolean;
  213.     begin
  214.     ep := ExtensionPos(FullName);
  215.     if ep > 0 then
  216.       TempBool := Rename(Concat(Copy(FullName, 1, ep),FileExt))
  217.     else
  218.       TempBool := Rename(Concat(FullName,'.',FileExt));
  219.     end;
  220.  
  221.   procedure FileEntry.ChangeName;
  222.     begin
  223.     Name := P;
  224.     end;
  225.  
  226.   procedure FileEntry.ChangePath;
  227.     begin
  228.     Name := P;  (* FileEntry object does not handle path *)
  229.     end;
  230.  
  231.   procedure FileEntry.ChangeTime;
  232.     begin
  233.     Time := T;
  234.     end;
  235.  
  236.   procedure FileEntry.ChangeAttr;
  237.     begin
  238.     Attr := A;
  239.     end;
  240.  
  241.   procedure FileEntry.Erase;
  242.     var
  243.       F:File;
  244.     begin
  245.     Assign(F,FullName);
  246.     Reset(F);
  247.     System.Erase(F); (* Remove ambiguity about 'Erase' call *)
  248.     Close(F);
  249.     end;
  250.  
  251.   function FileEntry.ResetTime;
  252.     var
  253.       F:File;
  254.     begin
  255.     Assign(F,FullName);
  256.     Reset(F);
  257.     SetFTime(F,FileTime);
  258.     ResetTime := IOResult = 0;
  259.     Close(F);
  260.     end;
  261.  
  262.   function FileEntry.SetTime;
  263.     var
  264.       F:File;
  265.     begin
  266.     Assign(F,FullName);
  267.     Reset(F);
  268.     SetFTime(F,T);
  269.     SetTime := IOResult = 0;
  270.     Close(F);
  271.     end;
  272.  
  273.   function FileEntry.ResetAttr;
  274.     var
  275.       F:File;
  276.     begin
  277.     Assign(F,FullName);
  278.     SetFAttr(F,FileAttr);
  279.     ResetAttr := IOResult = 0;
  280.     end;
  281.  
  282.   function FileEntry.SetAttr;
  283.     var
  284.       F:File;
  285.     begin
  286.     ChangeAttr(A);
  287.     SetAttr := ResetAttr;
  288.     end;
  289.  
  290.   constructor FileEntry.Load;
  291.     begin
  292.     BaseEntry.Load(S);
  293.     end;
  294.  
  295.   procedure FileEntry.Store;
  296.     begin
  297.     BaseEntry.Store(S);
  298.     end;
  299.  
  300.   (*--------------------------------------------------------------------*)
  301.   (* Methods for DirEntry                                               *)
  302.   (*--------------------------------------------------------------------*)
  303.  
  304.   constructor DirEntry.Init;
  305.     var
  306.       TempNode: Node;
  307.     begin
  308.     FileEntry.Init;
  309.     DirEntries.Delete;
  310.     end;
  311.  
  312.   destructor DirEntry.Done;
  313.     begin
  314.     DirEntries.Delete;
  315.     FileEntry.Done;
  316.     end;
  317.  
  318.   constructor DirEntry.Clear;
  319.     begin
  320.     DirEntries.Clear;
  321.     Init;
  322.     end;
  323.  
  324.   procedure DirEntry.FindFiles;
  325.     var
  326.       DirInfo: SearchRec;
  327.       TempFile: FileEntryPtr;
  328.     begin
  329.     FindFirst(FileSpec,Attrib,DirInfo);
  330.     while (DosError = 0) do
  331.       begin
  332.       TempFile := New(FileEntryPtr,Init);
  333.       TempFile^.ConvertRec(DirInfo);
  334.       DirEntries.Append(TempFile);
  335.       FindNext(DirInfo);
  336.       end;
  337.     end;
  338.  
  339.   procedure DirEntry.FindDirectories;
  340.     var
  341.       DirInfo: SearchRec;
  342.       TempDir: DirEntryPtr;
  343.     begin
  344.  
  345.     if FileSpec <> '' then
  346.       FindFiles(FileSpec,Attrib and not Dos.Directory);
  347.  
  348.     FindFirst('*.*',Dos.Directory,DirInfo);
  349.     while (DosError = 0) do
  350.       begin
  351.       if (DirInfo.Name[1] <> '.') and
  352.          ((DirInfo.Attr and Dos.Directory) = Dos.Directory) then
  353.          { if first character is '.' then name is either '.' or '..' }
  354.         begin
  355.         TempDir := New(DirEntryPtr,Clear);
  356.         TempDir^.ConvertRec(DirInfo);
  357.         DirEntries.Append(TempDir);
  358.         end;
  359.       FindNext(DirInfo);
  360.       end;
  361.  
  362.     TempDir := DirEntryPtr(DirEntries.First);
  363.     while TempDir <> nil do
  364.       begin
  365.       if TempDir^.IsDirectory then
  366.         begin
  367.         ChDir(TempDir^.FileName);
  368.         TempDir^.FindDirectories(FileSpec,Attrib);
  369.         ChDir('..');
  370.         end;
  371.       TempDir := DirEntryPtr(DirEntries.Next(TempDir));
  372.       end;
  373.     end;
  374.  
  375.   constructor DirEntry.Load;
  376.     begin
  377.     FileEntry.Load(S);
  378.     DirEntries.Load(S);
  379.     end;
  380.  
  381.   procedure DirEntry.Store;
  382.     begin
  383.     FileEntry.Store(S);
  384.     DirEntries.Store(S);
  385.     end;
  386.  
  387.   (*--------------------------------------------------------------------*)
  388.   (* Methods for DirStream                                               *)
  389.   (*--------------------------------------------------------------------*)
  390.  
  391.   procedure DirStream.RegisterTypes;
  392.     begin
  393.     DosStream.RegisterTypes;
  394.     Register(TypeOf(BaseEntry),@BaseEntry.Store,@BaseEntry.Load);
  395.     Register(TypeOf(FileEntry),@FileEntry.Store,@FileEntry.Load);
  396.     Register(TypeOf(DirEntry),@DirEntry.Store,@DirEntry.Load);
  397.     end;
  398.  
  399. (*---------------------------------------------------------------------*)
  400. (*  Miscellaneous Unit procedures and functions                        *)
  401. (*---------------------------------------------------------------------*)
  402.  
  403. function ExtensionPos;
  404.   var
  405.     Index: Word;
  406.   begin
  407.   Index := Length(FName)+1;
  408.   repeat
  409.     dec(Index);
  410.     until (FName[Index] = '.') OR (Index = 0);
  411.   IF (Pos('\', Copy(FName, Succ(Index), SizeOf(FName))) <> 0) THEN Index := 0;
  412.   ExtensionPos := Index;
  413.   end;
  414.  
  415. function CurDir;
  416.   var
  417.     P: PathStr;
  418.   begin
  419.   GetDir(0,P); { 0 = Current drive }
  420.   CurDir := P;
  421.   end;
  422.  
  423. function ReadString;
  424.   var
  425.     T: String;
  426.     L: Byte;
  427.  
  428.   begin
  429.   S.Read(L, 1);
  430.   T[0] := Chr(L);
  431.   S.Read(T[1], L);
  432.   IF S.Status = 0 then
  433.     ReadString := T
  434.   else
  435.     ReadString := '';
  436.   end;
  437.  
  438. procedure WriteString;
  439.   begin
  440.   S.Write(Str, Length(Str) + 1);
  441.   end;
  442.  
  443. (* No initialization code *)
  444. end.
  445.