author | alfadur <mail@none> |
Wed, 06 Feb 2019 22:40:38 +0300 | |
changeset 14712 | 2071da901c63 |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
1 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
2 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
3 |
@file dictionary.c |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
4 |
@author N. Devillard |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
5 |
@brief Implements a dictionary for string variables. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
6 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
7 |
This module implements a simple dictionary object, i.e. a list |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
8 |
of string/string associations. This object is useful to store e.g. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
9 |
informations retrieved from a configuration file (ini files). |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
10 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
11 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
12 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
13 |
/*--------------------------------------------------------------------------- |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
14 |
Includes |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
15 |
---------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
16 |
#include "dictionary.h" |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
17 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
18 |
#include <stdio.h> |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
19 |
#include <stdlib.h> |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
#include <string.h> |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
#include <unistd.h> |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
22 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
23 |
/** Maximum value size for integers and doubles. */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
#define MAXVALSZ 1024 |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
/** Minimal allocated number of entries in a dictionary */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
27 |
#define DICTMINSZ 128 |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
/** Invalid key token */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
#define DICT_INVALID_KEY ((char*)-1) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
/*--------------------------------------------------------------------------- |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
Private functions |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
---------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
35 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
/* Doubles the allocated size associated to a pointer */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
/* 'size' is the current allocated size. */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
static void * mem_double(void * ptr, int size) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
void * newptr ; |
10017 | 41 |
|
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
newptr = calloc(2*size, 1); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
if (newptr==NULL) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
return NULL ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
46 |
memcpy(newptr, ptr, size); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
47 |
free(ptr); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
48 |
return newptr ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
49 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
50 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
51 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
52 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
53 |
@brief Duplicate a string |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
54 |
@param s String to duplicate |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
55 |
@return Pointer to a newly allocated string, to be freed with free() |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
56 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
57 |
This is a replacement for strdup(). This implementation is provided |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
58 |
for systems that do not have it. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
59 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
60 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
61 |
static char * xstrdup(const char * s) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
62 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
63 |
char * t ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
64 |
if (!s) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
65 |
return NULL ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
66 |
t = (char*)malloc(strlen(s)+1) ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
67 |
if (t) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
68 |
strcpy(t,s); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
69 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
70 |
return t ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
71 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
72 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
73 |
/*--------------------------------------------------------------------------- |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
74 |
Function codes |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
75 |
---------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
76 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
77 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
78 |
@brief Compute the hash key for a string. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
79 |
@param key Character string to use for key. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
80 |
@return 1 unsigned int on at least 32 bits. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
81 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
82 |
This hash function has been taken from an Article in Dr Dobbs Journal. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
83 |
This is normally a collision-free function, distributing keys evenly. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
84 |
The key is stored anyway in the struct so that collision can be avoided |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
85 |
by comparing the key itself in last resort. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
86 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
87 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
88 |
unsigned dictionary_hash(const char * key) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
89 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
90 |
int len ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
91 |
unsigned hash ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
92 |
int i ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
93 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
94 |
len = strlen(key); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
95 |
for (hash=0, i=0 ; i<len ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
96 |
hash += (unsigned)key[i] ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
97 |
hash += (hash<<10); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
98 |
hash ^= (hash>>6) ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
99 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
100 |
hash += (hash <<3); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
101 |
hash ^= (hash >>11); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
102 |
hash += (hash <<15); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
103 |
return hash ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
104 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
105 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
106 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
107 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
108 |
@brief Create a new dictionary object. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
109 |
@param size Optional initial size of the dictionary. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
110 |
@return 1 newly allocated dictionary objet. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
111 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
112 |
This function allocates a new dictionary object of given size and returns |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
113 |
it. If you do not know in advance (roughly) the number of entries in the |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
114 |
dictionary, give size=0. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
115 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
116 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
117 |
dictionary * dictionary_new(int size) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
118 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
119 |
dictionary * d ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
120 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
121 |
/* If no size was specified, allocate space for DICTMINSZ */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
122 |
if (size<DICTMINSZ) size=DICTMINSZ ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
123 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
124 |
if (!(d = (dictionary *)calloc(1, sizeof(dictionary)))) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
125 |
return NULL; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
126 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
127 |
d->size = size ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
128 |
d->val = (char **)calloc(size, sizeof(char*)); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
129 |
d->key = (char **)calloc(size, sizeof(char*)); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
130 |
d->hash = (unsigned int *)calloc(size, sizeof(unsigned)); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
131 |
return d ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
132 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
133 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
134 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
135 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
136 |
@brief Delete a dictionary object |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
137 |
@param d dictionary object to deallocate. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
138 |
@return void |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
139 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
140 |
Deallocate a dictionary object and all memory associated to it. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
141 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
142 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
143 |
void dictionary_del(dictionary * d) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
144 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
145 |
int i ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
146 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
147 |
if (d==NULL) return ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
148 |
for (i=0 ; i<d->size ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
149 |
if (d->key[i]!=NULL) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
150 |
free(d->key[i]); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
151 |
if (d->val[i]!=NULL) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
152 |
free(d->val[i]); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
153 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
154 |
free(d->val); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
155 |
free(d->key); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
156 |
free(d->hash); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
157 |
free(d); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
158 |
return ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
159 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
160 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
161 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
162 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
163 |
@brief Get a value from a dictionary. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
164 |
@param d dictionary object to search. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
165 |
@param key Key to look for in the dictionary. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
166 |
@param def Default value to return if key not found. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
167 |
@return 1 pointer to internally allocated character string. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
168 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
169 |
This function locates a key in a dictionary and returns a pointer to its |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
170 |
value, or the passed 'def' pointer if no such key can be found in |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
171 |
dictionary. The returned character pointer points to data internal to the |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
172 |
dictionary object, you should not try to free it or modify it. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
173 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
174 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
175 |
char * dictionary_get(dictionary * d, const char * key, char * def) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
176 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
177 |
unsigned hash ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
178 |
int i ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
179 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
180 |
hash = dictionary_hash(key); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
181 |
for (i=0 ; i<d->size ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
182 |
if (d->key[i]==NULL) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
183 |
continue ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
184 |
/* Compare hash */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
185 |
if (hash==d->hash[i]) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
186 |
/* Compare string, to avoid hash collisions */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
187 |
if (!strcmp(key, d->key[i])) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
188 |
return d->val[i] ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
189 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
190 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
191 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
192 |
return def ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
193 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
194 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
195 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
196 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
197 |
@brief Set a value in a dictionary. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
198 |
@param d dictionary object to modify. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
199 |
@param key Key to modify or add. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
200 |
@param val Value to add. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
201 |
@return int 0 if Ok, anything else otherwise |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
202 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
203 |
If the given key is found in the dictionary, the associated value is |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
204 |
replaced by the provided one. If the key cannot be found in the |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
205 |
dictionary, it is added to it. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
206 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
207 |
It is Ok to provide a NULL value for val, but NULL values for the dictionary |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
208 |
or the key are considered as errors: the function will return immediately |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
209 |
in such a case. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
210 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
211 |
Notice that if you dictionary_set a variable to NULL, a call to |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
212 |
dictionary_get will return a NULL value: the variable will be found, and |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
213 |
its value (NULL) is returned. In other words, setting the variable |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
214 |
content to NULL is equivalent to deleting the variable from the |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
215 |
dictionary. It is not possible (in this implementation) to have a key in |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
216 |
the dictionary without value. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
217 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
218 |
This function returns non-zero in case of failure. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
219 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
220 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
221 |
int dictionary_set(dictionary * d, const char * key, const char * val) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
222 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
223 |
int i ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
224 |
unsigned hash ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
225 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
226 |
if (d==NULL || key==NULL) return -1 ; |
10017 | 227 |
|
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
228 |
/* Compute hash for this key */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
229 |
hash = dictionary_hash(key) ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
230 |
/* Find if value is already in dictionary */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
231 |
if (d->n>0) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
232 |
for (i=0 ; i<d->size ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
233 |
if (d->key[i]==NULL) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
234 |
continue ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
235 |
if (hash==d->hash[i]) { /* Same hash value */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
236 |
if (!strcmp(key, d->key[i])) { /* Same key */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
237 |
/* Found a value: modify and return */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
238 |
if (d->val[i]!=NULL) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
239 |
free(d->val[i]); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
240 |
d->val[i] = val ? xstrdup(val) : NULL ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
241 |
/* Value has been modified: return */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
242 |
return 0 ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
243 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
244 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
245 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
246 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
247 |
/* Add a new value */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
248 |
/* See if dictionary needs to grow */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
249 |
if (d->n==d->size) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
250 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
251 |
/* Reached maximum size: reallocate dictionary */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
252 |
d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
253 |
d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
254 |
d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
255 |
if ((d->val==NULL) || (d->key==NULL) || (d->hash==NULL)) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
256 |
/* Cannot grow dictionary */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
257 |
return -1 ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
258 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
259 |
/* Double size */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
260 |
d->size *= 2 ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
261 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
262 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
263 |
/* Insert key in the first empty slot. Start at d->n and wrap at |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
264 |
d->size. Because d->n < d->size this will necessarily |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
265 |
terminate. */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
266 |
for (i=d->n ; d->key[i] ; ) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
267 |
if(++i == d->size) i = 0; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
268 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
269 |
/* Copy key */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
270 |
d->key[i] = xstrdup(key); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
271 |
d->val[i] = val ? xstrdup(val) : NULL ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
272 |
d->hash[i] = hash; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
273 |
d->n ++ ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
274 |
return 0 ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
275 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
276 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
277 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
278 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
279 |
@brief Delete a key in a dictionary |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
280 |
@param d dictionary object to modify. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
281 |
@param key Key to remove. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
282 |
@return void |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
283 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
284 |
This function deletes a key in a dictionary. Nothing is done if the |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
285 |
key cannot be found. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
286 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
287 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
288 |
void dictionary_unset(dictionary * d, const char * key) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
289 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
290 |
unsigned hash ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
291 |
int i ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
292 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
293 |
if (key == NULL) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
294 |
return; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
295 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
296 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
297 |
hash = dictionary_hash(key); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
298 |
for (i=0 ; i<d->size ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
299 |
if (d->key[i]==NULL) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
300 |
continue ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
301 |
/* Compare hash */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
302 |
if (hash==d->hash[i]) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
303 |
/* Compare string, to avoid hash collisions */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
304 |
if (!strcmp(key, d->key[i])) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
305 |
/* Found key */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
306 |
break ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
307 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
308 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
309 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
310 |
if (i>=d->size) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
311 |
/* Key not found */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
312 |
return ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
313 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
314 |
free(d->key[i]); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
315 |
d->key[i] = NULL ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
316 |
if (d->val[i]!=NULL) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
317 |
free(d->val[i]); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
318 |
d->val[i] = NULL ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
319 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
320 |
d->hash[i] = 0 ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
321 |
d->n -- ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
322 |
return ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
323 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
324 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
325 |
/*-------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
326 |
/** |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
327 |
@brief Dump a dictionary to an opened file pointer. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
328 |
@param d Dictionary to dump |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
329 |
@param f Opened file pointer. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
330 |
@return void |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
331 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
332 |
Dumps a dictionary onto an opened file pointer. Key pairs are printed out |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
333 |
as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
334 |
output file pointers. |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
335 |
*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
336 |
/*--------------------------------------------------------------------------*/ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
337 |
void dictionary_dump(dictionary * d, FILE * out) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
338 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
339 |
int i ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
340 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
341 |
if (d==NULL || out==NULL) return ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
342 |
if (d->n<1) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
343 |
fprintf(out, "empty dictionary\n"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
344 |
return ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
345 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
346 |
for (i=0 ; i<d->size ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
347 |
if (d->key[i]) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
348 |
fprintf(out, "%20s\t[%s]\n", |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
349 |
d->key[i], |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
350 |
d->val[i] ? d->val[i] : "UNDEF"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
351 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
352 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
353 |
return ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
354 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
355 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
356 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
357 |
/* Test code */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
358 |
#ifdef TESTDIC |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
359 |
#define NVALS 20000 |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
360 |
int main(int argc, char *argv[]) |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
361 |
{ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
362 |
dictionary * d ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
363 |
char * val ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
364 |
int i ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
365 |
char cval[90] ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
366 |
|
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
367 |
/* Allocate dictionary */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
368 |
printf("allocating...\n"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
369 |
d = dictionary_new(0); |
10017 | 370 |
|
7175
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
371 |
/* Set values in dictionary */ |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
372 |
printf("setting %d values...\n", NVALS); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
373 |
for (i=0 ; i<NVALS ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
374 |
sprintf(cval, "%04d", i); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
375 |
dictionary_set(d, cval, "salut"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
376 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
377 |
printf("getting %d values...\n", NVALS); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
378 |
for (i=0 ; i<NVALS ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
379 |
sprintf(cval, "%04d", i); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
380 |
val = dictionary_get(d, cval, DICT_INVALID_KEY); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
381 |
if (val==DICT_INVALID_KEY) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
382 |
printf("cannot get value for key [%s]\n", cval); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
383 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
384 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
385 |
printf("unsetting %d values...\n", NVALS); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
386 |
for (i=0 ; i<NVALS ; i++) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
387 |
sprintf(cval, "%04d", i); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
388 |
dictionary_unset(d, cval); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
389 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
390 |
if (d->n != 0) { |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
391 |
printf("error deleting values\n"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
392 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
393 |
printf("deallocating...\n"); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
394 |
dictionary_del(d); |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
395 |
return 0 ; |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
396 |
} |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
397 |
#endif |
038e3415100a
Added ini reading/writing for game schemes to the frontend lib
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
398 |
/* vim: set ts=4 et sw=4 tw=75 */ |