#include "scltypes.h"
#include "realmat.h"
#include "sclfuncs.h"

using namespace std;
using namespace scl;

LIB_ERROR_HANDLER_PTR previous; 

void do_not_exit (string msg)
{
  throw lib_error(msg);  
}

int main()
{

  const INTEGER rr = 25;
  const INTEGER cc = 6;

  INTEGER i,j;

  realmat a;

  a.resize(rr,cc,1);

  static INT_32BIT seed = 987507;

  for(i=1; i<=rr; i++)
  for(j=1; j<=cc; j++)
    a(i,j)=REAL(i*j*i*j)*ran(&seed);

  cout << starbox("/a has random elements//_");
  cout << a << '\n';

  ofstream outnrcfile("tstvecrw.nrc");
  std::streamsize old_precision = outnrcfile.precision(REAL_DIG+1);
  outnrcfile.setf(std::ios::scientific,std::ios::floatfield);
  if (!outnrcfile) error("Error, main, cannot open tstvecrw.nrc for output");
  for(INTEGER i=1; i<=a.size(); i++) outnrcfile << a[i] << '\n';
  outnrcfile.precision(old_precision);
  outnrcfile.setf(std::ios::fmtflags(0),std::ios::floatfield);
  outnrcfile.close();

  INTEGER length;

  realmat b;

  cout << starbox("/write and read to tstvecrw.tmp//_");
  length = vecwrite("tstvecrw.tmp",a);
  cout << "\n     vecwrite returned " << length << '\n';
  length = vecread("tstvecrw.tmp",b);
  cout << "\n     vecread returned " << length << '\n';
  if (a != b) error("Error, tstvecrw, write or read failed, #1");
  length = vecread("tstvecrw.nrc",b,rr,cc);
  cout << "\n     vecread returned " << length << '\n';
  if (a != b) error("Error, tstvecrw, write or read failed, #2");

  cout << starbox("/length = vecwrite(cout,a)//_");
  length = vecwrite(cout,a);
  cout << "\n     vecwrite returned " << length << '\n';

  ofstream outdatfile("tstvecrw.dat");
  if (!outdatfile) error("Error, main, cannot open tstvecrw.dat for output");
  cout << starbox("/length = vecwrite(outdatfile,a)//_");
  length = vecwrite(outdatfile, a);
  cout << "\n     vecwrite returned " << length << '\n';
  outdatfile.close();

  ifstream datfile("tstvecrw.dat");
  if (!datfile) error("Error, main, cannot open tstvecrw.dat for input");
  cout << starbox("/length = vecread(datfile,a)//_");
  length = vecread(datfile,a);
  cout << "\n     vecread returned " << length << '\n';
  datfile.close();
  cout << starbox("/datfile/input a//_");
  cout << a;

  ifstream nrcfile("tstvecrw.nrc");
  if (!nrcfile) error("Error, main, cannot open tstvecrw.nrc for input");
  cout << starbox("/length = vecread(nrcfile,a,rr,cc)//_");
  length = vecread(nrcfile,a,rr,cc);
  cout << "\n     vecread returned " << length << '\n';  
  nrcfile.close();
  cout << starbox("/nrcfile/input a//_"); 
  cout << a; 

  ofstream chkfile("tstvecrw.chk");
  if (!chkfile) error("Error, main, cannot open tstvecrw.chk for output");

  cout << starbox("/length = vecwrite(chkfile,a)//_");
  length = vecwrite(chkfile,a);
  cout << "\n     vecwrite returned " << length << '\n';
  chkfile.close();

  ifstream badfile("tstvecrw.bad");
  if (!badfile) error("Error, main, cannot open tstvecrw.bad for input");

  cout << starbox("/length = vecread(badfile,a)//_");
  length = vecread(badfile,a);
  cout << "\n     vecwread returned " << length << '\n';
  badfile.close();

  cout << a;

  return 0;

}







