[BACK]Return to list.c CVS log [TXT][DIR] Up to [contributed] / early-roguelike / arogue7

Annotation of early-roguelike/arogue7/list.c, Revision 1.1.1.1

1.1       rubenllo    1: /*
                      2:  * list.c  -  Functions for dealing with linked lists of goodies
                      3:  *
                      4:  * Advanced Rogue
                      5:  * Copyright (C) 1984, 1985, 1986 Michael Morgan, Ken Dalka and AT&T
                      6:  * All rights reserved.
                      7:  *
                      8:  * Based on "Rogue: Exploring the Dungeons of Doom"
                      9:  * Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
                     10:  * All rights reserved.
                     11:  *
                     12:  * See the file LICENSE.TXT for full copyright and licensing information.
                     13:  */
                     14:
                     15: /*
                     16:  * Functions for dealing with linked lists of goodies
                     17:  *
                     18:  */
                     19:
                     20: #include "curses.h"
                     21: #include <stdlib.h>
                     22: #include "rogue.h"
                     23:
                     24: void r_discard(struct linked_list *item);
                     25: void t_discard(struct linked_list *item);
                     26:
                     27: /*
                     28:  * detach:
                     29:  *     Takes an item out of whatever linked list it might be in
                     30:  */
                     31:
                     32: void
                     33: _detach(struct linked_list **list, struct linked_list *item)
                     34: {
                     35:     if (*list == item)
                     36:        *list = next(item);
                     37:     if (prev(item) != NULL) item->l_prev->l_next = next(item);
                     38:     if (next(item) != NULL) item->l_next->l_prev = prev(item);
                     39:     item->l_next = NULL;
                     40:     item->l_prev = NULL;
                     41: }
                     42:
                     43: /*
                     44:  * _attach:
                     45:  *     add an item to the head of a list
                     46:  */
                     47:
                     48: void
                     49: _attach(struct linked_list **list, struct linked_list *item)
                     50: {
                     51:     if (*list != NULL)
                     52:     {
                     53:        item->l_next = *list;
                     54:        (*list)->l_prev = item;
                     55:        item->l_prev = NULL;
                     56:     }
                     57:     else
                     58:     {
                     59:        item->l_next = NULL;
                     60:        item->l_prev = NULL;
                     61:     }
                     62:
                     63:     *list = item;
                     64: }
                     65:
                     66: /*
                     67:  * o_free_list:
                     68:  *     Throw the whole object list away
                     69:  */
                     70:
                     71: void
                     72: _o_free_list(struct linked_list **ptr)
                     73: {
                     74:     register struct linked_list *item;
                     75:
                     76:     while (*ptr != NULL)
                     77:     {
                     78:        item = *ptr;
                     79:        *ptr = next(item);
                     80:        o_discard(item);
                     81:     }
                     82: }
                     83:
                     84: /*
                     85:  * o_discard:
                     86:  *     free up an item and its object(and maybe contents)
                     87:  */
                     88:
                     89: void
                     90: o_discard(struct linked_list *item)
                     91: {
                     92:     register struct object *obj;
                     93:
                     94:     obj = OBJPTR(item);
                     95:     if (obj->contents != NULL)
                     96:        o_free_list(obj->contents);
                     97:     total -= 2;
                     98:     FREE(obj);
                     99:     FREE(item);
                    100: }
                    101:
                    102: /*
                    103:  * r_free_list:
                    104:  *     Throw the whole list of room exits away
                    105:  */
                    106:
                    107: void
                    108: _r_free_list(struct linked_list **ptr)
                    109: {
                    110:     register struct linked_list *item;
                    111:
                    112:     while (*ptr != NULL)
                    113:     {
                    114:        item = *ptr;
                    115:        *ptr = next(item);
                    116:        r_discard(item);
                    117:     }
                    118: }
                    119:
                    120: /*
                    121:  * r_discard:
                    122:  *     free up an item and its room
                    123:  */
                    124:
                    125: void
                    126: r_discard(struct linked_list *item)
                    127: {
                    128:     total -= 2;
                    129:     FREE(DOORPTR(item));
                    130:     FREE(item);
                    131: }
                    132:
                    133: /*
                    134:  * t_free_list:
                    135:  *     Throw the whole thing list away
                    136:  */
                    137:
                    138: void
                    139: _t_free_list(struct linked_list **ptr)
                    140: {
                    141:     register struct linked_list *item;
                    142:
                    143:     while (*ptr != NULL)
                    144:     {
                    145:        item = *ptr;
                    146:        *ptr = next(item);
                    147:        t_discard(item);
                    148:     }
                    149: }
                    150:
                    151: /*
                    152:  * t_discard:
                    153:  *     free up an item and its thing
                    154:  */
                    155:
                    156: void
                    157: t_discard(struct linked_list *item)
                    158: {
                    159:     register struct thing *tp;
                    160:
                    161:     total -= 2;
                    162:     tp = THINGPTR(item);
                    163:     if (tp->t_name != NULL) FREE(tp->t_name);
                    164:     FREE(tp);
                    165:     FREE(item);
                    166: }
                    167:
                    168: /*
                    169:  * destroy_item:
                    170:  *     get rid of an item structure -- don't worry about contents
                    171:  */
                    172:
                    173: void
                    174: destroy_item(struct linked_list *item)
                    175: {
                    176:     total--;
                    177:     FREE(item);
                    178: }
                    179:
                    180: /*
                    181:  * new_item
                    182:  *     get a new item with a specified size
                    183:  */
                    184:
                    185: struct linked_list *
                    186: new_item(int size)
                    187: {
                    188:     register struct linked_list *item;
                    189:
                    190:     if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
                    191:        msg("Ran out of memory for header after %d items", total);
                    192:     if ((item->l_data = new(size)) == NULL)
                    193:        msg("Ran out of memory for data after %d items", total);
                    194:     item->l_next = item->l_prev = NULL;
                    195:     return item;
                    196: }
                    197:
                    198: /*
                    199:  * creat_item:
                    200:  *     Create just an item structure -- don't make any contents
                    201:  */
                    202:
                    203: struct linked_list *
                    204: creat_item(void)
                    205: {
                    206:     register struct linked_list *item;
                    207:
                    208:     if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
                    209:        msg("Ran out of memory for header after %d items", total);
                    210:     item->l_next = item->l_prev = NULL;
                    211:     return item;
                    212: }
                    213:
                    214: char *
                    215: new(int size)
                    216: {
                    217:     register char *space = ALLOC(size);
                    218:     static char errbuf[LINELEN];
                    219:
                    220:     if (space == NULL) {
                    221:        sprintf(errbuf,"Rogue ran out of memory (used = %ld, wanted = %d).",
                    222:                md_memused(), size);
                    223:        fatal(errbuf);
                    224:     }
                    225:     total++;
                    226:     return space;
                    227: }

CVSweb