source: trunk/src/dict.cpp @ 735

Last change on this file since 735 was 735, checked in by sam, 9 years ago

core: get rid of now useless <cstdio> includes.

  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstring>
16#include <cstdlib>
17
18#include "core.h"
19
20#if defined WIN32
21#   define strcasecmp _stricmp
22#endif
23
24namespace lol
25{
26
27/*
28 * Dict implementation class
29 */
30
31class DictData
32{
33    friend class Dict;
34
35public:
36    DictData() :
37        entities(0),
38        maxid(0),
39        nentities(0)
40    {
41        /* Nothing to do */
42    }
43
44    ~DictData()
45    {
46#if !LOL_RELEASE
47        if (nentities)
48            Log::Error("still %i entities in dict\n", nentities);
49#endif
50        free(entities);
51    }
52
53private:
54    Entity **entities;
55    int maxid, nentities;
56};
57
58/*
59 * Public Dict class
60 */
61
62Dict::Dict()
63  : data(new DictData())
64{
65}
66
67Dict::~Dict()
68{
69    delete data;
70}
71
72int Dict::MakeSlot(char const *name)
73{
74    int id, empty = -1;
75
76    /* If the entry is already registered, remember its ID. Look for an
77     * empty slot at the same time. */
78    for (id = 0; id < data->maxid; id++)
79    {
80        Entity *e = data->entities[id];
81        if (!e)
82        {
83            empty = id;
84            break;
85        }
86        else
87        {
88            char const *oldname = e->GetName();
89            if (*oldname == '<')
90            {
91                while (*oldname && *oldname != '>')
92                    oldname++;
93                while (*oldname == '>')
94                    oldname++;
95                while (*oldname == ' ')
96                    oldname++;
97            }
98
99            if (!strcasecmp(name, oldname))
100                break;
101        }
102    }
103
104    /* If this is a new entry, create a new slot for it. */
105    if (id == data->maxid || !data->entities[id])
106    {
107        if (id == data->maxid)
108        {
109            empty = data->maxid++;
110            data->entities = (Entity **)realloc(data->entities,
111                                                data->maxid * sizeof(Entity *));
112        }
113
114        data->entities[empty] = NULL;
115        id = empty;
116        data->nentities++;
117    }
118    else
119    {
120        Ticker::Ref(data->entities[id]);
121    }
122
123    return id;
124}
125
126void Dict::RemoveSlot(int id)
127{
128    if (Ticker::Unref(data->entities[id]) == 0)
129    {
130        data->entities[id] = NULL;
131        data->nentities--;
132    }
133}
134
135
136void Dict::SetEntity(int id, Entity *entity)
137{
138    Ticker::Ref(entity);
139    data->entities[id] = entity;
140}
141
142Entity *Dict::GetEntity(int id)
143{
144    return data->entities[id];
145}
146
147} /* namespace lol */
148
Note: See TracBrowser for help on using the repository browser.