summaryrefslogtreecommitdiff
path: root/im/src/im_attrib.cpp
blob: 10d45990056b40b428d034118669b3dd082cc709 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/** \file
 * \brief Attributes Table
 *
 * See Copyright Notice in im_lib.h
 * $Id: im_attrib.cpp,v 1.3 2009/08/22 04:31:04 scuri Exp $
 */

#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <assert.h>

#include "im_attrib.h"
#include "im_util.h"

#define IM_DEFAULTSIZE 101
#define IM_MULTIPLIER 31

// Unique Hash index for a name
static int iHashIndex(const char *name, int hash_size)
{
  unsigned short hash = 0;
  const unsigned char *p_name = (const unsigned char*)name;

  for(; *p_name; p_name++)
    hash = hash*IM_MULTIPLIER + *p_name;

  return hash % hash_size;
}


/*******************************************************************/


class imAttribNode
{
public:
  int data_type;
  int count;
  void* data;
  char* name;

  imAttribNode* next;

  imAttribNode(const char* name, int _data_type, int _count, const void* _data, imAttribNode* next);
  ~imAttribNode();
};

static char* utlStrDup(const char* str)
{
  int size;
  char* new_str;

  assert(str);

  size = strlen(str) + 1;
  new_str = (char*)malloc(size);
  memcpy(new_str, str, size);

  return new_str;
}

imAttribNode::imAttribNode(const char* name, int _data_type, int _count, const void* _data, imAttribNode* _next)
{
  if (_data_type == 0 && _count == -1)  /* BYTE meaning a string */
    _count = strlen((char*)_data)+1;

  this->name = utlStrDup(name);
  this->data_type = _data_type;
  this->count = _count;
  this->next = _next;

  int size = _count * imDataTypeSize(_data_type);
  this->data = malloc(size);
  if (_data) memcpy(this->data, _data, size);
  else memset(this->data, 0, size);
}

imAttribNode::~imAttribNode()
{
  free(this->name); 
  free(this->data);
}


/*******************************************************************/

struct imAttribTablePrivate
{
  int count,       
      hash_size;   
  imAttribNode* *hash_table;
};

imAttribTablePrivate* imAttribTableCreate(int hash_size)
{
  imAttribTablePrivate* ptable = (imAttribTablePrivate*)malloc(sizeof(imAttribTablePrivate));
  ptable->count = 0;
  ptable->hash_size = (hash_size == 0)? IM_DEFAULTSIZE: hash_size;
  ptable->hash_table = (imAttribNode**)malloc(ptable->hash_size*sizeof(imAttribNode*));
  memset(ptable->hash_table, 0, ptable->hash_size*sizeof(imAttribNode*));
  return ptable;
}

imAttribTablePrivate* imAttribArrayCreate(int count)
{
  imAttribTablePrivate* ptable = (imAttribTablePrivate*)malloc(sizeof(imAttribTablePrivate));
  ptable->hash_size = ptable->count = count;
  ptable->hash_table = (imAttribNode**)malloc(ptable->count*sizeof(imAttribNode*));
  memset(ptable->hash_table, 0, ptable->hash_size*sizeof(imAttribNode*));
  return ptable;
}

void imAttribTableDestroy(imAttribTablePrivate* ptable)
{
  imAttribTableRemoveAll(ptable);
  free(ptable->hash_table);
  free(ptable);
}

int imAttribTableCount(imAttribTablePrivate* ptable)
{
  return ptable->count;
}

void imAttribTableRemoveAll(imAttribTablePrivate* ptable)
{
  if (ptable->count == 0) return;

  int n = 0;
  for(int i = 0; i < ptable->hash_size; i++) 
  {
    imAttribNode* cur_node = ptable->hash_table[i];
    while (cur_node) 
    {
      imAttribNode* next_node = cur_node->next;
      delete cur_node;
      cur_node = next_node;
      n++;
    }

    ptable->hash_table[i] = NULL;

    if (n == ptable->count)
      break;
  }
  
  ptable->count = 0;
}

