[comp.lang.pascal] Cool misc. unit

wct@po.CWRU.Edu (William C. Thompson) (04/17/91)

Here's a unit that does cool miscellaneous stuff

unit misc;
interface
uses crt;

procedure beep(hz,dur: integer);
{ beeps "hz" for "dur" }

function inttostr(i:longint):string;
{ converts an integer to a string }

function realtostr(r:real;width,prec:byte):string;
{ converts an integer to a string }

function filexists(FileName: string):boolean;
{ returns true if the file exists, false if it does not }

function yesorno:char;
{ waits for the user to press 'y','Y','n','N' }

function getoneof(s:string):char;
{ waits for the user to input a character contained in s }

function datetoday(m,d,y: word):byte;
{ returns day of week for the appropriate month, day, and year
  0 = Sunday
  1 = Monday
  ...
  6 = Saturday }

implementation

procedure beep(hz,dur: integer);
begin
  sound(hz);
  delay(dur);
  nosound
end;

function inttostr(i:longint):string;
var s: string;
begin
  str(i,s);
  inttostr:=s
end;

function realtostr(r:real;width,prec:byte):string;
var s: string;
begin
  str(r:width:prec,s);
  realtostr:=s
end;

function filexists(FileName: string):boolean;
{ Returns True if file exists; otherwise,
    it returns False. Closes the file if
    it exists. }
var
  f: file;
begin
  {$I-}
  assign(f, FileName);
  reset(f);
  close(f);
  {$I+}
  filexists := (IOResult = 0) and (FileName <> '');
end;  { FileExists }

function yesorno:char;
{ waits for the user to press 'y','Y','n','N' }
var
  ch: char;
begin
  repeat
    ch:=upcase(readkey)
  until ch in ['Y','N'];
  yesorno:=ch
end;

function getoneof(s:string):char;
{ waits for the user to input a character contained in s }
var
  ch: char;
begin
  repeat
    ch:=readkey
  until pos(ch,s)>0;
  getoneof:=ch
end;

function datetoday(m,d,y: word):byte;
{ returns day of week for the appropriate month, day, and year
  0 = Sunday
  1 = Monday
  ...
  6 = Saturday }
var
  z: byte;
begin
  z:=y-ord(m<3);
  datetoday:=(23*m div 9+d+4+y+(z div 4)-(z div 100)+
     (z div 400)-2*ord(m>=3)) mod 7
end;
-- 
Ticking away, the moments that make up a dull day.   | William C. Thompson
You fritter and waste the hours in an offhand way.   | Michelson 620D (wct)
Kicking around on a piece of ground in your hometown,| a.k.a. Master of Time,
waiting for someone or something to show you the way.| Space, and Dimension