home *** CD-ROM | disk | FTP | other *** search
/ Beijing Paradise BBS Backup / PARADISE.ISO / software / BBSDOORW / RADOOR30.ZIP / FILTER.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-04-10  |  12.3 KB  |  349 lines

  1. {╔═════════════════════════════════════════════════════════════════════════╗
  2.  ║                                                                         ║
  3.  ║                   (c) CopyRight LiveSystems 1990, 1994                  ║
  4.  ║                                                                         ║
  5.  ║ Author    : Gerhard Hoogterp                                            ║
  6.  ║ FidoNet   : 2:282/100.5   2:283/7.33                                    ║
  7.  ║ BitNet    : GERHARD@LOIPON.WLINK.NL                                     ║
  8.  ║                                                                         ║
  9.  ║ SnailMail : Kremersmaten 108                                            ║
  10.  ║             7511 LC Enschede                                            ║
  11.  ║             The Netherlands                                             ║
  12.  ║                                                                         ║
  13.  ║        This module is part of the RADoor BBS doorwriters toolbox.       ║
  14.  ║                                                                         ║
  15.  ╚═════════════════════════════════════════════════════════════════════════╝}
  16. {
  17.  Description:
  18.  
  19.  This unit provides the toolbox with a standard output filter. This makes it
  20.  easy to give your programs a nice consistent look and feel...
  21.  Features:
  22.  
  23.      - HighAscii translation for ASCII users. This way you don't have to
  24.        care about the special language characters and linedrawing symbols.
  25.        This filter translates them into comparible low-ascii.
  26.  
  27.      - Brackets translation. Especialy nice for the scandinavian users who
  28.        use the [] for their own special characters. This unit translates
  29.        ^[ and ^] into the brackets you define.
  30.  
  31.      - Easy colormapping. Indirect color access through a colortable.
  32.        You define up to 10 color-combination in your program (Just TextAttr
  33.        values) and use ^0..^9 in the output strings. The filter translates
  34.        them into the correct ansi or avatar sequence, or skips them completely
  35.        for ASCII users.
  36.        The ANSI translation is smart, that is, it always sends the shortest
  37.        string needed..
  38.  
  39.      - You can define the used EscapeCharacter, default the ^ is used, but
  40.        it can be anything, also HighAscii.
  41.  
  42.      - The filter tries to be smart about and Clear until End Of Line.
  43.        If you put an ^! in your text, it's translated to the ANSI or
  44.        AVATAR ClrEol sequence. For ASCII an #13<79 Spaces>#13 is send.
  45.        This isn't the fasted methode, but as the unit doesn't know where
  46.        on the line it is, it's a failsave methode.. Anyone who knows a
  47.        better way is welcome..
  48.  
  49. - 12 Feb 92: Release
  50.  
  51. }
  52.  
  53. Unit Filter;
  54. Interface
  55.  
  56. {----------------------------------------------------------------------------|
  57.   Color definitions so you don't need the CRT unit for these
  58. |----------------------------------------------------------------------------}
  59.  
  60. Const Black        = 0;
  61.       Blue         = 1;
  62.       Green        = 2;
  63.       Cyan         = 3;
  64.       Red          = 4;
  65.       Magenta      = 5;
  66.       Brown        = 6;
  67.       LightGray    = 7;
  68.       DarkGray     = 8;
  69.       LightBlue    = 9;
  70.       LightGreen   = 10;
  71.       LightCyan    = 11;
  72.       LightRed     = 12;
  73.       LightMagenta = 13;
  74.       Yellow       = 14;
  75.       White        = 15;
  76.       Blink        = 128;
  77.  
  78. {---------------------------------------------------------------------------|
  79.   The table for the HighAscii translation.
  80.   Translates diacritical characters and linedrawing symbols plus a few
  81.   special characters. (The #254 which is often used as attention bullet
  82.   into a *,  Alpha -> a, Beta -> b, Gamma -> g f.e.)
  83. |---------------------------------------------------------------------------}
  84.  
  85. Const TransTable : Array[#128..#255] of Char =
  86.                   (
  87. {128}             'C'  ,'u'  ,'e'  ,'a'  ,'a'  ,'a'  ,'a'  ,'c',
  88. {136}             'e'  ,'e'  ,'e'  ,'i'  ,'i'  ,'i'  ,'A'  ,'A',
  89.  
  90. {144}             'E'  ,' '  ,' '  ,'o'  ,'o'  ,'o'  ,'u'  ,'u',
  91. {152}             'y'  ,'O'  ,'U'  ,'c'  ,' '  ,'O'  ,' ' ,'f',
  92.  
  93. {160}             'a'  ,'i'  ,'o'  ,'u'  ,'n'  ,'N'  ,'a'  ,'o',
  94. {168}             '?'  ,' '  ,' '  ,' '  ,' '  ,'i'  ,' '  ,' ',
  95.  
  96. {176}             ' '  ,' '  ,' '  ,'|'  ,'+'  ,'+'  ,'+'  ,'+',
  97. {184}             '+'  ,'+'  ,'|'  ,'+'  ,'+'  ,'+'  ,'+'  ,'+',
  98.  
  99. {192}             '+'  ,'+'  ,'+'  ,'+'  ,'-'  ,'+'  ,'+'  ,'+',
  100. {200}             '+'  ,'+'  ,'+'  ,'+'  ,'+'  ,'-'  ,'+'  ,'+',
  101.  
  102. {208}             '+'  ,'+'  ,'+'  ,'+'  ,'+'  ,'+'  ,'+'  ,'+',
  103. {216}             '+'  ,'+'  ,'+'  ,' '  ,' '  ,' '  ,' '  ,' ',
  104.  
  105. {224}             'a'  ,'b' ,'g'  ,' '  ,' '  ,' '  ,' '  ,' ',
  106. {232}             ' '  ,' '  ,' '  ,' '  ,' '  ,' '  ,' '  ,' ',
  107.  
  108. {240}             '='  ,' '  ,' '  ,' '  ,' '  ,' '  ,'/'  ,' ',
  109. {248}             ' '  ,' '  ,' '  ,' '  ,' '  ,' '  ,'*'  ,' '
  110.                   );
  111.  
  112.  
  113.  
  114.  
  115. {---------------------------------------------------------------------------|
  116.  Colortable for up to 10 TextAttr bytes. Should be more than enough in most
  117.  cases. Can be expanded without to much hassel. Although in my humble opinion
  118.  to many colors makes a program to "busy". Keep it simple..;)
  119.  
  120.  You should provide the contents of the table in the mainprogram. You can
  121.  use the same colorattributes as for normal CRT usage.
  122. |---------------------------------------------------------------------------}
  123.  
  124. Const TextFinder   : String[12] = 'COLORTABEL10';
  125.       ColorTable   : Array[0..9] Of Byte =
  126.                      (
  127.                        $0F,   { Color 0 }
  128.                        $0F,   { Color 1 }
  129.                        $0F,   { Color 2 }
  130.                        $0F,   { Color 3 }
  131.                        $0F,   { Color 4 }
  132.                        $0F,   { Color 5 }
  133.                        $0F,   { Color 6 }
  134.                        $0F,   { Color 7 }
  135.                        $0F,   { Color 8 }
  136.                        $0F    { Color 9 }
  137.                      );
  138.  
  139. {---------------------------------------------------------------------------|
  140.   Initialize the filter options.
  141.  
  142.   Because I liked to keep the filter as standalone as possible you have to
  143.   initialize the filter yourself.
  144.  
  145.   Grap     Graphics (Use ANSI)  Default OFF
  146.   Avt      AVATAR               Default OFF
  147.   LeftB    LeftBracket char     Default  [
  148.   RightB   RightBracket char    Default  ]
  149.   EscChar  Escape character.    Default  ^
  150. |---------------------------------------------------------------------------}
  151.  
  152. Procedure InitUsedFilter( Grap,
  153.                           Avt     : Boolean;
  154.                           LeftB,
  155.                           RightB,
  156.                           EscChar : Char
  157.                         );
  158.  
  159. {---------------------------------------------------------------------------|
  160.  And the filter itself. Note that the header comformes the OutputFilterType
  161.  procedure type definition in the FOSSIL unit, so if you write your own
  162.  filter, it MUST have the same definition:
  163.  
  164.     Procedure <ProcedureName>(Var <StringName> : String);
  165.  
  166.  It should be compiled in FAR mode. (When in an units interface section
  167.  this is automaticaly)
  168.  
  169. |---------------------------------------------------------------------------}
  170.  
  171.  
  172. Procedure UsedFilter(Var InStr : String);
  173.  
  174. Implementation
  175.  
  176. {---------------------------------------------------------------------------|
  177.   The default values of the filtervariables
  178. |---------------------------------------------------------------------------}
  179.  
  180. Const EscapeChar   : Char    = '^';
  181.       LeftBracket  : Char    = '[';
  182.       RightBracket : Char    = ']';
  183.       Graphics     : Boolean = False;
  184.       Avatar       : Boolean = False;
  185.  
  186.  
  187. {---------------------------------------------------------------------------|
  188.   The initialization procedure
  189. |---------------------------------------------------------------------------}
  190.  
  191. Procedure InitUsedFilter( Grap,
  192.                           Avt     : Boolean;
  193.                           LeftB,
  194.                           RightB,
  195.                           EscChar : Char
  196.                         );
  197. Begin
  198. Graphics:=Grap;
  199. Avatar:=AVT;
  200. LeftBracket:=LeftB;
  201. RightBracket:=RightB;
  202. EscapeChar:=EscChar;
  203. End;
  204.  
  205.  
  206. {---------------------------------------------------------------------------|
  207.   S  Translates a Word to a string of an given length. (Max. 20 characters)
  208. |---------------------------------------------------------------------------}
  209.  
  210.  
  211. Type Str20 = String[20];
  212.  
  213. Function S(Num : Word;Len : Byte):Str20;
  214. Var Temp : Str20;
  215. Begin
  216. Str(Num:Len,Temp);
  217. S:=Temp;
  218. End;
  219.  
  220.  
  221. {---------------------------------------------------------------------------|
  222.   Byte2Ansi translates an TextMode attribute byte into the shortest ANSI
  223.             sequence. This is done by storing the last foreground and
  224.             background colors and only sending something if there are
  225.             changes.
  226. |---------------------------------------------------------------------------}
  227.  
  228. Var LastFG    : Byte;
  229.     LastBG    : Byte;
  230.  
  231. Function Byte2Ansi(B : Byte):Str20;
  232. Const AnsiTable : Array[0..7] Of Byte
  233.                 = (0,4,2,6,1,5,3,7);
  234.  
  235. Var FG,BG : Byte;
  236.     Tmp   : Str20;
  237.     Temp  : Byte;
  238.  
  239. Begin
  240. Tmp:='';
  241.  
  242. BG:=(B And $70) Shr 4;
  243. FG:=(B and $0F);
  244. If FG<>LastFG
  245.    Then Begin
  246.         Temp:=FG;
  247.         If (FG>7)
  248.            Then Begin
  249.                 Dec(FG,8);
  250.                 If LastFG<=7
  251.                    Then Tmp:=Tmp+'0;1';
  252.                 End
  253.            Else Begin
  254.                 If LastFG>7
  255.                    Then Tmp:=Tmp+'0';
  256.                 End;
  257.         If (B And $80)=$80
  258.            Then Tmp:=Tmp+';6';
  259.         Tmp:=Tmp+';'+S(AnsiTable[FG]+30,0);
  260.         LastFG:=Temp;
  261.         End;
  262. If BG<>LastBG
  263.    Then Begin
  264.         LastBG:=BG;
  265.         Tmp:=Tmp+';'+S(AnsiTable[BG]+40,0);
  266.         End;
  267.  
  268. If Tmp[1]=';'
  269.    Then Delete(Tmp,1,1);
  270. If Tmp<>''
  271.    Then Byte2Ansi:=#27'['+Tmp+'m'
  272.    Else Byte2Ansi:='';
  273. End;
  274.  
  275. {---------------------------------------------------------------------------|
  276.    EmptyLine is the string used to clear the line with an ^! in ASCII mode.
  277.  
  278.    UsedFilter is the output filter procedure.
  279. |---------------------------------------------------------------------------}
  280.  
  281. Var EmptyLine : String[80];
  282.  
  283. Procedure UsedFilter(Var InStr : String);
  284. Var StrCount : Byte;
  285.     TempStr  : String;
  286.     Index    : Byte;
  287. Begin
  288. TempStr:='';
  289. StrCount:=1;
  290. While StrCount <= Length(InStr) Do
  291.  Begin
  292.  If (InStr[StrCount]=EscapeChar) And
  293.     (StrCount<Length(InStr))
  294.     Then Begin
  295.          Inc(StrCount);
  296.          Case InStr[StrCount] Of
  297.            '['     : Begin
  298.                      TempStr:=TempStr+LeftBracket;
  299.                      Inc(StrCount);
  300.                      End;
  301.            ']'     : Begin
  302.                      TempStr:=TempStr+RightBracket;
  303.                      Inc(StrCount);
  304.                      End;
  305.            '!'     : Begin
  306.                      If Graphics Or AVATAR
  307.                         Then Begin
  308.                              If AVATAR
  309.                                 Then TempStr:=TempStr+^V^G
  310.                                 Else TempStr:=TempStr+#27'[K';
  311.                              End
  312.                         Else TempStr:=TempStr+#13+EmptyLine+#13;
  313.                      Inc(StrCount);
  314.                      End;
  315.            '0'..'9': Begin
  316.                      If Graphics Or AVATAR
  317.                         Then Begin
  318.                              Index:=Ord(InStr[StrCount])-48;
  319.                              If AVATAR
  320.                                 Then TempStr:=TempStr+^V^A+Chr(ColorTable[Index])
  321.                                 Else TempStr:=TempStr+Byte2Ansi(ColorTable[Index]);
  322.                              End;
  323.                      Inc(StrCount);
  324.                      End;
  325.          End; {Case}
  326.          End
  327.     Else Begin
  328.          If (InStr[StrCount]=#12) And
  329.             (Graphics And (Not AVATAR))
  330.             Then TempStr:=TempStr+#27'[2J'
  331.             Else Begin
  332.                  If (InStr[StrCount]>#127) And
  333.                     (Not (Graphics Or Avatar))
  334.                     Then TempStr:=TempStr+TransTable[InStr[StrCount]]
  335.                     Else TempStr:=TempStr+InStr[StrCount];
  336.                  End;
  337.          Inc(StrCount);
  338.          End;
  339.  End; {While}
  340. InStr:=TempStr;
  341. End;
  342.  
  343. Begin
  344. FillChar(EmptyLine,SizeOf(EmptyLine),#32);
  345. EmptyLine[0]:=#79;
  346. LastFG:=0;
  347. LastBG:=0;
  348. End.
  349.