// mudlib: Lil
// file:   /single/simul_efun.c

#pragma show_error_context
#include <globals.h>
#include <lpctypes.h>

// Define a class directly in the simul_efun so the inherited
// cross_struct_lib's CrossStruct ends up at a different local class_num
// than in its defining program -- this is the slot-offset shape that
// reproduces the cross-program struct return-type bug.
class CrossEarly { int x; }

// Provides make_cross_struct() and `class CrossStruct'.  Inherited so the
// function's return type carries an inherited-program class_num.
inherit "std/cross_struct_lib.lpc";

// JSON sefun
inherit "std/json.lpc";
// Breakstring sefun
inherit "std/break_string.lpc";
// bitmap_font sefun
inherit "std/base64.lpc" ;
inherit "std/bitmap_font.lpc";
inherit "std/highest.lpc" ;
inherit "std/lowest.lpc" ;
inherit "std/range.lpc" ;
inherit "std/sum.lpc" ;
inherit "std/element_of_weighted.lpc" ; // requires: sum
inherit "std/number_string.lpc" ;
inherit "std/reduce.lpc" ;
inherit "std/percent.lpc" ;
inherit "std/all_environment.lpc" ;
inherit "std/present_clone.lpc" ;

int same(mixed x, mixed y) {
    // Allow comparing array with buffer
    if (!(typeof(x) == ARRAY && typeof(y) == BUFFER || typeof(y) == ARRAY && typeof(x) == BUFFER))
      if (typeof(x) != typeof(y))
        return 0;
    switch (typeof(x)) {
    case INT:
    case STRING:
    case OBJECT:
    case FLOAT:
	return x == y;
    case MAPPING:
	if (x == y) return 1; // speed up this case
	if (sizeof(x) != sizeof(y)) return 0;
	if (!same(keys(x), keys(y))) return 0;
	if (!same(values(x), values(y))) return 0;
	return 1;
    case BUFFER:
    case ARRAY:
	if (x == y) return 1; // speed up this case
	if (sizeof(x) != sizeof(y)) return 0;
	for (int i = 0; i < sizeof(x); i++) {
	    if (!same(x[i], y[i])) return 0;
	}
	return 1;
    case FUNCTION:
    case CLASS:
	error("Not implemented.");
    }
}

void __assert_eq(mixed expected, mixed actual, string where) {
  if (!same(expected, actual)) {
    OUTPUT(where + ", Check Failed: \n" +
        "Expected: \n" + sprintf("%O", expected) + "\nActual: \n" + sprintf("%O", actual) + "\nTrace: \n" + sprintf("%s", "/single/master.lpc"->get_last_error()) + "\n");
  }
}

void __assert_ne(mixed expected, mixed actual, string where) {
  if (same(expected, actual)) {
    OUTPUT(where + ", Check Failed: \n" +
        "Expect Not: \n" + sprintf("%O", expected) + "\nActual: \n" + sprintf("%O", actual) + "\nTrace: \n" + sprintf("%s", "/single/master.lpc"->get_last_error()) + "\n");
  }
}

void
cat(string file)
{
	write(read_file(file));
}

varargs int
getoid(object ob)
{
	int id;

	sscanf(file_name(ob || previous_object()), "%*s#%d", id);
	return id;
}

string
user_cwd(string name)
{
   return ("/u/" + name[0..0] + "/" + name);
}

string user_path(string name)
{
   return (user_cwd(name) + "/");
}

// Get the owner of a file.  Used by log_error() in master.c.

string
file_owner(string file)
{
    string temp;

    if (file[0] != '/') file = "/" + file;

    if (sscanf(file, "/u/%s/%s/%*s", temp, temp) == 2) {
        return temp;
    }
    return 0;
}

#include <lpctypes.h>

// dump_variable, author: Huthar@Portals, TMI
// - returns a printable representation of any variable.
// Changed by Symmetry@IdeaExchange, Tmi-2 5/4/95
// - the format is retained
// Beek - changed to use foreach()
string
dump_variable(mixed arg)
{
   string rtn;
   mixed x, y;

   switch(typeof(arg)) {
   case OBJECT: return "("+file_name(arg)+")";
   case STRING: return "\""+arg+"\"";
   case INT: return "#"+arg;
   case ARRAY:
       {
	   rtn = "ARRAY\n";
	   foreach (y in arg)
	       rtn += sprintf("[%d] == %s\n", x++, dump_variable(y));

	   return rtn;
       }

   case MAPPING:
       {
	   rtn = "MAPPING\n" +
	       implode(values(map_mapping(arg,
					  (: sprintf("[%s] == %s", $1, $2) :))), "\n");
	   return rtn;
       }

     case FUNCTION:
     case CLASS:
     case FLOAT:
     case BUFFER:
       {
	   return sprintf("%O\n", arg);
       }

       return "UNKNOWN";
   }
}

/*
// Thanks to Huthar for resolve_path.
// Rewrite by Symmetry 5/4/95
*/

string resolve_path(string curr, string newer) {
    int i, j, size;
    string *tmp;

    switch(newer) {
    case 0:
    case ".":
	return curr;

#ifndef __NO_ENVIRONMENT__
    case "here":
	return file_name(environment());
#endif

    default:
	if (newer[0..1] == "~/") newer = user_path((string)this_player()->query_name()) + newer[2..];
	else {
	    switch(newer[0]) {
	    case '~':
	    {
		i = strsrch(newer, '/');
		if (i < 0) newer = user_path(newer[1..]);
		else newer = user_path(newer[1..i-1]) + newer[i..];
		break;
	    }
	    case '/': break;
	    default: newer[<0..<1] = curr + "/";
	    }
	}

	if (newer[<1] != '/') newer += "/";
	size = sizeof(tmp = regexp(explode(newer, "/"), "."));

	i = j = 0;

	while (i < size) {
	    switch(tmp[i]) {
	    case "..":
		if (j) {
		    while (j-- && !tmp[j]);
		    if (j >= 0) tmp[j] = 0;
		    else j++;
		}
	    case ".":
		tmp[i++] = 0;
		break;

	    default:
		j = ++i;
		break;
	    }
	}
	return "/"+implode(tmp, "/");
    }
}


// domain_file should return the domain associated with a given file.

string
domain_file(string)
{
	return ROOT_UID;
}

// creator_file should return the name of the creator of a specific file.

string
creator_file(string)
{
	return ROOT_UID;
}

// author_file should return the name of the author of a specific file.

string
author_file(string)
{
	return ROOT_UID;
}

void simul() {}
