package nxm.train.prim;

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

/** A lite version of FILE COPY/L. */
public class primlab5 extends Primitive {
  private ListFile inFile;      // The input file
  private ListFile outFile;     // The output file
  private long     rowNum;      // The current row number.

  public int open() {
    BaseFile in  = MA.getFile("IN");
    BaseFile out = MA.getFile("OUT");
    rowNum = 0L;
    
    if (in instanceof ListFile) {
      inFile = (ListFile)in;
      inFile.open(BaseFile.INPUT);
    } else {
      throw new MidasException("Error IN="+in+" is not an instance of ListFile.");
    }
    
    if (out instanceof ListFile) {
      outFile = (ListFile)out;
      outFile.open(BaseFile.OUTPUT);
    } else {
      throw new MidasException("Error OUT="+out+" is not an instance of ListFile.");
    }
    
    return NORMAL;
  }
  
  public int process() {
    int status  = NORMAL;
    
    if (rowNum >= inFile.getNumberOfRows()) {
      status = FINISH; // End of file
    } else {
      Table row = inFile.getDataTable((double)rowNum);
      outFile.insertData(-1.0, row);
      rowNum++;
    }
    
    return status;
  }

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

    return NORMAL;
  }
}
