예제 #1
0
파일: dictionary.c 프로젝트: initlove/bti
/*--------------------------------------------------------------------------*/
int dictionary_set(dictionary * d, char *key, char *val)
{
	int i;
	unsigned hash;

	if (d == NULL || key == NULL)
		return -1;

	/* Compute hash for this key */
	hash = dictionary_hash(key);
	/* Find if value is already in dictionary */
	if (d->n > 0) {
		for (i = 0; i < d->size; i++) {
			if (d->key[i] == NULL)
				continue;
			if (hash == d->hash[i]) {	/* Same hash value */
				if (!strcmp(key, d->key[i])) {	/* Same key */
					/* Found a value: modify and return */
					if (d->val[i] != NULL)
						free(d->val[i]);
					d->val[i] = val ? xstrdup(val) : NULL;
					/* Value has been modified: return */
					return 0;
				}
			}
		}
	}
	/* Add a new value */
	/* See if dictionary needs to grow */
	if (d->n == d->size) {

		/* Reached maximum size: reallocate dictionary */
		d->val = (char **)mem_double(d->val, d->size * sizeof(char *));
		d->key = (char **)mem_double(d->key, d->size * sizeof(char *));
		d->hash =
		    (unsigned int *)mem_double(d->hash,
					       d->size * sizeof(unsigned));
		if ((d->val == NULL) || (d->key == NULL) || (d->hash == NULL)) {
			/* Cannot grow dictionary */
			return -1;
		}
		/* Double size */
		d->size *= 2;
	}

	/* Insert key in the first empty slot. Start at d->n and wrap at
	   d->size. Because d->n < d->size this will necessarily
	   terminate. */
	for (i = d->n; d->key[i];) {
		if (++i == d->size)
			i = 0;
	}
	/* Copy key */
	d->key[i] = xstrdup(key);
	d->val[i] = val ? xstrdup(val) : NULL;
	d->hash[i] = hash;
	d->n++;
	return 0;
}
예제 #2
0
/*--------------------------------------------------------------------------*/
int dictionary_set(dictionary * d, char * key, char * val, char * comment)
{
	int			i ;
	unsigned	hash ;
	if (d==NULL || key==NULL) return -1 ;
	/* Compute hash for this key */
	hash = dictionary_hash(key) ;
	/* Find if value is already in dictionary */
	if (d->n>0) {
		for (i=0 ; i<d->size ; i++) {
            if (d->key[i]==NULL)
                continue ;
			if (hash==d->hash[i]) { /* Same hash value */
				if (!strcmp(key, d->key[i])) {	 /* Same key */
					/* Found a value: modify and return */
					if (d->val[i]!=NULL)
					  free(d->val[i]);
                    			d->val[i] = val ? xstrdup(val) : NULL ;
					if (d->comment[i]!=NULL)
					  free(d->comment[i]);
                   			d->comment[i] = comment ? xstrdup(comment) : xstrdup("") ;
                    /* Value has been modified: return */
					return 0 ;
				}
			}
		}
	}
	/* Add a new value */
	/* See if dictionary needs to grow */
	if (d->n==d->size) {

		/* Reached maximum size: reallocate dictionary */
		d->val  = (char **)mem_double(d->val,  d->size * sizeof(char*)) ;
		d->comment  = (char **)mem_double(d->comment,  d->size * sizeof(char*)) ;
		d->key  = (char **)mem_double(d->key,  d->size * sizeof(char*)) ;
		d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
        if ((d->val==NULL) || (d->key==NULL) || (d->hash==NULL)) {
            /* Cannot grow dictionary */
            return -1 ;
        }
		/* Double size */
		d->size *= 2 ;
	}

    /* Insert key in the first empty slot */
    for (i=0 ; i<d->size ; i++) {
        if (d->key[i]==NULL) {
            /* Add key here */
            break ;
        }
    }
    /* Copy key */
    d->key[i]  = xstrdup(key);
    d->val[i]  = val ? xstrdup(val) : NULL ;
    d->comment[i]  = comment ? xstrdup(comment) : xstrdup("") ;
    d->hash[i] = hash;
    d->n ++ ;
	return 0 ;
}
예제 #3
0
파일: dictionary.c 프로젝트: 22noon/BatVI
void dictionary_set(dictionary * d, char * key, char * val)
{
	int			i ;
	unsigned	hash ;

	if (d==NULL || key==NULL) return ;
	
	/* Compute hash for this key */
	hash = dictionary_hash(key) ;
	/* Find if value is already in blackboard */
	if (d->n>0) {
		for (i=0 ; i<d->size ; i++) {
            if (d->key[i]==NULL)
                continue ;
			if (hash==d->hash[i]) { /* Same hash value */
				if (!strcmp(key, d->key[i])) {	 /* Same key */
					/* Found a value: modify and return */
					if (d->val[i]!=NULL)
						free(d->val[i]);
                    d->val[i] = val ? inistrdup(val) : NULL ;
                    /* Value has been modified: return */
					return ;
				}
			}
		}
	}
	/* Add a new value */
	/* See if dictionary needs to grow */
	if (d->n==d->size) {

		/* Reached maximum size: reallocate blackboard */
		d->val  = mem_double(d->val,  d->size * sizeof(char*)) ;
		d->key  = mem_double(d->key,  d->size * sizeof(char*)) ;
		d->hash = mem_double(d->hash, d->size * sizeof(unsigned)) ;

		/* Double size */
		d->size *= 2 ;
	}

    /* Insert key in the first empty slot */
    for (i=0 ; i<d->size ; i++) {
        if (d->key[i]==NULL) {
            /* Add key here */
            break ;
        }
    }
	/* Copy key */
	d->key[i]  = inistrdup(key);
    d->val[i]  = val ? inistrdup(val) : NULL ;
	d->hash[i] = hash;
	d->n ++ ;
	return ;
}
예제 #4
0
/*--------------------------------------------------------------------------*/
dictionary_value* dictionary_set(dictionary * d, const char * key, char * val, int type, void* ptr,void (*cb)(void))
{
  int i;
  unsigned  hash;

  if (d==NULL || key==NULL)
    return NULL ;

  /* Compute hash for this key */
  hash = dictionary_hash(key) ;
  /* Find if value is already in dictionary */
  if (d->n>0) {
    for (i=0 ; i<d->size ; i++) {
      if (d->key[i]==NULL)
        continue ;
      if (hash==d->hash[i]) { /* Same hash value */
        if (!strcmp(key, d->key[i])) { /* Same key */
          /* Found a value: modify and return */
          if (d->values[i].val!=NULL)
            free(d->values[i].val);

          d->values[i].val  = (val != NULL) ? xstrdup(val) : NULL ;
          /* Value has been modified: return */
          return &d->values[i];
        }
      }
    }
  }

  /* Add a new value */
  /* See if dictionary needs to grow */
  if (d->n==d->size) {
    /* Reached maximum size: reallocate dictionary */
    d->values = (dictionary_value *)mem_double(d->values,  d->size * sizeof(dictionary_value*)) ;
    d->key    = (char **)mem_double(d->key,  d->size * sizeof(char*)) ;
    d->hash   = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
    if ((d->values==NULL) || (d->key==NULL) || (d->hash==NULL)) {
        /* Cannot grow dictionary */
        return NULL;
    }
    /* Double size */
    d->size *= 2 ;
  }

  /* Insert key in the first empty slot */
  for (i=0 ; i<d->size ; i++) {
    if (d->key[i]==NULL) {
        /* Add key here */
        break ;
    }
  }
  /* Copy key */
  d->key[i]         = xstrdup(key);
  d->values[i].val  = (val != NULL) ? xstrdup(val) : NULL;
  d->values[i].type = type;
  d->values[i].callback = NULL;
  d->values[i].rw = 0;
  d->values[i].scope = -1;
  d->values[i].ptr  = ptr;
  d->hash[i]        = hash;
  d->n ++ ;

  return &d->values[i] ;
}
예제 #5
0
/*--------------------------------------------------------------------------*/
int dictionary_set
    (LPDICTIONARY lpDict,       // Ptr to workspace dictionary
     LPWCHAR      lpwKey,
     LPWCHAR      lpwVal)

