home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / textutil / pgnat101.zip / PAGINATE.PAS < prev    next >
Pascal/Delphi Source File  |  1993-10-22  |  6KB  |  174 lines

  1. PROGRAM PaginateTextFiles;
  2. {------------------------------------------------------------------------------
  3.  
  4.                                 REVISION HISTORY
  5.  
  6. v1.00  : 1993/07/14.  First public release.  DDA
  7. v1.01  : 1993/10/22.  Now removes existing page breaks automatically.  DDA
  8.  
  9. ------------------------------------------------------------------------------}
  10.  
  11. CONST
  12.      ProgData = 'PAGINATE- Free DOS utility: text file paginator.';
  13.      ProgDat2 = 'V1.01: October 22, 1993. (c) 1993 by David Daniel Anderson - Reign Ware.';
  14.      Usage   = 'Usage:   PAGINATE /i<infile> /o<outfile> /l##[:##] (lines per odd/even page)';
  15.      Example = 'Example: PAGINATE /iNOTPAGED.TXT /oPAGED.TXT /l55';
  16. VAR
  17.      IFL, OFL, LPP    : String[80];
  18.      InFile, OutFile  : Text;
  19.      OddL, EvenL      : Word;
  20.  
  21. PROCEDURE InValidParms( ErrCode : Word );
  22. VAR
  23.    Message : String[80];
  24. BEGIN
  25.      WriteLn(Usage);
  26.      WriteLn(Example);
  27.      CASE ErrCode OF
  28.           1 : Message := 'Incorrect number of parameters on command line.';
  29.           2 : Message := 'Invalid switch on command line.';
  30.           3 : Message := 'Cannot open input file '+ IFL + '.';
  31.           4 : Message := 'Cannot open NEW output file '+ OFL + '.';
  32.           5 : Message := 'Non-numeric '+ LPP + ' specified for lines per page.';
  33.      ELSE
  34.           Message := 'Unknown error.';
  35.      END;
  36.      Writeln(Message);
  37.      Halt;
  38. END;
  39.  
  40. PROCEDURE ParseComLine;
  41. CONST
  42.      Bad = '';   {An invalid filename character & invalid integer.}
  43. VAR
  44.      SwStr            : String[1];
  45.      SwChar           : Char;
  46.      ic               : Byte;
  47.      ArgText          : String[80];
  48. BEGIN
  49.      IF ParamCount <> 3 THEN
  50.         InValidParms(1);
  51.  
  52.      IFL := Bad; OFL := Bad; LPP := Bad;
  53.  
  54.      FOR ic := 1 to 3 DO
  55.      BEGIN
  56.          SwStr  := Copy(ParamStr(ic),1,1);
  57.          SwChar := UpCase(SwStr[1]);
  58.          IF (SwChar <> '/') THEN
  59.             InValidParms(2);
  60.          SwStr := Copy(ParamStr(ic),2,1);
  61.          SwChar := UpCase(SwStr[1]);
  62.          ArgText := Copy(ParamStr(ic),3,(Length(ParamStr(ic))-2));
  63.          CASE SwChar OF
  64.               'I' : IFL := ArgText;
  65.               'O' : OFL := ArgText;
  66.               'L' : LPP := ArgText;
  67.          END;
  68.      END;
  69. END;
  70.  
  71. PROCEDURE Open_I_O_Files;
  72. BEGIN
  73.      Assign(InFile,IFL);
  74. {$I-} Reset(InFile); {$I+}
  75.      IF IOResult <> 0 THEN
  76.         InValidParms(3);
  77.  
  78.      Assign(OutFile,OFL);
  79. {$I-} Rewrite(OutFile);  {$I+}
  80.      IF IOResult <> 0 THEN
  81.         InValidParms(4);
  82. END;
  83.  
  84. PROCEDURE GetEvenOdd ( lpp_param : string; var oddnum, evennum : Word);
  85.                        { determine page length for odd/ even pages }
  86. VAR
  87.    odds, evens,        { string of odd/ even lines per page }
  88.    lstr  : string[5];  { entire string containing numbers needed }
  89.  
  90.    vlppo,vlppe,        { numeric of lines per page odd/even }
  91.    lval  : byte;       { numeric of string containing numbers needed }
  92.  
  93.    pcode : integer; { error code, will be non-zero if strings are not numbers }
  94.  
  95. BEGIN
  96.      lstr := lpp_param;
  97.  
  98.      IF ((pos(':',lstr)) <> 0) THEN
  99.      BEGIN                                            { determine position of }
  100.         odds  := copy(lstr,1,((pos(':',lstr))-1));    { any colon, and divide }
  101.         evens := copy(lstr,((pos(':',lstr))+1),length(lstr)); { at that point }
  102.  
  103.         val(odds,vlppo,pcode);       { convert first part of string         }
  104.         IF (pcode <> 0) THEN         { into numeric                         }
  105.            InValidParms(5);          { show help if any errors              }
  106.  
  107.         val(evens,vlppe,pcode);      { convert first part of string         }
  108.         IF (pcode <> 0) THEN         { into numeric                         }
  109.            InValidParms(5);          { show help if any errors              }
  110.      END
  111.  
  112.      ELSE BEGIN  { if colon not present, lines/pg should be entire parameter }
  113.         val(lstr,lval,pcode);        { convert entire of string             }
  114.         IF (pcode <> 0) THEN         { into numeric                         }
  115.            InValidParms(5);          { show help if any errors              }
  116.         vlppo := lval;
  117.         vlppe := lval;
  118.      END;
  119.      oddnum  := vlppo;
  120.      evennum := vlppe;
  121. END;
  122.  
  123. procedure StripPB ( var cline : string );
  124. const
  125.      ff = ' ';
  126. begin
  127. end;
  128.  
  129. PROCEDURE InsertFF( OddLines, EvenLines : Word);
  130. CONST
  131.      FF = ' ';
  132. VAR
  133.      LinesCopied,
  134.      LinesPerPage,
  135.      PageCopying   : Word;
  136.      ALine         : String;
  137. BEGIN
  138.      LinesCopied := 0;
  139.      LinesPerPage := OddLines;
  140.      PageCopying := 1;
  141.      WHILE (NOT Eof(InFile)) DO
  142.      BEGIN
  143.           ReadLn(InFile,ALine);
  144.           WHILE ((pos(FF,aline))<>0) do delete (aline,(pos(FF,aline)),1);
  145.           IF (LinesCopied = LinesPerPage) THEN
  146.           BEGIN
  147.              ALine := FF + ALine;
  148.              LinesCopied := 0;
  149.              PageCopying := Succ(PageCopying);
  150.              IF ((PageCopying MOD 2) = 0) THEN
  151.                 LinesPerPage := EvenLines
  152.              ELSE
  153.                 LinesPerPage := OddLines;
  154.           END;
  155.           WriteLn(OutFile,ALine);
  156.           LinesCopied := Succ(LinesCopied);
  157.      END;
  158. END;
  159.  
  160. BEGIN                                { main }
  161.      Writeln(ProgData);
  162.      Writeln(ProgDat2);
  163.      Writeln;
  164.      ParseComLine;
  165.      Open_I_O_Files;
  166.      GetEvenOdd(LPP,OddL,EvenL);
  167.      Writeln('Input file: ',IFL,' - Output file: ',OFL,'.');
  168.      Writeln('Lines per odd / even page: ',Oddl,' / ',EvenL,'.');
  169.      InsertFF(OddL,EvenL);
  170.      Close(InFile);
  171.      Close(OutFile);
  172.      Writeln('Done!');
  173. END.
  174.