Clean up configure/build-process.

- assume C++ standard-conforming environment
- add a user-option for gnuplot-support
- separate gnuplot-code into declaration and implementation,
  so we can define at EO-build-time whether to use it or not.

Adopt code and Makefiles to above changes.

Some minor fixes.
This commit is contained in:
kuepper 2005-10-02 21:42:08 +00:00
commit 47af7cfe5a
22 changed files with 603 additions and 537 deletions

View file

@ -3,15 +3,19 @@
lib_LIBRARIES = libeoutils.a
libeoutils_a_SOURCES = eoData.cpp \
eoFileMonitor.cpp \
eoGnuplot.cpp \
eoGnuplot1DMonitor.cpp \
eoGnuplot1DSnapshot.cpp \
eoIntBounds.cpp \
eoParser.cpp \
eoRealBounds.cpp \
eoRNG.cpp \
eoState.cpp \
eoUpdater.cpp \
eoFileMonitor.cpp \
eoStdoutMonitor.cpp \
eoRealBounds.cpp \
eoIntBounds.cpp \
make_help.cpp
eoUpdater.cpp \
make_help.cpp \
pipecom.cpp
utilsincludedir = $(pkgincludedir)/utils
@ -50,5 +54,4 @@ utilsinclude_HEADERS = checkpointing \
rnd_generators.h \
selectors.h
AM_CXXFLAGS = -I$(top_srcdir)/src
AM_CXXFLAGS = -I$(top_srcdir)/src -DGNUPLOT_PROGRAM=\"@GNUPLOT@\"

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoFileMonitor.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
@ -21,33 +21,32 @@
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr
mkeijzer@dhi.dk
CVS Info: $Date: 2004-09-21 19:49:48 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/utils/eoFileMonitor.h,v 1.13 2004-09-21 19:49:48 maartenkeijzer Exp $ $Author: maartenkeijzer $
*/
//-----------------------------------------------------------------------------
/** Modified the default behavior, so that it erases existing files.
Can be modified in the ctor.
MS 25/11/00
*/
#ifndef _eoFileMonitor_h
#define _eoFileMonitor_h
#ifndef EO_eoFileMonitor_h
#define EO_eoFileMonitor_h
#include <string>
#include <fstream>
#include <stdexcept>
#include <utils/eoMonitor.h>
#include <eoObject.h>
#include "utils/eoMonitor.h"
#include "eoObject.h"
/**
Prints statistics to file
/** Prints statistics to file
Modified the default behavior, so that it erases existing files. Can
be modified in the ctor.
@version MS 25/11/00
*/
class eoFileMonitor : public eoMonitor
{
public :
eoFileMonitor(std::string _filename, std::string _delim = " ", bool _keep = false, bool _header=false)
: filename(_filename), delim(_delim), keep(_keep), header(_header), firstcall(true)
eoFileMonitor(std::string _filename, std::string _delim = " ", bool _keep = false, bool _header=false)
: filename(_filename), delim(_delim), keep(_keep), header(_header), firstcall(true)
{
if (! _keep) {
std::ofstream os(filename.c_str());
@ -57,23 +56,23 @@ public :
}
}
}
virtual eoMonitor& operator()(void);
virtual eoMonitor& operator()(std::ostream& os);
void printHeader(void);
virtual void printHeader(std::ostream& os);
virtual std::string getFileName() { return filename;}
private :
std::string filename;
std::string delim;
bool keep; // should we append or create a new file
bool header; // printing header at begin of file?
bool firstcall;
};
#endif

View file

