// ---------------------------------------------------------------------------
// - InputStream.cpp                                                         -
// - standard object library - input stream class implementation             -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  is  distributed in  the hope  that it will be useful, but -
// - without  any  warranty;  without  even   the   implied    warranty   of -
// - merchantability or fitness for a particular purpose.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2011 amaury darsch                                   -
// ---------------------------------------------------------------------------

#include "Byte.hpp"
#include "Ascii.hpp"
#include "Vector.hpp"
#include "Unicode.hpp"
#include "Boolean.hpp"
#include "Integer.hpp"
#include "Character.hpp"
#include "QuarkZone.hpp"
#include "Exception.hpp"
#include "InputStream.hpp"

namespace afnix {

  // -------------------------------------------------------------------------
  // - class section                                                         -
  // -------------------------------------------------------------------------

  // flush the input stream until a character is reached

  bool InputStream::flush (const char value) {
    wrlock ();
    bool result = false;
    try {
      while (valid () == true) {
	if (read () == value) {
	  result = true;
	  break;
	}
      }
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // flush the input stream until a character is reached

  bool InputStream::flush (const t_quad value) {
    wrlock ();
    bool result = false;
    try {
      while (valid () == true) {
	if (getu () == value) {
	  result = true;
	  break;
	}
      }
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // copy an input stream into a buffer

  long InputStream::copy (char* rbuf, const long size) {
    // check argument first
    if ((rbuf == nilp) || (size <= 0)) return 0;
    // lock and fill
    wrlock ();
    try {
      // initialize result
      long result = 0;
      // loop while valid
      for (long i = 0; i < size; i++) {
	if (valid () == false) break;
	rbuf[i] = read ();
	result++;
      }
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // read the next quad byte

  t_quad InputStream::getb (void) {
    wrlock ();
    try {
      // check for timeout
      if (valid () == false) {
	if (iseos () == true) {
	  t_quad result = encode (eosq);
	  unlock ();
	  return result;
	}
	throw Exception ("read-error", "read byte character timeout");
      }
      // read and encode character
      t_quad result = encode (read ());
      unlock ();
      return result;
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // read the next quad unicode character

  t_quad InputStream::getc (void) {
    wrlock ();
    try {
      // the utf byte buffer
      char ubuf[Unicode::MAX_UTF8_SIZE];
      // the read status
      bool status = true;
      // loop until full
      for (long i = 0; i < Unicode::MAX_UTF8_SIZE; i++) {
	// check for timeout
	if (valid () == false) {
	  if ((iseos () == true) && (status == true)) {
	    unlock ();
	    return eosq;
	  }
	  throw Exception ("read-error", "read unicode character timeout");
	}
	// read the character
	ubuf[i] = read ();
	// check for validity
	status = Unicode::valid (Encoding::UTF8, ubuf, i+1);
	if (status == true) {
	  t_quad result = Unicode::decode (ubuf);
	  unlock ();
	  return result;
	}
      }
      // invalid character read
      throw Exception ("read-error", "cannot read unicode character");
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // read the next unicode character

  t_quad InputStream::getu (void) {
    wrlock ();
    try {
      // process byte mode
      if (d_emod == Encoding::BYTE) {
	t_quad result = getb ();
	unlock ();
	return result;
      }
      // process utf mode
      if (d_emod == Encoding::UTF8) {
	t_quad result = getc ();
	unlock ();
	return result;
      }
      // internal error
      throw Exception ("internal-error", "unicode character read mode");
    } catch (...) {
      unlock ();
      throw;
    }
  }
  
  // read a buffer by size

  Buffer* InputStream::read (const long size) {
    wrlock ();
    Buffer* result = new Buffer;
    try {
      for (long i = 0; i < size; i++) {
	if (valid () == false) break;
	result->add (read ());
      }
      unlock ();
      return result;
    } catch (...) {
      delete result;
      unlock ();
      throw;
    }
  }

  // read a line from this input stream

  String InputStream::readln (void) {
    wrlock ();
    try {
      // create a buffer to accumulate characters
      Buffer buf;
      bool   flg = false;
      // read the character in the buffer
      while (valid () == true) {
	t_quad c = getu ();
	if (c == crlq) {
	  flg = true;
	  continue;
	}
	if (c == eolq) break;
	if (flg == true) {
	  buf.add (crlq);
	  flg = false;
	}
	buf.add (c);
      }
      unlock ();
      return buf.tostring ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // pushback a string on this stream
  
  long InputStream::pushback (const String& s) {
    wrlock ();
    try {
      // check for byte mode
      if (d_emod == Encoding::BYTE) {
	// get the quad buffer
	t_quad* sbuf = s.toquad ();
	if (sbuf == nilp) {
	  unlock ();
	  return 0;
	}
	// transcode to the character buffer
	char* cbuf = decode (sbuf);
	delete [] sbuf;
	// write the buffer and clean
	long result = pushback (cbuf);
	delete [] cbuf;
	unlock ();
	return result;
      }
      // check for utf-8 mode
      if (d_emod == Encoding::UTF8) {
	char* cbuf = Unicode::encode (d_emod, s);
	if (cbuf == nilp) {
	  unlock ();
	  return 0;
	}
	// write string content
	long result = pushback (cbuf);
	delete [] cbuf;
	unlock ();
	return result;
      }
      throw Exception ("pushback-error", "invalid mode for string pushback");
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // -------------------------------------------------------------------------
  // - object section                                                        -
  // -------------------------------------------------------------------------

  // the quark zone
  static const long QUARK_ZONE_LENGTH = 11;
  static QuarkZone  zone (QUARK_ZONE_LENGTH);

  // the object supported quarks
  static const long QUARK_EOSP     = zone.intern ("eos-p");
  static const long QUARK_READ     = zone.intern ("read");
  static const long QUARK_GETU     = zone.intern ("getu");
  static const long QUARK_FLUSH    = zone.intern ("flush");
  static const long QUARK_PUSHB    = zone.intern ("pushback");
  static const long QUARK_VALIDP   = zone.intern ("valid-p");
  static const long QUARK_READLN   = zone.intern ("readln");
  static const long QUARK_GETOUT   = zone.intern ("get-timeout");
  static const long QUARK_SETOUT   = zone.intern ("set-timeout");
  static const long QUARK_BUFLEN   = zone.intern ("get-buffer-length");
  static const long QUARK_TOSTRING = zone.intern ("to-string");

  // return true if the given quark is defined

  bool InputStream::isquark (const long quark, const bool hflg) const {
    rdlock ();
    if (zone.exists (quark) == true) {
      unlock ();
      return true;
    }
    bool result = hflg ? Stream::isquark (quark, hflg) : false;
    unlock ();
    return result;
  }

  // apply this object with a set of arguments and a quark

  Object* InputStream::apply (Runnable* robj, Nameset* nset, const long quark,
			      Vector* argv) {
    // get the number of arguments
    long argc = (argv == nilp) ? 0 : argv->length ();
    
    // dispatch 0 argument
    if (argc == 0) {
      if (quark == QUARK_READ)     return new Byte      (read     ());
      if (quark == QUARK_GETU)     return new Character (getu     ());
      if (quark == QUARK_EOSP)     return new Boolean   (iseos    ());
      if (quark == QUARK_VALIDP)   return new Boolean   (valid    ());
      if (quark == QUARK_READLN)   return new String    (readln   ());
      if (quark == QUARK_GETOUT)   return new Integer   (gettout  ());
      if (quark == QUARK_BUFLEN)   return new Integer   (buflen   ());
      if (quark == QUARK_TOSTRING) return new String    (tostring ());
      if (quark == QUARK_FLUSH) {
	flush ();
	return nilp;
      }
    }
    // dispatch 1 argument
    if (argc == 1) {
     if (quark == QUARK_FLUSH) {
	t_quad c = argv->getchar (0);
	return new Boolean (flush (c));
      }
      if (quark == QUARK_SETOUT) {
	long tout = argv->getlong (0);
	settout (tout);
	return nilp;
      }

      if (quark == QUARK_READ) {
	long size = argv->getlong (0);
	return read (size);
      }
      if (quark == QUARK_PUSHB) {
	Object* obj = argv->get (0);
	// check for a byte
	Byte* bobj = dynamic_cast <Byte*> (obj);
	if (bobj != nilp) {
	  long result = pushback ((char) bobj->tobyte ());
	  return new Integer (result);
	}
	// check for a character
	Character* cobj = dynamic_cast <Character*> (obj);
	if (cobj != nilp) {
	  long result = pushback (cobj->toquad ());
	  return new Integer (result);
	}
	String* s = dynamic_cast <String*> (obj);
	if (s != nilp) {
	  long result = pushback (*s);
	  return new Integer (result);
	}
	throw Exception ("type-error", "invalid object with pushback method",
			 Object::repr (obj));
      }
    }
    // apply these arguments with the stream object
    return Stream::apply (robj, nset, quark, argv);
  }
}
