home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_PAS / TVTOYS.ZIP / TOYUTILS.PAS < prev    next >
Pascal/Delphi Source File  |  1994-01-03  |  5KB  |  179 lines

  1. (***************************************************************************
  2.   Utils unit
  3.   Odd stuff
  4.   PJB November 3, 1993, CompuServe mail to INTERNET:d91-pbr@nada.kth.se
  5.   Copyright 1993, All Rights Reserved
  6.   Free source, use at your own risk.
  7.   If modified, please state so if you pass this around.
  8.  
  9.   These are non-TV specific helpers with no error checking
  10.  
  11. ***************************************************************************)
  12. unit toyUtils;
  13.  
  14. interface
  15.  
  16.   uses
  17.     Dos;
  18.  
  19.   function AddBackslash(const s:String):String;
  20.   function DefaultExtension(const FileName, Ext:String; Force:Boolean):String;
  21.  
  22.   procedure FlushSmartDrive;
  23.  
  24.   function HexChar(i:Integer):Char;
  25.   function HexStr(w:Word):String;
  26.   function HexStrValue(const s:String):Word;
  27.   function HexValue(c:Char):Byte;
  28.  
  29.   function Min(a,b:Integer):Integer;
  30.   function Max(a,b:Integer):Integer;
  31.   function MemComp(const m1, m2; Len:Integer):Boolean;
  32.   function ToStr(l:Longint):String;
  33.  
  34.   const
  35.     HexChars : array [0..15] of char = '0123456789ABCDEF';
  36.  
  37.  
  38. (***************************************************************************
  39. ***************************************************************************)
  40. implementation
  41.  
  42.  
  43.   (*******************************************************************
  44.     Make sure we can append a file name
  45.   *******************************************************************)
  46.   function AddBackslash(const s:String):String;
  47.   begin
  48.     if (s<>'') and not (s[Length(s)] in ['\',':']) then
  49.       AddBackslash:=s+'\'
  50.     else
  51.       AddBackslash:=s;
  52.   end;
  53.  
  54.  
  55.   (*******************************************************************
  56.     Add extension if necessary
  57.   *******************************************************************)
  58.   function DefaultExtension;
  59.     var
  60.       P: PathStr;
  61.       D: DirStr;
  62.       N: NameStr;
  63.       E: ExtStr;
  64.   begin
  65.     FSplit(FileName, D, N, E);
  66.     if (E = '') or Force then
  67.       E:=Ext;
  68.     DefaultExtension:=D+N+E;
  69.   end;
  70.  
  71.  
  72.   (*******************************************************************
  73.     Flush the SmartDrive disk cache, same as smartdrv /c
  74.   *******************************************************************)
  75.   procedure FlushSmartDrive; assembler;
  76.   asm
  77.       mov  ax,4A10h
  78.       mov  bx,1
  79.       int  2Fh
  80.   end;
  81.  
  82.  
  83.   (*******************************************************************
  84.     Convert 0-15 to hex (0-F)
  85.   *******************************************************************)
  86.   function HexChar;
  87.   begin
  88.     HexChar:=HexChars[i and 15];
  89.   end;
  90.  
  91.  
  92.   (*******************************************************************
  93.     Convert word to four hex chars, zero extended
  94.   *******************************************************************)
  95.   function HexStr;
  96.   begin
  97.     HexStr[0]:=Chr(4);
  98.     HexStr[1]:=HexChars[Hi(w) shr 4];
  99.     HexStr[2]:=HexChars[Hi(w) and $F];
  100.     HexStr[3]:=HexChars[Lo(w) shr 4];
  101.     HexStr[4]:=HexChars[Lo(w) and $F];
  102.   end;
  103.  
  104.  
  105.   (*******************************************************************
  106.     Convert hex string to word
  107.   *******************************************************************)
  108.   function HexStrValue;
  109.     var
  110.       sum, i : Word;
  111.   begin
  112.     sum:=0;
  113.     for i:=1 to Length(s) do
  114.       sum:=16*sum+HexValue(s[i]);
  115.     HexStrValue:=sum;
  116.   end;
  117.  
  118.  
  119.   (*******************************************************************
  120.     Convert hex char (0-F) to byte (0-15)
  121.   *******************************************************************)
  122.   function HexValue;
  123.   begin
  124.     c:=Upcase(c);
  125.     if c>'9' then
  126.       Dec(Byte(c), 7);
  127.     HexValue:=Ord(c)-Ord('0');
  128.   end;
  129.  
  130.  
  131.   (*******************************************************************
  132.     Min and Max
  133.   *******************************************************************)
  134.   function Min;
  135.   begin
  136.     if a<b then Min:=a else Min:=b;
  137.   end;
  138.  
  139.   function Max;
  140.   begin
  141.     if a>b then Max:=a else Max:=b;
  142.   end;
  143.  
  144.  
  145.   (*******************************************************************
  146.     Compare two blocks of memory, returns True if equal
  147.   *******************************************************************)
  148.   function MemComp; assembler;
  149.   asm
  150.       push ds
  151.       les  di,m1
  152.       lds  si,m2
  153.       mov  cx,Len
  154.       cld
  155.       rep  cmpsb
  156.       mov  al,1
  157.       je   @Fin
  158.       dec  al
  159.     @Fin:
  160.       pop  ds
  161.   end;
  162.  
  163.  
  164.   (*******************************************************************
  165.     Function version of Str(), incurs speed overhead
  166.   *******************************************************************)
  167.   function ToStr;
  168.     var
  169.       s : String[15];
  170.   begin
  171.     Str(l,s);
  172.     ToStr:=s;
  173.   end;
  174.  
  175.  
  176. end.
  177.  
  178.  
  179.