home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / handson / archive / Issue159 / files / copydelp.exe / StreamIn / stin.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-23  |  5.9 KB  |  181 lines

  1. unit stin;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ComCtrls;
  8.  
  9. type
  10.   charray = array[0..255] of char;
  11.  
  12.   TForm1 = class(TForm)
  13.     LoadFileBtn: TButton;
  14.     PlainTextRBtn: TCheckBox;
  15.     RichEdit1: TRichEdit;
  16.     procedure LoadFileBtnClick(Sender: TObject);
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure PlainTextRBtnClick(Sender: TObject);
  19.   private
  20.     { Private declarations }
  21.   public
  22.     { Public declarations }
  23.     procedure ReadStream;
  24.     procedure WriteRTFHeader( ms : TMemoryStream );
  25.     procedure processStream( msin, msout : TMemoryStream );
  26.     function KeyWord( token : string ) : boolean;
  27.     procedure WriteToken( token: charray; msout : TMemoryStream );
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33.  
  34. implementation
  35.  
  36. {$R *.DFM}
  37.  
  38. const
  39.        INFILE = 'Stin.pas'; // the input file - this very unit.
  40.        CR = #13;
  41.        LF = #10;
  42.        CRLF = #13#10;
  43.        TERMINATOR : char = '}';
  44.        SLASH : char = '\';
  45.        PARCODE = '\par ';
  46.        KWCODE = '\fs16\cf1\b ';  // bold blue for keywords
  47.        PLAINCODE = '\plain\fs16\cf0 ';  // plain black for other text
  48.        SPECIALCHARS = ['{','}','\'] ;
  49.        ALPHANUMERIC =['a'..'z', 'A'..'Z', '_', '0'..'9']; // '_' is included
  50.                                      // as it's valid in a Pascal identifier
  51.   NUMKEYWORDS = 50;
  52.   Keywords : array[0..NUMKEYWORDS] of string =
  53.               ( 'and', 'array', 'as', 'begin', 'case', 'class', 'const',
  54.               'constructor', 'destructor', 'div', 'do', 'downto', 'else',
  55.               'end', 'except', 'file', 'finally', 'for', 'function', 'if',
  56.               'implementation', 'in', 'inherited', 'interface', 'is', 'mod',
  57.               'nil', 'not', 'object', 'of', 'on', 'or', 'private', 'procedure',
  58.               'program', 'property', 'public', 'published', 'repeat', 'set',
  59.               'string', 'then', 'to', 'try', 'type', 'unit', 'until', 'uses',
  60.               'var', 'while', 'with');
  61.  
  62. function TForm1.KeyWord( token : string ) : boolean;
  63. var
  64.    isKW : boolean;
  65.    i    : integer;
  66. begin
  67.    isKW := false;
  68.    i := 0;
  69.    while ((i <= NUMKEYWORDS) and (isKW = false )) do
  70.    begin
  71.    //!! Test is case-insensitive (OK for Pascal. Change this for C or Java)
  72.       if lowercase(token) = KeyWords[i] then
  73.          isKW := true
  74.       else
  75.          Inc(i);
  76.    end;
  77.    result := isKW;
  78. end;
  79.  
  80. procedure TForm1.WriteRTFHeader( ms : TMemoryStream );
  81. var
  82.    header : charray;
  83.    ctable : charray;
  84. begin
  85.    header := '{\rtf1\ansi\ansicpg1252\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}'+CRLF+'{\f2\fswiss MS Sans Serif;}{\f3\fswiss\fprq2 System;}}'+CRLF;
  86.    ctable := '{\colortbl\red0\green0\blue0;\red0\green0\blue255;}'+CRLF+'\deflang2057\pard\plain\f0\fs16\cf0 ';
  87.    try
  88.       ms.WriteBuffer( header, length(header) );
  89.       ms.WriteBuffer( ctable, length(ctable) );
  90.    except
  91.       on EWriteError do
  92.          ShowMessage( 'Failed to write header and ctable to stream!' );
  93.    end;
  94. end;
  95.  
  96. procedure TForm1.WriteToken( token: charray; msout : TMemoryStream );
  97. begin
  98.    msout.WriteBuffer( KWCODE, length( KWCODE ) );
  99.    msout.WriteBuffer( token, length( token ) );
  100.    msout.WriteBuffer( PLAINCODE, length( PLAINCODE ) );
  101. end;
  102.  
  103. procedure TForm1.processStream( msin, msout : TMemoryStream );
  104. var
  105.    i : integer;
  106.    c : char;
  107.    token : charray;
  108. begin
  109. Screen.Cursor := crHourglass;       // Show hourglass cursor
  110. try
  111. // ---- Read Chars from Input Stream until we are at the end of the Stream -----
  112.    while msin.position < msin.size do     // iterate through input stream
  113.    begin
  114.       msin.ReadBuffer( c, sizeof(c) );    // read one char at a time
  115.       token := '';
  116.       i := 0;
  117. // a) Look For a Token
  118.       while (c in ALPHANUMERIC) do       // if char is alphanumeric, keep
  119.       begin                              // reading subsequent chars and
  120.         token[i] := c;                   // build a token from them
  121.         INC(i);
  122.         msin.ReadBuffer( c, sizeof(c) );
  123.       end;
  124. // b) If we found a token, see if it's a Keyword
  125.       if token <> '' then                // if we've built a token, see
  126.       begin                              // if it's a keyword and
  127.          if KeyWord(token) then          // colour it if necessary
  128.             WriteToken(token, msout)
  129.          else
  130.             msout.WriteBuffer( token, length( token ) );
  131.       end;
  132. // c) if c is has a special meaning in RTF format, precede it with a '\'
  133.       if c in SPECIALCHARS then          // * deal with special RTF chars
  134.           msout.WriteBuffer( SLASH, sizeof( SLASH ) )  // and precede
  135.       else                               // them with a '\' char
  136. // d) else if it's a Carriage Return, add an RTF paragraph code
  137.       if c = CR then                     // * deal with CR - add \para
  138.          msout.WriteBuffer( PARCODE, sizeof( PARCODE ) );
  139.            msout.WriteBuffer( c, sizeof( c ) );
  140.   end;
  141. finally
  142.   Screen.Cursor := crDefault;    // restore normal cusror
  143. end;
  144. end;
  145.  
  146. procedure TForm1.ReadStream;
  147. var
  148.    msin, msout : TMemoryStream;
  149. begin
  150.    msin := TMemoryStream.Create;
  151.    msout := TMemoryStream.Create;
  152.    msin.LoadFromFile(INFILE);
  153.    msin.Position := 0;  // set pos to 0 prior to loading from stream
  154.    msout.Position := 0;
  155.    WriteRTFHeader( msout );
  156.    processStream(msin, msout);
  157.    msout.WriteBuffer(TERMINATOR, sizeof(TERMINATOR)); // terminate RTF buffer
  158.    msout.Position := 0;
  159.    RichEdit1.Lines.LoadFromStream(msout);
  160.    msin.Free;
  161.    msout.Free;
  162. end;
  163.  
  164. procedure TForm1.LoadFileBtnClick(Sender: TObject);
  165. begin
  166.    ReadStream;
  167. end;
  168.  
  169. procedure TForm1.FormCreate(Sender: TObject);
  170. begin
  171.   RichEdit1.Plaintext := false;
  172.   PlainTextRBtn.Checked := RichEdit1.PlainText;
  173. end;
  174.  
  175. procedure TForm1.PlainTextRBtnClick(Sender: TObject);
  176. begin
  177.    RichEdit1.Plaintext := PlainTextRBtn.Checked;
  178. end;
  179.  
  180. end.
  181.