EMILE/libconfig/config_set_indexed_property.c

86 lines
2.0 KiB
C
Raw Normal View History

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-09-08 23:09:14 +00:00
int config_set_indexed_property(int8_t *configuration,
2007-08-26 09:17:42 +00:00
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 */
2007-09-08 23:09:14 +00:00
last_index = strlen((char*)configuration);
2007-08-26 19:22:43 +00:00
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
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)
2007-08-26 20:18:47 +00:00
{
2007-09-08 23:09:14 +00:00
last_index = strlen((char*)configuration);
2007-08-26 20:18:47 +00:00
if (last_index > 0)
last_index++; /* to insert a '\n' */
}
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)
2007-09-08 23:09:14 +00:00
index = strlen((char*)configuration);
2007-08-26 19:22:43 +00:00
}
2007-09-08 23:09:14 +00:00
len = strlen((char*)configuration + index);
2007-08-26 19:22:43 +00:00
memmove(configuration + last_index + len_new,
configuration + index, len);
if (last_index > 0)
configuration[last_index - 1] = '\n';
2007-09-08 23:09:14 +00:00
sprintf((char*)configuration + last_index,
2007-08-26 09:17:42 +00:00
"%s %s", name, property);
2007-08-26 19:22:43 +00:00
configuration[last_index + len_new - 1] = '\n';
/* remove ending '\n' */
2007-09-08 23:09:14 +00:00
len = strlen((char*)configuration + last_index);
2007-08-26 09:17:42 +00:00
if (configuration[last_index + len - 1] == '\n')
len--;
configuration[last_index + len] = 0;
2007-08-31 20:42:27 +00:00
return last_index;
2007-08-25 21:31:10 +00:00
}