package nxm.train.prim;

import nxm.sys.inc.*;
import nxm.sys.lib.*;

/** A lite version of noop. */
public class primlab4 extends Primitive {
  private DataFile inFile;      // The input file
  private DataFile outFile;     // The output file
  private Data     dataBuffer;  // The data buffer

  public int open() {
    inFile = MA.getDataFile("IN");
    inFile.open();
    
    outFile = MA.getDataFile("OUT", inFile, 0);
    outFile.open();
    
    int xfer = MA.getL("/TL", 1024);
    dataBuffer = inFile.getDataBuffer(xfer);
    
    return NORMAL;
  }
  
  public int process() {
    int status  = NORMAL;
    int numRead = inFile.read(dataBuffer);
    
    if (numRead < 0) {
      status = FINISH; // End of file
    } else if (numRead == 0) {
      status = NOOP;   // No data available, wait for pipe.
    } else {
      outFile.write(dataBuffer, numRead);
    }
    
    return status;
  }

  public int close() {
    inFile.close();
    outFile.close();

    return NORMAL;
  }
}
