/* ************************************************************************
* File: utils.c Part of CircleMUD *
* Usage: various internal functions of a utility nature *
* *
* All rights reserved. See license.doc for complete information. *
* *
* Copyright (C) 1993, 94 by the Trustees of the Johns Hopkins University *
* CircleMUD is based on DikuMUD, Copyright (C) 1990, 1991. *
************************************************************************ */
/*
* Code from utils.c needed for world conversion process
*
* Same as the others. If you have made any changes, make sure they're
* done here too.
*/
#include "sysdep.h"
#include "structs.h"
#include "utils.h"
#include "main.h"
#include "db.h"
/*
* get_line reads the next non-blank line off of the input stream.
* The newline character is removed from the input. Lines which begin
* with '*' are considered to be comments.
*
* Returns the number of lines advanced in the file. Buffer given must
* be at least READ_SIZE (256) characters large.
*/
int get_line(FILE *fl, char *buf)
{
char temp[READ_SIZE];
int lines = 0;
int sl;
do {
if (!fgets(temp, READ_SIZE, fl))
return (0);
lines++;
} while (*temp == '*' || *temp == '\n' || *temp == '\r');
/* Last line of file doesn't always have a \n, but it should. */
sl = strlen(temp);
while (sl > 0 && (temp[sl - 1] == '\n' || temp[sl - 1] == '\r'))
temp[--sl] = '\0';
strcpy(buf, temp); /* strcpy: OK, if buf >= READ_SIZE (256) */
return (lines);
}
size_t strlcpy(char *dest, const char *source, size_t totalsize)
{
strncpy(dest, source, totalsize - 1); /* strncpy: OK (we must assume 'totalsize' is correct) */
dest[totalsize - 1] = '\0';
return strlen(source);
}
char *CAP(char *txt)
{
*txt = UPPER(*txt);
return (txt);
}
char *fname(const char *namelist)
{
static char holder[30];
char *point;
for (point = holder; isalpha(*namelist); namelist++, point++)
*point = *namelist;
*point = '\0';
return (holder);
}
/* Be wary of sign issues with this. */
int MIN(int a, int b)
{
return (a < b ? a : b);
}
/* Be wary of sign issues with this. */
int MAX(int a, int b)
{
return (a > b ? a : b);
}
int get_filename(char *filename, size_t fbufsize, int mode, const char *orig_name)
{
const char *prefix = NULL, *middle = NULL, *suffix = NULL;
char name[PATH_MAX], *ptr;
if (filename == NULL) {
log("get_filename(): invalid 'filename' string.");
} else if (orig_name == NULL || *orig_name == '\0') {
log("get_filename(): invalid 'orig_name' string.");
} else {
switch (mode) {
case CRASH_FILE:
prefix = ETC_PLROBJS;
suffix = SUF_OBJS;
break;
case ALIAS_FILE:
prefix = ETC_PLRALIAS;
suffix = SUF_ALIAS;
break;
case ETEXT_FILE:
prefix = ETC_PLRTEXT;
suffix = SUF_TEXT;
break;
case ZONE_FILE:
prefix = ETC_ZONES;
suffix = SUF_ZONE;
break;
default:
return (FALSE);
}
strlcpy(name, orig_name, sizeof(name));
for (ptr = name; *ptr; ptr++)
*ptr = LOWER(*ptr);
if (mode != ZONE_FILE) {
switch (LOWER(*name)) {
case 'a': case 'b': case 'c': case 'd': case 'e':
middle = "A-E";
break;
case 'f': case 'g': case 'h': case 'i': case 'j':
middle = "F-J";
break;
case 'k': case 'l': case 'm': case 'n': case 'o':
middle = "K-O";
break;
case 'p': case 'q': case 'r': case 's': case 't':
middle = "P-T";
break;
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
middle = "U-Z";
break;
default:
middle = "ZZZ";
break;
}
}
snprintf(filename, fbufsize, "%s%s%s%s.%s",
prefix,
middle && *middle != '\0' ? middle : "",
middle && *middle != '\0' ? SLASH : "",
name,
suffix);
return (TRUE);
}
return (FALSE);
}
size_t sprinttype(int type, const char *names[], char *result, size_t reslen)
{
int nr = 0;
while (type && *names[nr] != '\n') {
type--;
nr++;
}
return strlcpy(result, *names[nr] != '\n' ? names[nr] : "UNDEFINED", reslen);
}