@ -0,0 +1,93 @@
//-----------------------------------------------------------------------------
// (c) Marc Schoenauer, 2001
// Copyright (C) 2005 Jochen Küpper
/*
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: Marc.Schoenauer@polytechnique.fr
*/
//-----------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sstream>
#include <stdexcept>
#include "eoGnuplot.h"
unsigned eoGnuplot::numWindow=0;
eoGnuplot::eoGnuplot(std::string _title, std::string _extra)
: firstTime(true)
{
initGnuPlot(_title, _extra);
}
eoGnuplot::~eoGnuplot()
{
if( gpCom ) {
PipeComSend( gpCom, "quit\n" );
PipeComClose( gpCom );
gpCom =NULL;
}
}
void eoGnuplot::gnuplotCommand(const char *_command)
{
if(gpCom) {
PipeComSend( gpCom, _command );
PipeComSend( gpCom, "\n" );
}
}
void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
{
std::ostringstream os;
os << "250x150-0+" << numWindow*170;
numWindow++;
char *args[6];
args[0] = strdup( GNUPLOT_PROGRAM );
args[1] = strdup( "-geometry" );
args[2] = strdup( os.str().c_str());
args[3] = strdup( "-title" );
args[4] = strdup( _title.c_str() );
args[5] = 0;
gpCom = PipeComOpenArgv( GNUPLOT_PROGRAM, args );
if(! gpCom )
throw std::runtime_error("Cannot spawn gnuplot\n");
else {
PipeComSend( gpCom, "set grid\n" );
PipeComSend( gpCom, _extra.c_str() );
PipeComSend( gpCom, "\n" );
}
}
// Local Variables:
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -1,5 +1,3 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoGnuplot1DMonitor.h
// (c) Marc Schoenauer, 2001
@ -21,275 +19,81 @@
Contact: Marc.Schoenauer@polytechnique.fr
*/
//-----------------------------------------------------------------------------
#ifndef NO_GNUPLOT
#ifndef _eoGnuplot_H
#define _eoGnuplot_H
#ifndef EO_eoGnuplot_H
#define EO_eoGnuplot_H
#include <string>
/**
@author Marc Schoenauer 2001
@version 0.0
#include "pipecom.h"
This class is the abstract class that will be used by further gnuplot calls
to plots what is already written by some eoMonitor into a file
/** Base class for calls to gnuplot
This class is the abstract class that will be used by further gnuplot
calls to plots what is already written by some eoMonitor into a file
@author Marc Schoenauer
@version 0.0 (2001)
*/
//-----------------------------------------------------------------------------
#include <fstream>
#include <utils/pipecom.h>
/** eoGnuplot: base class that will be used to call gnuplot
*/
class eoGnuplot
{
public:
// Ctor
eoGnuplot(std::string _title, std::string _extra = std::string("")) :
firstTime(true)
{
// opens pipe with Gnuplot
initGnuPlot(_title, _extra);
}
public:
// Dtor
virtual ~eoGnuplot() {
// close - the gnuplot windows if pipe was correctly opened
if( gpCom ) {
PipeComSend( gpCom, "quit\n" ); // Ferme gnuplot
PipeComClose( gpCom );
gpCom =NULL;
}
}
/** Open pipe to Gnuplot.
/// Class name.
virtual std::string className() const { return "eoGnuplot"; }
@param _title Title for gnuplot window.
@param _extra Extra parameters to gnuplot.
*/
eoGnuplot(std::string _title, std::string _extra = std::string(""));
/** send a command to gnuplot directly
*/
void gnuplotCommand(std::string _command)
{
if( gpCom ) {
PipeComSend( gpCom, _command.c_str() );
PipeComSend( gpCom, "\n" );
}
}
/** Destructor
/** send a command to gnuplot directly
*/
void gnuplotCommand(char * _command)
{
if( gpCom ) {
PipeComSend( gpCom, _command );
PipeComSend( gpCom, "\n" );
}
}
Close the gnuplot windows if pipe was correctly opened
*/
virtual ~eoGnuplot();
/** Class name */
virtual std::string className() const
{ return "eoGnuplot"; }
/** Send command to gnuplot */
void gnuplotCommand(const char * _command);
/** Send command to gnuplot
@overload
*/
void gnuplotCommand(std::string _command)
{ gnuplotCommand(_command.c_str()); }
protected:
void initGnuPlot(std::string _title, std::string _extra);
// the private data
bool firstTime; // the stats might be unknown in Ctor
PCom *gpCom; // Communication with gnuplot OK
private:
/** Initialize gnuplot
@param _title Title for gnuplot window.
@param _extra Extra parameters to gnuplot.
*/
void initGnuPlot(std::string _title, std::string _extra);
/** The stats might be unknown in Ctor */
bool firstTime;
/** Communication with gnuplot OK */
PCom *gpCom;
/** Internal counter for gnuplot windows */
static unsigned numWindow;
};
// the following should be placed in a separate eoGnuplot.cpp
static unsigned numWindow=0;
////////////////////////////////////////////////////////////
inline void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
/////////////////////////////////////////////////////////
{
std::ostringstream os;
os << "250x150-0+" << numWindow*170;
numWindow++;
char *args[6];
args[0] = strdup( "gnuplot" );
args[1] = strdup( "-geometry" );
args[2] = strdup( os.str().c_str());
args[3] = strdup( "-title" );
args[4] = strdup( _title.c_str() );
args[5] = 0;
gpCom = PipeComOpenArgv( "gnuplot", args );
if( ! gpCom )
throw std::runtime_error("Impossible to spawn gnuplot\n");
else {
PipeComSend( gpCom, "set grid\n" );
PipeComSend( gpCom, _extra.c_str() );
PipeComSend( gpCom, "\n" );
}
}
// the following should be placed in a separate file pipecom.c
// together with the corresponding pipecom.h
// but first their MSC equivalent must be written and tested
// or some #idef instructions put with clear message at compile time
// that this is for Unix only ???
/* ----------------------------------------------------------------------
* Where........: CMAP - Polytechnique
* File.........: pipecom.c
* Author.......: Bertrand Lamy (Equipe genetique)
* Created......: Mon Mar 13 13:50:11 1995
* Description..: Communication par pipe bidirectionnel avec un autre process
*
* Ident........: $Id: eoGnuplot.h,v 1.13 2005-09-28 21:49:26 kuepper Exp $
* ----------------------------------------------------------------------
*/
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
// #include "pipecom.h"
inline int Check( PCom *com )
{
if( ! com ) {
fprintf( stderr, "PipeCom: Null pointer.\n" );
fflush( stderr );
return 0;
}
if( kill( com->pid, 0 ) != 0 ) {
fprintf( stderr, "PipeCom: process doesn't exists.\n" );
fflush( stderr );
return 0;
}
return 1;
}
inline PCom * PipeComOpen( char *prog )
{
char *args[2];
args[0] = prog;
args[1] = NULL;
return PipeComOpenArgv( prog, args );
}
inline PCom * PipeComOpenArgv( char *prog, char *argv[] )
{
int toFils[2];
int toPere[2];
int sonPid;
PCom * ret = NULL;
if( pipe( toFils ) < 0 ) {
perror( "PipeComOpen: Creating pipes" );
return ret;
}
if( pipe( toPere ) < 0 ) {
perror( "PipeComOpen: Creating pipes" );
return ret;
}
switch( (sonPid = vfork()) ) {
case -1:
perror("PipeComOpen: fork failed" );
return ret;
break;
case 0:
/* --- Here's the son --- */
/* --- replace old stdin --- */
if( dup2( toFils[0], fileno(stdin) ) < 0 ) {
perror( "PipeComOpen(son): could not connect" );
exit( -1 );
/* --- AVOIR: kill my father --- */
}
if( dup2( toPere[1], fileno(stdout) ) < 0 ) {
perror( "PipeComOpen(son): could not connect" );
exit( -1 );
}
if( execvp( prog, argv ) < 0 ) {
perror( prog );
perror( "PipeComOpen: can't exec" );
exit(1);
}
break;
default:
ret = (PCom *) malloc( sizeof(PCom) );
if( ! ret )
return NULL;
ret->fWrit = (FILE *)fdopen( toFils[1], "w" );
ret->fRead = (FILE *)fdopen( toPere[0], "r" );
ret->pid = sonPid;
}
return ret;
}
inline int PipeComSend( PCom *to, const char *line )
{
int nb = 0;
if( ! Check(to ) )
return nb;
nb = fprintf( to->fWrit, line );
fflush( to->fWrit );
return nb;
}
inline int PipeComSendn( PCom *to, const char *data, int n )
{
int nb = 0;
if( ! Check(to) )
return nb;
nb = fwrite( data, 1, n, to->fWrit );
fflush( to->fWrit );
return nb;
}
inline int PipeComReceive( PCom *from, char *data, int max )
{
if( ! Check(from) )
return 0;
if( ! data ) {
fprintf( stderr, "PipeComReceive: Invalid data pointer\n" );
fflush( stderr );
return 0;
}
if( fgets( data, max, from->fRead ) )
return strlen(data);
return 0;
}
#endif // EO_eoGnuplot_H
inline int PipeComClose( PCom *to )
{
if( ! Check(to) )
return 0;
fclose( to->fRead );
fclose( to->fWrit );
free( to );
return 1;
}
inline int PipeComWaitFor( PCom *from, char *what )
{
char buffer[256];
do {
if( ! PipeComReceive( from, buffer, 256 ) )
return 0;
} while( strcmp( buffer, what ) );
return 1;
}
#endif
#endif
// Local Variables:
// c-file-style: "Stroustrup"
// comment-column: 35
// fill-column: 80
// mode: C++
// End:

