home *** CD-ROM | disk | FTP | other *** search
/ Phoenix CD 2.0 / Phoenix_CD.cdr / 15a / murutil.zip / SORT80.PAS < prev    next >
Pascal/Delphi Source File  |  1986-12-10  |  6KB  |  260 lines

  1. PROGRAM SORT80;
  2.  
  3. {  This Turbo Pascal program sorts an input file of up to 6000
  4.    80-character records and writes a sorted output file.
  5.  
  6.    Note:  The maximum number of input records is declared in
  7.           MAXNINP.   The value of 6000 is appropriate for  a
  8.           computer with 640 Kilobytes.   For computers  with
  9.           smaller memory, this figure should be reduced.
  10.  
  11.    Program by Harry M. Murphy,  18 August 1986.
  12.    Revised by H.M.M. on 21 Oct 1986 to check for record overflow.
  13.    Revised by H.M.M. on 29 Oct 1986 to simplify the pointers and
  14.    to trim possible trailing blanks.
  15.  
  16.                                 NOTICE
  17.  
  18.        Copyright 1986, Harry M. Murphy.
  19.  
  20.        A general license is hereby  granted  for  non-commercial
  21.        use,  copying and free exchange of this  program  without
  22.        payment of any royalties,  provided that  this  copyright
  23.        notice is not altered nor deleted.   All other rights are
  24.        reserved.  }
  25.  
  26.  
  27.   CONST
  28.         LENSPEC = 65;
  29.         LINELEN = 80;
  30.         MAXNINP = 6000;
  31.  
  32.   TYPE
  33.        FILESPEC = STRING[LENSPEC];
  34.        TEXTLINE = STRING[LINELEN];
  35.        LINEP    = ^TEXTLINE;
  36.  
  37.   VAR
  38.       FREE0  : REAL;
  39.       FREE1  : REAL;
  40.       FREE   : REAL;
  41.       INP    : TEXT[2048];
  42.       INPNAME: FILESPEC;
  43.       NINP   : INTEGER;
  44.       OUT    : TEXT[2048];
  45.       OUTNAME: FILESPEC;
  46.       LINPA  : ARRAY [1..MAXNINP] OF LINEP;
  47.  
  48.  
  49. PROCEDURE GETINPFIL(VAR INPNAME: FILESPEC);
  50.  
  51. {  This file gets an input file, either as the first parameter
  52.    on the command line or by requesting it from the user.
  53.  
  54.    Procedure by Harry M. Murphy,  22 February 1986.  }
  55.  
  56.   VAR
  57.       L: INTEGER;
  58.  
  59.   BEGIN
  60.     IF PARAMCOUNT = 0
  61.       THEN
  62.         BEGIN
  63.           WRITE('Input  file: ');
  64.           READLN(INPNAME)
  65.         END
  66.       ELSE
  67.         INPNAME := PARAMSTR(1);
  68.     FOR L:=1 TO LENGTH(INPNAME) DO INPNAME[L] := UPCASE(INPNAME[L]);
  69.     ASSIGN(INP,INPNAME);
  70.     {$I-} RESET(INP); {$I+}
  71.     IF IORESULT <> 0
  72.       THEN
  73.         BEGIN
  74.           CLOSE(INP);
  75.           WRITELN('ERROR!  Can''t find file ',INPNAME,'!');
  76.           HALT
  77.         END;
  78.   END {Procedure GETINPFIL};
  79.  
  80.  
  81. PROCEDURE GETOUTFIL(VAR OUTNAME: FILESPEC);
  82.  
  83. {  This file gets an output file, either as the second parameter
  84.    on the command line or by requesting it from the user.
  85.  
  86.    Procedure by Harry M. Murphy,  22 February 1986.  }
  87.  
  88.  VAR
  89.      L: INTEGER;
  90.  
  91.   BEGIN
  92.     IF PARAMCOUNT < 2
  93.       THEN
  94.         BEGIN
  95.           WRITE('Output file: ');
  96.           READLN(OUTNAME)
  97.         END
  98.       ELSE
  99.         OUTNAME := PARAMSTR(2);
  100.     FOR L:=1 TO LENGTH(OUTNAME) DO OUTNAME[L] := UPCASE(OUTNAME[L]);
  101.     ASSIGN(OUT,OUTNAME);
  102.     {$I-} REWRITE(OUT); {$I-}
  103.     IF IORESULT <> 0
  104.       THEN
  105.         BEGIN
  106.           CLOSE(OUT);
  107.           WRITELN('ERROR!  Can''t open ',OUTNAME,'!');
  108.           HALT
  109.         END
  110.   END {Procedure GETOUTFIL};
  111.  
  112.  
  113. FUNCTION KBYTFREE: REAL;
  114.  
  115. {  This Turbo Pascal function returns the size of the largest
  116.    consecutive block of free space, in Kilobytes, on the heap
  117.    as a REAL number.
  118.  
  119.    Function by Harry M. Murphy,  18 August 1986.  }
  120.  
  121.   CONST
  122.         CON = 0.016;  {Kilobytes per "paragraph" of 16 bytes.}
  123.  
  124.   VAR
  125.       MAXA : INTEGER;
  126.  
  127.   BEGIN
  128.     MAXA := MAXAVAIL;
  129.     IF MAXA < 0
  130.       THEN
  131.         KBYTFREE := CON*(65536.0+MAXA)
  132.       ELSE
  133.         KBYTFREE := CON*MAXA
  134.   END {Function KBYTFREE};
  135.  
  136.  
  137.   PROCEDURE GETTEXT;
  138.  
  139.   {  This routine reads the input file, updates the pointer array,
  140.      LINPA and stores the input records in LINE^. }
  141.  
  142.   VAR
  143.       L    : 0..LINELEN;
  144.       LINE : LINEP;
  145.  
  146.   BEGIN
  147.     NINP := 0;
  148.     LINE := NIL;
  149.     WHILE (NOT EOF(INP)) AND (KBYTFREE > 10.0) DO
  150.       BEGIN
  151.         NINP:=NINP+1;
  152.         IF NINP <= MAXNINP
  153.           THEN
  154.             BEGIN
  155.               NEW(LINE);
  156.               LINPA[NINP] := LINE
  157.             END;
  158.           READLN(INP,LINE^);
  159.           L := LENGTH(LINE^);
  160.           LINE^[0] := CHR(0);
  161.           WHILE LINE^[L] = ' ' DO
  162.             BEGIN
  163.               L := L-1;
  164.               LINE^[0] := CHR(L)
  165.             END
  166.       END
  167.   END {Procedure GETTEXT};
  168.  
  169.  
  170.   PROCEDURE PUTTEXT;
  171.  
  172.    {  This procedure writes the sorted output file. }
  173.  
  174.   VAR
  175.       I    : INTEGER;
  176.       LINE : LINEP;
  177.  
  178.   BEGIN
  179.     FOR I:=1 TO NINP DO
  180.       BEGIN
  181.          LINE := LINPA[I];
  182.          WRITELN(OUT,LINE^)
  183.       END
  184.   END {Procedure PUTTEXT};
  185.  
  186.  
  187.   PROCEDURE SRTTEXT;
  188.  
  189.   {  This procedure sorts the data, using a Shell pointer sort. }
  190.  
  191.   VAR 
  192.       I   : INTEGER;
  193.       J   : INTEGER;
  194.       M   : INTEGER;
  195.       SRT : BOOLEAN;
  196.       SWAP: LINEP;
  197.  
  198.   BEGIN
  199.     I := 1;
  200.     WHILE I <= NINP DO I := I+I;
  201.     M := I-1;
  202.     WHILE M > 1 DO
  203.       BEGIN
  204.         M := M DIV 2;
  205.         REPEAT
  206.           SRT := TRUE;
  207.           FOR J:=1 TO NINP-M DO
  208.             BEGIN
  209.               I := J+M;
  210.               IF LINPA[J]^ > LINPA[I]^
  211.                 THEN
  212.                   BEGIN
  213.                     SWAP := LINPA[I];
  214.                     LINPA[I] := LINPA[J];
  215.                     LINPA[J] := SWAP;
  216.                     SRT := FALSE
  217.                   END
  218.             END
  219.         UNTIL SRT
  220.       END
  221.   END {Procedure SRTTEXT};
  222.  
  223.  
  224.   BEGIN {Program SORT80}
  225.     LOWVIDEO;
  226.     GETINPFIL(INPNAME);
  227.     GETOUTFIL(OUTNAME);
  228.     WRITELN;
  229.     WRITELN('SORT80 sorting ',INPNAME,' ==> ',OUTNAME,':');
  230.     WRITELN;
  231.     FREE0 := KBYTFREE;
  232.     GETTEXT;
  233.     CLOSE(INP);
  234.     FREE1 := KBYTFREE;
  235.     FREE := FREE0-FREE1;
  236.     WRITELN(NINP:8,' records read from ',INPNAME,'.');
  237.     IF NINP > MAXNINP
  238.       THEN
  239.         BEGIN
  240.           NORMVIDEO;
  241.           WRITELN('This exceeds the maximum of',MAXNINP:6,' records.');
  242.           WRITELN('This run is aborted.');
  243.           LOWVIDEO;
  244.           CLOSE(OUT);
  245.           ERASE(OUT)
  246.         END
  247.       ELSE
  248.         BEGIN   
  249.           WRITELN(FREE:8:3,' Kilobytes used.');
  250.           WRITELN(FREE1:8:3,' Kilobytes free.');
  251.           WRITE(' Sorting the records now.',CHR(13));
  252.           SRTTEXT;
  253.           WRITELN(' Writing',NINP:6,' records to ',OUTNAME,'.');
  254.           PUTTEXT;
  255.           CLOSE(OUT);
  256.           WRITELN;
  257.           WRITELN('SORT80 is done.')
  258.         END
  259. END.
  260.