home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / TURBOPAS / ENVIRON.LBR / STDUTIL.PZS / STDUTIL.PAS
Pascal/Delphi Source File  |  2000-06-30  |  6KB  |  280 lines

  1. { standard utilities for Turbo Pascal, a la Kernighan and Plauger }
  2.  
  3. procedure halt;
  4.  
  5. { halts program, returns to operating system }
  6.  
  7. begin
  8.      bdos(0);
  9. end;
  10.  
  11. procedure error(s:string80);
  12.  
  13. { writes msg., then halts }
  14. { may require macro implementation on some systems }
  15.  
  16. begin
  17.      writeln(s);
  18.      halt;
  19. end;
  20.  
  21. function islower(c:character):boolean;
  22.  
  23. { returns 'true' if c is lower case }
  24.  
  25. begin
  26.      islower := (chartbl[c]='L');
  27. end;
  28.  
  29. function isupper(c:character):boolean;
  30.  
  31. { returns 'true' if c is upper case }
  32.  
  33. begin
  34.      isupper := (chartbl[c]='U');
  35. end;
  36.  
  37. function toupper(c:character):character;
  38.  
  39. { converts a character to upper case }
  40.  
  41. begin
  42.      if islower(c) then toupper := c - CASEDIFF else toupper := c;
  43. end;
  44.  
  45. function uc(c:character):character;
  46.  
  47. { alias for toupper }
  48.  
  49. begin
  50.      uc := toupper(c);
  51. end;
  52.  
  53. function tolower(c:character):character;
  54.  
  55. { makes a character lower-case }
  56.  
  57. begin
  58.      if isupper(c) then tolower := c + CASEDIFF else tolower := c;
  59. end;
  60.  
  61. function isletter(c:character):boolean;
  62.  
  63. { returns 'true' if c is a letter }
  64.  
  65. begin
  66.      isletter := (chartbl[c] = 'L') or (chartbl[c] = 'U');
  67. end;
  68.  
  69. function isdigit(c:character):boolean;
  70.  
  71. { returns 'true' if c is a digit }
  72.  
  73. begin
  74.      isdigit := (chartbl[c] = 'D');
  75. end;
  76.  
  77. function isalphanum(c:character):boolean;
  78.  
  79. { returns 'true' if character is a number or a digit }
  80.  
  81. begin
  82.      isalphanum := chartbl[c] <> 'X';
  83. end;
  84.  
  85. function max(x,y:integer):integer;
  86.  
  87. { returns maximum of x and y }
  88.  
  89. begin
  90.      if x>y then max := x else max := y;
  91. end;
  92.  
  93. function min(x,y:integer):integer;
  94.  
  95. { returns minimum of x and y }
  96.  
  97. begin
  98.      if x<y then min := x else min := y;
  99. end;
  100.  
  101. function slength(var s:textline):integer;
  102.  
  103. { returns length of a string }
  104.  
  105. var
  106.      i,ls :integer;
  107. begin
  108.      i := 1; ls := 0;
  109.      while (s[i]<>EOS) do begin ls := ls + 1; i:=i+1 end;
  110.      slength := ls;
  111. end;
  112.  
  113. function addstr(c:character; var outset: textline;
  114.         var j:integer; maxset: integer):boolean;
  115.  
  116. { add c to outset[j]; if it fits, increment j. }
  117.  
  118. begin
  119.      if (j>maxset) then
  120.       addstr := false
  121.      else
  122.      begin
  123.       outset[j] := c;
  124.       j := j + 1;
  125.       addstr := true;
  126.      end;
  127. end;
  128.  
  129. function concat(var s1,s2:textline):boolean;
  130.  
  131. { adds s2 to the end of s1, returns true if not overflow }
  132.  
  133. var
  134.      i,j       :integer;
  135.      toomuch   :boolean;
  136. begin
  137.      i := slength(s1)+1; j:=1;
  138.      toomuch := false;
  139.      while (not toomuch) and (s2[j]<>EOS) do
  140.      begin
  141.       toomuch := not addstr(s2[j],s1,i,MAXSTR);
  142.       if not toomuch then j := j + 1;
  143.      end;
  144.      s1[i] := EOS;
  145.      concat := not toomuch;
  146. end;
  147.  
  148. procedure setstring(var st:textline; ss:string80);
  149.  
  150. { initializes string variable st to literal string ss }
  151. { this may require a macro implementation for some compilers }
  152.  
  153. var
  154.      i      :integer;
  155. begin
  156.      i := 1;
  157.      while i <= min(ord(ss[0]),MAXSTR) do
  158.      begin
  159.       st[i] := ord(ss[i]);
  160.       i := i + 1;
  161.      end;
  162.      st[i] := EOS;
  163. end;
  164.  
  165. function makestring(var s:textline):string80;
  166.  
  167. { converts our string format to native string format }
  168. { needed for implementation of some primitives, should not be called
  169.   by application programs }
  170.  
  171. var
  172.      i : integer;
  173.      ns :string80;
  174. begin
  175.      ns := '';
  176.      i := 1;
  177.      while s[i] <> EOS do begin ns := ns + chr(s[i]); i := i + 1; end;
  178.      makestring := ns;
  179. end;
  180.  
  181. function index(c:character; start:integer; var s:textline):integer;
  182.  
  183. { searches for character c, starting at s[start] }
  184. { returns index at which s[index]=c, or 0 if c is not in s }
  185. { caution: may bomb if start not in 1..length(s) }
  186.  
  187. var
  188.      k        :integer;
  189. begin
  190.      k := start;
  191.      while not (s[k] in [c,EOS]) do
  192.       k := k + 1;
  193.      if s[k] = EOS then
  194.       index := 0
  195.      else
  196.       index := k;
  197. end;
  198.  
  199. function skipsp(var s:textline;var i:integer):character;
  200.  
  201. { skips spaces and tabs, returns 1st non-blank char. and index to it }
  202.  
  203. begin
  204.      while s[i] in [SPACE,TAB] do i:=i+1;
  205.      skipsp := s[i];
  206. end;
  207.  
  208. procedure scopy(var src: textline; i :integer;
  209.         var dest: textline; j :integer);
  210.  
  211. { copy string from src[i] to dest[j] until EOS }
  212.  
  213. begin
  214.      while src[i] <> EOS do
  215.      begin
  216.       dest[j] := src[i];
  217.       i := i + 1; j := j + 1;
  218.      end;
  219.      dest[j] := EOS;
  220. end;
  221.  
  222. function equal(var s1,s2:textline):boolean;
  223.  
  224. { test two strings for equality }
  225.  
  226. var
  227.      i      :integer;
  228. begin
  229.      i := 1;
  230.      while (s1[i] = s2[i]) and (s1[i] <> EOS) do
  231.       i := i + 1;
  232.      equal := s1[i] = s2[i];
  233. end;
  234.  
  235. function ctoi(var s:textline; var i:integer):integer;
  236.  
  237. { converts string at s[i] to integer, increments i to point past string }
  238.  
  239. var
  240.      n,sign    :integer;
  241.      c           :character;
  242. begin
  243.      if skipsp(s,i) = minus then
  244.       sign := - 1
  245.      else
  246.       sign := + 1;
  247.      if s[i] in [PLUS,MINUS] then i := i + 1;
  248.      n := 0;
  249.      while isdigit(s[i]) do
  250.      begin
  251.       n := 10*n + s[i] - ord('0');
  252.       i := i + 1;
  253.      end;
  254.      ctoi := sign*n;
  255. end;
  256.  
  257. {$a-}
  258. function itoc(n:integer; var s:textline; i: integer): integer;
  259.  
  260. { converts integer n to character string s[i], returns end of s }
  261.  
  262. begin
  263.      if (n<0) then
  264.      begin
  265.       s[i] := minus;
  266.       itoc := itoc(-n,s,i+1);
  267.      end
  268.      else
  269.      begin
  270.       if (n >= 10) then
  271.            i := itoc(n div 10,s,i);
  272.       s[i] := n mod 10 + ord('0');
  273.       s[i+1] := EOS;
  274.       itoc := i + 1;
  275.      end;
  276. end;
  277. {$a+}
  278.  
  279.  
  280.