/* $Id: utils.h,v 1.3 1996/10/11 11:30:12 roitzsch Exp $ */

#ifndef UTILS_H
#define UTILS_H

#include <stddef.h>  
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

#include "kvector.h"
#include "matrix.h"
#include "numerics.h"

//-------------------------------------------------------------------------

class  MultiCompNode;
extern MultiCompNode MCNode;

//-------------------------------------------------------------------------

#define FORNCOMP(n)  for (n=1; n<=nComp; ++n) 

class MultiCompNode
{
  public:
    int block(int node, int nComp)   { return (node-1)/nComp + 1; }
    int comp (int node, int nComp)   { return (node-1)%nComp + 1; }
    int node (int block, int comp, int nComp) { return nComp*(block-1) + comp; }
};
//-------------------------------------------------------------------------

void strToLower(char* s);
void strToUpper(char *s);
char *Form(const char *format ...);

//-------------------------------------------------------------------------


class Parser
{

  public:

    enum State    { ok=1, end=0, eof= -1 };
    enum Position { beginning=0, current=1, endOfFile=2 }; 

    Parser (const char* file, const char* endToken = "end", 
	    const char CommentFlag = '#');
    virtual ~Parser();

    int state() const { return status; }

    virtual int getString(char* s);
    virtual int getValue(char* c);
    virtual int getValue(int* i);
    virtual int getValue(float* x);
    virtual int getValue(double* x);

    virtual int getLine(char* line);
    virtual int skipRestOfLine();

    int findWord(const char* target, int completeMatch=0);
    int findChar(const char target);

    virtual void rewind();
    virtual long ftell();
    virtual int  fseek(long offset, int mode=beginning); 

    virtual char getChar();		// should not be used

  protected:

    enum { WORDLENGTH=256 };

    State  status;
    char*  endToken;
    char   commentFlag;
    char*  fileName;
    FILE*  fp;

    void readError(const char* function, char* word);
};
//-------------------------------------------------------------------------


class BufferedParser : public Parser
{
  protected:

    char *buffer, *pos, *posBefore;
    long  bufferLength;

  public:

    BufferedParser(const char* file, const char* endToken = "end", 
		   const char CommentFlag = '#');
    virtual ~BufferedParser();

    virtual int getString(char* s);

    int getSpecValue(char* c);
    virtual int getValue(char* c);
    virtual int getValue(int* i);
    virtual int getValue(float* x);
    virtual int getValue(double* x);

    virtual int getLine(char* line);
    virtual int skipRestOfLine();

    virtual void rewind();
    virtual long ftell();
    virtual int  fseek(long offset, int mode=beginning); 

    virtual char getChar();		// should not be used
};
//-------------------------------------------------------------------------

#include <time.h>

class Timer 
{
    double t0;
    time_t  abst0;

  public:

    Timer();
    float cpu  (int print=1);		// cpu time in seconds
    long  total(int print=1);		// total elapsed time in seconds
};
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
/*
class charbuf : public streambuf
{ 
  public:

    const int len;
    char *buf;

    int overflow(int c=EOF) { 
	// printf("\n *** overflow in charbuf "); 
	return EOF;
    }
    int underflow() { return EOF; }

    streambuf* setbuf(char* p, int len0) { setp(p,p+len0-1); return this; }
    
  public:

    charbuf (int size): len(size), buf(new char[size]) 
      				{ setbuf(buf,len); buf[0]='\0'; }
    ~charbuf() { delete buf; }
    int full() { return  this->pptr() >= this->epptr(); }
};
*/
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------


void invert(Matrix<Real>& A, Matrix<Real>& AInv);
void Decompose(Matrix<Real>& A, Vector<int>& index);
void FBSubst(Matrix<Real>& A, Vector<int>& index, Vector<Real>& x);

void invert(Matrix<Complex>& A, Matrix<Complex>& AInv);
void Decompose(Matrix<Complex>& A, Vector<int>& index);
void FBSubst(Matrix<Complex>& A, Vector<int>& index, Vector<Complex>& x);

#endif