View file

@ -0,0 +1,82 @@
//-----------------------------------------------------------------------------
// eoGnuplot1DMonitor.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sstream>
#include "eoGnuplot1DMonitor.h"
#include "eoParam.h"
eoMonitor& eoGnuplot1DMonitor::operator() (void)
{
// update file using the eoFileMonitor
eoFileMonitor::operator()();
// sends plot order to gnuplot
// assumes successive plots will have same nb of columns!!!
if (firstTime)
{
FirstPlot();
firstTime = false;
}
else
{
if( gpCom ) {
PipeComSend( gpCom, "replot\n" );
}
}
return *this;
}
void eoGnuplot1DMonitor::FirstPlot()
{
if (vec.size() < 2)
{
throw std::runtime_error("Must have some stats to plot!\n");
}
std::ostringstream os;
os << "plot";
for (unsigned i=1; i<vec.size(); i++) {
os << " '" << getFileName().c_str() <<
"' using 1:" << i+1 << " title '" << (vec[i])->longName() << "' with lines" ;
if (i<vec.size()-1)
os << ", ";
}
os << '\n';
PipeComSend( gpCom, os.str().c_str());
}
// Local Variables:
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -23,100 +23,51 @@
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef NO_GNUPLOT
#ifndef _eoGnuplot1DMonitor_H
#define _eoGnuplot1DMonitor_H
#ifndef EO_eoGnuplot1DMonitor_H
#define EO_eoGnuplot1DMonitor_H
#include <fstream>
#include <string>
#include <utils/eoMonitor.h>
#include <utils/eoGnuplot.h>
#include <eoObject.h>
#include "eoObject.h"
#include "utils/eoFileMonitor.h"
#include "utils/eoGnuplot.h"
#include "utils/pipecom.h"
/**
@author Marc Schoenauer 2000
@version 0.0
/** Plot eoStat
@author Marc Schoenauer
@version 0.0 (2000)
This class plots through gnuplot the eoStat given as argument
eoGnuplot1DMonitor plots stats through gnuplot
Assumes that the same file is appened every so and so, and replots it
everytime
*/
//-----------------------------------------------------------------------------
#include <fstream>
#include <utils/pipecom.h>
/** eoGnuplot1DMonitor plots stats through gnuplot
* assumes that the same file is appened every so and so,
* and replots it everytime
*/
class eoGnuplot1DMonitor: public eoFileMonitor, public eoGnuplot
class eoGnuplot1DMonitor : public eoFileMonitor, public eoGnuplot
{
public:
// Ctor
eoGnuplot1DMonitor(std::string _filename, bool _top=false) :
eoFileMonitor(_filename, " "),
eoGnuplot(_filename,(_top?"":"set key bottom"))
{}
public:
using eoMonitor::vec;
// Dtor
virtual ~eoGnuplot1DMonitor(){}
/** Constructor */
eoGnuplot1DMonitor(std::string _filename, bool _top=false) :
eoFileMonitor(_filename, " "),
eoGnuplot(_filename,(_top?"":"set key bottom"))
{}
virtual eoMonitor& operator() (void) ;
virtual void FirstPlot();
/** Destructor */
virtual ~eoGnuplot1DMonitor(){}
/// Class name.
virtual std::string className() const { return "eoGnuplot1DMonitor"; }
virtual eoMonitor& operator()();
private:
virtual void FirstPlot();
/** Class name */
virtual std::string className() const
{ return "eoGnuplot1DMonitor"; }
};
// the following should be placed in a separate eoGnuplot1DMonitor.cpp
// then the inline specifier should dissappear
////////////////////////////////////////////////////////////
inline eoMonitor& eoGnuplot1DMonitor::operator() (void)
/////////////////////////////////////////////////////////
{
// update file using the eoFileMonitor
eoFileMonitor::operator()();
// sends plot order to gnuplot
// assumes successive plots will have same nb of columns!!!
if (firstTime)
{
FirstPlot();
firstTime = false;
}
else
{
if( gpCom ) {
PipeComSend( gpCom, "replot\n" );
}
}
return *this;
}
////////////////////////////////////////////////////////////
inline void eoGnuplot1DMonitor::FirstPlot()
////////////////////////////////////////////////////////
{
if (vec.size() < 2)
{
throw std::runtime_error("Must have some stats to plot!\n");
}
std::ostringstream os;
os << "plot";
for (unsigned i=1; i<vec.size(); i++) {
os << " '" << getFileName().c_str() <<
"' using 1:" << i+1 << " title '" << vec[i]->longName() << "' with lines" ;
if (i<vec.size()-1)
os << ", ";
}
os << '\n';
PipeComSend( gpCom, os.str().c_str());
}
#endif
#endif

