/* $Header: stringutil.c,v 1.4 90/05/02 20:25:32 lachesis Exp $
 * $Log:    stringutil.c,v $
 * Revision 1.4  90/05/02  20:25:32  lachesis
 * Converted gender substitution code to read properties,
 * changed page to provide a message, too.
 *
 * Revision 1.3  90/04/24  14:46:34  lachesis
 * Fixed the @odrop on rooms to links.
 *
 * Revision 1.2  90/04/21  17:20:50  lachesis
 * Added property lists.
 *
 * Revision 1.1  90/04/14  14:56:54  lachesis
 * Initial revision
 *
 */
#include "copyright.h"

/* String utilities */

#include "os.h"

#include "externs.h"

#define DOWNCASE(x) (isupper(x) ? tolower(x) : (x))

int string_compare (const char *s1, const char *s2)
{
  while (*s1 && *s2 && DOWNCASE (*s1) == DOWNCASE (*s2))
    s1++, s2++;

  return (DOWNCASE (*s1) - DOWNCASE (*s2));
}

int string_prefix (const char *string, const char *prefix)
{
  while (*string && *prefix && DOWNCASE (*string) == DOWNCASE (*prefix))
    string++, prefix++;
  return *prefix == '\0';
}

/* accepts only nonempty matches starting at the beginning of a word */
const char *string_match (const char *src, const char *sub)
{
  if (*sub != '\0') {
    while (*src) {
      if (string_prefix (src, sub))
        return src;
      /* else scan to beginning of next word */
      while (*src && isalnum (*src))
        src++;
      while (*src && !isalnum (*src))
        src++;
    }
  }

  return 0;
}

#ifdef GENDER
/*
 * pronoun_substitute()
 *
 * %-type substitutions for pronouns
 *
 * %s/%S for subjective pronouns (he/she/it, He/She/It)
 * %o/%O for objective pronouns (him/her/it, Him/Her/It)
 * %p/%P for possessive pronouns (his/her/its, His/Her/Its)
 * %n    for the player's name.
 */
void pronoun_substitute (char *result, dbref player, const char *str,
  int prepend)
{
  char c;
  char prn[3];
  const char *self_sub;         /* self substitution code */

  static const char *subjective[4] = { "", "it", "she", "he" };
  static const char *possessive[4] = { "", "its", "her", "his" };
  static const char *objective[4] = { "", "it", "her", "him" };

  prn[0] = '%';
  prn[2] = '\0';

#ifdef COMPRESS
  str = uncompress (str);
#endif /* COMPRESS */
  if (prepend) {
    (void) strcpy (result, db[player].name);
    result += strlen (result);
    *result++ = ' ';
  }
  while (*str) {
    if (*str == '%') {
      *result = '\0';
      prn[1] = c = *(++str);
      self_sub = get_property_class (player, prn);
      if (self_sub) {
        strcat (result, self_sub);
        result += strlen (result);
        str++;
      } else if (genderof (player) == GENDER_UNASSIGNED) {
        switch (c) {
        case 'n':
        case 'N':
        case 'o':
        case 'O':
        case 's':
        case 'S':
          strcat (result, db[player].name);
          break;
        case 'p':
        case 'P':
          strcat (result, db[player].name);
          strcat (result, "'s");
          break;
        default:
          result[0] = *str;
          result[1] = 0;
          break;
        }
        str++;
        result += strlen (result);
      } else {
        switch (c) {
        case 's':
        case 'S':
          strcat (result, subjective[genderof (player)]);
          break;
        case 'p':
        case 'P':
          strcat (result, possessive[genderof (player)]);
          break;
        case 'o':
        case 'O':
          strcat (result, objective[genderof (player)]);
          break;
        case 'n':
        case 'N':
          strcat (result, db[player].name);
          break;
        default:
          *result = *str;
          result[1] = '\0';
          break;
        }
        if (isupper (c) && islower (*result)) {
          *result = toupper (*result);
        }

        result += strlen (result);
        str++;
      }
    } else {
      *result++ = *str++;
    }
  }
  *result = '\0';
}

#endif /* GENDER */