--- deliantra/server/common/readable.C 2006/08/29 08:01:35 1.2 +++ deliantra/server/common/readable.C 2006/09/03 14:33:47 1.4 @@ -1,6 +1,6 @@ /* * static char *rcsid_readable_c = - * "$Id: readable.C,v 1.2 2006/08/29 08:01:35 root Exp $"; + * "$Id: readable.C,v 1.4 2006/09/03 14:33:47 elmex Exp $"; */ /* @@ -79,23 +79,24 @@ */ /* 'title' and 'titlelist' are used by the readable code */ -typedef struct titlestruct { - const char *name; /* the name of the book */ - const char *authour; /* the name of the book authour */ - const char *archname; /* the archetype name of the book */ +typedef struct titlestruct : zero_initialised +{ + shstr name; /* the name of the book */ + shstr authour; /* the name of the book authour */ + shstr archname; /* the archetype name of the book */ int level; /* level of difficulty of this message */ int size; /* size of the book message */ int msg_index; /* an index value derived from book message */ struct titlestruct *next; } title; -typedef struct titleliststruct { +typedef struct titleliststruct : zero_initialised +{ int number; /* number of items in the list */ struct titlestruct *first_book; /* pointer to first book in this list */ struct titleliststruct *next; /* pointer to next book list */ } titlelist; - /* special structure, used only by art_name_array[] */ typedef struct namebytype @@ -542,9 +543,8 @@ static titlelist * get_empty_booklist (void) { - titlelist *bl = (titlelist *) malloc (sizeof (titlelist)); - if (bl == NULL) - fatal (OUT_OF_MEMORY); + titlelist *bl = new titlelist; + bl->number = 0; bl->first_book = NULL; bl->next = NULL; @@ -554,9 +554,8 @@ static title * get_empty_book (void) { - title *t = (title *) malloc (sizeof (title)); - if (t == NULL) - fatal (OUT_OF_MEMORY); + title *t = new title; + t->name = NULL; t->archname = NULL; t->authour = NULL; @@ -703,7 +702,7 @@ LOG (llevDebug, "Warning: this string exceeded max book buf size:"); LOG (llevDebug, " %s", msgbuf); } - tmp->name = add_string (msgbuf); + tmp->name = msgbuf; tmp->next = first_msg; first_msg = tmp; nrofmsg++; @@ -711,7 +710,7 @@ } else if (!strncmp (cp, "MSG", 3)) { - tmp = (linked_char *) malloc (sizeof (linked_char)); + tmp = new linked_char; strcpy (msgbuf, " "); /* reset msgbuf for new message */ continue; } @@ -773,18 +772,18 @@ if (!strncmp (cp, "title", 4)) { book = get_empty_book (); /* init new book entry */ - book->name = add_string (strchr (cp, ' ') + 1); + book->name = strchr (cp, ' ') + 1; type = -1; nroftitle++; continue; } if (!strncmp (cp, "authour", 4)) { - book->authour = add_string (strchr (cp, ' ') + 1); + book->authour = strchr (cp, ' ') + 1; } if (!strncmp (cp, "arch", 4)) { - book->archname = add_string (strchr (cp, ' ') + 1); + book->archname = strchr (cp, ' ') + 1; } else if (sscanf (cp, "level %d", &value)) { @@ -974,8 +973,8 @@ } break; } - free_string (book->name); - book->name = add_string (name); + + book->name = name; } /* add_book_author() @@ -1020,7 +1019,7 @@ } sprintf (title, "of %s", name); - op->title = add_string (title); + op->title = title; } /* unique_book() - check to see if the book title/msg is unique. We @@ -1061,11 +1060,11 @@ } t = get_empty_book (); - t->name = add_string (book->name); - t->authour = add_string (book->title); + t->name = book->name; + t->authour = book->title; t->size = strlen (book->msg); t->msg_index = strtoint (book->msg); - t->archname = add_string (book->arch->name); + t->archname = book->arch->name; t->level = book->level; t->next = tl->first_book; @@ -1118,16 +1117,13 @@ /* alter book properties */ if ((tmpbook = get_archetype (t->archname)) != NULL) { - if (tmpbook->msg) - free_string (book->msg); - tmpbook->msg = add_string (book->msg); + tmpbook->msg = book->msg; copy_object (tmpbook, book); free_object (tmpbook); } - book->title = add_string (t->authour); - free_string (book->name); - book->name = add_string (t->name); + book->title = t->authour; + book->name = t->name; book->level = t->level; } /* Don't have any default title, so lets make up a new one */ @@ -1137,8 +1133,8 @@ const char *old_title; const char *old_name; - old_title = book->title ? add_string(book->title) : NULL; - old_name = add_string(book->name); + old_title = book->title; + old_name = book->name; /* some pre-generated books have title already set (from * maps), also don't bother looking for unique title if @@ -1158,9 +1154,6 @@ LOG (llevDebug, "titles for list %d full (%d possible).\n", msgtype, maxnames); #endif - if (old_title != NULL) - free_string(old_title); - free_string(old_name); break; } /* shouldnt change map-maker books */ @@ -1190,29 +1183,21 @@ book->name, book->title, numb, maxnames); #endif /* restore old book properties here */ - free_string (book->name); - free_string (book->title); - if (old_title!=NULL) - book->title = add_string (old_title); + book->title = old_title; if (RANDOM () % 4) { /* Lets give the book a description to individualize it some */ char new_name[MAX_BUF]; snprintf (new_name, MAX_BUF, "%s %s", book_descrpt[RANDOM () % nbr], old_name); - book->name = add_string (new_name); + book->name = new_name; } else { - book->name = add_string (old_name); + book->name = old_name; } } else if (book->title && strlen (book->msg) > 5) { /* archive if long msg texts */ add_book_to_list (book, msgtype); } - - if (old_title != NULL) - free_string(old_title); - free_string(old_name); - } break; } @@ -1315,7 +1300,7 @@ { static char retbuf[HUGE_BUF]; - sprintf (retbuf, " *** %s ***\n", mon->name); + sprintf (retbuf, " *** %s ***\n", &mon->name); strcat (retbuf, describe_item(mon, NULL)); return retbuf; @@ -1469,11 +1454,11 @@ temp = next; next = next->next; } - while ((next != (linked_char *) NULL) && !RANDOM () % 2); - sprintf (buf, "%s A %s of %s", buf, temp->name, art->item->name); + while (next && !RANDOM () % 2); + sprintf (buf, "%s A %s of %s", buf, &temp->name, &art->item->name); } else /* default name is used */ - sprintf (buf, "%s The %s of %s", buf, name, art->item->name); + sprintf (buf, "%s The %s of %s", buf, name, &art->item->name); /* chance of finding */ chance = (int) (100 * ((float) art->chance / al->total_chance)); @@ -1607,7 +1592,7 @@ if (fl->total_chance == 0) { - book->msg = add_string(" \n"); + book->msg = "\n"; new_text_name(book, 4); add_author(book,4); return; @@ -1622,7 +1607,7 @@ } if (!formula || formula->arch_names <= 0) { - book->msg = add_string(" \n"); + book->msg = "\n"; new_text_name(book, 4); add_author(book,4); @@ -1636,7 +1621,7 @@ /* preamble */ sprintf(retbuf, "Herein is described a project using %s: \n", - formula->skill?formula->skill:"an unknown skill"); + formula->skill ? &formula->skill : "an unknown skill"); if ((at = find_archetype (op_name)) != (archetype *) NULL) op_name = at->clone.name; @@ -1646,7 +1631,7 @@ /* item name */ if (strcmp (formula->title, "NONE")) { - sprintf (retbuf, "%sThe %s of %s", retbuf, op_name, formula->title); + sprintf (retbuf, "%sThe %s of %s", retbuf, op_name, &formula->title); /* This results in things like pile of philo. sulfur. * while philo. sulfur may look better, without this, * you get things like 'the wise' because its missing the @@ -1654,7 +1639,7 @@ */ sprintf(title,"%s: %s of %s", formula_book_name[RANDOM() % (sizeof(formula_book_name) / sizeof(char*))], - op_name, formula->title); + op_name, &formula->title); } else { @@ -1671,12 +1656,8 @@ } } /* Lets name the book something meaningful ! */ - if (book->name) free_string(book->name); - book->name = add_string(title); - if (book->title) { - free_string(book->title); - book->title = NULL; - } + book->name = title; + book->title = NULL; /* ingredients to make it */ if (formula->ingred != NULL) @@ -1698,10 +1679,10 @@ } else LOG (llevError, "formula_msg() no ingredient list for object %s of %s\n", - op_name, formula->title); + op_name, &formula->title); if (retbuf[strlen(retbuf)-1]!= '\n') strcat(retbuf, "\n"); - if (book->msg) free_string(book->msg); - book->msg = add_string(retbuf); + + book->msg = retbuf; } } @@ -2010,9 +1991,7 @@ strcat (msgbuf, "\n"); /* safety -- we get ugly map saves/crashes w/o this */ if (strlen (msgbuf) > 1) { - if (book->msg) - free_string (book->msg); - book->msg = add_string (msgbuf); + book->msg = msgbuf; /* lets give the "book" a new name, which may be a compound word */ change_book (book, msg_type); } @@ -2039,25 +2018,19 @@ for (tlist = booklist; tlist != NULL; tlist = tnext) { tnext = tlist->next; + for (title1 = tlist->first_book; title1; title1 = titlenext) { titlenext = title1->next; - if (title1->name) - free_string (title1->name); - if (title1->authour) - free_string (title1->authour); - if (title1->archname) - free_string (title1->archname); - free (title1); + delete title1; } - free (tlist); + + delete tlist; } for (lmsg = first_msg; lmsg; lmsg = nextmsg) { nextmsg = lmsg->next; - if (lmsg->name) - free_string (lmsg->name); - free (lmsg); + delete lmsg; } for (monlink = first_mon_info; monlink; monlink = nextmon) { @@ -2102,9 +2075,9 @@ for (book = bl->first_book; book; book = book->next) if (book && book->authour) { - fprintf (fp, "title %s\n", book->name); - fprintf (fp, "authour %s\n", book->authour); - fprintf (fp, "arch %s\n", book->archname); + fprintf (fp, "title %s\n", &book->name); + fprintf (fp, "authour %s\n", &book->authour); + fprintf (fp, "arch %s\n", &book->archname); fprintf (fp, "level %d\n", book->level); fprintf (fp, "type %d\n", index); fprintf (fp, "size %d\n", book->size);