Just a comparison of what can be accomplished when you try to simplify
code.
Before:
void write_aliases(struct char_data *ch)
{
FILE *file;
char fn[127],*buf;
struct alias *temp;
int length;
get_filename(GET_NAME(ch),fn,ALIAS_FILE);
unlink(fn);
if( !GET_ALIASES(ch) )
return;
file = fopen(fn,"wt");
temp = GET_ALIASES(ch);
while( temp )
{
length = strlen(temp->alias);
fprintf(file,"%d\n",length);
fprintf(file,"%s\n",temp->alias);
buf = strdup(temp->replacement);
while( *++buf == ' ' );
length = strlen(buf);
fprintf(file,"%d\n",length);
fprintf(file,"%s\n",buf);
fprintf(file,"%d\n",temp->type);
temp = temp->next;
}
fclose(file);
}
After:
void write_aliases(struct char_data *ch)
{
FILE *file;
char fn[MAX_STRING_LENGTH];
struct alias_data *temp;
get_filename(GET_NAME(ch), fn, ALIAS_FILE);
remove(fn);
if (GET_ALIASES(ch) == NULL)
return;
if ((file = fopen(fn, "w")) == NULL) {
log("SYSERR: Couldn't save aliases for %s in '%s'.", GET_NAME(ch), fn);
return;
}
for (temp = GET_ALIASES(ch); temp; temp = temp->next)
fprintf(file, "%d\n%s\n" /* Alias */
"%d\n%s\n" /* Replacement */
"%d\n", /* Type */
strlen(temp->alias), temp->alias,
strlen(temp->replacement) - 1, temp->replacement + 1,
temp->type);
fclose(file);
}
Before:
void read_aliases(struct char_data *ch)
{
FILE *file;
char fn[127];
struct alias *t2;
int length;
char temp_buf[127],buf[127];
get_filename(GET_NAME(ch),fn,ALIAS_FILE);
file = fopen(fn,"r");
if( !file )
return;
CREATE(GET_ALIASES(ch),struct alias,1);
t2 = GET_ALIASES(ch);
do
{
fscanf(file,"%d\n",&length);
fgets(buf,length+1,file);
t2->alias=strdup(buf);
fscanf(file,"%d\n",&length);
fgets(buf,length+1,file);
strcpy(temp_buf," ");
strcat(temp_buf,buf);
t2->replacement=strdup(temp_buf);
fscanf(file,"%d\n",&length);
t2->type = length;
if( !feof(file) ){
CREATE(t2->next,struct alias,1);
t2 = t2->next;
}
} while( !feof(file) );
fclose(file);
}
After:
void read_aliases(struct char_data *ch)
{
FILE *file;
char xbuf[MAX_STRING_LENGTH];
struct alias_data *t2;
int length;
get_filename(GET_NAME(ch), xbuf, ALIAS_FILE);
if ((file = fopen(xbuf, "r")) == NULL) {
if (errno != ENOENT) {
log("SYSERR: Couldn't open alias file '%s' for %s.", xbuf, GET_NAME(ch));
perror("SYSERR: read_aliases");
}
return;
}
CREATE(GET_ALIASES(ch), struct alias_data, 1);
t2 = GET_ALIASES(ch);
for (;;) {
/* Read the aliased command. */
fscanf(file, "%d\n", &length);
fgets(xbuf, length + 1, file);
t2->alias = str_dup(xbuf);
/* Build the replacement. */
fscanf(file, "%d\n", &length);
*xbuf = ' '; /* Doesn't need terminated, fgets() will. */
fgets(xbuf + 1, length + 1, file);
t2->replacement = str_dup(xbuf);
/* Figure out the alias type. */
fscanf(file, "%d\n", &length);
t2->type = length;
if (feof(file))
break;
CREATE(t2->next, struct alias_data, 1);
t2 = t2->next;
};
fclose(file);
}
You'll note the writing is much easier as well as the replacement reading.
--
George Greer, greerga@circlemud.org | Genius may have its limitations, but
http://mouse.van.ml.org/ (mostly) | stupidity is not thus handicapped.
http://www.van.ml.org/CircleMUD/ | -- Elbert Hubbard
+------------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
| http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
+------------------------------------------------------------+
This archive was generated by hypermail 2b30 : 12/15/00 PST