{
    int  i;
    UINT hash;

    Assert (lpDict NE NULL);
    Assert (lpwKey NE NULL);

    // Hash the key
    hash = dictionary_hash (lpwKey);

    /* Find if value is already in dictionary */
    if (lpDict->n > 0)
    {
        for (i = 0; i < lpDict->size; i++)
        if (lpDict->key[i] NE NULL)
        {
            if (hash EQ lpDict->hash[i])
            { /* Same hash value */
                if (!lstrcmpW (lpwKey, lpDict->key[i]))
                {   /* Same key */
                    /* Found a value: modify and return */
                    if (lpwVal EQ NULL
                     || (lpDict->inifile <= lpwVal
                      &&                    lpwVal < (lpDict->inifile + lpDict->size)))
                        lpDict->val[i] = lpwVal;
                    else
                    {
                        if (lpDict->val[i] NE NULL)
                        {
                            free (lpDict->val[i]); lpDict->val[i] = NULL;
                        } // End IF

                        if (lpwVal NE NULL)
                        {
                            lpwVal = strdupW (lpwVal);
                            if (lpwVal EQ NULL)
                                return -1;
                        } // End IF

                        lpDict->val[i] = lpwVal;
                    } // End IF/ELSE

                    /* Value has been modified: return */
                    return 0;
                } // End IF
            } // End IF
        } // End FOR/IF
    } // End IF

    /* Add a new value */
    /* See if dictionary needs to grow */
    if (lpDict->n EQ lpDict->size)
    {
        LPWCHAR *val;               // Ptr to list of string values
        LPWCHAR *key;               // Ptr to list of string keys
        LPUINT   hash;              // Ptr to list of hash values for keys

        /* Reached maximum size: reallocate dictionary */
        val  = (LPWCHAR *) mem_double (lpDict->val,  lpDict->size * sizeof (*lpDict->val));
        key  = (LPWCHAR *) mem_double (lpDict->key,  lpDict->size * sizeof (*lpDict->key));
        hash = (LPUINT)    mem_double (lpDict->hash, lpDict->size * sizeof (*lpDict->hash));
        if ((val EQ NULL) || (key EQ NULL) || (hash EQ NULL))
        {
            // Replace the values that succeeded in doubling
            if (val  NE NULL) lpDict->val  = val;
            if (key  NE NULL) lpDict->key  = key;
            if (hash NE NULL) lpDict->hash = hash;

            /* Cannot grow dictionary */
            return -1;
        } // End IF

        lpDict->val  = val;
        lpDict->key  = key;
        lpDict->hash = hash;

        /* Double size */
        lpDict->size *= 2;
    } // End IF

    /* Insert key in the first empty slot */
    for (i = 0; i < lpDict->size; i++)
    if (lpDict->key[i] EQ NULL)
        /* Add key here */
        break;

    // If the key is NULL or within the file contents, it can be
    //   stored directly; otherwise it must be duplicated.
    if (lpwKey EQ NULL
     || (lpDict->inifile <= lpwKey
      &&                    lpwKey < (lpDict->inifile + lpDict->size)))
        lpDict->key[i] = lpwKey;
    else
    {
        lpDict->key[i] = strdupW (lpwKey);
        if (lpDict->key[i] EQ NULL)
            return -1;
    } // End IF/ELSE

    // If the value is NULL or within the file contents, it can be
    //   stored directly; otherwise it must be duplicated.
    if (lpwVal EQ NULL
     || (lpDict->inifile <= lpwVal
      &&                    lpwVal < (lpDict->inifile + lpDict->size)))
        lpDict->val[i] = lpwVal;
    else
    {
        if (lpDict->val[i] NE NULL)
        {
            free (lpDict->val[i]); lpDict->val[i] = NULL;
        } // End IF

        lpDict->val[i] = strdupW (lpwVal);
        if (lpDict->val[i] EQ NULL)
            return -1;
    } // End IF/ELSE

    lpDict->hash[i] = hash;
    lpDict->n++;

    return 0;
} // End dictionary_set
예제 #6
0
/*--------------------------------------------------------------------------*/
int dictionary_setbuf(dictionary * d, char * key, void * buf,unsigned int bufsize)
{
	int			i ;
	unsigned	hash ;

	if (d==NULL || key==NULL) return -1 ;
	
	/* Compute hash for this key */
	hash = dictionary_hash(key) ;
	/* Find if buf is already in dictionary */
	if (d->n>0) {
	  for (i=0 ; i<d->size ; i++) {
            if (d->key[i]==NULL)
                continue ;
	    if (hash==d->hash[i]) { /* Same hash value */
	      if (!strcmp(key, d->key[i])) {	 /* Same key */
		/* Found a value: modify and return */
	        if(d->buf[i]!=NULL) free(d->buf[i]);
	        d->buf[i]=malloc(bufsize);
	        d->bufsize[i] = bufsize;
	        memset(d->buf[i],0,bufsize);	
	        if(buf!=NULL) memmove(d->buf[i],buf,bufsize);	
                /* Value has been modified: return */
	        return 0 ;
	      }
	    }
          }
	}
	/* Add a new value */
	/* See if dictionary needs to grow */
	if (d->n==d->size) {

		/* Reached maximum size: reallocate dictionary */
		d->val  = (char **)mem_double(d->val,  d->size * sizeof(char*)) ;
		d->buf  = (void **)mem_double(d->buf,  d->size * sizeof(void*)) ;
		d->key  = (char **)mem_double(d->key,  d->size * sizeof(char*)) ;
		d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
		d->bufsize = (unsigned int *)mem_double(d->bufsize, d->size * sizeof(unsigned)) ;
        if ((d->val==NULL) || (d->key==NULL) || (d->hash==NULL)) {
            /* Cannot grow dictionary */
            return -1 ;
        }
        if ((d->buf==NULL) || (d->bufsize==NULL)) {
            /* Cannot grow dictionary */
            return -1 ;
        }
		/* Double size */
		d->size *= 2 ;
	}

    /* Insert key in the first empty slot */
    for (i=0 ; i<d->size ; i++) {
        if (d->key[i]==NULL) {
            /* Add key here */
            break ;
        }
    }
	/* Copy key */
	d->key[i]  = xstrdup(key);
	if(d->buf[i]!=NULL) free(d->buf[i]);
	d->buf[i]=malloc(bufsize);
	d->bufsize[i] = bufsize;
	memset(d->buf[i],0,bufsize);	
	if(buf!=NULL) memmove(d->buf[i],buf,bufsize);	
	d->hash[i] = hash;
	d->n ++ ;
	return 0 ;
}
예제 #7
0
파일: Dictionary.c 프로젝트: jordanfin/aoc
////////////////////////////////////////////////////////////
///	@brief    Set a value in a Dictionary.
///	@param    d       Dictionary object to modify.
///	@param    key     Key to modify or add.
///	@param    val     Value to add.
///	@return   int     0 if Ok, anything else otherwise
///	
///	If the given key is found in the Dictionary, the associated value is
///	replaced by the provided one. If the key cannot be found in the
///	Dictionary, it is added to it.
///	
///	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 Dictionaryset a variable to NULL, a call to
///	Dictionaryget 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.
///	
///	This function returns non-zero in case of failure.
////////////////////////////////////////////////////////////
int DictionarySet(struct Dictionary * d, char * key, char * val)
{
	Int32		i;
	unsigned	hash;

	if (d == NULL || key == NULL) return -1;
	
	// Compute hash for this key
	hash = DictionaryHash(key);

	// Find if value is already in Dictionary
	if (d->n > 0)
	{
		for (i = 0; i < d->size; i++)
		{
            if (d->key[i] == NULL)
                continue;

			// Same hash value
			if (hash == d->hash[i])
			{
				// Same key
				if (!strcmp(key, d->key[i]))
				{
					// Found a value: modify and return
					if (d->val[i] != NULL)
						free(d->val[i]);

                    d->val[i] = val ? xstrdup(val) : NULL ;

                    // Value has been modified: return
					return 0;
				}
			}
		}
	}

	// Add a new value
	// See if Dictionary needs to grow
	if (d->n == d->size)
	{
		// Reached maximum size: reallocate Dictionary
		d->val  = (char**)mem_double(d->val, d->size * sizeof(char*));
		d->key  = (char**)mem_double(d->key, d->size * sizeof(char*));
		d->hash = (unsigned int*)mem_double(d->hash, d->size * sizeof(unsigned));
        
		// Cannot grow Dictionary
		if ((d->val == NULL) || (d->key == NULL) || (d->hash == NULL))
            return -1;

		// Double size
		d->size <<= 1;
	}

    // Insert key in the first empty slot
    for (i = 0; i < d->size; i++)
	{
		// Add key here
        if (d->key[i] == NULL)
            break;
    }

	// Copy key
	d->key[i]  = xstrdup(key);
    d->val[i]  = val ? xstrdup(val) : NULL;
	d->hash[i] = hash;
	d->n++;

	return 0;
}