// ---------------------------------------------------------------------------
// - Pathname.cpp                                                            -
// - standard object library - path name 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 "Regex.hpp"
#include "Vector.hpp"
#include "System.hpp"
#include "Integer.hpp"
#include "Pathname.hpp"
#include "Pathname.hpp"
#include "QuarkZone.hpp"
#include "Exception.hpp"

namespace afnix {

  // -------------------------------------------------------------------------
  // - private section                                                       -
  // -------------------------------------------------------------------------

  // this procedure returns true if the name is absolute
  static bool is_root (const String& name) {
    // get the root name
    String rdir = System::rootdir ();
    long   rlen = rdir.length ();
    long   nlen = name.length ();
    // check the name
    if (nlen < rlen) return false;
    return (name.lsubstr (rlen) == rdir);
  }

  // this procedure return a path without the root name
  static String rm_root (const String& name) {
    // get the root name
    String rdir = System::rootdir ();
    long   rlen = rdir.length ();
    long   nlen = name.length ();
    // check the name
    if (nlen < rlen) return name;
    if (name.lsubstr (rlen) == rdir) return name.rsubstr (rlen);
    // not a root name
    return name;
  }

  // this procedure rebuilds a path from a directory vector
  static String to_dnam (const Strvec dvec) {
    // get vector length and check
    long dlen = dvec.length ();
    // initialize result
    String result = (dlen == 0) ? "" : dvec.get (0);
    if (dlen <= 1) return result;
    // rebuild path
    for (long i = 1; i < dlen; i++) {
      result = System::join (result, dvec.get (i));
    }
    return result;
  }

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

  // create a default path name

  Pathname::Pathname (void) {
    reset ();
  }

  // create a path name by file name

  Pathname::Pathname (const String& fnam) {
    d_dvec.reset ();
    setfnam (fnam);
  }

  // create a path name by file and directory name

  Pathname::Pathname (const String& fnam, const String& dnam) {
    reset   ();
    setdnam (dnam);
    setfnam (fnam);
  }

  // copy construct this path name

  Pathname::Pathname (const Pathname& that) {
    that.rdlock ();
    try {
      d_root = that.d_root;
      d_fnam = that.d_fnam;
      d_dvec = that.d_dvec;
      that.unlock ();
    } catch (...) {
      that.unlock ();
      throw;
    }
  }

  //  assign a path name to this one

  Pathname& Pathname::operator = (const Pathname& that) {
    wrlock ();
    that.rdlock ();
    try {
      d_root = that.d_root;
      d_fnam = that.d_fnam;
      d_dvec = that.d_dvec;
      that.unlock ();
      unlock ();
      return *this;
    } catch (...) {
      that.unlock ();
      unlock ();
      throw;
    }
  }

  // return the class name

  String Pathname::repr (void) const {
    return "Pathname";
  }

  // reset this path name

  void Pathname::reset (void) {
    wrlock ();
    try {
      d_root = "";
      d_fnam = "";
      d_dvec.reset ();
      unlock ();
    } catch (...) {
      unlock ();
      throw;
    }
  }

  // set the file path name

  void Pathname::setfnam (const String& name) {
    wrlock ();
    try {
      if (is_root (name) == true) {
	// reset directory vector
	d_dvec.reset ();
	// save root name
	d_root = System::rootdir ();
      }
      // compute working name
      String wnam = rm_root (name);
      // split the name and update vector
      Strvec nvec = Strvec::split (wnam, System::dirsep ());
      long   nlen = nvec.length ();
      long   dlen = nlen - 1;
      for (long i = 0; i < dlen; i++) d_dvec.add (nvec.get (i));
      d_fnam = nvec.get (dlen);
      unlock ();
    } catch (...) {
      reset  ();
      unlock ();
      throw;
    }
  }

  // return the file path name
    
  String Pathname::getfnam (void) const {
    rdlock ();
    String result = d_fnam;
    unlock ();
    return result;
  }

  // set the directory path name

  void Pathname::setdnam (const String& name) {
    wrlock ();
    try {
      if (is_root (name) == true) {
	// reset directory vector
	d_dvec.reset ();
	// save root name
	d_root = System::rootdir ();
      }
      // compute working name
      String wnam = rm_root (name);
      // split the name and update vector
      d_dvec = Strvec::split (wnam, System::dirsep ());
      unlock ();
    } catch(...) {
      reset  ();
      unlock ();
      throw;
    }
  }

  // return the directory path name
    
