MudOSa4DGD/
MudOSa4DGD/bin/
MudOSa4DGD/data/
MudOSa4DGD/doc/
MudOSa4DGD/doc/driver/
MudOSa4DGD/doc/efun/bitstrings/
MudOSa4DGD/doc/efun/command/
MudOSa4DGD/doc/efun/communication/
MudOSa4DGD/doc/efun/heart_beat/
MudOSa4DGD/doc/efun/interactive/
MudOSa4DGD/doc/efun/inventory/
MudOSa4DGD/doc/efun/living/
MudOSa4DGD/doc/efun/mappings/
MudOSa4DGD/doc/efun/strings/
MudOSa4DGD/doc/efun/uid/
MudOSa4DGD/doc/funs/
MudOSa4DGD/doc/language/
MudOSa4DGD/mudlib/dgd/doc/
MudOSa4DGD/mudlib/dgd/lib/include/dgd/
MudOSa4DGD/mudlib/dgd/lib/std/
MudOSa4DGD/mudlib/dgd/lib/sys/
MudOSa4DGD/mudlib/dgd/log/
MudOSa4DGD/mudlib/log/
MudOSa4DGD/mudlib/std/include/
MudOSa4DGD/mudlib/std/obj/
/*
 * regexp.c
 *
 * Return results of a regular expression
 * NB! Needs the regexp kfun package for DGD.
 *
 * (C) Frank Schmidt, Jesus@NorseMUD
 *
 */


#ifndef __AUTO         /* In the regexp object (simul_efun) */


/* how to init us */
#define INIT_REGEXP()         init_regexp()

/* define a default cache size to use for compiled regular expressions */
#ifndef REGEXP_CACHE_SIZE
# define REGEXP_CACHE_SIZE    100    /* default regexp cache size */
#endif


/* cache to store repeated patterns in, speeds up process */
private mapping regexp_cache;


/* initialize cache and list */
static void init_regexp() {
  regexp_cache = ([ ]);
}


/* return the regular expression, use caching of patterns */
varargs int *regexp(string subject, string pattern, int incase, int reverse) {
  string *buffer, cachepatt;

  /* add info about if the search should be case-insensitive.
     (This info tag will be added no matter what, so it will never
     influence the resulting pattern, by neither look-up nor store. */
  cachepatt = pattern + "@" + (string)incase;

  /* compile regexp pattern or get it from cache */
  if ((buffer = regexp_cache[cachepatt]) == 0) {
    /* compile... */
    buffer = ::regexp_compile(pattern, incase);

    while (map_sizeof(regexp_cache) >= REGEXP_CACHE_SIZE) {
      /* exceeding cache, delete an element */
      regexp_cache = map_delete(regexp_cache, 
   		    map_indices(regexp_cache)[random(REGEXP_CACHE_SIZE)]);
    }
    /* add current buffer to cache */
    regexp_cache[cachepatt] = buffer;
  }
  /* return final result */
  return ::regexp_match(buffer, subject, reverse);
}


#else                  /* In the auto object */


/* define the regexp object (this object) */
#define REGEXP_OB             SIMUL_EFUN


/* regexp sfun */
static varargs int *regexp(string subject, string pattern, int incase, int reverse) {
  return REGEXP_OB->regexp(subject, pattern, incase, reverse);
}


/* override kfuns that (maybe) shouldn't be accessed? */
#if 0
private varargs string *regexp_compile(string pattern, int case_insensitive) {}
private varargs int *regexp_match(string *pattern, string subject, int reverse) {}
#endif


#endif