View file

@ -0,0 +1,30 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "eoGnuplot1DSnapshot.h"
inline eoMonitor& eoGnuplot1DSnapshot::operator() (void)
{
// update file using the eoFileMonitor method
eoFileSnapshot::operator()();
// sends plot order to gnuplot
//std::string buff; // need local memory
std::ostringstream os;
os << "set title 'Gen. " << getCounter() << "'; plot '"
// mk: had to use getFilename().c_str(), because it seems the string(stream) lib is screwed in gcc3.2
<< getFileName().c_str() << "' notitle with points ps " << pointSize;
os << std::endl;
PipeComSend( gpCom, os.str().c_str());
return (*this);
}
// Local Variables:
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -23,36 +23,30 @@
mkeijzer@dhi.dk
*/
//-----------------------------------------------------------------------------
#ifndef NO_GNUPLOT
#ifndef _eoGnuplot1DSnapshot_H
#define _eoGnuplot1DSnapshot_H
#ifndef EO_eoGnuplot1DSnapshot_H
#define EO_eoGnuplot1DSnapshot_H
#include <fstream>
#include <sstream>
#include <string>
#include <eoObject.h>
#include "eoRealVectorBounds.h"
#include <utils/pipecom.h>
#include <utils/eoFileSnapshot.h>
#include <utils/eoGnuplot.h>
#include <eoObject.h>
/**
/** Plot stats through gnuplot
@author Marc Schoenauer 2000
@version 0.0
This class plots through gnuplot the eoStat given as argument
*/
//-----------------------------------------------------------------------------
#include <fstream>
#include "eoRealVectorBounds.h"
#include <utils/pipecom.h>
/** eoGnuplot1DSnapshot plots stats through gnuplot
* assumes that the same file is re-written every so and so,
* and plots it from scratch everytime it's called
Assumes that the same file is re-written every so and so, and plots it
from scratch everytime it's called
*/
class eoGnuplot1DSnapshot: public eoFileSnapshot, public eoGnuplot
{
@ -118,25 +112,5 @@ private:
};
// the following should be placed in a separate eoGnuplot1DMonitor.cpp
////////////////////////////////////////////////////////////
inline eoMonitor& eoGnuplot1DSnapshot::operator() (void)
/////////////////////////////////////////////////////////
{
// update file using the eoFileMonitor method
eoFileSnapshot::operator()();
// sends plot order to gnuplot
//std::string buff; // need local memory
std::ostringstream os;
os << "set title 'Gen. " << getCounter() << "'; plot '"
// mk: had to use getFilename().c_str(), because it seems the string(stream) lib is screwed in gcc3.2
<< getFileName().c_str() << "' notitle with points ps " << pointSize;
os << std::endl;
PipeComSend( gpCom, os.str().c_str());
return (*this);
}
#endif
#endif

