関数 | |
static void * | mem_double (void *ptr, int size) |
unsigned | dictionary_hash (char *key) |
Compute the hash key for a string. | |
dictionary * | dictionary_new (int size) |
Create a new dictionary object. | |
void | dictionary_del (dictionary *d) |
Delete a dictionary object | |
char * | dictionary_get (dictionary *d, char *key, char *def) |
Get a value from a dictionary. | |
char | dictionary_getchar (dictionary *d, char *key, char def) |
Get a value from a dictionary, as a char. | |
int | dictionary_getint (dictionary *d, char *key, int def) |
Get a value from a dictionary, as an int. | |
double | dictionary_getdouble (dictionary *d, char *key, double def) |
Get a value from a dictionary, as a double. | |
void | dictionary_set (dictionary *d, char *key, char *val) |
Set a value in a dictionary. | |
void | dictionary_unset (dictionary *d, char *key) |
Delete a key in a dictionary | |
void | dictionary_setint (dictionary *d, char *key, int val) |
Set a key in a dictionary, providing an int. | |
void | dictionary_setdouble (dictionary *d, char *key, double val) |
Set a key in a dictionary, providing a double. | |
void | dictionary_dump (dictionary *d, FILE *out) |
Dump a dictionary to an opened file pointer. | |
int | main (int argc, char *argv[]) |
dictionary.c で定義されています。
static void* mem_double | ( | void * | ptr, | |
int | size | |||
) | [static] |
dictionary.c の 51 行で定義されています。
00052 { 00053 void * newptr ; 00054 00055 newptr = calloc(2*size, 1); 00056 memcpy(newptr, ptr, size); 00057 free(ptr); 00058 return newptr ; 00059 }
unsigned dictionary_hash | ( | char * | key | ) |
Compute the hash key for a string.
key | Character string to use for key. |
dictionary.c の 79 行で定義されています。
参照先 len.
参照元 dictionary_get()・dictionary_set()・dictionary_unset().
00080 { 00081 int len ; 00082 unsigned hash ; 00083 int i ; 00084 00085 len = strlen(key); 00086 for (hash=0, i=0 ; i<len ; i++) { 00087 hash += (unsigned)key[i] ; 00088 hash += (hash<<10); 00089 hash ^= (hash>>6) ; 00090 } 00091 hash += (hash <<3); 00092 hash ^= (hash >>11); 00093 hash += (hash <<15); 00094 return hash ; 00095 }
dictionary* dictionary_new | ( | int | size | ) |
Create a new dictionary object.
size | Optional initial size of the dictionary. |
dictionary.c の 110 行で定義されています。
参照先 _dictionary_::hash・_dictionary_::key・_dictionary_::size・_dictionary_::val.
参照元 iniparser_load()・main().
00111 { 00112 dictionary * d ; 00113 00114 /* If no size was specified, allocate space for DICTMINSZ */ 00115 if (size<DICTMINSZ) size=DICTMINSZ ; 00116 00117 if (!(d = (dictionary *)calloc(1, sizeof(dictionary)))) { 00118 return NULL; 00119 } 00120 d->size = size ; 00121 d->val = (char **)calloc(size, sizeof(char*)); 00122 d->key = (char **)calloc(size, sizeof(char*)); 00123 d->hash = (unsigned int *)calloc(size, sizeof(unsigned)); 00124 return d ; 00125 }
void dictionary_del | ( | dictionary * | d | ) |
Delete a dictionary object
d | dictionary object to deallocate. |
dictionary.c の 138 行で定義されています。
参照先 _dictionary_::hash・_dictionary_::key・_dictionary_::size・_dictionary_::val.
参照元 iniparser_freedict()・main().
00139 { 00140 int i ; 00141 00142 if (d==NULL) return ; 00143 for (i=0 ; i<d->size ; i++) { 00144 if (d->key[i]!=NULL) 00145 free(d->key[i]); 00146 if (d->val[i]!=NULL) 00147 free(d->val[i]); 00148 } 00149 free(d->val); 00150 free(d->key); 00151 free(d->hash); 00152 free(d); 00153 return ; 00154 }
char* dictionary_get | ( | dictionary * | d, | |
char * | key, | |||
char * | def | |||
) |
Get a value from a dictionary.
d | dictionary object to search. | |
key | Key to look for in the dictionary. | |
def | Default value to return if key not found. |
dictionary.c の 172 行で定義されています。
参照先 dictionary_hash()・_dictionary_::hash・_dictionary_::key・_dictionary_::size・_dictionary_::val.
参照元 dictionary_getchar()・dictionary_getdouble()・dictionary_getint()・iniparser_getstring()・main().
00173 { 00174 unsigned hash ; 00175 int i ; 00176 00177 hash = dictionary_hash(key); 00178 for (i=0 ; i<d->size ; i++) { 00179 if (d->key==NULL) 00180 continue ; 00181 /* Compare hash */ 00182 if (hash==d->hash[i]) { 00183 /* Compare string, to avoid hash collisions */ 00184 if (!strcmp(key, d->key[i])) { 00185 return d->val[i] ; 00186 } 00187 } 00188 } 00189 return def ; 00190 }
char dictionary_getchar | ( | dictionary * | d, | |
char * | key, | |||
char | def | |||
) |
Get a value from a dictionary, as a char.
d | dictionary object to search. | |
key | Key to look for in the dictionary. | |
def | Default value for the key if not found. |
dictionary.c の 204 行で定義されています。
参照先 dictionary_get().
00205 { 00206 char * v ; 00207 00208 if ((v=dictionary_get(d,key,DICT_INVALID_KEY))==DICT_INVALID_KEY) { 00209 return def ; 00210 } else { 00211 return v[0] ; 00212 } 00213 }
int dictionary_getint | ( | dictionary * | d, | |
char * | key, | |||
int | def | |||
) |
Get a value from a dictionary, as an int.
d | dictionary object to search. | |
key | Key to look for in the dictionary. | |
def | Default value for the key if not found. |
dictionary.c の 229 行で定義されています。
参照先 dictionary_get().
00230 { 00231 char * v ; 00232 00233 if ((v=dictionary_get(d,key,DICT_INVALID_KEY))==DICT_INVALID_KEY) { 00234 return def ; 00235 } else { 00236 return atoi(v); 00237 } 00238 }
double dictionary_getdouble | ( | dictionary * | d, | |
char * | key, | |||
double | def | |||
) |
Get a value from a dictionary, as a double.
d | dictionary object to search. | |
key | Key to look for in the dictionary. | |
def | Default value for the key if not found. |
dictionary.c の 253 行で定義されています。
参照先 dictionary_get().
00254 { 00255 char * v ; 00256 00257 if ((v=dictionary_get(d,key,DICT_INVALID_KEY))==DICT_INVALID_KEY) { 00258 return def ; 00259 } else { 00260 return atof(v); 00261 } 00262 }
void dictionary_set | ( | dictionary * | d, | |
char * | key, | |||
char * | val | |||
) |
Set a value in a dictionary.
d | dictionary object to modify. | |
key | Key to modify or add. | |
val | Value to add. |
It is Ok to provide a NULL value for val, but NULL values for the dictionary or the key are considered as errors: the function will return immediately in such a case.
Notice that if you dictionary_set a variable to NULL, a call to dictionary_get will return a NULL value: the variable will be found, and its value (NULL) is returned. In other words, setting the variable content to NULL is equivalent to deleting the variable from the dictionary. It is not possible (in this implementation) to have a key in the dictionary without value.
dictionary.c の 290 行で定義されています。
参照先 dictionary_hash()・_dictionary_::hash・_dictionary_::key・_dictionary_::n・_dictionary_::size・strdup()・_dictionary_::val.
参照元 dictionary_setdouble()・dictionary_setint()・iniparser_add_entry()・iniparser_setstr()・main().
00291 { 00292 int i ; 00293 unsigned hash ; 00294 00295 if (d==NULL || key==NULL) return ; 00296 00297 /* Compute hash for this key */ 00298 hash = dictionary_hash(key) ; 00299 /* Find if value is already in blackboard */ 00300 if (d->n>0) { 00301 for (i=0 ; i<d->size ; i++) { 00302 if (d->key[i]==NULL) 00303 continue ; 00304 if (hash==d->hash[i]) { /* Same hash value */ 00305 if (!strcmp(key, d->key[i])) { /* Same key */ 00306 /* Found a value: modify and return */ 00307 if (d->val[i]!=NULL) 00308 free(d->val[i]); 00309 d->val[i] = val ? strdup(val) : NULL ; 00310 /* Value has been modified: return */ 00311 return ; 00312 } 00313 } 00314 } 00315 } 00316 /* Add a new value */ 00317 /* See if dictionary needs to grow */ 00318 if (d->n==d->size) { 00319 00320 /* Reached maximum size: reallocate blackboard */ 00321 d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ; 00322 d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ; 00323 d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ; 00324 00325 /* Double size */ 00326 d->size *= 2 ; 00327 } 00328 00329 /* Insert key in the first empty slot */ 00330 for (i=0 ; i<d->size ; i++) { 00331 if (d->key[i]==NULL) { 00332 /* Add key here */ 00333 break ; 00334 } 00335 } 00336 /* Copy key */ 00337 d->key[i] = strdup(key); 00338 d->val[i] = val ? strdup(val) : NULL ; 00339 d->hash[i] = hash; 00340 d->n ++ ; 00341 return ; 00342 }
void dictionary_unset | ( | dictionary * | d, | |
char * | key | |||
) |
Delete a key in a dictionary
d | dictionary object to modify. | |
key | Key to remove. |
dictionary.c の 355 行で定義されています。
参照先 dictionary_hash()・_dictionary_::hash・_dictionary_::key・_dictionary_::size.
参照元 iniparser_unset()・main().
00356 { 00357 unsigned hash ; 00358 int i ; 00359 00360 if (key == NULL) { 00361 return; 00362 } 00363 00364 hash = dictionary_hash(key); 00365 for (i=0 ; i<d->size ; i++) { 00366 if (d->key[i]==NULL) 00367 continue ; 00368 /* Compare hash */ 00369 if (hash==d->hash[i]) { 00370 /* Compare string, to avoid hash collisions */ 00371 if (!strcmp(key, d->key[i])) { 00372 /* Found key */ 00373 break ; 00374 } 00375 } 00376 } 00377 if (i>=d->size) 00378 /* Key not found */ 00379 return ; 00380 00381 free(d->key[i]); 00382 d->key[i] = NULL ; 00383 if (d->val[i]!=NULL) { 00384 free(d->val[i]); 00385 d->val[i] = NULL ; 00386 } 00387 d->hash[i] = 0 ; 00388 d->n -- ; 00389 return ; 00390 }
void dictionary_setint | ( | dictionary * | d, | |
char * | key, | |||
int | val | |||
) |
Set a key in a dictionary, providing an int.
d | Dictionary to update. | |
key | Key to modify or add | |
val | Integer value to store (will be stored as a string). |
dictionary.c の 407 行で定義されています。
参照先 dictionary_set()・sprintf().
00408 { 00409 char sval[MAXVALSZ]; 00410 sprintf(sval, "%d", val); 00411 dictionary_set(d, key, sval); 00412 }
void dictionary_setdouble | ( | dictionary * | d, | |
char * | key, | |||
double | val | |||
) |
Set a key in a dictionary, providing a double.
d | Dictionary to update. | |
key | Key to modify or add | |
val | Double value to store (will be stored as a string). |
dictionary.c の 429 行で定義されています。
参照先 dictionary_set()・sprintf().
00430 { 00431 char sval[MAXVALSZ]; 00432 sprintf(sval, "%g", val); 00433 dictionary_set(d, key, sval); 00434 }
void dictionary_dump | ( | dictionary * | d, | |
FILE * | out | |||
) |
Dump a dictionary to an opened file pointer.
d | Dictionary to dump | |
f | Opened file pointer. |
[Key]=[Value], one per line. It is Ok to provide stdout or stderr as output file pointers.
dictionary.c の 451 行で定義されています。
参照先 fprintf()・_dictionary_::key・_dictionary_::n・_dictionary_::size・_dictionary_::val.
00452 { 00453 int i ; 00454 00455 if (d==NULL || out==NULL) return ; 00456 if (d->n<1) { 00457 fprintf(out, "empty dictionary\n"); 00458 return ; 00459 } 00460 for (i=0 ; i<d->size ; i++) { 00461 if (d->key[i]) { 00462 fprintf(out, "%20s\t[%s]\n", 00463 d->key[i], 00464 d->val[i] ? d->val[i] : "UNDEF"); 00465 } 00466 } 00467 return ; 00468 }
int main | ( | int | argc, | |
char * | argv[] | |||
) |
dictionary.c の 475 行で定義されています。
参照先 dictionary_del()・dictionary_get()・dictionary_new()・dictionary_set()・dictionary_unset()・_dictionary_::n・printf()・sprintf().
00476 { 00477 dictionary * d ; 00478 char * val ; 00479 int i ; 00480 char cval[90] ; 00481 00482 /* allocate blackboard */ 00483 printf("allocating...\n"); 00484 d = dictionary_new(0); 00485 00486 /* Set values in blackboard */ 00487 printf("setting %d values...\n", NVALS); 00488 for (i=0 ; i<NVALS ; i++) { 00489 sprintf(cval, "%04d", i); 00490 dictionary_set(d, cval, "salut"); 00491 } 00492 printf("getting %d values...\n", NVALS); 00493 for (i=0 ; i<NVALS ; i++) { 00494 sprintf(cval, "%04d", i); 00495 val = dictionary_get(d, cval, DICT_INVALID_KEY); 00496 if (val==DICT_INVALID_KEY) { 00497 printf("cannot get value for key [%s]\n", cval); 00498 } 00499 } 00500 printf("unsetting %d values...\n", NVALS); 00501 for (i=0 ; i<NVALS ; i++) { 00502 sprintf(cval, "%04d", i); 00503 dictionary_unset(d, cval); 00504 } 00505 if (d->n != 0) { 00506 printf("error deleting values\n"); 00507 } 00508 00509 printf("deallocating...\n"); 00510 dictionary_del(d); 00511 return 0 ; 00512 }