source: trunk/src/dict.cpp @ 420

Last change on this file since 420 was 420, checked in by sam, 10 years ago

Fix the Dict class. It wasn't even implemented properly.

  • 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 <cstdio>
17#include <cstdlib>
18
19#include "core.h"
20
21#if defined WIN32
22#   define strcasecmp _stricmp
23#endif
24
25/*
26 * Dict implementation class
27 */
28
29class DictData
30{
31    friend class Dict;
32
33public:
34    DictData() :
35        entities(0),
36        maxid(0),
37        nentities(0)
38    {
39        /* Nothing to do */
40    }
41
42    ~DictData()
43    {
44#if !FINAL_RELEASE
45        if (nentities)
46            fprintf(stderr, "ERROR: still %i entities in dict\n", nentities);
47#endif
48        free(entities);
49    }
50
51private:
52    Entity **entities;
53    int maxid, nentities;
54};
55
56/*
57 * Public Dict class
58 */
59
60Dict::Dict()
61  : data(new DictData())
62{
63}
64
65Dict::~Dict()
66{
67    delete data;
68}
69
70int Dict::MakeSlot(char const *name)
71{
72    int id, empty = -1;
73
74    /* If the entry is already registered, remember its ID. Look for an
75     * empty slot at the same time. */
76    for (id = 0; id < data->maxid; id++)
77    {
78        Entity *e = data->entities[id];
79        if (!e)
80        {
81            empty = id;
82            break;
83        }
84        else
85        {
86            char const *oldname = e->GetName();
87            if (*oldname == '<')
88            {
89                while (*oldname && *oldname != '>')
90                    oldname++;
91                while (*oldname == '>')
92                    oldname++;
93                while (*oldname == ' ')
94                    oldname++;
95            }
96
97            if (!strcasecmp(name, oldname))
98                break;
99        }
100    }
101
102    /* If this is a new entry, create a new slot for it. */
103    if (id == data->maxid || !data->entities[id])
104    {
105        if (id == data->maxid)
106        {
107            empty = data->maxid++;
108            data->entities = (Entity **)realloc(data->entities,
109                                                data->maxid * sizeof(Entity *));
110        }
111
112        data->entities[empty] = NULL;
113        id = empty;
114        data->nentities++;
115    }
116    else
117    {
118        Ticker::Ref(data->entities[id]);
119    }
120
121    return id;
122}
123
124void Dict::RemoveSlot(int id)
125{
126    if (Ticker::Unref(data->entities[id]) == 0)
127    {
128        data->entities[id] = NULL;
129        data->nentities--;
130    }
131}
132
133
134void Dict::SetEntity(int id, Entity *entity)
135{
136    Ticker::Ref(entity);
137    data->entities[id] = entity;
138}
139
140Entity *Dict::GetEntity(int id)
141{
142    return data->entities[id];
143}
144
Note: See TracBrowser for help on using the repository browser.