out_display.hxx

Go to the documentation of this file.
00001 // out_display.hxx: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2005 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_TOOLS_OUT_DISPLAY_HXX
00018 # define VCSN_TOOLS_OUT_DISPLAY_HXX
00019 
00028 namespace vcsn {
00029 
00030   namespace tools {
00031 
00032     template <class S, class T>
00033     bool
00034     out_display(const AutomataBase<S>&,
00035                 const T&,
00036                 const Element<S, T>& a,
00037                 const std::string& name,
00038                 const bool bg,
00039                 char *const argv[],
00040                 void (*function)(std::ostream& o,
00041                                  const Element<S, T>& a,
00042                                  const std::string& name))
00043 
00044     {
00045       int filedes[2];
00046       if (pipe(filedes))
00047         return false;
00048 
00049       pid_t child_pid = fork();
00050       if (child_pid)
00051         {
00052           close(filedes[0]);
00053           if (child_pid == -1)
00054             {
00055               close(filedes[1]);
00056               return false;
00057             }
00058           else
00059             {
00060               // FIXME: That trick is dirty!
00061               {
00062                 std::cout.flush();
00063                 int old = dup(STDOUT_FILENO);
00064                 if (old != -1)
00065                   dup2(filedes[1], STDOUT_FILENO);
00066                 close(filedes[1]);
00067 
00068                 function(std::cout, a, name);
00069 
00070                 if (old != -1)
00071                   {
00072                     dup2(old, STDOUT_FILENO);
00073                     close(old);
00074                   }
00075               }
00076               return bg or waitpid(child_pid, NULL, 0) == child_pid;
00077             }
00078         }
00079       else
00080         {
00081           close(filedes[1]);
00082           dup2(filedes[0], STDIN_FILENO);
00083           close(filedes[0]);
00084           execvp(argv[0], argv);
00085           return false;
00086         }
00087     }
00088 
00089   } // End of namespace tools.
00090 
00091 } // End of namespace vcsn.
00092 
00093 #endif // ! VCSN_TOOLS_OUT_DISPLAY_HXX

Generated on Fri Jul 28 12:18:49 2006 for Vaucanson by  doxygen 1.4.6