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, 2006 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 
00026 #ifndef VCSN_TOOLS_OUT_DISPLAY_HXX
00027 # define VCSN_TOOLS_OUT_DISPLAY_HXX
00028 
00029 # include <vaucanson/tools/out_display.hh>
00030 # include <sys/types.h>
00031 # include <sys/wait.h>
00032 # include <unistd.h>
00033 
00034 namespace vcsn {
00035 
00036   namespace tools {
00037 
00038     template <class S, class T>
00039     bool
00040     out_display(const AutomataBase<S>&,
00041                 const T&,
00042                 const Element<S, T>& a,
00043                 const std::string& name,
00044                 const bool bg,
00045                 char *const argv[],
00046                 void (*function)(std::ostream& o,
00047                                  const Element<S, T>& a,
00048                                  const std::string& name))
00049 
00050     {
00051       int filedes[2];
00052       if (pipe(filedes))
00053         return false;
00054 
00055       pid_t child_pid = fork();
00056       if (child_pid)
00057         {
00058           close(filedes[0]);
00059           if (child_pid == -1)
00060             {
00061               close(filedes[1]);
00062               return false;
00063             }
00064           else
00065             {
00066               // FIXME: That trick is dirty!
00067               {
00068                 std::cout.flush();
00069                 int old = dup(STDOUT_FILENO);
00070                 if (old != -1)
00071                   dup2(filedes[1], STDOUT_FILENO);
00072                 close(filedes[1]);
00073 
00074                 function(std::cout, a, name);
00075 
00076                 if (old != -1)
00077                   {
00078                     dup2(old, STDOUT_FILENO);
00079                     close(old);
00080                   }
00081               }
00082               return bg or waitpid(child_pid, NULL, 0) == child_pid;
00083             }
00084         }
00085       else
00086         {
00087           close(filedes[1]);
00088           dup2(filedes[0], STDIN_FILENO);
00089           close(filedes[0]);
00090           execvp(argv[0], argv);
00091           return false;
00092         }
00093     }
00094 
00095   } // End of namespace tools.
00096 
00097 } // End of namespace vcsn.
00098 
00099 #endif // ! VCSN_TOOLS_OUT_DISPLAY_HXX

Generated on Wed Jun 13 17:00:28 2007 for Vaucanson by  doxygen 1.5.1