#include "libscl.h" 

using namespace scl;
using namespace std;

int main(int argc, char** argp, char** envp)
{

  cerr << "This program also tests isREAL and isINTEGER" << '\n';
  cout << "This program also tests isREAL and isINTEGER" << '\n';

  const char* str[] = {
    "55.0",
    "55",
    "55.0   ",
    "55   ",
    "55.0  5  ",
    "55  5  ",
    "55.0e5",
    "55.0e-5",
    "-55.0",
    "-55",
    "  ",
    " ",
    ""
  };

  REAL x;
  INTEGER k;
  bool b;

  cout << boolalpha;

  for (INTEGER i=0; i<13; ++i) {
    const char** argi = &str[i];
    b = isREAL(*argi,x);
    cout << "for " << *argi << " isReal(s,v) returns "<< b <<' '<< x << '\n';
    b = isREAL(*argi);
    cout << "for " << *argi << " isReal(s) returns "<< b << '\n';
    b = isINTEGER(*argi,k);
    cout << "for " << *argi << " isINTEGER(s,v) returns "<< b <<' '<< k <<'\n';
    b = isINTEGER(*argi);
    cout << "for " << *argi << " isINTEGER(s) returns "<< b <<'\n';
  }

  vector<string> vs(13);
  vs[0] = "55.0";
  vs[1] = "55";
  vs[2] = "55.0   ";
  vs[3] = "55   ";
  vs[4] = "55.0  5  ";
  vs[5] = "55  5  ";
  vs[6] = "55.0e5";
  vs[7] = "55.0e-5";
  vs[8] = "-55.0";
  vs[9] = "-55";
  vs[10] = "  ";
  vs[11] = " ";
  vs[12] = "";

  for (INTEGER i=0; i<13; ++i) {
    b = isREAL(vs[i],x);
    cout << "for " << vs[i] << " isReal(s,v) returns "<< b <<' '<< x << '\n';
    b = isREAL(vs[i]);
    cout << "for " << vs[i] << " isReal(s) returns "<< b << '\n';
    b = isINTEGER(vs[i],k);
    cout << "for " << vs[i] << " isINTEGER(s,v) returns "<< b <<' '<< k <<'\n';
    b = isINTEGER(vs[i]);
    cout << "for " << vs[i] << " isINTEGER(s) returns "<< b <<'\n';
  }

  typedef vector<string>::size_type vec_sz_type;

  bool is_header_line = true;

  const char* filename = "epa.csv";
  //char * filename = "smge.csv";

  vector<string> string_names;
  vector<string> numeric_names;
  vector<string> unknown_names;

  vector< vector<string> > strings;

  realmat X;

  INTEGER rv = csvread(filename, is_header_line,
    string_names, strings, numeric_names, X, unknown_names);
  
  vec_sz_type string_count = string_names.size();
  vec_sz_type numeric_count = numeric_names.size();
  vec_sz_type unknown_count = unknown_names.size();

  vec_sz_type n = 0;

  if (string_count > 0) { 
    n = strings[0].size();
    cout << starbox("/Strings//");
    for (vec_sz_type i=0; i<string_count; ++i) {
      cout << string_names[i] << '\n';
    }
    cout << '\n';
    for (vec_sz_type t=0; t<n; ++t) {
      cout << "| ";
      for (vec_sz_type i=0; i<string_count; ++i) {
        cout << strings[i][t] << " | ";
      }
      cout << '\n';
    }
  }

  if (numeric_count > 0) {
    n = X.nrow();
    cout << starbox("/Numerics//");
    for (vec_sz_type i=0; i<numeric_count; ++i) {
      cout << numeric_names[i] << '\n';
    }
    cout << '\n';
    cout << X;
  }

  if (unknown_count > 0) {
    cout << starbox("/Unknowns//");
    for (vec_sz_type i=0; i<unknown_count; ++i) {
      cout << string_names[i] << '\n';
    }
    cout << '\n';
  }

  cout << '\n';
  cout << "csvread returned " << rv << '\n';

  filename = "epa_no_header.csv";

  is_header_line = false;

  rv = csvread(filename, is_header_line,
    string_names, strings, numeric_names, X, unknown_names);
  
  string_count = string_names.size();
  numeric_count = numeric_names.size();
  unknown_count = unknown_names.size();

  n = 0;

  if (string_count > 0) { 
    n = strings[0].size();
    cout << starbox("/Strings//");
    for (vec_sz_type i=0; i<string_count; ++i) {
      cout << string_names[i] << '\n';
    }
    cout << '\n';
    for (vec_sz_type t=0; t<n; ++t) {
      cout << "| ";
      for (vec_sz_type i=0; i<string_count; ++i) {
        cout << strings[i][t] << " | ";
      }
      cout << '\n';
    }
  }

  if (numeric_count > 0) {
    n = X.nrow();
    cout << starbox("/Numerics//");
    for (vec_sz_type i=0; i<numeric_count; ++i) {
      cout << numeric_names[i] << '\n';
    }
    cout << '\n';
    cout << X;
  }

  if (unknown_count > 0) {
    cout << starbox("/Unknowns//");
    for (vec_sz_type i=0; i<unknown_count; ++i) {
      cout << string_names[i] << '\n';
    }
    cout << '\n';
  }

  cout << '\n';
  cout << "csvread returned " << rv << '\n';

  filename = "smge.csv";

  is_header_line = true;

  rv = csvread(filename, is_header_line,
    string_names, strings, numeric_names, X, unknown_names);
  
  string_count = string_names.size();
  numeric_count = numeric_names.size();
  unknown_count = unknown_names.size();

  n = 0;

  if (string_count > 0) { 
    n = strings[0].size();
    cout << starbox("/Strings//");
    for (vec_sz_type i=0; i<string_count; ++i) {
      cout << string_names[i] << '\n';
    }
    cout << '\n';
    for (vec_sz_type t=0; t<n; ++t) {
      cout << "| ";
      for (vec_sz_type i=0; i<string_count; ++i) {
        cout << strings[i][t] << " | ";
      }
      cout << '\n';
    }
  }

  if (numeric_count > 0) {
    n = X.nrow();
    cout << starbox("/Numerics//");
    for (vec_sz_type i=0; i<numeric_count; ++i) {
      cout << numeric_names[i] << '\n';
    }
    cout << '\n';
    cout << X;
  }

  if (unknown_count > 0) {
    cout << starbox("/Unknowns//");
    for (vec_sz_type i=0; i<unknown_count; ++i) {
      cout << string_names[i] << '\n';
    }
    cout << '\n';
  }

  cout << '\n';
  cout << "csvread returned " << rv << '\n';

  return 0;
}
