home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / packer / arc / arctool / wordwrap.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1994-04-11  |  4.0 KB  |  121 lines

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