home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ppcsmalleiffel / lib_se / pretty.e < prev    next >
Encoding:
Text File  |  1998-01-16  |  4.6 KB  |  198 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  4. --                       http://www.loria.fr/SmallEiffel
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it 
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later 
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License 
  11. -- for  more  details.  You  should  have  received a copy of the GNU General 
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class PRETTY -- The command.
  17. --
  18.  
  19. inherit GLOBALS;
  20.    
  21. creation make
  22.    
  23. feature 
  24.    
  25.    make is
  26.       do
  27.      if argument_count < 1 then
  28.         echo.w_put_string("Bad use of command `pretty'.%N");
  29.         print_help("pretty");
  30.         die_with_code(exit_failure_code);
  31.      else
  32.         automat;
  33.      end;
  34.       end;
  35.    
  36. feature {NONE}
  37.    
  38.    state: INTEGER;
  39.    
  40.    style: STRING;
  41.    
  42.    class_names: ARRAY[STRING] is
  43.       once
  44.      !!Result.make(1,10);
  45.      Result.clear;
  46.       end;
  47.    
  48.    automat is
  49.       local
  50.      arg: INTEGER;
  51.      a: STRING;
  52.      -- state 0  : nothing done.
  53.      -- state 1  : end.
  54.      -- state 2  : error.
  55.       do
  56.      from  
  57.         arg := 1;
  58.      until
  59.         arg > argument_count or else state > 0
  60.      loop
  61.         a := argument(arg);
  62.         if a.item(1) /= '-' then
  63.            class_names.add_last(a);
  64.         elseif ("-no_warning").is_equal(a) then
  65.            eh.set_no_warning;
  66.         elseif ("-default").is_equal(a) then
  67.            if style /= Void then
  68.           error_style(a);
  69.            else
  70.           fmt.set_default;
  71.           style := a;
  72.            end;
  73.         elseif ("-zen").is_equal(a) then
  74.            if style /= Void then
  75.           error_style(a);
  76.            else
  77.           fmt.set_zen;
  78.           style := a;
  79.            end;
  80.         elseif ("-end").is_equal(a) then
  81.            if style /= Void then
  82.           error_style(a);
  83.            else
  84.           fmt.set_end;
  85.           style := a;
  86.            end;
  87.         elseif ("-parano").is_equal(a) then
  88.            if style /= Void then
  89.           error_style(a);
  90.            else
  91.           fmt.set_parano;
  92.           style := a;
  93.            end;
  94.         else
  95.            echo.w_put_string(fz_08);
  96.            echo.w_put_string(a);
  97.            echo.w_put_character('%N');
  98.            state := 2;
  99.         end;
  100.         arg := arg + 1;
  101.      end;
  102.      if nb_errors > 0 then
  103.         eh.append("No pretty printing done.");
  104.         eh.print_as_error;
  105.      else
  106.         if class_names.empty then
  107.            eh.append("No Class to Pretty Print.");
  108.            eh.print_as_error;
  109.         else
  110.            pretty_print;
  111.         end;
  112.      end;
  113.       end;
  114.    
  115.    error_style(style2: STRING) is
  116.       do
  117.      state := 2;
  118.      eh.append("pretty: format style is already set to ");
  119.      eh.append(style);
  120.      eh.append(". Bad flag ");
  121.      eh.append(style2);
  122.      eh.append(fz_dot);
  123.      eh.print_as_error;
  124.       end;
  125.    
  126.    pretty_print is
  127.       local
  128.      i: INTEGER;
  129.       do
  130.      from  
  131.         i := class_names.lower;
  132.      until
  133.         i > class_names.upper
  134.      loop
  135.         pretty_for(class_names.item(i));
  136.         i := i + 1;
  137.      end;
  138.       end;
  139.    
  140.    pretty_for(name: STRING) is
  141.       require
  142.      name /= Void;
  143.       local
  144.      e_class: BASE_CLASS;
  145.       do
  146.      e_class := small_eiffel.load_class(name);
  147.      if e_class = Void then
  148.         eh.append("No pretty printing done for %"");
  149.         eh.append(name);
  150.         fatal_error("%".");
  151.      else
  152.         path.copy(e_class.path);
  153.         backup.copy(path);
  154.         backup.remove_suffix(eiffel_suffix);
  155.         backup.append(backup_suffix);
  156.         if file_exists(backup) then
  157.            eh.append("Old backup file %"");
  158.            eh.append(backup);
  159.            fatal_error("%" already exists.");
  160.         end;
  161.         rename_file(path,backup);
  162.         if not file_exists(backup) then
  163.            eh.append("Cannot rename %"");
  164.            eh.append(path);
  165.            fatal_error("%".");
  166.         end;
  167.         sfw_connect(new_file,path);
  168.         fmt.connect_to(new_file);
  169.         e_class.pretty_print;
  170.         new_file.disconnect;
  171.         if not small_eiffel.re_load_class(e_class) then
  172.            eh.append("Error during `pretty' printing.%N%
  173.              %Cannot parse output of pretty.%N%
  174.              %Backup %"");
  175.            eh.append(backup);
  176.            fatal_error("%" not removed.");
  177.         end;
  178.      end;
  179.       end;
  180.  
  181.    path: STRING is
  182.       once
  183.      !!Result.make(256);
  184.       end;
  185.  
  186.    backup: STRING is
  187.       once
  188.      !!Result.make(256);
  189.       end;
  190.  
  191.    new_file: STD_FILE_WRITE is
  192.       once
  193.      !!Result.make;
  194.       end;
  195.  
  196. end -- PRETTY -- The command.
  197.  
  198.