home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / turbopas / fmanage.zip / FMANAGE.PAS next >
Pascal/Delphi Source File  |  1992-10-11  |  7KB  |  213 lines

  1. Unit Fmanage;
  2. {=========================================================}
  3. { A TP unit containing some basic file handling routines. }
  4. {                                                         }
  5. { Fmanage has been checked on TP 6.0, but may work on     }
  6. { other versions as well.                                 }
  7. {=========================================================}
  8.  
  9.  
  10. Interface
  11.  
  12. Var
  13.   FileNameSet: set of char;
  14.   { A character set containing all characters valid in DOS file names. }
  15.  
  16. function  IsDirName(DirName: string): boolean;
  17. {================================================================}
  18. { Returns TRUE if DirName is a valid (not necessarily existing!) }
  19. { directory string.                                              }
  20. {================================================================}
  21.  
  22. function  IsFileName(FileName: string): boolean;
  23. {=================================================================}
  24. { Returns TRUE if FileName is a valid (not necessarily existing!) }
  25. { file name string.                                               }
  26. {=================================================================}
  27.  
  28. function  FileExist(FileName: string): Boolean;
  29. {==================================}
  30. { Returns TRUE if FileName exists. }
  31. {==================================}
  32.  
  33. function  TextFileSize(FileName: String): LongInt;
  34. {======================================================}
  35. { Returns the size in bytes of the text file FileName. }
  36. {======================================================}
  37.  
  38. procedure Fdel(FileName: string; Var ErrCode: byte);
  39. {===================================================================}
  40. { Deletes the file FileName. ErrCode returns the standard DOS error }
  41. { codes if unsuccessful.                                            }
  42. {===================================================================}
  43.  
  44. procedure Frename(SourceFile,TargetFile: string; Var ErrCode: byte);
  45. {===============================================================}
  46. { Rename the file SourceName to TargetName. ErrCode returns the }
  47. { standard DOS error codes if unsuccessful.                     }
  48. {===============================================================}
  49.  
  50. procedure Unique(Path: String; Var FileName: String);
  51. {==============================================================}
  52. { Return a unique file name in the directory Path. FileName is }
  53. { empty if unsuccessful.                                       }
  54. {===============================================================}
  55.  
  56.  
  57. Implementation
  58.  
  59. Uses Dos;
  60.  
  61. Function IsDirName(DirName: string): boolean;
  62. Var
  63.   i: byte;
  64.   ch: char;
  65.   ok: boolean;
  66. begin                              { IsDirName }
  67.   ok:=true; ch:=DirName[1];
  68.   if Pos(':',DirName)>0 then ok:=(ch in ['A'..'Z','a'..'z']);
  69.   if ok and (Pos(':',DirName)>2) then ok:=false;
  70.   if ok and (Pos(':',DirName)=2) then
  71.   begin
  72.     Delete(DirName,1,2);
  73.     if Pos(':',DirName)>0 then ok:=false;
  74.   end;
  75.   if ok then
  76.   for i:=1 to length(DirName) do
  77.   begin
  78.     ch:=DirName[i];
  79.     if not (ch in FileNameSet) then ok:=false;
  80.   end;
  81.   IsDirName:=ok;
  82. end;                               { IsDirName }
  83.  
  84. Function IsFileName(FileName: string): boolean;
  85. Var
  86.   i: byte;
  87.   ch: char;
  88.   ok: boolean;
  89.   Dir: DirStr;
  90.   Name: NameStr;
  91.   Ext: ExtStr;
  92.   tmp: string;
  93. begin                                 { IsFileName }
  94.   ok:=true;
  95.   Fsplit(FileName,Dir,Name,Ext);
  96.   if Name='' then
  97.   begin
  98.     IsFileName:=false;
  99.     Exit;
  100.   end;
  101.   ok:=IsDirName(Dir);
  102.   if ok then
  103.   for i:=1 to length(Name) do
  104.   begin
  105.     ch:=Name[i];
  106.     if not (ch in FileNameSet-[':']) then ok:=false;
  107.   end;
  108.   if ok then
  109.   begin
  110.     if (length(Ext)>0) and (Ext[length(Ext)]='.') then
  111.     begin
  112.       tmp:=Ext; Delete(tmp,length(tmp),1); Ext:=tmp;
  113.     end;
  114.     if Ext[1]='.' then
  115.       for i:=2 to length(Ext) do
  116.       begin
  117.         ch:=Ext[i];
  118.         if not (ch in FileNameSet-[':','.','\']) then ok:=false;
  119.       end
  120.     else if length(Ext)>0 then ok:=false;
  121.   end;
  122.   isfilename:=ok;
  123. end;                                  { IsFileName }
  124.  
  125. function FileExist(FileName: string): Boolean;
  126. Var
  127.   tmpfile: Text;
  128.   Attrib: Word;
  129. begin                          { FileExist }
  130.   if FileName='' then
  131.   begin
  132.     FileExist:=false; Exit;
  133.   end;
  134.   assign(tmpfile,FileName);
  135.   GetFAttr(tmpfile,Attrib);
  136.   FileExist:=(DosError=0);
  137. end;                            { FileExist }
  138.  
  139. Function TextFileSize(FileName: String): LongInt;
  140. var
  141.   Attrib: Word;
  142.   Sr: SearchRec;
  143. begin
  144.   if IsFileName(FileName) then
  145.   begin
  146.     FindFirst(FileName,AnyFile and (not (sysfile or Directory)),Sr);
  147.     if DosError=0 then TextFileSize:=Sr.size
  148.     else TextFileSize:=-1;
  149.   end else TextFileSize:=-1;
  150. end;
  151.  
  152. procedure Fdel(FileName: string; Var ErrCode: byte);
  153. var
  154.   reg: registers;
  155. begin                                   { Fdel }
  156.   FileName:=concat(FileName,#0);
  157.   reg.ds:=Seg(FileName[1]); reg.dx:=Ofs(FileName[1]);
  158.   reg.ah:=$41;
  159.   MsDos(reg);
  160.   ErrCode:=0;
  161.   if (reg.flags AND FCarry)=1 then ErrCode:=reg.ax;
  162. end;                                    { Fdel }
  163.  
  164. procedure Frename(SourceFile,TargetFile: string; Var ErrCode: byte);
  165. var
  166.   reg: registers;
  167. begin                                   { Frename }
  168.   SourceFile:=concat(SourceFile,#0);
  169.   TargetFile:=concat(TargetFile,#0);
  170.   reg.ds:=Seg(SourceFile[1]); reg.dx:=Ofs(SourceFile[1]);
  171.   reg.es:=Seg(TargetFile[1]); reg.di:=Ofs(TargetFile[1]);
  172.   reg.ah:=$56;
  173.   MsDos(reg);
  174.   ErrCode:=0;
  175.   if (reg.flags AND FCarry)=1 then ErrCode:=reg.ax;
  176. end;                                    { Frename }
  177.  
  178. Procedure Unique(Path: String; Var FileName: String);
  179. Var
  180.   reg: Registers;
  181.   i: integer;
  182.   ErrCode: Byte;
  183. begin                                      { Unique }
  184.   FileName:='';
  185.   if Path='' then Exit;
  186.   for i:=1 to 15 do Path:=concat(Path,#0);
  187.   reg.ds:=Seg(Path[1]); reg.dx:=Ofs(Path[1]);
  188.   reg.cx:=0;
  189.   reg.ah:=$5A;
  190.   MsDos(reg);
  191.   ErrCode:=0;
  192.   if (reg.flags AND FCarry)=1 then ErrCode:=reg.ax;
  193.   if ErrCode=0 then
  194.   begin
  195.     FileName:=Path;
  196.     i:=1;
  197.     while (i<length(FileName)) and (FileName[i]<>#0) do Inc(i);
  198.     if FileName[i]=#0 then Delete(FileName,i,length(FileName)-i+1);
  199.     {
  200.       Now delete the zero length file created by DOS
  201.     }
  202.     reg.ds:=Seg(Path[1]); reg.dx:=Ofs(Path[1]);
  203.     reg.ah:=$3E;
  204.     reg.bx:=reg.ax;
  205.     MsDos(reg);
  206.   end;
  207. end;                                      { Unique }
  208.  
  209. begin
  210.   FileNameSet:=['!','#'..')',#45,#46,'0'..':','@'..'Z','\','`'..#123,
  211.                 #125,'~','_'];
  212. end.
  213.