//
//  StringRep.h
//
//  Qdos - String representation of Qd*_var objects
//
//  David Burgess
//  May 2000
//

#ifndef STRINGREP_H
#define STRINGREP_H

#ifdef HAVE_SSTREAM_H
#include <sstream>
#else
#include <strstream>
#endif
#include <string>
#include <complex>
#include "Std.h"
#include "Var.h"

namespace QDOS {

class Dim;
class QdObject;
typedef Var<QdObject> QdObject_var;

// Utility functions

inline string upper_case(const string& s)
{
  string res(s);
  int sz=res.size();
  for(int i=0;i<sz;i++)
   res[i]=toupper(res[i]);
  return res;
}
    
inline int string_to_int( const string& s )
{
  int i=0;
  istrstream ist(s.c_str());
  ist >> i;
  return i;
}

inline string int_to_string( int i )
{
  char b[50]; memset(b,0,50);
  #ifdef HAVE_SSTREAM_H
  ostrstream ost( b );
  #else
  ostrstream ost( b,50);
  #endif
  ost<<i;
  return ost.str();
}

inline double string_to_double( const string& s )
{
  double d;
  istrstream ist(s.c_str());
  ist >> d;
  return d;
}

inline string double_to_string( double x )
{
  char b[50]; memset(b,0,50);
  #ifdef HAVE_SSTREAM_H
  ostrstream ost( b );
  #else
  ostrstream ost( b,50);
  #endif
  ost.setf(ios::showpoint);
  ost<<x;
  ost.unsetf(ios::showpoint);
  return ost.str();
}

inline double_complex string_to_complex( const string& s )
{
  double_complex c;
  istrstream ist(s.c_str());
  ist >> c;
  return c;
}

inline string complex_to_string( double_complex x )
{
  char b[50]; memset(b,0,50);
  #ifdef HAVE_SSTREAM_H
  ostrstream ost( b );
  #else
  ostrstream ost( b,50);
  #endif
  ost<<x;
  return ost.str();
}

class StringRep {

public:
  typedef enum TokenType_e {
   SR_TOKEN_SEPARATOR,
   SR_TOKEN_STRING_DELIMITER,
   SR_TOKEN_BEGIN_SEQ,
   SR_TOKEN_END_SEQ,
   SR_TOKEN_BEGIN_DIMS,
   SR_TOKEN_END_DIMS,
   SR_TOKEN_UNKNOWN_DIMS,
   SR_TOKEN_DIM_SEPARATOR,
   SR_TOKEN_BEGIN_MATRIX,
   SR_TOKEN_END_MATRIX,
   SR_TOKEN_TIME_SEPARATOR,
   SR_TOKEN_TIME_TRAILING_CHAR
    } TokenType;

private:
  string token_separator;
  string token_string_delimiter;
  string token_begin_seq;
  string token_end_seq;
  string token_begin_dims;
  string token_end_dims;
  string token_unknown_dims;
  string token_dim_separator;
  string token_begin_matrix;
  string token_end_matrix;
  string token_time_separator;
  string token_time_trailing_char;

public:
  StringRep(void)
  : token_string_delimiter( "\""),
    token_separator(", "), token_begin_seq("{ "), token_end_seq(" }"),
    token_begin_dims("["), token_end_dims("]"),
    token_unknown_dims("*"), token_dim_separator(","),
    token_begin_matrix("{ "), token_end_matrix(" }"),
    token_time_separator(" "), token_time_trailing_char("")
    {;}

  string operator()( QdObject_var obj, int seqi=0, int seqn=-1 );
  string object_type( QdObject_var obj );
  void set_token( TokenType token_type, const string& tok );
  string get_token( TokenType token_type) const;
  string matrix_dims_to_string( const Dim& d ) const;
};

} // end namespace QDOS

#endif // #ifndef STRINGREP_H

