home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_PAS / XLIB_TP5.ZIP / UNITS / X_FILEIO.PAS < prev    next >
Pascal/Delphi Source File  |  1994-01-14  |  10KB  |  377 lines

  1. Unit X_FileIO;
  2.  
  3. (*
  4.     File procedures.
  5.  
  6.     ****** XLIB - Mode X graphics library                ****************
  7.     ******                                               ****************
  8.     ****** Converted By Christian Harms in TP            ****************
  9.  
  10.     Harms   : harms@minnie.informatik.uni-stuttgart.de
  11.  
  12.  
  13.     These function are uses by all file load/save-functions of XLib.
  14.  
  15.     If you have many files ( for example PBM's), you can "copy" all to
  16.     one big file (Masterfile) - ( with help of Make_MF ).
  17.  
  18.     After Init_Masterfile, all procedures search firstly, if the file
  19.     present in MasterFile. If true, file-variable will set to MasterFile
  20.     and Masterfile-position set to begin of file. If not Masterfile
  21.     activated, this functions are normaly filefunction.
  22.  
  23.     Because var MasterFile:File, you can only Read with BlockRead !
  24.  
  25.  
  26.     MasterFile structure :
  27.  
  28.     Word    :  Count of Files
  29.     Index_1 :  Filename1 : String : 1. Byte         : length n
  30.                                     2. .. n+1. Byte : Filename
  31.                            Start  : DWord           : absolute Startposition
  32.                                                       of File1 in MasterFile
  33.                            Size   : DWord           : Filesize of File1
  34.  
  35.        ""
  36.     Index_Count
  37.  
  38.     File_1     - Data
  39.        ""
  40.     File_Count - Data
  41.  
  42.     Demo of creating MasterFile - look in Make_MF.pas oder Make_MF.EXE.
  43.  
  44. *)
  45.  
  46.  
  47. interface
  48.  
  49. (* result = 0, if Name not exist, else Size in Bytes. MasterFile supported.*)
  50. function  F_Size(Name:String):LongInt;
  51.  
  52. (* Open an File for Read. True,if opens.              MasterFile supported.*)
  53. function  F_Open_Read(var F:File;Name:String):Boolean;
  54.  
  55. (* Open an File for Write.                                                 *)
  56. procedure F_Open_Write(var F:File;Name:String);
  57.  
  58. (* Close an File.                                     MasterFile supported.*)
  59. procedure F_Close(var F:File);
  60. procedure Close(var F:File); (* security definition *)
  61.  
  62. (* Make on every Char of S an Uppercase.                                   *)
  63. function  Upper(S:String):String;
  64.  
  65. (* Delete and Add, if incorrect or non Ext in FileName S.                  *)
  66. function  Only_one_Ext(S:String;Ext:String):String;
  67.  
  68. (* Open MasterFile and load Filelist.                                      *)
  69. function  Init_MasterFile(Name:String):Boolean;
  70.  
  71. (* Close MasterFile, all function are now normal file-functions.           *)
  72. procedure Close_MasterFile;
  73.  
  74. (* If you dont know, how to read from F:File with BlockRead, use these     *)
  75. (* functions.                                                              *)
  76. function  Read_Byte(var F:File):Byte;          (* Read a Byte              *)
  77. function  Read_Word(var F:File):Word;          (* Read a Word    (2 Bytes) *)
  78. function  Read_LongInt(var F:File):LongInt;    (* Read a LongInt (4 Bytes) *)
  79. function  Read_Line(var F:File):String;        (* Read a String  (S[0]...) *)
  80. function  Read_String(var F:File):String;      (* Read a Line (Textfile)   *)
  81. procedure Write_Text(Var F:File;S:String);     (* Write a String as text   *)
  82. procedure Write_Line(Var F:File;S:String);     (* Write Text with CR       *)
  83.  
  84. (* Only for creating own Masterfile - demo in Make_MF *)
  85. procedure Init_File_List;
  86. function  Add_File_List(Name:String):Boolean;
  87. procedure Kill_File_List;
  88. function  Get_File_item_count:Word;
  89. procedure Make_MasterFile(Name:String);
  90.  
  91. implementation
  92.  
  93. uses crt;
  94.  
  95. function Read_Byte(var F:File):Byte;
  96. var B:Byte;
  97. begin;
  98.   BlockRead(F,B,1);
  99.   Read_Byte:=B;
  100. end;
  101.  
  102. function Read_Word(var F:File):Word;
  103. var w:Word;
  104. begin;
  105.   BlockRead(F,w,2);
  106.   Read_Word:=w;
  107. end;
  108.  
  109. function Read_LongInt(var F:File):LongInt;
  110. var i:LongInt;
  111. begin;
  112.   BlockRead(F,i,4);
  113.   Read_LongInt:=i;
  114. end;
  115.  
  116. function Read_Line(var F:File):String;
  117. var C:Char;
  118.     S:String;
  119. begin;
  120.   c:=#0;
  121.   S:='';
  122.   while (not eof(F))and(c<>#10) do
  123.   begin;
  124.     BlockRead(F,c,1);
  125.     if not (C in [#10,#13]) then S:=S+c;
  126.   end;
  127.   Read_Line:=S;
  128. end;
  129.  
  130. function Read_String(var F:File):String;
  131. var S:String;
  132. begin;
  133.   BlockRead(F,S[0],1);
  134.   BlockRead(f,S[1],length(S));
  135.   Read_String:=S;
  136. end;
  137.  
  138. procedure Write_Text(Var F:File;S:String);
  139. begin;
  140.   BlockWrite(F,s[1],length(s));
  141. end;
  142.  
  143. procedure Write_Line(Var F:File;S:String);
  144. begin;
  145.   Write_Text(F,S+#13#10);
  146. end;
  147.  
  148. const MaxIndex  = 1024;
  149. type
  150.      Index      = record
  151.           Name  : ^String;
  152.           Start : LongInt;
  153.           Size  : LongInt;
  154.      end;
  155.      ListP      = Array[0..1024] of Index;
  156. var  Name_Count : Word;
  157.      List       : ^ListP;
  158.      MF_File    : File;
  159.  
  160. function GetIndex(Name:String):Word;
  161. var I:Word;
  162. begin;
  163.   if (List=NIL)or(List^[0].Size=MaxIndex) then begin;GetIndex:=0;exit;end;
  164.   Name:=Upper(Name);
  165.   i:=1;
  166.   while (i<=Name_Count)and(List^[i].Name^<>Name) do Inc(i);
  167.  
  168.   if List^[i].Name^=Name then GetIndex:=i
  169.                          else GetIndex:=0;
  170. end;
  171.  
  172. function F_Size(Name:String):LongInt;
  173. var F:File;
  174.     i:Word;
  175. begin;
  176.   i:=GetIndex(Name);                 (* MasterFile Handling *)
  177.   if i>0 then F_Size:=List^[i].Size
  178.          else begin;
  179.                 Assign(F,Name);      (* Normal File Handling *)
  180.                 {$I-}
  181.                 Reset(F,1);
  182.                 {$I+}
  183.                 if IOResult=0 then begin;
  184.                                      F_Size:=FileSize(F);
  185.                                      System.Close(F);
  186.                                    end
  187.                               else F_Size:=0;
  188.               end;
  189. end;
  190.  
  191. function F_Open_Read(var F:File;Name:String):Boolean;
  192. var I:Word;
  193. begin;
  194.   I:=GetIndex(Name);                 (* MasterFile Handling *)
  195.   if i>0 then begin;
  196.                 move(MF_File,F,sizeof(F));
  197.                 Seek(F,List^[i].Start);
  198.                 end
  199.          else begin;                 (* Normal File Handling *)
  200.                 Assign(F,Name);
  201.                 {$I-}
  202.                 Reset(F,1);
  203.                 {$I+}
  204.                 if IOResult=0 then F_Open_Read:=True
  205.                               else F_Open_Read:=False;
  206.               end;
  207. end;
  208.  
  209. procedure F_Open_Write(var F:File;Name:String);
  210. begin;
  211.   Assign(F,Name);
  212.   Rewrite(F,1);
  213. end;
  214.  
  215. procedure F_Close(var F:File);
  216. begin;
  217.   if (List=NIL)or
  218.      (List^[0].Size=MaxIndex) or
  219.      (MEMW[seg(F):ofs(F)]<>MEMW[seg(MF_File):ofs(MF_File)])
  220.         then System.Close(F)
  221.         {else begin;sound(500);delay(100);nosound;end};
  222. end;
  223.  
  224. procedure Close(var F:File);
  225. begin;
  226.   F_Close(f);
  227. end;
  228.  
  229. function Upper(S:String):String;
  230. var I:Byte;
  231. begin;
  232.   for i:=1 to Length(S) do S[i]:=Upcase(S[i]);
  233.   Upper:=S;
  234. end;
  235.  
  236.  
  237. function Only_one_Ext(S:String;Ext:String):String;
  238. var i:Byte;
  239. begin;
  240.   S:=Upper(S);
  241.  
  242.   if (pos('.'+Ext,S)<>length(S)-length(Ext)) or
  243.      (pos('.',S)<>length(S)-4)                    then
  244.       while (Pos('.',S)>0) do S:=copy(S,1,pos('.',S)-1);
  245.  
  246.   S:=S+'.'+Ext;
  247.   Only_one_Ext:=S;
  248. end;
  249.  
  250. (* Open MasterFile and load Filelist.                                      *)
  251. function Init_MasterFile(Name:String):Boolean;
  252. var I,J : LongInt;
  253.     S   : String;
  254. begin;
  255.   Assign(MF_File,Name);
  256.   {$I-}Reset(MF_File,1);{$I+}
  257.   if (IOResult<>0)or(List<>NIL) then begin;Init_MasterFile:=False;exit;end;
  258.  
  259.   Name_Count:=Read_Word(MF_File);
  260.   if Name_Count>=MaxIndex then Name_Count:=MaxIndex-1;
  261.  
  262.   GetMEM(List,(Name_Count+1)*SizeOf(Index));
  263.  
  264.   (* 0. Index.Size<1024 - init here, =1024 - init by Init_File_List *)
  265.   List^[0].Size:=Name_Count;
  266.  
  267.   for i:=1 to Name_Count do
  268.   begin;
  269.     S:=Read_String(MF_File);
  270.     GetMEM(List^[i].Name,length(s)+1);
  271.     List^[i].Name^:=Upper(S);
  272.     List^[i].Start:=Read_LongInt(MF_File);
  273.     List^[i].Size :=Read_LongInt(MF_File);
  274.   end;
  275. end;
  276.  
  277.  
  278. (* Close MasterFile, all function are now normal file-functions.           *)
  279. procedure Close_MasterFile;
  280. var I:Word;
  281. begin;
  282.   if Name_Count>0 then
  283.   begin;
  284.     for i:=1 to Name_Count do FreeMEM(List^[i].Name,length(List^[i].Name^)+1);
  285.     FreeMEM(List,(Name_Count+1)*SizeOf(Index));
  286.     List:=NIL;
  287.     System.Close(MF_File);
  288.   end;
  289.   Name_Count:=0;
  290. end;
  291.  
  292. procedure Init_File_List;
  293. begin;
  294.   New(List);
  295.   List^[0].Size:=MaxIndex;
  296.   Name_Count:=0;
  297. end;
  298.  
  299. (* false, if init by Init_MastFile and not by Init_FileList *)
  300. function Add_File_List(Name:String):Boolean;
  301. var i:LongInt;
  302. begin;
  303.   i:=F_Size(Name);
  304.   if (i=0)or(Name_Count=MaxIndex-1) then begin;Add_File_List:=false;exit;end;
  305.  
  306.   Inc(Name_Count);
  307.   GetMEM(List^[Name_Count].Name,length(Name)+1);
  308.   List^[Name_Count].Name^:=Name;
  309.   List^[Name_Count].Start:=0;
  310.   List^[Name_Count].Size :=i;
  311. end;
  312.  
  313. procedure Kill_File_List;
  314. var i:Word;
  315. begin;
  316.   for i:=1 to Name_Count do FreeMEM(List^[i].Name,sizeof(List^[i].Name^)+1);
  317.   Dispose(List);
  318. end;
  319.  
  320. function Get_File_item_count:Word;
  321. begin;
  322.   Get_File_item_count:=Name_Count;
  323. end;
  324.  
  325. procedure Make_MasterFile(Name:String);
  326. var i:Word;
  327.     Count:LongInt;
  328.     F,F1:File;
  329.     Buffer:Array[0..1023] of Byte;
  330.     T1,T2:LongInt;
  331. begin;
  332.   F_Open_Write(F,Name);
  333.  
  334.   Count:=2;                                 (* Name_Count *)
  335.  
  336.   for i:=1 to Name_Count do
  337.       Inc(Count,length(List^[i].Name^)+1);  (* + length(All_Names) *)
  338.  
  339.   Count:=Count+8*Name_Count;                (* + All_Starts + All_Size *)
  340.  
  341.   BlockWrite(F,Name_Count,2);
  342.  
  343.   for i:=1 to Name_Count do           (* Write File_List               *)
  344.   begin;
  345.     BlockWrite(F,List^[i].Name^[0],length(List^[i].Name^)+1);
  346.     BlockWrite(F,Count,4);
  347.     BlockWrite(F,List^[i].Size,4);
  348.     Inc(Count,List^[i].Size);
  349.   end;
  350.  
  351.   for i:=1 to Name_Count do           (* Write Files                   *)
  352.   begin;
  353.     if F_Open_Read(F1,List^[i].Name^) then
  354.     begin;
  355.       T1:=List^[i].Size;
  356.       while T1>1024 do begin;
  357.                          BlockRead(F1,Buffer,1024);
  358.                          BlockWrite(F,Buffer,1024);
  359.                          Dec(T1,1024);
  360.                        end;
  361.       BlockRead(F1,Buffer,T1);
  362.       BlockWrite(F,Buffer,T1);
  363.       Close(F1);
  364.     end;
  365.   end;
  366.  
  367.   Close(F);
  368. end;
  369.  
  370.  
  371.  
  372.  
  373.  
  374. begin;
  375.   Name_Count:=0;
  376.   List:=NIL;
  377. end.