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

Annotation of early-roguelike/xrogue/list.c, Revision 1.1

1.1     ! rubenllo    1: /*
        !             2:     list.c - Functions for dealing with linked lists of goodies
        !             3:
        !             4:     XRogue: Expeditions into the Dungeons of Doom
        !             5:     Copyright (C) 1991 Robert Pietkivitch
        !             6:     All rights reserved.
        !             7:
        !             8:     Based on "Advanced Rogue"
        !             9:     Copyright (C) 1984, 1985 Michael Morgan, Ken Dalka and AT&T
        !            10:     All rights reserved.
        !            11:
        !            12:     Based on "Rogue: Exploring the Dungeons of Doom"
        !            13:     Copyright (C) 1980, 1981 Michael Toy, Ken Arnold and Glenn Wichman
        !            14:     All rights reserved.
        !            15:
        !            16:     See the file LICENSE.TXT for full copyright and licensing information.
        !            17: */
        !            18:
        !            19: #include <stdlib.h>
        !            20: #include <string.h>
        !            21: #include <curses.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_fire_list
        !           104:        Throw the whole list of fire monsters away. But don't
        !           105:        discard the item (monster) itself as that belong to mlist.
        !           106: */
        !           107:
        !           108: void
        !           109: _r_free_fire_list(struct linked_list **ptr)
        !           110: {
        !           111:     register struct linked_list *item;
        !           112:
        !           113:         while (*ptr != NULL)
        !           114:         {
        !           115:             item = *ptr;
        !           116:             *ptr = next(item);
        !           117:             free(item);
        !           118:         }
        !           119: }
        !           120: /*
        !           121:  * r_free_list:
        !           122:  *      Throw the whole list of room exits away
        !           123:  */
        !           124:
        !           125: void
        !           126: _r_free_list(struct linked_list **ptr)
        !           127: {
        !           128:     register struct linked_list *item;
        !           129:
        !           130:     while (*ptr != NULL)
        !           131:     {
        !           132:         item = *ptr;
        !           133:         *ptr = next(item);
        !           134:         r_discard(item);
        !           135:     }
        !           136: }
        !           137:
        !           138: /*
        !           139:  * r_discard:
        !           140:  *      free up an item and its room
        !           141:  */
        !           142:
        !           143: void
        !           144: r_discard(struct linked_list *item)
        !           145: {
        !           146:     total -= 2;
        !           147:     FREE(DOORPTR(item));
        !           148:     FREE(item);
        !           149: }
        !           150:
        !           151: /*
        !           152:  * t_free_list:
        !           153:  *      Throw the whole thing list away
        !           154:  */
        !           155:
        !           156: void
        !           157: _t_free_list(struct linked_list **ptr)
        !           158: {
        !           159:     register struct linked_list *item;
        !           160:
        !           161:     while (*ptr != NULL)
        !           162:     {
        !           163:         item = *ptr;
        !           164:         *ptr = next(item);
        !           165:         t_discard(item);
        !           166:     }
        !           167: }
        !           168:
        !           169: /*
        !           170:  * t_discard:
        !           171:  *      free up an item and its thing
        !           172:  */
        !           173:
        !           174: void
        !           175: t_discard(struct linked_list *item)
        !           176: {
        !           177:     register struct thing *tp;
        !           178:
        !           179:     total -= 2;
        !           180:     tp = THINGPTR(item);
        !           181:     if (tp->t_name != NULL) FREE(tp->t_name);
        !           182:     if (tp->t_pack != NULL)
        !           183:         o_free_list(tp->t_pack);
        !           184:     FREE(tp);
        !           185:     FREE(item);
        !           186: }
        !           187:
        !           188: /*
        !           189:  * destroy_item:
        !           190:  *      get rid of an item structure -- don't worry about contents
        !           191:  */
        !           192:
        !           193: void
        !           194: destroy_item(struct linked_list *item)
        !           195: {
        !           196:     total--;
        !           197:     FREE(item);
        !           198: }
        !           199:
        !           200: /*
        !           201:  * new_item
        !           202:  *      get a new item with a specified size
        !           203:  */
        !           204:
        !           205: struct linked_list *
        !           206: new_item(int size)
        !           207: {
        !           208:     register struct linked_list *item;
        !           209:
        !           210:     if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
        !           211:         msg("Ran out of memory for header after %d items", total);
        !           212:     if ((item->l_data = new(size)) == NULL)
        !           213:         msg("Ran out of memory for data after %d items", total);
        !           214:     item->l_next = item->l_prev = NULL;
        !           215:     memset(item->l_data,0,size);
        !           216:     return item;
        !           217: }
        !           218:
        !           219: /*
        !           220:  * creat_item:
        !           221:  *      Create just an item structure -- don't make any contents
        !           222:  */
        !           223:
        !           224: struct linked_list *
        !           225: creat_item(void)
        !           226: {
        !           227:     register struct linked_list *item;
        !           228:
        !           229:     if ((item = (struct linked_list *) new(sizeof *item)) == NULL)
        !           230:         msg("Ran out of memory for header after %d items", total);
        !           231:     item->l_next = item->l_prev = NULL;
        !           232:     return item;
        !           233: }
        !           234:
        !           235: char *
        !           236: new(int size)
        !           237: {
        !           238:     register char *space = ALLOC(size);
        !           239:
        !           240:     if (space == NULL) {
        !           241:         sprintf(prbuf,"Rogue ran out of memory (used = %ld, wanted = %d).",
        !           242:                 md_memused(), size);
        !           243:         fatal(prbuf);
        !           244:     }
        !           245:     total++;
        !           246:     return space;
        !           247: }
        !           248:

CVSweb