utils.c
smug/utils.c · 1.4 KB
From the archive smug.tar.gz
Download the archive « Back to the archive contents
/* Copyright 1989, 1990 by James Aspnes, David Applegate, and Bennet Yee */
/* See the file COPYING for distribution information */
#include <ctype.h>
#include "db.h"
/* note: it's ok if from == to */
void strip_whitespace(const char *from, char *to)
{
while(*from) {
/* scan over word */
while(*from && !isspace(*from)) *to++ = *from++;
/* smash spaces */
while(*from && isspace(*from)) from++;
if(*from) *to++ = ' '; /* add a space to separate next word */
}
/* terminate */
*to = '\0';
}
const char *time_string(datum t)
{
extern char *ctime(long *);
char *time_string;
time_string = ctime(&t);
/* kludge to get around high-octane ctime brain-damage */
time_string[24] = '\0';
return time_string;
}
const char *alloc_string(const char *string)
{
char *s;
/* NULL->NULL */
if(string == 0) return 0;
if((s = (char *) malloc(strlen(string)+1)) == 0) {
abort();
}
strcpy(s, string);
return s;
}
datum concat(datum s1, datum s2)
{
char buf[MAX_STRLEN];
const char *c1;
const char *c2;
if((c1 = string(s1)) == NOTHING) {
return s2;
} else if((c2 = string(s2)) == NOTHING) {
return s1;
} else if(strlen(c1) + strlen(c2) + 1 > MAX_STRLEN) {
/* too big, you lose */
return NOTHING;
} else {
strcpy(buf, c1);
strcat(buf, c2);
return intern(buf);
}
}
datum num_to_string(datum x)
{
char buf[MAX_STRLEN];
sprintf(buf, "%d", x);
return intern(buf);
}