173
eo/src/utils/pipecom.cpp Normal file
View file

@ -0,0 +1,173 @@
/* ----------------------------------------------------------------------
* Where........: CMAP - Polytechnique
* File.........: pipecom.c
* Author.......: Bertrand Lamy (Equipe genetique)
* Created......: Mon Mar 13 13:50:11 1995
* Description..: Communication par pipe bidirectionnel avec un autre process
* ----------------------------------------------------------------------
*/
// MSC equivalent must be written and tested or some #idef instructions added
// with a clear message at compile time that this is for Unix only ???
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstdlib>
#include <cstring>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "pipecom.h"
int Check( PCom *com )
{
if( ! com ) {
fprintf( stderr, "PipeCom: Null pointer.\n" );
fflush( stderr );
return 0;
}
if( kill( com->pid, 0 ) != 0 ) {
fprintf( stderr, "PipeCom: process doesn't exists.\n" );
fflush( stderr );
return 0;
}
return 1;
}
PCom * PipeComOpen( char *prog )
{
char *args[2];
args[0] = prog;
args[1] = NULL;
return PipeComOpenArgv( prog, args );
}
PCom * PipeComOpenArgv( char *prog, char *argv[] )
{
int toFils[2];
int toPere[2];
int sonPid;
PCom * ret = NULL;
if( pipe( toFils ) < 0 ) {
perror( "PipeComOpen: Creating pipes" );
return ret;
}
if( pipe( toPere ) < 0 ) {
perror( "PipeComOpen: Creating pipes" );
return ret;
}
switch( (sonPid = vfork()) ) {
case -1:
perror("PipeComOpen: fork failed" );
return ret;
break;
case 0:
/* --- Here's the son --- */
/* --- replace old stdin --- */
if( dup2( toFils[0], fileno(stdin) ) < 0 ) {
perror( "PipeComOpen(son): could not connect" );
exit( -1 );
/* --- AVOIR: kill my father --- */
}
if( dup2( toPere[1], fileno(stdout) ) < 0 ) {
perror( "PipeComOpen(son): could not connect" );
exit( -1 );
}
if( execvp( prog, argv ) < 0 ) {
perror( prog );
perror( "PipeComOpen: can't exec" );
exit(1);
}
break;
default:
ret = (PCom *) malloc( sizeof(PCom) );
if( ! ret )
return NULL;
ret->fWrit = (FILE *)fdopen( toFils[1], "w" );
ret->fRead = (FILE *)fdopen( toPere[0], "r" );
ret->pid = sonPid;
}
return ret;
}
int PipeComSend( PCom *to, const char *line )
{
int nb = 0;
if( ! Check(to ) )
return nb;
nb = fprintf( to->fWrit, line );
fflush( to->fWrit );
return nb;
}
int PipeComSendn( PCom *to, const char *data, int n )
{
int nb = 0;
if( ! Check(to) )
return nb;
nb = fwrite( data, 1, n, to->fWrit );
fflush( to->fWrit );
return nb;
}
int PipeComReceive( PCom *from, char *data, int max )
{
if( ! Check(from) )
return 0;
if( ! data ) {
fprintf( stderr, "PipeComReceive: Invalid data pointer\n" );
fflush( stderr );
return 0;
}
if( fgets( data, max, from->fRead ) )
return strlen(data);
return 0;
}
int PipeComClose( PCom *to )
{
if( ! Check(to) )
return 0;
fclose( to->fRead );
fclose( to->fWrit );
free( to );
return 1;
}
int PipeComWaitFor( PCom *from, char *what )
{
char buffer[256];
do {
if( ! PipeComReceive( from, buffer, 256 ) )
return 0;
} while( strcmp( buffer, what ) );
return 1;
}
// Local Variables:
// coding: iso-8859-1
// mode: C++
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -1,48 +1,47 @@
/* ----------------------------------------------------------------------
* Where........: CMAP - Polytechnique
* Where........: CMAP - Polytechnique
* File.........: pipecom.h
* Author.......: Bertrand Lamy (EEAAX)
* Created......: Thu Mar 9 17:21:15 1995
* Description..: Pipe communication with a process
*
* Ident........: $Id: pipecom.h,v 1.3 2001-10-08 09:13:16 evomarc Exp $
* ----------------------------------------------------------------------
*/
#ifndef PIPECOM_H
#define PIPECOM_H
// This file cannot be used from C any more due to some const additions.
// However, if you remove the const, it should work in C as well.
// this file cannot be used from C or C++ any more due to some const additions
// however, if you remove the const, it should work in C also
#ifdef __cplusplus
extern "C" {
#endif
#ifndef EO_PIPECOM_H
#define EO_PIPECOM_H
#include <stdio.h>
typedef struct PipeCommunication {
typedef struct PipeCommunication {
FILE *fWrit;
FILE *fRead;
int pid;
} PCom;
PCom *PipeComOpen( char *prog );
PCom *PipeComOpenArgv( char *prog, char *argv[] );
extern PCom *PipeComOpen( char *prog );
extern PCom *PipeComOpenArgv( char *prog, char *argv[] );
int PipeComSend( PCom *to, const char *line );
int PipeComSendn( PCom *to, const char *data, int n );
extern int PipeComSend( PCom *to, const char *line );
extern int PipeComSendn( PCom *to, const char *data, int n );
int PipeComReceive( PCom *from, char *data, int max );
extern int PipeComReceive( PCom *from, char *data, int max );
int PipeComClose( PCom *to );
extern int PipeComClose( PCom *to );
extern int PipeComWaitFor( PCom *from, char *what );
int PipeComWaitFor( PCom *from, char *what );
#ifdef __cplusplus
} /* ferme extern "C" */
#endif
#endif // EO_PIPECOM_H
#endif
// Local Variables:
// coding: iso-8859-1
// mode: C++
// c-file-style: "Stroustrup"
// fill-column: 80
// End: