2007-08-25 21:31:10 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) 2005 Laurent Vivier <Laurent@lvivier.info>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "libconfig.h"
|
|
|
|
|
2007-08-26 09:17:42 +00:00
|
|
|
void config_set_indexed_property(char *configuration,
|
|
|
|
char *index_name, char *index_property,
|
|
|
|
char *name, char *property)
|
2007-08-25 21:31:10 +00:00
|
|
|
{
|
|
|
|
int last_index;
|
2007-08-26 19:22:43 +00:00
|
|
|
int index;
|
|
|
|
int len, len_new;
|
2007-08-25 21:31:10 +00:00
|
|
|
|
2007-08-26 09:17:42 +00:00
|
|
|
len_new = strlen(name) + 1 + strlen(property) + 1;
|
|
|
|
|
2007-08-26 19:22:43 +00:00
|
|
|
/* does this property exists in this indexed field ? */
|
|
|
|
|
2007-08-26 09:17:42 +00:00
|
|
|
last_index = config_find_indexed_property(configuration,
|
|
|
|
index_name, index_property,
|
|
|
|
name, NULL);
|
|
|
|
|
2007-08-26 19:22:43 +00:00
|
|
|
if (last_index == -1)
|
|
|
|
{
|
|
|
|
/* if not, does this indexed field exist ? */
|
2007-08-26 09:17:42 +00:00
|
|
|
|
|
|
|
last_index = config_find_indexed_property(configuration,
|
2007-08-26 19:22:43 +00:00
|
|
|
index_name, index_property,
|
|
|
|
NULL, NULL);
|
|
|
|
if (last_index == -1)
|
|
|
|
{
|
|
|
|
/* no, we add this property at the end */
|
|
|
|
|
|
|
|
last_index = strlen(configuration);
|
|
|
|
if (last_index > 0)
|
|
|
|
last_index++; /* to insert a '\n' */
|
|
|
|
index = last_index;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* yes, we add this property at the end of this indexed field */
|
2007-08-26 09:17:42 +00:00
|
|
|
|
2007-08-26 19:47:45 +00:00
|
|
|
last_index = config_get_next_property(configuration, last_index, NULL, NULL);
|
|
|
|
if (last_index != -1)
|
|
|
|
last_index = config_find_entry(configuration + last_index, index_name, NULL);
|
|
|
|
if (last_index == -1)
|
|
|
|
last_index = strlen(configuration);
|
|
|
|
index = last_index;
|
2007-08-26 19:22:43 +00:00
|
|
|
}
|
2007-08-25 21:31:10 +00:00
|
|
|
}
|
2007-08-26 19:22:43 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
index = config_get_next_property(configuration, last_index, NULL, NULL);
|
|
|
|
if (index == -1)
|
|
|
|
index = strlen(configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(configuration + index);
|
|
|
|
memmove(configuration + last_index + len_new,
|
|
|
|
configuration + index, len);
|
|
|
|
|
|
|
|
if (last_index > 0)
|
|
|
|
configuration[last_index - 1] = '\n';
|
2007-08-26 09:17:42 +00:00
|
|
|
sprintf(configuration + last_index,
|
|
|
|
"%s %s", name, property);
|
2007-08-26 19:22:43 +00:00
|
|
|
configuration[last_index + len_new - 1] = '\n';
|
|
|
|
|
|
|
|
/* remove ending '\n' */
|
|
|
|
|
2007-08-26 09:17:42 +00:00
|
|
|
len = strlen(configuration + last_index);
|
|
|
|
if (configuration[last_index + len - 1] == '\n')
|
|
|
|
len--;
|
|
|
|
configuration[last_index + len] = 0;
|
2007-08-25 21:31:10 +00:00
|
|
|
}
|