lib/cfg_parse.c
/**
 * This function sets a single key-value pair in a cfg_struct.
 * If the key already exists, its value will be updated.
 * If not, a new item is added to the cfg_struct list.
 * There is a commented-out option to treat blank value as a delete operation:
 *  uncomment if your project needs this feature.
 * @param cfg Pointer to cfg_struct to search.
 * @param key String containing key to search for.
 * @param value String containing new value to assign to key.
 */
This function sets a single key-value pair in a cfg_struct.
If the key already exists, its value will be updated.
If not, a new item is added to the cfg_struct list.
There is a commented-out option to treat blank value as a delete operation:
uncomment if your project needs this feature.
@param cfg Pointer to cfg_struct to search.
@param key String containing key to search for.
@param value String containing new value to assign to key.
This function sets a single key - value pair in a cfg_struct . If the key already exists its value will be updated . If not a new item is added to the cfg_struct list . There is a commented - out option to treat blank value as a delete operation : uncomment if your project needs this feature . @param cfg Pointer to cfg_struct to search . @param key String containing key to search for . @param value String containing new value to assign to key .
void cfg_set(struct cfg_struct *cfg, const char *key, const char *value)
{
  unsigned int i, len;
  char *tkey, *tvalue;
  struct cfg_node *temp;
  if (cfg == NULL || key == NULL || value == NULL) return;
  tkey = cfg_trim(key);
  if (! strcmp(tkey,"")) { free(tkey); return; }
  len = strlen(tkey);
  for (i = 0; i < len; i++)
    tkey[i] = tolower(tkey[i]);
  tvalue = cfg_trim(value);
  temp = cfg->head;
  while (temp != NULL)
  {
    if (! strcmp(tkey, temp->key))
    {
      free(tkey);
      free(temp->value);
      temp->value = tvalue;
      return;
    }
    temp = temp->next;
  }
  temp = (struct cfg_node *)cfg_malloc(sizeof(struct cfg_node));
  temp->key = tkey;
  temp->value = tvalue;
  temp->next = cfg->head;
  cfg->head = temp;
}
void cfg_set ( struct cfg_struct * cfg , const char * key , const char * value ) { unsigned int i , len ; char * tkey , * tvalue ; struct cfg_node * temp ; if ( cfg == NULL || key == NULL || value == NULL ) return ; tkey = cfg_trim ( key ) ; if ( ! strcmp ( tkey , " " ) ) { free ( tkey ) ; return ; } len = strlen ( tkey ) ; for ( i = 0 ; i < len ; i ++ ) tkey [ i ] = tolower ( tkey [ i ] ) ; tvalue = cfg_trim ( value ) ; temp = cfg -> head ; while ( temp != NULL ) { if ( ! strcmp ( tkey , temp -> key ) ) { free ( tkey ) ; free ( temp -> value ) ; temp -> value = tvalue ; return ; } temp = temp -> next ; } temp = ( struct cfg_node * ) cfg_malloc ( sizeof ( struct cfg_node ) ) ; temp -> key = tkey ; temp -> value = tvalue ; temp -> next = cfg -> head ; cfg -> head = temp ; }
This function sets a single key-value pair in a cfg_struct.
This function sets a single key - value pair in a cfg_struct .
/* safety check: null input */ /* Trim key. */ /* Exclude empty key */ /* Lowercase key */ /* Trim value. */ /* Depending on implementation, you may wish to treat blank value
     as a "delete" operation */ /* if (! strcmp(tvalue,"")) { free(tvalue); cfg_delete(cfg,tkey); free(tkey); return; } */ /* point at first item in list */ /* search list for existing key */ /* found a match: no longer need temp key */ /* update value */ /* not found: create new element */ /* assign key, value */ /* prepend */