source: trunk/src/dict.cpp @ 686

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

Put everything in the "lol" namespace. Better late than never.

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