package nxm.ucl.prim;

import nxm.sys.lib.BaseFile;
import nxm.sys.lib.DataFile;
import nxm.sys.lib.Primitive;
import nxm.sys.lib.Table;

/**
  put3000 - Modifies or appends a record in a type-3000 file

  &lt;file&gt;   - Input type-3000 file to modify
  &lt;table&gt;  - Name of table containing new values
  &lt;offset&gt; - Offset of Record in file to insert (0..size)

  Modifies or appends a record to a type-3000 file given a Table
  with entries matching the file record names.

  @author John Lannon/Earl Ferrara
*/
public class put3000 extends Primitive {

  DataFile hcbInOut;
  double size;
  Table table;
  double offset;

  /** default constructor */
  public put3000 () {}

  public int open () {

    // Open type-3000 file for INPUT/OUTPUT
    hcbInOut = MA.getDataFile("FILE");
    hcbInOut.open(BaseFile.INOUT);
    if (!hcbInOut.isOpen()) {
      M.warning ("'" + hcbInOut.getFileName().getFullName() + "' does not exist");
      return FINISH;
    } else if (hcbInOut.typeClass != 3) {
      M.error ("File must be type 3000!");
      return FINISH;
    }

    // Get parameters
    table = MA.getTable("TABLE");
    offset = MA.getD("OFFSET", 0.0);

    return NORMAL;
  }

  public int process () {
    int nelem = hcbInOut.setData(offset, table);
    return FINISH;
  }

  public int close () {
    // JRL Workaround we should not need
    //if (hcbOut.isOpen()) {hcbOut.seek(size); hcbOut.close();}
    if (hcbInOut.isOpen()) {hcbInOut.close();}
    return NORMAL;
  }

}
