aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/config/symbolicNameTable.c
blob: ccf416d670885fd8e4a9142c6c5e2a338c1339e8 (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
/*
 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <ctype.h>
#include <parc/algol/parc_Hash.h>
#include <parc/algol/parc_HashCodeTable.h>
#include <parc/algol/parc_Memory.h>
#include <parc/assert/parc_Assert.h>
#include <src/config.h>
#include <stdio.h>

#include <src/config/symbolicNameTable.h>

struct symblic_name_table {
  PARCHashCodeTable *symbolicNameTable;
  PARCHashCodeTable *indexToNameTable;
};

// ========================================================================================
// symbolic name table functions

static bool _symbolicNameEquals(const void *keyA, const void *keyB) {
  return (strcasecmp((const char *)keyA, (const char *)keyB) == 0);
}

static HashCodeType _symbolicNameHash(const void *keyA) {
  const char *str = (const char *)keyA;
  size_t length = strlen(str);
  return parcHash32_Data(str, length);
}

static bool _connectionIdEquals(const void *keyA, const void *keyB) {
  unsigned idA = *((unsigned *)keyA);
  unsigned idB = *((unsigned *)keyB);
  return (idA == idB);
}

static HashCodeType _connectionIdHash(const void *keyA) {
  unsigned idA = *((unsigned *)keyA);
  return parcHash32_Int32(idA);
}

// ========================================================================================

SymbolicNameTable *symbolicNameTable_Create(void) {
  SymbolicNameTable *table = parcMemory_Allocate(sizeof(SymbolicNameTable));

  if (table) {
    // key = char *
    // value = uint32_t *
    table->symbolicNameTable = parcHashCodeTable_Create(
        _symbolicNameEquals, _symbolicNameHash, parcMemory_DeallocateImpl,
        parcMemory_DeallocateImpl);
    table->indexToNameTable = parcHashCodeTable_Create(
        _connectionIdEquals, _connectionIdHash, parcMemory_DeallocateImpl,
        parcMemory_DeallocateImpl);
  }

  return table;
}

void symbolicNameTable_Destroy(SymbolicNameTable **tablePtr) {
  SymbolicNameTable *table = *tablePtr;
  parcHashCodeTable_Destroy(&table->symbolicNameTable);
  // parcHashCodeTable_Destroy(&table->indexToNameTable);
  parcMemory_Deallocate((void **)&table);
  *tablePtr = NULL;
}

static char *_createKey(const char *symbolicName) {
  char *key = parcMemory_StringDuplicate(symbolicName, strlen(symbolicName));

  // convert key to upper case
  char *p = key;

  // keeps looping until the first null
  while ((*p = toupper(*p))) {
    p++;
  }
  return key;
}

bool symbolicNameTable_Exists(SymbolicNameTable *table,
                              const char *symbolicName) {
  parcAssertNotNull(table, "Parameter table must be non-null");
  parcAssertNotNull(symbolicName, "Parameter symbolicName must be non-null");

  char *key = _createKey(symbolicName);
  bool found = (parcHashCodeTable_Get(table->symbolicNameTable, key) != NULL);
  parcMemory_Deallocate((void **)&key);
  return found;
}

void symbolicNameTable_Remove(SymbolicNameTable *table,
                              const char *symbolicName) {
  parcAssertNotNull(table, "Parameter table must be non-null");
  parcAssertNotNull(symbolicName, "Parameter symbolicName must be non-null");

  char *key = _createKey(symbolicName);

  unsigned id = symbolicNameTable_Get(table, symbolicName);
  uint32_t *value = parcMemory_Allocate(sizeof(uint32_t));
  *value = id;

  parcHashCodeTable_Del(table->symbolicNameTable, key);
  parcHashCodeTable_Del(table->indexToNameTable, value);
  parcMemory_Deallocate((void **)&key);
  parcMemory_Deallocate((void **)&value);
}

bool symbolicNameTable_Add(SymbolicNameTable *table, const char *symbolicName,
                           unsigned connid) {
  parcAssertNotNull(table, "Parameter table must be non-null");
  parcAssertNotNull(symbolicName, "Parameter symbolicName must be non-null");
  parcAssertTrue(connid < UINT32_MAX, "Parameter connid must be less than %u",
                 UINT32_MAX);

  char *key = _createKey(symbolicName);

  uint32_t *value = parcMemory_Allocate(sizeof(uint32_t));
  *value = connid;

  bool success = parcHashCodeTable_Add(table->symbolicNameTable, key, value);
  success = parcHashCodeTable_Add(table->indexToNameTable, value, key);
  if (!success) {
    parcMemory_Deallocate((void **)&key);
    parcMemory_Deallocate((void **)&value);
  }

  return success;
}

unsigned symbolicNameTable_Get(SymbolicNameTable *table,
                               const char *symbolicName) {
  parcAssertNotNull(table, "Parameter table must be non-null");
  parcAssertNotNull(symbolicName, "Parameter symbolicName must be non-null");

  unsigned connid = UINT32_MAX;

  char *key = _createKey(symbolicName);

  uint32_t *value = parcHashCodeTable_Get(table->symbolicNameTable, key);
  if (value) {
    connid = *value;
  }

  parcMemory_Deallocate((void **)&key);
  return connid;
}

const char *symbolicNameTable_GetNameByIndex(SymbolicNameTable *table,
                                             unsigned id) {
  parcAssertNotNull(table, "Parameter table must be non-null");

  uint32_t *value = parcMemory_Allocate(sizeof(uint32_t));
  *value = id;

  const char *name = parcHashCodeTable_Get(table->indexToNameTable, value);
  if (name == NULL) name = "";

  parcMemory_Deallocate((void **)&value);
  return name;
}