home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / WRDWRAP.ZIP / WORDWRAP.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1987-11-21  |  3.8 KB  |  112 lines

  1. {$R-,S-,I+,D+,T-,F-,V+,B-,N-,L+ }
  2. {$M 2048,0,0 }
  3.  
  4. PROGRAM WordWrap(INPUT,OUTPUT); {Version 1.01, 21 Nov 87}
  5.  
  6.    {This code is designed for the Turbo Pascal 4.0 compiler.  Version 1.00 was
  7.      released to the public domain on 17 Nov 87.}
  8.    {This program demonstrates word-wrapping so that text is professionally
  9.      displayed on a console screen.  The source code and compiled program were
  10.      developed by Rob Rosenberger, P.O. Box #643, O'Fallon, IL  62269-0643,
  11.      CompuServe ID 74017,1344.  The source code and compiled program have been
  12.      released to the public domain.  PROGRAMMERS: Please tell me about any
  13.      improvements you feel can be made to this code!  I give credit to the
  14.      first contributor of each idea I use.  Send an EasyPlex message with your
  15.      improvements.  (Please tell me if you don't want your name included.)}
  16.    {Version 1.01, released 21 Nov 87, adds the ability to split a word at a
  17.      hyphen as well as to simply drop a complete word to the next line.}
  18.  
  19.  
  20. USES
  21.    CRT;
  22.  
  23. CONST
  24.    FKeyCode          = #0;
  25.    Space             = ' ';
  26.    Hyphen            = '-';
  27.    BackSpace         = ^H;
  28.    CarriageReturn    = ^M;
  29.    MaxWordLineLength = 80;
  30.  
  31. VAR
  32.    WordLine  : STRING[MaxWordLineLength];
  33.    Index1    : BYTE;
  34.    Index2    : BYTE;
  35.    InputChar : CHAR;
  36.  
  37. BEGIN {WordWrap}
  38. {Initialize the program.}
  39. WordLine  := '';
  40. Index1    := 0;
  41. Index2    := 0;
  42. InputChar := Space;
  43.  
  44. {Open INPUT and OUTPUT.}
  45. ASSIGNCRT(INPUT);
  46. ASSIGNCRT(OUTPUT);
  47. RESET(INPUT);
  48. REWRITE(OUTPUT);
  49.  
  50. {Tell the user what this is all about.}
  51. WRITELN(OUTPUT);
  52. WRITELN(OUTPUT,'WORDWRAP Demonstration Program v1.01, 21 Nov 87');
  53. WRITELN(OUTPUT,'This program demonstrates word-wrapping for programs which');
  54. WRITELN(OUTPUT,'accept input that could traverse the 80-column screen width');
  55. WRITELN(OUTPUT,'of most computers.  Type normally and press ENTER only when');
  56. WRITELN(OUTPUT,'you''re finished.  Words that don''t fit on the current line');
  57. WRITELN(OUTPUT,'will "wrap" onto the next.  Words that have hyphens in them');
  58. WRITELN(OUTPUT,'will be split at the hyphen.  Begin typing now.');
  59. WRITELN(OUTPUT);
  60. WRITELN(OUTPUT);
  61.  
  62. {Read in the first char from the user.}
  63. InputChar := READKEY;
  64.  
  65. {Do the job.}
  66. WHILE (InputChar <> CarriageReturn)
  67.  DO BEGIN
  68.     CASE InputChar OF
  69.       BackSpace: {write destructive backspace & remove char from WordLine}
  70.         BEGIN
  71.         WRITE(OUTPUT,BackSpace,Space,BackSpace);
  72.         DELETE(WordLine,(LENGTH(WordLine) - 1),1)
  73.         END;
  74.       FKeyCode: {user pressed a function key, so dismiss it}
  75.         BEGIN
  76.         InputChar := READKEY; {function keys send two-char scan code!}
  77.         InputChar := Space
  78.         END
  79.       ELSE {InputChar contains a valid char, so deal with it}
  80.         BEGIN
  81.         WRITE(OUTPUT,InputChar);
  82.         WordLine := (WordLine + InputChar);
  83.         IF (LENGTH(WordLine) >= (MaxWordLineLength - 1))
  84.          THEN {we have to do a word-wrap}
  85.             BEGIN
  86.             Index1 := (MaxWordLineLength - 1);
  87.             WHILE ((WordLine[Index1] <> Space)
  88.              AND (WordLine[Index1] <> Hyphen)
  89.              AND (Index1 <> 0))
  90.              DO Index1 := (Index1 - 1);
  91.             IF (Index1 = 0) {whoah, no space was found to split line!}
  92.              THEN Index1 := (MaxWordLineLength - 1); {forces split}
  93.             DELETE(WordLine,1,Index1);
  94.             FOR Index2 := 1 TO LENGTH(WordLine)
  95.              DO WRITE(OUTPUT,BackSpace,Space,BackSpace);
  96.             WRITELN(OUTPUT);
  97.             WRITE(OUTPUT,WordLine)
  98.             END
  99.         END
  100.      END; {CASE InputChar}
  101.     {Get next key from user.}
  102.     InputChar := READKEY
  103.     END; {WHILE (InputChar <> CarriageReturn)}
  104.  
  105. {Wrap up the program.}
  106. WRITELN(OUTPUT);
  107. WRITELN(OUTPUT);
  108. CLOSE(INPUT);
  109. CLOSE(OUTPUT)
  110.  
  111. {And that's all he wrote!}
  112. END.