home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / wct / misc.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-02-25  |  4.4 KB  |  205 lines

  1. unit misc;
  2.  
  3. { Written by William C. Thompson }
  4.  
  5. { This unit does a few miscellaneous things }
  6.  
  7. interface
  8.  
  9. uses
  10.   dos,crt,xcrt,keydef;
  11.  
  12. var
  13.   startingtime, endingtime: real;
  14.  
  15. procedure getcommandline(var s:string);
  16. function searchpath(fn:string):dirstr;
  17. function inttostr(i:longint):string;
  18. function realtostr(r:real; width,prec:byte):string;
  19. function fileexists(fn:string; attr:word) : boolean;
  20. function isdigit(c:char):boolean;
  21. function datetoday(m,d,y:word):byte;
  22. function printerstatus:byte;
  23. function printerokay:boolean;
  24. procedure starttimer;
  25. function elapsedtime:real;
  26. procedure unpackseconds(t:real; var hour,min,sec,sec100:word);
  27. procedure debug(s:string);
  28.  
  29. implementation
  30.  
  31. procedure getcommandline(var s:string);
  32. { Retrieves entire command line into string s }
  33. var
  34.   p:^string;
  35. begin
  36.   p:=ptr(prefixseg,$80);
  37.   s:=p^
  38. end;
  39.  
  40. function searchpath(fn:string):dirstr;
  41. { Searches path for file Fn and return path to it if found, and '' o/w }
  42. var
  43.   s: pathstr;
  44.   e: extstr;
  45.   d: dirstr;
  46.   n: namestr;
  47. begin
  48.   s:=fsearch(fn,getenv('PATH'));
  49.   if s='' then searchpath:=''
  50.   else begin
  51.     fsplit(fexpand(s),d,n,e);
  52.     searchpath:=d
  53.     end
  54. end;
  55.  
  56. function inttostr(i:longint):string;
  57. var s: string;
  58. begin
  59.   str(i,s);
  60.   inttostr:=s
  61. end;
  62.  
  63. function realtostr(r:real;width,prec:byte):string;
  64. var s: string;
  65. begin
  66.   str(r:width:prec,s);
  67.   realtostr:=s
  68. end;
  69.  
  70. function fileexists(fn:string; attr:word):boolean;
  71. { attr=archive ($20) / directory ($10) }
  72. var
  73.   sr : searchrec;
  74. begin
  75.   findfirst(fn,attr,sr);
  76.   fileexists := doserror = 0;
  77. end;
  78.  
  79. function fptr(var f:text):char;
  80. { Returns next char in file, with out a read - kennedym@topaz.ucq.edu.au }
  81. var
  82.   fb:textrec absolute f;
  83. begin
  84.   with fb do fptr:=buffer[bufpos]
  85. end;
  86.  
  87. function isdigit(c:char):boolean;
  88. { returns TRUE if c is a digit }
  89. begin
  90.   isdigit:=c in ['0'..'9']
  91. end;
  92.  
  93. function datetoday(m,d,y: word):byte;
  94. { returns day of week for the appropriate month, day, and year
  95.   0 = Sunday
  96.   1 = Monday
  97.   ...
  98.   6 = Saturday }
  99. var
  100.   z: byte;
  101. begin
  102.   z:=y-ord(m<3);
  103.   datetoday:=(23*m div 9+d+4+y+(z div 4)-(z div 100)+
  104.      (z div 400)-2*ord(m>=3)) mod 7
  105. end;
  106.  
  107. function printerstatus:byte;
  108. { Returns the actual status of the printer
  109.   Definition of status byte bits: (1 & 2 are not used)
  110.   Bit -- 7 ---  ---- 6 ----  -- 5 ---  -- 4 ---  -- 3 --  --- 0 ---
  111.       Not Busy  Acknowledge  No Paper  Selected  I/O Err. Timed-out }
  112. var
  113.   regs : registers;
  114. begin
  115.   with regs do begin
  116.     ah:=2;
  117.     dx:=0;
  118.     intr($17,regs);
  119.     printerstatus:=ah;
  120.     end;
  121. end;
  122.  
  123. function printerokay:boolean;
  124. { Returns TRUE if the printer is selected, then printer has paper and no
  125.   I/O or time out error has occurred. }
  126. var
  127.   n: byte;
  128. begin
  129.   n:=printerstatus;
  130.   if ((n and $10)<>0) and ((n and $29)=0) then printerokay:=true
  131.      { selected set & no paper, i/o error, timed-out not set }
  132.   else printerokay := false;
  133. end;
  134.  
  135. procedure starttimer;
  136. { This procedure sets the starting time (in seconds) }
  137. var
  138.   h,m,s,s100: word;
  139. begin
  140.   gettime(h,m,s,s100);
  141.   startingtime:=h*3600.0+m*60.0+s+s100/100;
  142. end;
  143.  
  144. function elapsedtime:real;
  145. { This function returns the elapsed time since the timer was started.
  146.   It also sets ending time to the current time (in seconds) }
  147. var
  148.   h,m,s,s100: word;
  149. begin
  150.   gettime(h,m,s,s100);
  151.   endingtime:=h*3600.0+m*60.0+s+s100/100;
  152.   if endingtime>startingtime then elapsedtime:=endingtime-startingtime
  153.   else elapsedtime:=86400.0-startingtime+endingtime
  154. end;
  155.  
  156. procedure unpackseconds(t: real; var hour,min,sec,sec100: word);
  157. { This procedure converts a time in seconds to something more
  158.   meaningful. }
  159. begin
  160.   sec100:=trunc(frac(t)*100);
  161.   sec:=trunc(t) mod 60;
  162.   hour:=trunc(t) div 60;
  163.   min:=hour mod 60;
  164.   hour:=hour div 60
  165. end;
  166.  
  167. procedure debug(s:string);
  168. var
  169.   w: block;
  170.   ch: char;
  171.   row: byte;
  172.   i,j: byte;
  173.  
  174.   procedure draw;
  175.   begin
  176.     savewindow(1,row,80,row,w);
  177.     attrblock(1,row,80,row,redbg+white);
  178.     writexy(1,row,s);
  179.   end;
  180.  
  181. begin
  182.   if maxavail<240 then exit;
  183.   i:=length(s);
  184.   if i>80 then i:=80;
  185.   s[0]:=#80;
  186.   for j:=i+1 to 80 do s[j]:=' ';
  187.   row:=1;
  188.   draw;
  189.   repeat
  190.     ch:=getoneof(enter+esc+uparrow+downarrow);
  191.     recallwindow(1,row,w);
  192.     killwindow(w);
  193.     if ch=uparrow then begin
  194.       if row>1 then row:=row-1;
  195.       draw
  196.       end
  197.     else if ch=downarrow then begin
  198.       if row<25 then row:=row+1;
  199.       draw
  200.       end
  201.   until (ch=esc) or (ch=enter)
  202. end;
  203.  
  204. end.
  205.