// ---------------------------------------------------------------------------
// - Stream.cpp                                                              -
// - standard object library - base 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-2008 amaury darsch                                   -
// ---------------------------------------------------------------------------

#include "Item.hpp"
#include "Stream.hpp"
#include "Vector.hpp"
#include "QuarkZone.hpp"
#include "Exception.hpp"

namespace afnix {

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

  // create a default stream

  Stream::Stream (void) {
    d_emod = System::BYTE;
  }

  // create a stream by coding mode

  Stream::Stream (const System::t_emod emod) {
    d_emod = emod;
  }

  // return the stream encoding mode

  System::t_emod Stream::getemod (void) const {
    rdlock ();
    System::t_emod result = d_emod;
    unlock ();
    return result;
  }

  // set the stream encoding mode

  void Stream::setemod (const System::t_emod emod) {
    wrlock ();
    // save mode
    d_emod = emod;
    // reset transcoder mode
    switch (emod) {
    case System::UTF8:
      settmod (System::DEFAULT);
      break;
    default:
      break;
    }
    unlock ();
  }

  // set the stream encoding mode by name

  void Stream::setemod (const String& mode) {
    // check for mode first
    if (mode.isnil () == true) return;
    // compute encoding and transcoder mode
    System::t_emod emod = System::toemod (mode);
    System::t_tmod tmod = System::totmod (mode);
    // set the modes
    wrlock ();
    try {
      settmod (tmod);
      setemod (emod);
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // -------------------------------------------------------------------------
  // - object section                                                        -
  // -------------------------------------------------------------------------
  
  // the object eval quarks
  static const long QUARK_BYTE    = String::intern ("BYTE");
  static const long QUARK_UTF8    = String::intern ("UTF-8");
  static const long QUARK_STREAM  = String::intern ("Stream");

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

  // the object supported quarks
  static const long QUARK_GETEMOD = zone.intern ("get-encoding-mode");
  static const long QUARK_SETEMOD = zone.intern ("set-encoding-mode");

  // map an enumeration item to a stream codingmode
  static inline System::t_emod item_to_emod (const Item& item) {
    // check for a stream item 
    if (item.gettid () != QUARK_STREAM)
      throw Exception ("item-error", "item is not a stream item");
    // map the item to the enumeration
    long quark = item.getquark (); 
    if (quark == QUARK_BYTE) return System::BYTE;
    if (quark == QUARK_UTF8) return System::UTF8;
    throw Exception ("item-error", "cannot map item to stream encoding mode");
  }

  // map a coding mode to an item
  static inline Item* emod_to_item (const System::t_emod emod) {
    if (emod == System::BYTE) return new Item (QUARK_STREAM, QUARK_BYTE);
    if (emod == System::UTF8) return new Item (QUARK_STREAM, QUARK_UTF8);
    throw Exception ("mode-error", "cannot map stream encoding mode to item");
  }

  // evaluate a quark statically

  Object* Stream::meval (Runnable* robj, Nameset* nset, const long quark) {
    if (quark == QUARK_BYTE) 
      return new Item (QUARK_STREAM, QUARK_BYTE);
    if (quark == QUARK_UTF8) 
      return new Item (QUARK_STREAM, QUARK_UTF8);
    throw Exception ("eval-error", "cannot evaluate member",
                     String::qmap (quark));
  }

  // return true if the given quark is defined

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

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

  Object* Stream::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_GETEMOD) return emod_to_item (getemod ());
    }

    // dispatch 1 argument
    if (argc == 1) {
      if (quark == QUARK_SETEMOD) {
	// check for an item
	Object*  obj = argv->get (0);
	Item*   item = dynamic_cast <Item*> (obj);
	if (item != nilp) {	  
	  System::t_emod emod = item_to_emod (*item);
	  setemod (emod);
	  return nilp;
	}
	// check for a string
	String* sobj = dynamic_cast <String*> (obj);
	if (sobj != nilp) {	  
	  setemod (*sobj);
	  return nilp;
	}
	throw Exception ("argument-error", 
			 "invalid arguments with set-coding-mode");
      }
    }
    // apply these arguments with the transcoder
    return Transcoder::apply (robj, nset, quark, argv);
  }
}
