musicmud-2.1.6/data/
musicmud-2.1.6/data/help/
musicmud-2.1.6/data/policy/
musicmud-2.1.6/data/wild/
musicmud-2.1.6/data/world/
musicmud-2.1.6/doc/
musicmud-2.1.6/src/ident/
musicmud-2.1.6/src/lua/
musicmud-2.1.6/src/lua/include/
musicmud-2.1.6/src/lua/src/lib/
musicmud-2.1.6/src/lua/src/lua/
musicmud-2.1.6/src/lua/src/luac/
/*
 * MusicMUD Daemon, version 1.0
 * Copyright (C) 1998-2003 Abigail Brady
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * 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.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "colour.h"
#include "flags.h"
#include "musicmud.h"
#include "Player.h"
#include "msi.h"
#include "vsprintf.h"

namespace colour {

stringlength_t msi_stringlength = 0;
colourparse_t msi_colourfilter = 0;

size_t colour_strlen(const char *a, const colourinfo_t &ci) {
  if (msi_stringlength)
    return msi_stringlength(a, -1, ci);
  return strlen(a);
}

size_t colour_strnlen(const char *a, int b, const colourinfo_t &ci) {
  int l = colour_strlen(a, ci);
  if (b >= 0 && b < l) return b;
  return l;
}

size_t colour_strlenof(const char *a, int b, const colourinfo_t &ci) {
  if (msi_stringlength)
    return msi_stringlength(a, b, ci);

  size_t i = 0;
  while (*a && b) {
    a++;
    i++;
    b--;
  }
  
  return i;
}

const colourinfo_t def_cols = {0,CHAR_LATIN1,""};
const colourinfo_t asc_cols = {0,CHAR_ASCII,""};

colourinfo_t colinfo(const MudObject *who) {
  if (!who) return def_cols;

  colourinfo_t ci = {who->get_flag(FL_COLOUR),CHAR_LATIN1,
		     who->get("colours")};

  ci.cset = get_charset(who);
  ci.xtermcols = who->get_int("xtermcols", 8);
  ci.who = 0;

  if (!ci.scheme) ci.scheme = "";
  if (is_player(who)) {
    const Player *p = (const Player*)who;
    ci.who = p;
    if (p->p) {
      ci.term = p->p->term.c_str();
      if (p->p->thingy_from==2)
	ci.colour = 0;
    }
  }

  if (ci.term == "xterm-256colour") {
    ci.xtermcols = 256;
    ci.term = "xterm";
  }

  ci.dec = who->get_int("linedraw", 1);
  return ci;
}

string escape_colour(const string &s2, int withcol) 
{
  const char *what = s2.c_str();
  string s = "";
  while (*what) {
    if (*what=='&') 
      s += *what;
    
    if (*what=='^' && strchr("PpDdoLBRGCYMWlbcrgcymwntsikS", what[1])) {
      {
	if (withcol)
	  s += ssprintf("^^%c^%c", what[1], what[1]);
	else
	  s += ssprintf("^^%c", what[1]);
      }

      what++;
      
    } else if (*what=='^') {
      s += '^';
      s += *what;
    } else {
      s += *what;
    }
    what++;
  }
  return s;
}

string lose_colour(const char *what)
{
  if (!what || !*what)
    return "";
  if (msi_colourfilter)
    return msi_colourfilter(NULL, what, def_cols);
  return what;
}

size_t colour_strlen(const char *blah, MudObject *to) 
{
  if (!to)
    return 0;
  return colour_strlen(blah, colinfo(to));
}

}