home *** CD-ROM | disk | FTP | other *** search
/ Action Games (2008) / akcnihry1.iso / AT-Robots 2.10 / MYFILE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-04-27  |  8.4 KB  |  420 lines

  1. (*
  2. Copyright (c) 1999, Ed T. Toton III. All rights reserved.
  3.  
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7.  
  8.    Redistributions of source code must retain the above copyright notice,
  9.    this list of conditions and the following disclaimer.
  10.  
  11.    Redistributions in binary form must reproduce the above copyright notice, 
  12.    this list of conditions and the following disclaimer in the documentation
  13.    and/or other materials provided with the distribution.
  14.  
  15.    All advertising materials mentioning features or use of this software
  16.    must display the following acknowledgement:
  17.  
  18.         This product includes software developed by Ed T. Toton III &
  19.         NecroBones Enterprises.
  20.  
  21.    No modified or derivative copies or software may be distributed in the
  22.    guise of official or original releases/versions of this software. Such
  23.    works must contain acknowledgement that it is modified from the original.
  24.  
  25.    Neither the name of the author nor the name of the business or
  26.    contributers may be used to endorse or promote products derived
  27.    from this software without specific prior written permission.
  28.  
  29. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 
  30. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  31. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
  33. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  34. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  36. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  38. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *)
  40.  
  41. { This unit contains functions useful for handling files, including
  42.   renaming, deleting, and accessing in high-speed variable-length
  43.   random access. }
  44.  
  45.  
  46. {$G+}{$N+}{$E+}{$X+}{$D-}
  47. Unit myfile;
  48.  
  49. interface
  50.  
  51. uses filelib;
  52.  
  53. var
  54.  the_name:string;
  55.  the_eof:byte;
  56.  
  57. function  file_size(fn:string):longint;
  58. procedure delete_file(fn:string);
  59. procedure rename_file(fn,fn2:string);
  60. Procedure open_filein(var h:word; fn:string);
  61. Procedure open_fileout(var h:word; fn:string);
  62. Procedure open_fileboth(var h:word; fn:string);
  63. Procedure create_fileout(var h:word; fn:string);
  64. Procedure read_file(h:word; ploc:pointer; var len:integer);
  65. Procedure write_file(h:word; ploc:pointer; var len:integer);
  66. procedure close_file(h:word);
  67. Procedure file_seek(h,d:word; m:byte; var p:word);
  68. Procedure long_seek(h:word; d:longint; m:byte; var p:longint);
  69. Procedure read_long(h:word; ploc:pointer; var len:word);
  70. Procedure write_long(h:word; ploc:pointer; var len:word);
  71.  
  72.  
  73. implementation
  74.  
  75.  
  76. function lstr(s1:string; l:integer):string;
  77. begin
  78.  if length(s1)<=l then lstr:=s1
  79.  else lstr:=copy(s1,1,l);
  80. end;
  81.  
  82. function file_size(fn:string):longint;
  83. var
  84.  f:file of byte;
  85. begin
  86.  if not exist(fn) then
  87.   begin file_size:=-1; exit; end;
  88.  assign(f,fn);
  89.  reset(f);
  90.  file_size:=filesize(f);
  91.  close(f);
  92. end;
  93.  
  94. procedure delete_file(fn:string);
  95. var
  96.  f:file of byte;
  97. begin
  98.  if not exist(fn) then exit;
  99.  assign(f,fn);
  100.  erase(f);
  101. end;
  102.  
  103. procedure rename_file(fn,fn2:string);
  104. var
  105.  f:file of byte;
  106. begin
  107.  if not exist(fn) then exit;
  108.  assign(f,fn);
  109.  rename(f,fn2);
  110. end;
  111.  
  112. Procedure open_filein(var h:word; fn:string);
  113. var
  114.  w:word;
  115. begin
  116.  the_name:=fn+#0;
  117.  asm
  118.   push  ds
  119.   mov   dx,     seg the_name
  120.   mov   ds,     dx
  121.   mov   dx,     offset the_name
  122.   inc   dx
  123.   mov   ax,     3D00h;
  124.   int   21h
  125.   pop   ds
  126.   mov   w,      ax
  127.  end;
  128.  h:=w;
  129. end;
  130.  
  131. Procedure open_fileboth(var h:word; fn:string);
  132. var
  133.  w:word;
  134. begin
  135.  the_name:=fn+#0;
  136.  asm
  137.   push  ds
  138.   mov   dx,     seg the_name
  139.   mov   ds,     dx
  140.   mov   dx,     offset the_name
  141.   inc   dx
  142.   mov   ax,     3D02h;
  143.   int   21h
  144.   pop   ds
  145.   mov   w,      ax
  146.  end;
  147.  h:=w;
  148. end;
  149.  
  150. Procedure open_fileout(var h:word; fn:string);
  151. var
  152.  w:word;
  153. begin
  154.  the_name:=fn+#0;
  155.  if exist(lstr(fn,length(fn)-1)) then
  156.  asm
  157.   push  ds
  158.   mov   dx,     seg the_name
  159.   mov   ds,     dx
  160.   mov   dx,     offset the_name
  161.   inc   dx
  162.   mov   ax,     3D01h;
  163.   int   21h
  164.   pop   ds
  165.   mov   w,      ax
  166.  end
  167.  else
  168.  asm
  169.   push  ds
  170.   mov   dx,     seg the_name
  171.   mov   ds,     dx
  172.   mov   dx,     offset the_name
  173.   inc   dx
  174.   mov   cx,     20h
  175.   mov   ax,     3C00h;
  176.   int   21h
  177.   pop   ds
  178.   mov   w,      ax
  179.  end;
  180.  h:=w;
  181. end;
  182.  
  183. Procedure create_fileout(var h:word; fn:string);
  184. var
  185.  w:word;
  186. begin
  187.  the_name:=fn+#0;
  188.  asm
  189.   push  ds
  190.   mov   dx,     seg the_name
  191.   mov   ds,     dx
  192.   mov   dx,     offset the_name
  193.   inc   dx
  194.   mov   cx,     20h
  195.   mov   ax,     3C00h;
  196.   int   21h
  197.   pop   ds
  198.   mov   w,      ax
  199.  end;
  200.  h:=w;
  201. end;
  202.  
  203.  
  204. Procedure read_file(h:word; ploc:pointer; var len:integer);
  205. var
  206.  tseg,tofs,pp,w:word;
  207.  ll:integer;
  208. label ok,uh_oh,alright;
  209. begin
  210.  tseg:=seg(ploc^);
  211.  tofs:=ofs(ploc^);
  212.  ll:=len; w:=0;
  213.  asm
  214.   push  ds
  215.   mov   bx,     h
  216.   mov   cx,     ll
  217.   mov   dx,     tseg
  218.   mov   ds,     dx
  219.   mov   dx,     tofs
  220.   mov   ax,     3F00h;
  221.   int   21h
  222.   jc    uh_oh
  223.   jmp   alright
  224.   uh_oh:
  225.   mov   w,      ax
  226.   alright:
  227.   pop   ds
  228.   cmp   ll,     ax
  229.   je    ok
  230.   mov   the_eof,1
  231.   ok:
  232.   mov   ll,     ax
  233.  end;
  234.  len:=ll;
  235.  if w<>0 then begin writeln(' ****** ',w,' ****** '); {pausescr;} end;
  236. end;
  237.  
  238. Procedure write_file(h:word; ploc:pointer; var len:integer);
  239. var
  240.  tseg,tofs:word;
  241.  ll:integer;
  242. begin
  243.  tseg:=seg(ploc^);
  244.  tofs:=ofs(ploc^);
  245.  ll:=len;
  246.  asm
  247.   push  ds
  248.   mov   bx,     h
  249.   mov   cx,     ll
  250.   mov   dx,     tseg
  251.   mov   ds,     dx
  252.   mov   dx,     tofs
  253.   mov   ax,     4000h;
  254.   int   21h
  255.   pop   ds
  256.   mov   ll,     ax
  257.  end;
  258.  len:=ll;
  259. end;
  260.  
  261. Procedure read_long(h:word; ploc:pointer; var len:word);
  262. var
  263.  tseg,tofs,pp,w:word;
  264.  ll:word;
  265. label ok,uh_oh,alright;
  266. begin
  267.  tseg:=seg(ploc^);
  268.  tofs:=ofs(ploc^);
  269.  ll:=len; w:=0;
  270.  asm
  271.   push  ds
  272.   mov   bx,     h
  273.   mov   cx,     ll
  274.   mov   dx,     tseg
  275.   mov   ds,     dx
  276.   mov   dx,     tofs
  277.   mov   ax,     3F00h;
  278.   int   21h
  279.   jc    uh_oh
  280.   jmp   alright
  281.   uh_oh:
  282.   mov   w,      ax
  283.   alright:
  284.   pop   ds
  285.   cmp   ll,     ax
  286.   je    ok
  287.   mov   the_eof,1
  288.   ok:
  289.   mov   ll,     ax
  290.  end;
  291.  len:=ll;
  292.  if w<>0 then begin writeln(' ****** ',w,' ****** '); {pausescr;} end;
  293. end;
  294.  
  295. Procedure write_long(h:word; ploc:pointer; var len:word);
  296. var
  297.  tseg,tofs:word;
  298.  ll:word;
  299. begin
  300.  tseg:=seg(ploc^);
  301.  tofs:=ofs(ploc^);
  302.  ll:=len;
  303.  asm
  304.   push  ds
  305.   mov   bx,     h
  306.   mov   cx,     ll
  307.   mov   dx,     tseg
  308.   mov   ds,     dx
  309.   mov   dx,     tofs
  310.   mov   ax,     4000h;
  311.   int   21h
  312.   pop   ds
  313.   mov   ll,     ax
  314.  end;
  315.  len:=ll;
  316. end;
  317.  
  318. procedure close_file(h:word);
  319. begin
  320.  asm
  321.   mov   ax,     3E00h
  322.   mov   bx,     h
  323.   int   21h
  324.  end;
  325. end;
  326.  
  327. Procedure file_seek(h,d:word; m:byte; var p:word);
  328. var
  329.  w:word;
  330. begin
  331.  asm
  332.   mov   bx,     h
  333.   mov   cx,     0
  334.   mov   dx,     d
  335.   mov   al,     m
  336.   mov   ah,     42h
  337.   int   21h
  338.   mov   w,      ax
  339.  end;
  340.  p:=w;
  341. end;
  342.  
  343. Procedure long_seek(h:word; d:longint; m:byte; var p:longint);
  344. var
  345.  d1,d2:word;
  346. begin
  347.  d1:=d shr 16;
  348.  d2:=d and $FFFF;
  349.  asm
  350.   mov   bx,     h
  351.   mov   cx,     d1
  352.   mov   dx,     d2
  353.   mov   al,     m
  354.   mov   ah,     42h
  355.   int   21h
  356.   mov   d1,      ax
  357.   mov   d2,      dx
  358.  end;
  359.  p:=d2; p:=p shl 16;
  360.  p:=p or d1;
  361. end;
  362.  
  363. {procedure read_long(var h:word; p:pointer; var l:longint);
  364. var
  365.  len:longint;
  366.  k:integer;
  367. begin
  368.  l:=len;
  369.  if len<=0 then exit;
  370.  if len>65535 then len:=65535;
  371.  if len<=32000 then
  372.   begin
  373.    k:=len;
  374.    read_file(h,p,k);
  375.    l:=k;
  376.   end
  377.  else
  378.   begin
  379.    k:=32000;
  380.    read_file(h,p,k);
  381.    if k<32000 then l:=k else
  382.     begin
  383.      k:=len-32000;
  384.      read_file(h,ptr(seg(p^),ofs(p^)+32000),k);
  385.      l:=k+32000;
  386.     end;
  387.   end;
  388. end;
  389.  
  390. procedure write_long(var h:word; p:pointer; var l:longint);
  391. var
  392.  len:longint;
  393.  k:integer;
  394. begin
  395.  l:=len;
  396.  if len<=0 then exit;
  397.  if len>65535 then len:=65535;
  398.  if len<=32000 then
  399.   begin
  400.    k:=len;
  401.    write_file(h,p,k);
  402.    l:=k;
  403.   end
  404.  else
  405.   begin
  406.    k:=32000;
  407.    write_file(h,p,k);
  408.    if k<32000 then l:=k else
  409.     begin
  410.      k:=len-32000;
  411.      write_file(h,ptr(seg(p^),ofs(p^)+32000),k);
  412.      l:=k+32000;
  413.     end;
  414.   end;
  415. end;}
  416.  
  417. begin
  418.  the_eof:=0;
  419. end.
  420.