void imAttribTableSet(imAttribTablePrivate* ptable, const char* name, int data_type, int count, const void* data)
{
  assert(name);

  int index = iHashIndex(name, ptable->hash_size);
  imAttribNode* first_node = ptable->hash_table[index];

  // The name already exists ?
  imAttribNode* cur_node = first_node;
  imAttribNode* prev_node = NULL;
  while (cur_node) 
  {
    if (imStrEqual(cur_node->name, name))
    {
      // Found, replace current node.
      imAttribNode* new_node = new imAttribNode(name, data_type, count, data, cur_node->next);

      // Is first node ?
      if (cur_node == first_node)
        ptable->hash_table[index] = new_node;
      else
        prev_node->next = new_node;

      delete cur_node;
      return;
    }

    prev_node = cur_node;
    cur_node = cur_node->next;
  }

  // Not found, the new item goes first.
  cur_node = new imAttribNode(name, data_type, count, data, first_node);
  ptable->hash_table[index] = cur_node;
	ptable->count++;
}

void imAttribTableUnSet(imAttribTablePrivate* ptable, const char *name)
{
  assert(name);

  if (ptable->count == 0) return;

  int index = iHashIndex(name, ptable->hash_size);

  imAttribNode* cur_node = ptable->hash_table[index];
  imAttribNode* prev_node = cur_node;
  while (cur_node) 
  {
    if (imStrEqual(cur_node->name, name))
    {
      // Is first node ?
      if (cur_node == prev_node)
        ptable->hash_table[index] = cur_node->next;
      else
        prev_node->next = cur_node->next;

      delete cur_node;
      ptable->count--;
      return;
    }

    prev_node = cur_node;
    cur_node = cur_node->next;
  }
}

const void* imAttribTableGet(const imAttribTablePrivate* ptable, const char *name, int *data_type, int *count)
{
  assert(name);

  if (ptable->count == 0) return NULL;

  int index = iHashIndex(name, ptable->hash_size);

  imAttribNode* cur_node = ptable->hash_table[index];
  while (cur_node) 
  {
    if (imStrEqual(cur_node->name, name))
    {
      if (data_type) *data_type = cur_node->data_type;
      if (count) *count = cur_node->count;
      return cur_node->data;
    }

    cur_node = cur_node->next;
  }

  return NULL;
}

void imAttribArraySet(imAttribTablePrivate* ptable, int index, const char* name, int data_type, int count, const void* data)
{
  assert(name);
  assert(index < ptable->count);

  if (index >= ptable->count) return;

  imAttribNode* node = ptable->hash_table[index];
  if (node) delete node;

  ptable->hash_table[index] = new imAttribNode(name, data_type, count, data, NULL);
}

const void* imAttribArrayGet(const imAttribTablePrivate* ptable, int index, char *name, int *data_type, int *count)
{
  if (ptable->count == 0) return NULL;

  imAttribNode* node = ptable->hash_table[index];
  if (node) 
  {
    if (name) strcpy(name, node->name);
    if (data_type) *data_type = node->data_type;
    if (count) *count = node->count;
    return node->data;
  }

  return NULL;
}

void imAttribTableForEach(const imAttribTablePrivate* ptable, void* user_data, imAttribTableCallback attrib_func)
{
  assert(attrib_func);

  if (ptable->count == 0) return;

  int index = 0;
  for(int i = 0; i < ptable->hash_size; i++) 
  {
    imAttribNode* cur_node = ptable->hash_table[i];
    while (cur_node) 
    {
      if (!attrib_func(user_data, index, cur_node->name, cur_node->data_type, cur_node->count, cur_node->data))
        return;

      index++;
      cur_node = cur_node->next;
    }

    if (index == ptable->count)
      return;
  }
}

static int iCopyFunc(void* user_data, int index, const char* name, int data_type, int count, const void* data)
{                  
  (void)index;
  imAttribTablePrivate* ptable = (imAttribTablePrivate*)user_data;
  imAttribTableSet(ptable, name, data_type, count, data);
  return 1;
}

void imAttribTableCopyFrom(imAttribTablePrivate* ptable_dst, const imAttribTablePrivate* ptable_src)
{
  imAttribTableForEach(ptable_src, (void*)ptable_dst, iCopyFunc);
}

static int iCopyArrayFunc(void* user_data, int index, const char* name, int data_type, int count, const void* data)
{                  
  (void)index;
  imAttribTablePrivate* ptable = (imAttribTablePrivate*)user_data;
  imAttribArraySet(ptable, index, name, data_type, count, data);
  return 1;
}

void imAttribArrayCopyFrom(imAttribTablePrivate* ptable_dst, const imAttribTablePrivate* ptable_src)
{
  imAttribTableForEach(ptable_src, (void*)ptable_dst, iCopyArrayFunc);
}