home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TP5WIO.ZIP / TP5MISC.PAS next >
Encoding:
Pascal/Delphi Source File  |  1989-03-23  |  11.4 KB  |  386 lines

  1. { TP5MISC.PAS creates a unit which performs misc functions on
  2.   strings.  These have been extracted from tp5wio and the various
  3.   application programs to enable us to manage the source code more
  4.   effectively.  Added File management functions.
  5.                       Revision History
  6.   ------------------------------------------------------------------
  7.   Rel 1.00 Collected procedures and functions from elsewhere     gbr
  8.   Rel 1.10 24 Mar 89 Added File management functions             gbr
  9. }
  10. unit tp5misc;
  11.  
  12. { -------------- }
  13. interface
  14. type
  15.    st2     = string[2];
  16.    st4     = string[4];
  17.    st5     = string[5];
  18.  
  19. function wdtostr(n:word):st2;
  20.          { converts word to packed two char string }
  21. function strtowd(s:st2):word;
  22.          { converts packed two char string to word }
  23. function bttostr(n:byte):st2;
  24.          { converts byte to packed char string }
  25. function strtobt(s:st2):byte;
  26.          { converts packed char string to byte }
  27. function dbasetodate(s:string):longint;
  28.          { convert the dbase sdf date dump (YYYYMMDD) to a longint with
  29.            the same format }
  30. function datetodbase(var dbdate:longint):string;
  31.          { convert the hs date record to dbase sdf date dump (YYYYMMDD) }
  32. function strtointeger(st:st5):integer;
  33.          { Converts a string to integer value, returns -1 on error }
  34. function strtoword(st:st5):word;
  35.          { Converts a string to word value, returns 0 on error }
  36. function strtobyte(st:st5):byte;
  37.          { Converts a string to byte value, returns 0 on error }
  38. FUNCTION PAD (st : string ; ch : char ; i : integer) : string ;
  39.          { Pad string with ch to length of i. }
  40. FUNCTION UPPER (st :string):string;
  41.          { returns upper case of st }
  42. FUNCTION STRIPCH (instr:string ; inchar:char) : string ;
  43.          {Strips leading instances of the character from the string}
  44. FUNCTION TRIM (st:string;len:integer):string;
  45.          { Chops spaces from string or truncates at l length }
  46. FUNCTION CHOPCH (instr:string ; inchar:char) : string ;
  47.          {Chops trailing instances of the character from the string}
  48. FUNCTION INTTOSTR(n:integer):st2;
  49.          { converts integer to packed two char string }
  50. FUNCTION STRTOINT(s:st2):integer;
  51.          { converts packed two char string to integer }
  52. FUNCTION LINTTOST4(n:longint):st4;
  53.          { converts long integer to packed 4 character string }
  54. FUNCTION ST4TOLINT(s:st4):longint;
  55.          { converts packed four character string to longint }
  56. { --- File tools --- }
  57. FUNCTION EXIST(FN : String) : boolean;
  58.          { Returns true if file named by FN exists }
  59. FUNCTION REMOVE(FN : string):boolean;
  60.          { Erases the file named by FN, returns TRUE if erased }
  61.  
  62. { -------------- }
  63. implementation
  64.  
  65. type
  66.    { the following variant record is used to map a longint to two integers }
  67.    intlong = record
  68.       case integer of
  69.          0 :(lint:longint);
  70.          1 :(lowint,highint:integer);
  71.       end;
  72.  
  73. function wdtostr(n:word):st2;
  74. { converts word to packed two char string }
  75. begin
  76.    wdtostr := chr(hi(n)) + chr(lo(n));
  77. end;    { function wdtostr }
  78.  
  79. { -------------------------------------------------------------------------- }
  80.  
  81. function strtowd(s:st2):word;
  82. { converts packed two char string to word }
  83. begin
  84.    strtowd := swap(ord(s[1])) + ord(s[2]);
  85. end;    { function strtowd }
  86.  
  87. { -------------------------------------------------------------------------- }
  88.  
  89. function bttostr(n:byte):st2;
  90. { converts byte to packed char string }
  91. begin
  92.    bttostr := chr(n);
  93. end;    { function bttostr }
  94.  
  95. { -------------------------------------------------------------------------- }
  96.  
  97. function strtobt(s:st2):byte;
  98. { converts packed char string to byte }
  99. begin
  100.    strtobt := ord(s[1]);
  101. end;    { function bttostr }
  102.  
  103. { -------------------------------------------------------------------------- }
  104.  
  105. function dbasetodate(s:string):longint;
  106. { convert the dbase sdf date dump (YYYYMMDD) to a longint with the same
  107.   format }
  108.     var
  109.       yr,mo,dy,code     :integer ;
  110.       result   :longint;
  111.       i        :byte;
  112.  
  113.     begin
  114.       for i := 1 to 8 do  { fill to 2 digits of year }
  115.          begin
  116.          if length(s) < i then s := concat(s,'0');
  117.          if s[i] = ' ' then s[i] := '0'; { fill any spaces with 0 }
  118.       end;
  119.       val (copy(s,5,2),mo,code) ;
  120.       if code <> 0 then
  121.          begin
  122.          write ('** MONTH CONVERSION ERROR ',code) ;
  123.          halt
  124.       end ;
  125.       val (copy(s,7,2),dy,code) ;
  126.       if code <> 0 then
  127.          begin
  128.          write ('** DAY CONVERSION ERROR ',code) ;
  129.          halt
  130.       end ;
  131.       val (copy(s,1,4),yr,code) ;
  132.       if code <> 0 then
  133.          begin
  134.          write ('** YEAR CONVERSION ERROR ',code) ;
  135.          halt
  136.       end ;
  137.       if ((yr = 0) and (mo = 0) and (dy = 0)) then { default to nodate }
  138.          dbasetodate := 0
  139.       else
  140.          begin
  141.          result := yr;
  142.          result := (result * 100) + mo;
  143.          result := (result * 100) + dy;
  144.          dbasetodate := result;
  145.       end;
  146. end;  {function dbasetodate}
  147.  
  148. { -------------------------------------------------------------------------- }
  149.  
  150. function datetodbase(var dbdate:longint):string;
  151. { convert the hs date record to dbase sdf date dump (YYYYMMDD) }
  152. var
  153.    yr,mo,dy,i  :integer;
  154.    result      :longint;
  155.    stmo,stdy   :string[2];
  156.    styr        :string[4];
  157. begin
  158.    if dbdate = 0 then datetodbase := '        '
  159.    else
  160.       begin
  161.       dy := (dbdate mod 100);
  162.       result := (dbdate - dy); { subtract the number of days }
  163.       result := result div 100;  { move to right }
  164.       mo := (result mod 100);  { get the month }
  165.       yr := (result div 100); { get year }
  166.       str(yr:1,styr);
  167.       str(mo:1,stmo);
  168.       if length(stmo) = 1 then stmo := concat('0',stmo);
  169.       str(dy:1,stdy);
  170.       if length(stdy) = 1 then stdy := concat('0',stdy);
  171.       datetodbase  := concat(styr,stmo,stdy);
  172.    end;
  173. end;  {function datetodbase}
  174.  
  175. { -------------------------------------------------------------------------- }
  176.  
  177. function strtointeger(st:st5):integer;
  178. { Converts a string to integer value, returns -1 on error }
  179. var
  180.    i,result :integer;
  181.    s1    :string[5];
  182. begin
  183.    s1 := '';
  184.    for i := 1 to length(st) do
  185.       if st[i] <> ' ' then
  186.          s1 := concat(s1,st[i]);
  187.    val(s1,i,result);
  188.    if result = 0 then
  189.       strtointeger := i
  190.    else
  191.       strtointeger := -1;
  192. end;   {function strtointeger}
  193.  
  194. { -------------------------------------------------------------------------- }
  195.  
  196. function strtoword(st:st5):word;
  197. { Converts a string to word value, returns 0 on error }
  198. var
  199.    i,result :integer;
  200.    wd       :word;
  201.    s1    :string[5];
  202. begin
  203.    s1 := '';
  204.    for i := 1 to length(st) do
  205.       if st[i] <> ' ' then
  206.          s1 := concat(s1,st[i]);
  207.    val(s1,wd,result);
  208.    if result = 0 then
  209.       strtoword := wd
  210.    else
  211.       strtoword := 0;
  212. end;   {function strtoword}
  213.  
  214. { -------------------------------------------------------------------------- }
  215.  
  216. function strtobyte(st:st5):byte;
  217. { Converts a string to byte value, returns 0 on error }
  218. var
  219.    i,result :integer;
  220.    bt       :byte;
  221.    s1    :string[5];
  222. begin
  223.    s1 := '';
  224.    for i := 1 to length(st) do
  225.       if st[i] <> ' ' then
  226.          s1 := concat(s1,st[i]);
  227.    val(s1,bt,result);
  228.    if result = 0 then
  229.       strtobyte := bt
  230.    else
  231.       strtobyte := 0;
  232. end;   {function strtobyte}
  233.  
  234. { -------------------------------------------------------------------------- }
  235.  
  236. FUNCTION UPPER(st :string):string;
  237. { make string upper case }
  238. var i:integer;
  239. begin
  240.    if (length(st) > 0) then
  241.       for i := 1 to length(st) do st[i] := upcase(st[i]);
  242.    upper := st;
  243. end;  {function upper}
  244.  
  245. { -------------------------------------------------------------------------- }
  246.  
  247. function pad(st : string ; ch : char ; i : integer) : string;
  248. { Pad string with ch to length of i }
  249. var
  250.   l : integer ;
  251. begin
  252.   l := length(st);
  253.   if l > i then st := copy(st,1,i); { if too long then shorten it }
  254.   if l < i then
  255.     begin
  256.       fillchar (st[l+1],i-l,ch);
  257.       st[0] := chr(i)
  258.     end ;
  259.   pad := st
  260. end;
  261.  
  262. { -------------------------------------------------------------------------- }
  263.  
  264. function stripch(instr:string ; inchar:char) : string;
  265. {Strips leading instances of the character from the string}
  266. begin
  267.    while not (length(instr) = 0) and (instr[1] = inchar) do
  268.       delete (instr, 1, 1);
  269.    stripch := instr
  270. end ;
  271.  
  272. { -------------------------------------------------------------------------- }
  273.  
  274. function chopch(instr:string ; inchar:char) : string;
  275. {Chops trailing instances of the character from the string}
  276. begin
  277.    while not (length(instr) = 0) and (instr[length(instr)] = inchar) do
  278.       delete (instr, length(instr), 1);
  279.    chopch := instr
  280. end ;
  281.  
  282. { -------------------------------------------------------------------------- }
  283.  
  284. function inttostr(n:integer):st2;
  285. { converts integer to packed two char string }
  286. begin
  287.    n := n + (-32768);
  288.    inttostr := chr(hi(n)) + chr(lo(n));
  289. end;    { function inttostr }
  290.  
  291. { -------------------------------------------------------------------------- }
  292.  
  293. function strtoint(s:st2):integer;
  294. { converts packed two char string to integer }
  295. begin
  296.    strtoint := swap(ord(s[1])) + ord(s[2]) + (-32768);
  297. end;    { function strtoint }
  298.  
  299. { -------------------------------------------------------------------------- }
  300.  
  301. function linttost4(n:longint):st4;
  302. { converts a long integer to a 4 character string for indexes }
  303. var intrec :intlong;
  304.     s1,s2  :string[2];
  305. begin
  306.    intrec.lint := n;
  307.    s1 := chr(hi(intrec.lowint)) + chr(lo(intrec.lowint));
  308.    s2 := chr(hi(intrec.highint)) + chr(lo(intrec.highint));
  309.    linttost4 := concat(s2,s1);
  310. end;  {function linttost4}
  311.  
  312. { -------------------------------------------------------------------------- }
  313.  
  314. function st4tolint(s:st4):longint;
  315. { converts a packed 4 character string back to a longint }
  316. var intrec :intlong;
  317.     st     :string[2];
  318. begin
  319.    st := copy(s,3,2);
  320.    intrec.lowint := swap(ord(st[1])) + ord(st[2]);
  321.    st := copy(s,1,2);
  322.    intrec.highint := swap(ord(st[1])) + ord(st[2]);
  323.    st4tolint := intrec.lint;
  324. end;  {function st4tolint}
  325.  
  326. { -------------------------------------------------------------------------- }
  327.  
  328. function trim(st:string;len:integer):string;
  329. { trims right blanks from string and returns a string of len or less }
  330. var
  331.    i   :integer;
  332.  
  333. begin
  334.    if length(st) > len then trim := copy(st,1,len)
  335.    else
  336.       begin
  337.       i := length(st);
  338.       while (i >= 1) and (st[i] = ' ') do i := i - 1;
  339.       if i = 0 then trim := ''
  340.          else trim := copy(st,1,i);
  341.    end;
  342. end;  { function trim }
  343.  
  344. { ------------------------------------------------------------ }
  345.  
  346. function Exist(FN : String) : boolean;
  347. { Returns true if file named by FN exists }
  348. var
  349.    F : file;
  350.    found : boolean;
  351. begin
  352.    Assign(f, FN);
  353.    {$I-}
  354.    Reset(f);
  355.    Found := (IOResult = 0);
  356.    if Found then
  357.       Close(f);
  358.    {$I+}
  359.    Exist := Found;
  360. end; { Exist }
  361.  
  362. { ------------------------------------------------------------ }
  363.  
  364. function Remove(FN : string):boolean;
  365. { Erases the file named by FN, returns TRUE if erased }
  366. var
  367.    F : File;
  368. begin
  369.    remove := false;   { default to not erased }
  370.    Assign(F, FN);
  371.    {$I-}
  372.    Reset(F);
  373.    if IOResult = 0 then
  374.    begin
  375.       Close(F);
  376.       Erase(F);
  377.       remove := true; { flag as erased }
  378.    end;
  379.    {$I+}
  380. end; { Remove }
  381.  
  382. { ---- end of implementation ---- }
  383.  
  384. begin     { --- initialization --- }
  385. end.  { tp5misc.pas }
  386.