home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / LEFTJUST.ZIP / LEFTJUST.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-08-31  |  7.5 KB  |  284 lines

  1. PROGRAM LeftJust;
  2.  
  3. {$B-,D+,R-,S-,V-}
  4.  
  5. USES DOS, CRT;
  6.  
  7. CONST
  8.   Bell = #7;
  9.  
  10. TYPE
  11.   line    = STRING[255];
  12.  
  13. VAR
  14.   LineRead    : line;
  15.   InFile      : TEXT;
  16.   OutFile     : TEXT;
  17.   InFileName  : line;
  18.   OutFileName : line;
  19.   Version     : line;
  20.   Buf1        : Array[1..16384] of Char;
  21.   Buf2        : Array[1..16384] of Char;
  22.  
  23. {
  24.    ┌────────────────────────────────────────────────────┐
  25.    │ PROCEDURE Error_Message                            │
  26.    └────────────────────────────────────────────────────┘
  27. }
  28.  
  29. PROCEDURE Error_Message (message : string);
  30.  
  31. BEGIN
  32.   WRITELN (Bell,message);                      { ding bell & write message }
  33.   HALT;
  34. END;
  35.  
  36. {
  37.    ┌────────────────────────────────────────────────────┐
  38.    │ PROCEDURE Usage                                    │
  39.    └────────────────────────────────────────────────────┘
  40. }
  41.  
  42. PROCEDURE Usage;
  43.  
  44. CONST
  45.   NL = #13#10;
  46.  
  47. BEGIN
  48.   WRITELN (Bell,
  49. 'The purpose of this program is to left justify ASCII text files.  That is:',NL,
  50. 'remove a border consisting of blanks (ASCII character 32) from ASCII text',NL,
  51. 'files.  The program also removes form feeds (ASCII 12) as they make it more',NL,
  52. 'difficult to identify the width of the border.  An option is to remove ONLY',NL,
  53. '(1) the excess left border spaces (automargin adjust), or (2) remove left',NL,
  54. 'border spaces up to a set number.',NL,
  55. '',NL,
  56. 'USAGE:     LEFTJUST [in_filename] [out_filename] {/m{#}}',NL,
  57. '',NL,
  58. 'The /m option allows you to automatically remove an "excess border" of',NL,
  59. 'left-side spaces in the ASCII file.  For example, if ALL lines have at',NL,
  60. 'least 5 left spaces, then EVERY line will have those (and only those) 5',NL,
  61. 'spaces removed.  If a number is specified after the /M option (e.g.,',NL,
  62. '"/m4"), then every line will have UP TO 4 spaces removed.',NL);
  63.  
  64.   Halt;
  65. END;
  66.  
  67. {
  68.    ┌────────────────────────────────────────────────────┐
  69.    │ PROCEDURE Read_Params                              │
  70.    └────────────────────────────────────────────────────┘
  71. }
  72.  
  73. PROCEDURE Read_Params (VAR param_option : integer; VAR InFileNameV : line;
  74.                        VAR OutFileNameV : line);
  75.  
  76. VAR
  77.   param : string;
  78.   code  : integer;
  79.  
  80. BEGIN
  81.   IF (ParamCount IN [2,3]) THEN
  82.     BEGIN
  83.       InFileNameV  := ParamStr(1);
  84.       OutFileNameV := ParamStr(2);
  85.       IF ParamStr(3) = '' THEN
  86.         BEGIN
  87.           param_option := 0;
  88.           EXIT;
  89.         END;
  90.                                              { implied ELSE routine        }
  91.       param := ParamStr(3);                  { select automargin adjust w/ }
  92.       IF POS ('/',param) = 1 THEN            { /m or /M; by entering #,    }
  93.         BEGIN                                { left margin reduced by #    }
  94.           IF ((Param[2] = 'm') OR (Param[2] = 'M')) THEN
  95.             BEGIN
  96.               IF LENGTH (param) = 2 THEN
  97.                 BEGIN
  98.                   param_option := -1;        { if input param. is ONLY /m, }
  99.                   EXIT;                      { set var. to -1              }
  100.                 END
  101.               ELSE
  102.                 BEGIN                        { if # entered, set var to #  }
  103.                   DELETE (param,1,2);
  104.                   VAL (param, param_option, code);
  105.                   IF code <> 0 THEN
  106.                     Error_Message ('Error -- Input margin number invalid');
  107.                   IF param_option = 0 THEN param_option := -1;
  108.                 END;
  109.             END
  110.           ELSE
  111.             Error_Message ('Error -- Illegal parameter');
  112.         END
  113.       ELSE
  114.         Error_Message ('Error -- Illegal parameter');
  115.     END
  116.   ELSE
  117.     Usage;
  118. END;
  119.  
  120. {
  121.    ┌────────────────────────────────────────────────────┐
  122.    │ PROCEDURE OPEN_INFILE                              │
  123.    └────────────────────────────────────────────────────┘
  124. }
  125.  
  126. PROCEDURE Open_InFile (InFileNameV : line; VAR InFileV : TEXT);
  127.  
  128. VAR
  129.   FileAttr : word;
  130.  
  131. BEGIN
  132. {$I-}
  133.   ASSIGN (InFileV,InFileNameV);
  134.   IF IOresult <> 0 THEN Error_Message ('Error -- cannot assign filename');
  135.  
  136.   GetFAttr (InFileV, FileAttr);
  137.   IF (FileAttr AND Directory) <> 0 THEN
  138.     Error_Message ('Error -- input file does not exist in current directory');
  139.  
  140.   RESET (InFileV);
  141.   IF IOresult <> 0 THEN Error_Message ('Error -- cannot open input file');
  142.  
  143.   SETTEXTBUF (InFileV, Buf1);
  144. {$I+}
  145.  
  146. END;
  147.  
  148. {
  149.    ┌────────────────────────────────────────────────────┐
  150.    │ PROCEDURE OPEN_OUTFILE                             │
  151.    └────────────────────────────────────────────────────┘
  152. }
  153.  
  154. PROCEDURE Open_OutFile (OutFileNameV : line; VAR OutFileV : TEXT);
  155.  
  156. BEGIN
  157. {$I-}
  158.   ASSIGN (OutFileV,OutFileNameV);
  159.   IF IOresult <> 0 THEN Error_Message ('Error -- cannot assign filename');
  160.  
  161.   REWRITE (OutFileV);
  162.   IF IOresult <> 0 THEN Error_Message ('Error -- cannot open output file');
  163.  
  164.   SETTEXTBUF (OutFileV, Buf2);
  165. {$I+}
  166. END;
  167.  
  168. {
  169.    ┌────────────────────────────────────────────────────┐
  170.    │ PROCEDURE CLOSE_FILES                              │
  171.    └────────────────────────────────────────────────────┘
  172. }
  173.  
  174. PROCEDURE Close_Files (VAR InFileV : TEXT; VAR OutFileV : TEXT);
  175.  
  176. BEGIN
  177.   CLOSE (InFileV);
  178.   CLOSE (OutFileV);
  179.   WRITELN (Bell);                               { ding bell }
  180. END;
  181.  
  182. {
  183.    ┌────────────────────────────────────────────────────┐
  184.    │ PROCEDURE Cnt_Excess_Margin                        │
  185.    └────────────────────────────────────────────────────┘
  186. }
  187.  
  188. PROCEDURE Cnt_Excess_Margin (VAR Margin : integer);
  189.  
  190. VAR
  191.   Count   : integer;
  192.  
  193. BEGIN
  194.   Count   := 1;
  195.   Margin  := 256;
  196.  
  197.   WRITELN;
  198.   WRITELN ('Automatically checking left margin size ...');
  199.  
  200.   WHILE NOT EOF(InFile) DO
  201.     BEGIN
  202.       READLN (InFile,LineRead);
  203.  
  204.       DELETE (LineRead,POS(#12,LineRead),1);        { delete FF's           }
  205.  
  206.       WHILE LineRead[Count] = ' ' DO INC (Count);   { count leading spaces  }
  207.       IF (Count < Margin) AND (LENGTH(LineRead) > 0) THEN Margin := Count;
  208.  
  209.       Count := 1;
  210.     END;
  211.  
  212.   CLOSE (InFile);
  213.   RESET (InFile);
  214. END;
  215.  
  216. {
  217.    ┌────────────────────────────────────────────────────┐
  218.    │ PROCEDURE PROCESS_INFILE                           │
  219.    └────────────────────────────────────────────────────┘
  220. }
  221.  
  222. PROCEDURE Process_InFile (margin : integer; VAR InFileV  : TEXT;
  223.                                             VAR OutFileV : TEXT);
  224.  
  225. VAR
  226.   Count    : integer;
  227.   DelLimit : integer;
  228.  
  229. BEGIN
  230.   DelLimit := 256;
  231.  
  232.   IF margin = -1 THEN Cnt_Excess_margin (DelLimit);
  233.   IF margin >  0 THEN DelLimit := margin;
  234.  
  235.   WHILE NOT EOF(InFileV) DO
  236.     BEGIN
  237.       Count    := 1;
  238.  
  239.       READLN (InFileV,LineRead);
  240.  
  241.       WHILE POS (#12,LineRead) <> 0 DO
  242.         DELETE (LineRead,POS(#12,LineRead),1);    { delete FF's           }
  243.  
  244.       WHILE (LineRead[Count] = ' ') AND (Count <= DelLimit) DO
  245.         INC (Count);
  246.  
  247.       DELETE (LineRead,1,Count - 1);              { delete counted spaces }
  248.  
  249.       WRITELN (OutFileV, LineRead);
  250.  
  251.     END; { while#1 }
  252.  
  253.   FLUSH (OutFileV);                               { ensure all lines written }
  254. END;
  255.  
  256. {
  257.    ┌────────────────────────────────────────────────────┐
  258.    │ MAIN PROGRAM                                       │
  259.    └────────────────────────────────────────────────────┘
  260. }
  261.  
  262. VAR
  263.   Option  : integer;
  264.  
  265. BEGIN
  266.  
  267.   Version := 'Version 1.2, 8-26-88 -- Public Domain by John Land';
  268.  
  269.   CLRSCR;
  270.  
  271.   Read_Params (Option, InFileName, OutFileName);
  272.  
  273.   Open_InFile (InFileName, InFile);
  274.  
  275.   Open_OutFile (OutFileName, OutFile);
  276.  
  277.   WRITELN ('PROCESSING ',InFileName, ' INTO ', OutFileName);
  278.  
  279.   Process_InFile (Option, InFile, OutFile);
  280.  
  281.   Close_Files (InFile, OutFile);
  282.  
  283. END.
  284.