  String Pathname::getdnam (void) const {
    rdlock ();
    try {
      String result = d_root;
      if (result.isnil () == true) {
	result = to_dnam (d_dvec);
      } else {
	result = System::join (result, to_dnam (d_dvec));
      }
      unlock ();
      return result;
    } catch(...) {
      unlock ();
      throw;
    }
  }

  // return the directory root value
    
  String Pathname::getroot (void) const {
    rdlock ();
    String result = d_root;
    unlock ();
    return result;
  }

  // return the full path name

  String Pathname::getfull (void) const {
    rdlock ();
    try {
      String   dnam = getdnam ();
      String result = dnam.isnil () ? d_fnam : System::join (dnam, d_fnam);
      unlock ();
      return result;
    } catch(...) {
      unlock ();
      throw;
    }
  }

  // return the system path name

  String Pathname::getsysp (void) const {
    rdlock ();
    try {
      String result = getfull ();
      if (d_root.isnil () == true) {
	result = System::join (System::getcwd (), result);
      }
      unlock ();
      return result;
    } catch(...) {
      unlock ();
      throw;
    }
  }

  // return the directory path length
    
  long Pathname::length (void) const {
    rdlock ();
    long result = d_dvec.length ();
    unlock ();
    return result;
  }

  // return the directory path by index
    
  String Pathname::getpath (const long index) const {
    rdlock ();
    try {
      // get the path element
      String result = d_dvec.get (index);
      // special case for index 0
      if ((index == 0) && (d_root.isnil () == false)) {
	result = System::join (d_root, result);
      }
      unlock ();
      return result;
    } catch(...) {
      unlock ();
      throw;
    }
  }

  // add a path element by index

  void Pathname::addpath (const String& name) {
    wrlock ();
    try {
      d_dvec.add (name);
      unlock ();
    } 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_RESET   = zone.intern ("reset");
  static const long QUARK_LENGTH  = zone.intern ("length");
  static const long QUARK_GETROOT = zone.intern ("get-root");
  static const long QUARK_GETFULL = zone.intern ("get-full");
  static const long QUARK_GETSYST = zone.intern ("get-system-path");
  static const long QUARK_ADDPATH = zone.intern ("add-path");
  static const long QUARK_GETPATH = zone.intern ("get-path");
  static const long QUARK_GETFNAM = zone.intern ("get-file-name");
  static const long QUARK_SETFNAM = zone.intern ("set-file-name");
  static const long QUARK_GETDNAM = zone.intern ("get-directory-name");
  static const long QUARK_SETDNAM = zone.intern ("set-directory-name");

  // create a new object in a generic way

  Object* Pathname::mknew (Vector* argv) {
    long argc = (argv == nilp) ? 0 : argv->length ();
    // check for 0 arguments
    if (argc == 0) return new Pathname;
    // check for 1 argument
    if (argc == 1) {
      String fnam = argv->getstring (0);
      return new Pathname (fnam);
    }
    // check for 2 arguments
    if (argc == 2) {
      String fnam = argv->getstring (0);
      String dnam = argv->getstring (1);
      return new Pathname (fnam, dnam);
    }
    throw Exception ("argument-error",
		     "too many argument with pathname constructor");
  }

  // return true if the given quark is defined

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

  // apply this object with a set of arguments and a quark
  
  Object* Pathname::apply (Runnable* robj, Nameset* nset, const long quark,
			    Vector* argv) {
    // get the number of arguments
    long argc = (argv == nilp) ? 0 : argv->length ();
 
    // check for 0 argument
    if (argc == 0) {
      if (quark == QUARK_LENGTH)  return new Integer (length ());
      if (quark == QUARK_GETFNAM) return new String  (getfnam ());
      if (quark == QUARK_GETDNAM) return new String  (getdnam ());
      if (quark == QUARK_GETROOT) return new String  (getroot ());
      if (quark == QUARK_GETFULL) return new String  (getfull ());
      if (quark == QUARK_GETSYST) return new String  (getsysp ());
      if (quark == QUARK_RESET) {
	reset ();
	return nilp;
      }
    }

    // check for 1 argument
    if (argc == 1) {
      if (quark == QUARK_SETFNAM) {
	String fnam = argv->getstring (0);
	setfnam (fnam);
	return nilp;
      }
      if (quark == QUARK_SETDNAM) {
	String dnam = argv->getstring (0);
	setdnam (dnam);
	return nilp;
      }
      if (quark == QUARK_ADDPATH) {
	String name = argv->getstring (0);
	addpath (name);
	return nilp;
      }
      if (quark == QUARK_GETPATH) {
	long index = argv->getint (0);
	return new String (getpath (index));
      }
    }
    // call the object method 
    return Object::apply (robj, nset, quark, argv);
  }
}
