MCPcopy Create free account
hub / github.com/couchbase/forestdb / dictionary_set

Function dictionary_set

utils/iniparser.cc:385–437  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

383/*--------------------------------------------------------------------------*/
384
385static void dictionary_set(dictionary * d, char * key, char * val)
386{
387 int i ;
388 unsigned hash ;
389
390 if (d==NULL || key==NULL) return ;
391
392 /* Compute hash for this key */
393 hash = dictionary_hash(key) ;
394 /* Find if value is already in blackboard */
395 if (d->n>0) {
396 for (i=0 ; i<d->size ; i++) {
397 if (d->key[i]==NULL)
398 continue ;
399 if (hash==d->hash[i]) { /* Same hash value */
400 if (!strcmp(key, d->key[i])) { /* Same key */
401 /* Found a value: modify and return */
402 if (d->val[i]!=NULL)
403 free(d->val[i]);
404 d->val[i] = val ? strdup(val) : NULL ;
405 /* Value has been modified: return */
406 return ;
407 }
408 }
409 }
410 }
411 /* Add a new value */
412 /* See if dictionary needs to grow */
413 if (d->n==d->size) {
414
415 /* Reached maximum size: reallocate blackboard */
416 d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ;
417 d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ;
418 d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
419
420 /* Double size */
421 d->size *= 2 ;
422 }
423
424 /* Insert key in the first empty slot */
425 for (i=0 ; i<d->size ; i++) {
426 if (d->key[i]==NULL) {
427 /* Add key here */
428 break ;
429 }
430 }
431 /* Copy key */
432 d->key[i] = strdup(key);
433 d->val[i] = val ? strdup(val) : NULL ;
434 d->hash[i] = hash;
435 d->n ++ ;
436 return ;
437}
438
439/*-------------------------------------------------------------------------*/
440/**

Callers 2

iniparser_add_entryFunction · 0.85
iniparser_setstrFunction · 0.85

Calls 2

dictionary_hashFunction · 0.85
mem_doubleFunction · 0.85

Tested by

no test coverage detected