Righto, this looks to do the job.
static void write_aliases_ascii(FILE *file, struct char_data *ch)
{
struct alias_data *temp;
int count = 0;
if (GET_ALIASES(ch) == NULL)
return;
for (temp = GET_ALIASES(ch); temp; temp = temp->next)
count++;
fprintf(file, "Alis: %d\n", count);
for (temp = GET_ALIASES(ch); temp; temp = temp->next)
fprintf(file, " %s\n" /* Alias: prepend a space in order to avoid issues with aliases beginning
* with * (get_line treats lines beginning with * as comments and ignores them */
"%s\n" /* Replacement: always prepended with a space in memory anyway */
"%d\n", /* Type */
temp->alias,
temp->replacement,
temp->type);
}
static void read_aliases_ascii(FILE *file, struct char_data *ch, int count)
{
int i;
if (count == 0) {
GET_ALIASES(ch) = NULL;
return; /* No aliases in the list. */
}
/* This code goes both ways for the old format (where alias and replacement start at the
* first character on the line) and the new (where they are prepended by a space in order
* to avoid the possibility of a * at the start of the line */
for (i = 0; i < count; i++) {
char abuf[MAX_INPUT_LENGTH+1], rbuf[MAX_INPUT_LENGTH+1], tbuf[MAX_INPUT_LENGTH];
/* Read the aliased command. */
get_line(file, abuf);
/* Read the replacement. This needs to have a space prepended before placing in
* the in-memory struct. The space may be there already, but we can't be certain! */
rbuf[0] = ' ';
get_line(file, rbuf+1);
/* read the type */
get_line(file, tbuf);
if (abuf[0] && rbuf[1] && *tbuf) {
struct alias_data *temp;
CREATE(temp, struct alias_data, 1);
temp->alias = strdup(abuf[0] == ' ' ? abuf+1 : abuf);
temp->replacement = strdup(rbuf[1] == ' ' ? rbuf+1 : rbuf);
temp->type = atoi(tbuf);
temp->next = GET_ALIASES(ch);
GET_ALIASES(ch) = temp;
}
}
}
Since replacement has a space in front of it in memory anyway, I decided to just stick a space in front of both alias and replacement in the pfile and parse either format of input.