home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / utility / unittool / turbo5 / intrfac5 / util.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1989-02-14  |  5.9 KB  |  262 lines

  1. unit util;
  2.  
  3. interface
  4.   uses dos;
  5.  
  6.   function add_offset(p:pointer; add:word):pointer;
  7.  
  8.   function asciiz2s(var asciiz):string;
  9.  
  10.   function ptr_diff(p1,p2:pointer):longint;
  11.  
  12.   function minw(i,j:word):word;
  13.  
  14.   function maxw(i,j:word):word;
  15.  
  16.   function minl(i,j:longint):longint;
  17.  
  18.   function maxl(i,j:longint):longint;
  19.  
  20.   function word_at(var b:byte):word;
  21.  
  22.   function read_file(filename: string;var buffer:pointer):word;
  23.   { Attempts to read a file into buffer; returns nil if there was a problem }
  24.  
  25.   function roundup(n,r:word):word;
  26.  
  27.   procedure get_load_path(var s:string);
  28.   { Returns the path to the currently running program;  needs DOS 3+ }
  29.  
  30.   function get_unique_filename(var path:string; attr:word):word;
  31.   { Creates new file in given directory, appends name to path, returns error }
  32.  
  33.   function is_a_file(var f):boolean;
  34.   { Determines if the file in f is really a file, or is a device.
  35.     f may be either a TP file type or a DOS file handle
  36.     Assumes f is open
  37.   }
  38.   function freeheap:integer;
  39.   { Frees memory from the heap pointer up to the top of the free list
  40.     for use by other programs.  Will destroy the free list!
  41.     Returns 0 if successful, dos error code if not.  Should always
  42.     be successful?
  43.   }
  44.   function restoreheap:integer;
  45.   { Restores memory freed by freeheap.
  46.     Does not restore the free list;  will leave garbage in it.
  47.     Returns 0 if successful, dos error code if not.  Will fail if memory
  48.     is no longer free, e.g. a TSR was run in it.
  49.   }
  50.  
  51. implementation
  52.  
  53. var
  54.   regs : registers;
  55.  
  56. function add_offset(p:pointer; add:word):pointer;
  57. var
  58.   s,o:word;
  59.   new:pointer;
  60. begin
  61.   { Normalize p }
  62.   s := seg(p^);
  63.   o := ofs(p^);
  64.   if o > $f then
  65.   begin
  66.     s := s + o shr 4;
  67.     o := o and $f;
  68.   end;
  69.   { Add new offset }
  70.   o := o + add;
  71.   add_offset := ptr(s,o);
  72. end;
  73.  
  74. function asciiz2s(var asciiz):string;
  75. var a:array[0..255] of char absolute asciiz;
  76.     i:integer;
  77.     s:string;
  78. begin
  79.  i:=0;
  80.  while a[i]<>chr(0) do inc(i);
  81.  {$r-}
  82.  s[0]:=chr(i);
  83.  move(a,s[1],i);
  84.  {$r+}
  85.  asciiz2s:=s
  86. end;
  87.  
  88. function ptr_diff(p1,p2:pointer):longint;
  89. begin
  90.   ptr_diff := 16*(longint(seg(p1^))-longint(seg(p2^))) + ofs(p1^) - ofs(p2^);
  91. end;
  92.  
  93. function minw(i,j:word):word;
  94. begin
  95.   if i<j then
  96.     minw := i
  97.   else
  98.     minw := j;
  99. end;
  100.  
  101. function maxw(i,j:word):word;
  102. begin
  103.   if i<j then
  104.     maxw := j
  105.   else
  106.     maxw := i;
  107. end;
  108.  
  109. function minl(i,j:longint):longint;
  110. begin
  111.   if i<j then
  112.     minl := i
  113.   else
  114.     minl := j;
  115. end;
  116.  
  117. function maxl(i,j:longint):longint;
  118. begin
  119.   if i<j then
  120.     maxl := j
  121.   else
  122.     maxl := i;
  123. end;
  124.  
  125. function word_at(var b:byte):word;
  126. var
  127.   p:^byte;
  128. begin
  129.   p := add_offset(@b,1);
  130.   word_at := word(b) + word(p^) shl 8;
  131. end;
  132.  
  133. function read_file(filename: string;var buffer:pointer):word;
  134. { Attempts to read a file into buffer; returns nil if there was a problem }
  135. var
  136.   f:file;
  137.   size : word;
  138. begin
  139.   assign(f,filename);
  140.   read_file := 0;
  141.   buffer := nil;
  142.   {$i-} reset(f,1); {$i+}
  143.   if ioresult <> 0 then
  144.     exit;
  145.   if filesize(f) > 65521 then
  146.   begin
  147.     writeln('File ',filename,' too large.  File not read.');
  148.     exit;
  149.   end;
  150.   if maxavail < filesize(f) then
  151.   begin
  152.     writeln('Out of memory.  File ',filename,' not read.');
  153.     exit;
  154.   end;
  155.   getmem(buffer,filesize(f));
  156.   blockread(f,buffer^,filesize(f),size);
  157.   close(f);
  158.   read_file := size;
  159. end;
  160.  
  161. function roundup(n,r:word):word;
  162. begin
  163.   roundup := r*((n+r-1) div r);
  164. end;
  165.  
  166. procedure get_load_path(var s:string);
  167. { Returns the path to the currently running program;  needs DOS 3+ }
  168. var
  169.   p,q:pointer;
  170.   l:longint absolute p;
  171.   len:byte;
  172. begin
  173.   p := ptr(prefixseg,$2c);    { Point to environment segment number }
  174.   p := ptr(word(p^),0);       { Point to start of environment segment }
  175.   while word(p^) <> 0 do      { Find terminating double 0 }
  176.     inc(l);
  177.   inc(l,4);                   { Skip double zero and count word }
  178.  
  179.   q := p;                     { Save start of string }
  180.   len := 0;
  181.   while byte(p^) <> 0 do
  182.   begin
  183.     inc(len);
  184.     inc(l);
  185.   end;
  186.   s[0] := char(len);
  187.   move(q^,s[1],len);
  188. end;
  189.  
  190. function get_unique_filename(var path:string; attr:word):word;
  191. { Appends new name to path;  Returns error value or zero if ok }
  192. begin
  193.   path[length(path)+1] := char(0);
  194.   regs.ah := $5A;
  195.   regs.ds := seg(path[1]);
  196.   regs.dx := ofs(path[1]);
  197.   regs.cx := attr;
  198.   msdos(regs);
  199.   if ((regs.flags and fcarry) <> 0) then
  200.     get_unique_filename := regs.ax
  201.   else
  202.   begin
  203.     get_unique_filename := 0;
  204.     path := asciiz2s(path[1]);
  205.   end;
  206. end;
  207.  
  208. function is_a_file(var f):boolean;
  209. { Determines if the file in f is really a file, or is a device
  210.   Assumes f is open
  211. }
  212. var
  213.   handle : word absolute f;
  214. begin
  215.   regs.ah := $44;  { IOCTL }
  216.   regs.al :=   0;  { Get device information }
  217.   regs.bx := handle;
  218.   msdos(regs);
  219.   if (regs.flags and fcarry) <> 0 then
  220.     is_a_file := false
  221.   else
  222.     is_a_file := (regs.dx and (1 shl 7)) = 0;
  223. end;
  224.  
  225. function freeheap:integer;
  226. { Frees memory from the heap pointer up to the top of the free list
  227.   for use by other programs.  Will destroy the free list!
  228.   Returns 0 if successful, dos error code if not.  Should always
  229.   be successful?
  230. }
  231. begin
  232.   regs.ah := $4a;   { Setblock }
  233.   regs.bx := seg(heapptr^) + ofs(heapptr^) div 16 + 1 - prefixseg;
  234.   regs.es := prefixseg;
  235.   msdos(regs);
  236.   if (regs.flags and fcarry) = 0 then
  237.     freeheap := 0
  238.   else
  239.     freeheap := regs.ax;
  240. end;
  241.  
  242. function restoreheap:integer;
  243. { Restores memory freed by freeheap.
  244.   Does not restore the free list;  will leave garbage in it.
  245.   Returns 0 if successful, dos error code if not.  Will fail if memory
  246.   is no longer free, e.g. a TSR was run in it.
  247. }
  248. begin
  249.   regs.ah := $4a;   { Setblock }
  250.   regs.bx := seg(freeptr^) + $1000 - prefixseg;
  251.   regs.es := prefixseg;
  252.   msdos(regs);
  253.   if (regs.flags and fcarry) = 0 then
  254.     restoreheap := 0
  255.   else
  256.     restoreheap := regs.ax;
  257. end;
  258.  
  259. end.
  260.  
  261.  
  262.