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

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

1.1       rubenllo    1: /*
                      2:  * Functions for dealing with linked lists of goodies
                      3:  *
                      4:  * Advanced Rogue
                      5:  * Copyright (C) 1984, 1985 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: #include <stdlib.h>
                     16: #include "curses.h"
                     17: #include "rogue.h"
                     18:
                     19: /*
                     20:  * detach:
                     21:  *     Takes an item out of whatever linked list it might be in
                     22:  */
                     23:
                     24: void
                     25: _detach(struct linked_list **list, struct linked_list *item)
                     26: {
                     27:     if (*list == item)
                     28:        *list = next(item);
                     29:     if (prev(item) != NULL) item->l_prev->l_next = next(item);
                     30:     if (next(item) != NULL) item->l_next->l_prev = prev(item);
                     31:     item->l_next = NULL;
                     32:     item->l_prev = NULL;
                     33: }
                     34:
                     35: /*
                     36:  * _attach:
                     37:  *     add an item to the head of a list
                     38:  */
                     39:
                     40: void
                     41: _attach(struct linked_list **list, struct linked_list *item)
                     42: {
                     43:     if (*list != NULL)
                     44:     {
                     45:        item->l_next = *list;
                     46:        (*list)->l_prev = item;
                     47:        item->l_prev = NULL;
                     48:     }
                     49:     else
                     50:     {
                     51:        item->l_next = NULL;
                     52:        item->l_prev = NULL;
                     53:     }
                     54:
                     55:     *list = item;
                     56: }
                     57:
                     58: /*
                     59:  * o_free_list:
                     60:  *     Throw the whole object list away
                     61:  */
                     62:
                     63: void
                     64: _o_free_list(struct linked_list **ptr)
                     65: {
                     66:     register struct linked_list *item;
                     67:
                     68:     while (*ptr != NULL)
                     69:     {
                     70:        item = *ptr;
                     71:        *ptr = next(item);
                     72:        o_discard(item);
                     73:     }
                     74: }
                     75:
                     76: /*
                     77:  * o_discard:
                     78:  *     free up an item and its object(and maybe contents)
                     79:  */
                     80:
                     81: void
                     82: o_discard(struct linked_list *item)
                     83: {
                     84:     register struct object *obj;
                     85:     obj = OBJPTR(item);
                     86:     if (obj->contents != NULL)
                     87:        o_free_list(obj->contents);
                     88:     total -= 2;
                     89:     FREE(item->l_data);
                     90:     FREE(item);
                     91: }
                     92:
                     93: /*
                     94:  * t_free_list:
                     95:  *     Throw the whole thing list away
                     96:  */
                     97:
                     98: void
                     99: _t_free_list(struct linked_list **ptr)
                    100: {
                    101:     register struct linked_list *item;
                    102:
                    103:     while (*ptr != NULL)
                    104:     {
                    105:        item = *ptr;
                    106:        *ptr = next(item);
                    107:        t_discard(item);
                    108:     }
                    109: }
                    110:
                    111: /*
                    112:  * t_discard:
                    113:  *     free up an item and its thing
                    114:  */
                    115:
                    116: void
                    117: t_discard(struct linked_list *item)
                    118: {
                    119:     total -= 2;
                    120:     FREE(item->l_data);
                    121:     FREE(item);
                    122: }
                    123:
                    124: /*
                    125:  * destroy_item:
                    126:  *     get rid of an item structure -- don't worry about contents
                    127:  */
                    128:
                    129: void
                    130: destroy_item(struct linked_list *item)
                    131: {
                    132:     total--;
                    133:     FREE(item);
                    134: }
                    135:
                    136: /*
                    137:  * new_item
                    138:  *     get a new item with a specified size
                    139:  */
                    140:
                    141: struct linked_list *
                    142: new_item(int size)
                    143: {
                    144:     register struct linked_list *item;
                    145:
                    146:     if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
                    147:        msg("Ran out of memory for header after %d items", total);
                    148:     if ((item->l_data = new(size)) == NULL)
                    149:        msg("Ran out of memory for data after %d items", total);
                    150:     item->l_next = item->l_prev = NULL;
                    151:     return item;
                    152: }
                    153:
                    154: /*
                    155:  * creat_item:
                    156:  *     Create just an item structure -- don't make any contents
                    157:  */
                    158:
                    159: struct linked_list *
                    160: creat_item(void)
                    161: {
                    162:     register struct linked_list *item;
                    163:
                    164:     if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
                    165:        msg("Ran out of memory for header after %d items", total);
                    166:     item->l_next = item->l_prev = NULL;
                    167:     return item;
                    168: }
                    169:
                    170: char *
                    171: new(int size)
                    172: {
                    173:     register char *space = ALLOC(size);
                    174:     static char errbuf[LINELEN];
                    175:
                    176:     if (space == NULL) {
                    177:        sprintf(errbuf,"Rogue ran out of memory (used = %ld, wanted = %d).",
                    178:                md_memused(), size);
                    179:        fatal(errbuf);
                    180:     }
                    181:     total++;
                    182:     return space;
                    183: }

CVSweb