home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1988 / 09 / stitt.lis < prev   
File List  |  1988-08-22  |  4KB  |  116 lines

  1. _USING ACTION CHARTS_
  2. by
  3. Martin Stitt
  4.  
  5. Listing One
  6.  
  7. { Read each line of an action chart file and filter out the bracket }
  8. { charactors.  Format the remaining text with regards to the left }
  9. { margin and indentation. }
  10. { NOTE - comments ending with a ** denote areas of this program which were }
  11. {        left incomplete for the sake of brevity. }
  12. { You should customize the following constants to suit your particular }
  13. { tastes as far as indentation.  You may wish to use variables instead }
  14. { and obtain their values from command line switches or a configuration }
  15. { file. }
  16.   
  17. const
  18.  
  19.   margin_spaces    : integer = 0;         { customize these variables per }
  20.   margin_tabs      : integer = 0;         { your particular taste. }
  21.   spaces_per_level : integer = 2;         { Better yet, prompt for them at }
  22.   skip_null_lines  : boolean = false;     { run time or read from a
  23.  
  24. { These character sets are the block graphics characters which }
  25. { this program filters out. }
  26.  
  27.   top_set  : set of char = ['Z','U'];    { alt-218, alt-213 }
  28.   bot_set  : set of char = ['@','T'];    { alt-192, alt-212 }
  29.   else_set : set of char = ['C'];        { alt-195 }
  30.   pass_set : set of char = [' ','D','M','3'];
  31.  
  32.                                          { space, alt-196, alt-205, alt-179 }
  33. type
  34.   str_type = string[80];
  35.   chset = set of char;
  36.  
  37. var
  38.   source,dest       : text;
  39.   total_spaces,c_index,
  40.   indent_adj,indent_level : integer;
  41.   indent_str,work_str : str_type;
  42.  
  43.  
  44. procedure advance_index(w_str : str_type; var ix : integer; test_set : chset);
  45.  
  46. { accept a string, an index and a set of characters to the string test for. }
  47. { advance the index past all charactors in the specified set, returning an }
  48. { updated value of the index to the calling program. }
  49.  
  50. var
  51.   w_str_len : integer;
  52.  
  53. begin
  54. w_str_len := ord(w_str[0]);
  55. while(true) do begin
  56.   if ix > w_str_len then exit;
  57.   if not (w_str[ix] in test_set) then exit;
  58.   ix := ix + 1
  59.   end
  60. end;
  61.  
  62. begin  { MAIN PROGRAM LOGIC}
  63.  
  64. indent_level := 0;
  65.  
  66. { validate command line parameters and manage file open errors ** }      
  67.  
  68. assign(source,paramstr(1));         
  69. reset(source);                      
  70. assign(dest,paramstr(2));
  71. rewrite(dest);
  72.  
  73. { read each line from the source file and filter out the characters which }
  74. { are used to produce the action chart brackets.  Write the resulting line }
  75. { to the destination file. }
  76.  
  77. while(not eof(source)) do begin
  78.   readln(source,work_str);
  79.   c_index := 1;
  80.   indent_adj := 0;
  81.   advance_index(work_str,c_index,pass_set);
  82.   if c_index <= ord(work_str[0]) then
  83.     if work_str[c_index] in top_set + bot_set + else_set then begin
  84.       if work_str[c_index] in top_set then indent_adj := 1
  85.       else
  86.         if work_str[c_index] in bot_set then
  87.           indent_level := indent_level - 1
  88.         else begin     { must be in the else set }
  89.           indent_level := indent_level - 1;
  90.           indent_adj := 1
  91.           end;
  92.       advance_index(work_str,c_index,top_set + bot_set + else_set + pass_set);
  93.       end;
  94.   if c_index <= ord(work_str[0]) then begin
  95.     total_spaces := margin_spaces + (indent_level * spaces_per_level);
  96.     indent_str[0] := chr(margin_tabs + total_spaces);
  97.     fillchar(indent_str[1],margin_tabs,#9);
  98.     fillchar(indent_str[margin_tabs+1],total_spaces,' ');
  99.     writeln(dest,concat(indent_str,copy(work_str,c_index,999)))
  100.     end
  101.   else
  102.     if not skip_null_lines then writeln(dest);
  103.   indent_level := indent_level + indent_adj
  104.   end;
  105.  
  106. close(dest);
  107. close(source);
  108.  
  109. { manage file errors ** }
  110.  
  111. end.
  112.  
  113.  
  114.  
  115.  
  116.