/////////////////////////////////////////////////////////////////////////
//
// Name:    CellularAutomata2D.C
//
// Purpose: Abstract base class for 2-dimensional lattice
//          cellular automata models.
//
/////////////////////////////////////////////////////////////////////////

#include <stdlib.h>
#include <fstream.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "CellularAutomata2D.h"
#include "Plot.h"



// type for file names
typedef char    FileName_t[BUFSIZ];




//////////////////////////////////////////////////////////////////
//
//  CellularAutomata2D::PlotData
//      Makes a raw input data file for use by gnuplot. This
//      raw data describes the current contents in the 2D
//      lattice.
//
//  Input:
//      None
//
//  Output:
//      None
//
//  Returns:
//      void
//
//////////////////////////////////////////////////////////////////
void
CellularAutomata2D::PlotData() const
{
    //
    // Before doing any of this, make sure that the FIFO
    // exists
    //
    if (access(DATA_FIFO, F_OK) != 0) {
        perror(DATA_FIFO);
        exit(3);

    } else {

        //
        // Create the raw data file and transfer
        // it to the named pipe (fifo) with a UNIX cp
        // Note that this will "block" (i.e., wait) for
        // the gnuplot process to finish reading data
        // from the previous iteration which was sent to
        // the same fifo. This will slow things down, but
        // it guarantees that crystallize and gnuplot will
        // be in sync.
        //
        FileName_t  plotFileName;   // Name of raw plot data
        char        sysCmd[BUFSIZ]; // Buffer for system() call

        //
        // Create unique file name for this plot data
        //
        sprintf(plotFileName, ".gpdata.%d.%d", getpid(), iters);

        ofstream plotFile(plotFileName);    // raw gnuplot input data

        //
        // Send the current lattice contents to a data
        // file for input to gnuplot
        //
        plotFile << *this;
        plotFile.close();

        //
        // Copy the contents of the data file to the FIFO
        // so that gnuplot will display it.
        //
        sprintf(sysCmd, "cp %s %s", plotFileName, DATA_FIFO);
        system(sysCmd);
        remove(plotFileName);
    }
}




//////////////////////////////////////////////////////////////////
//
//  operator<< (overloaded for CellularAutomata2D)
//      For each cell in C's current lattice, if state is ALIVE,
//      print the row & col of the cell, separated by a space.
//
//  Input:
//      stream: the ostream object
//      C:      the CellularAutomata2D object
//
//  Output:
//      stream: the ostream object
//
//  Returns:
//      reference to ostream
//
//////////////////////////////////////////////////////////////////
ostream&
operator<<(
ostream&                    stream,
const CellularAutomata2D&   C)
{
    Dimension_t i, j;
    for (i=0; i<C.Rows(); i++) {
        for (j=0; j<C.Columns(); j++) {
            if (C.latticeArr[C.curr](i, j).isAlive()) {
                stream << i << ' ' << j << endl;
            }
        }
    }
    return stream;
}
