#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
typedef struct
{
unsigned int version;
const char *name;
int id;
int not_saved_value;
} My_Conf_Type;
typedef struct
{
const char *server;
int port;
} My_Conf_Subtype;
static const char MY_CONF_FILE_ENTRY[] = "config";
static void
_my_conf_descriptor_init(void)
{
#define MY_CONF_ADD_BASIC(member, eet_type) \
EET_DATA_DESCRIPTOR_ADD_BASIC \
(_my_conf_descriptor, My_Conf_Type, # member, member, eet_type)
#define MY_CONF_SUB_ADD_BASIC(member, eet_type) \
EET_DATA_DESCRIPTOR_ADD_BASIC \
(_my_conf_sub_descriptor, My_Conf_Subtype, # member, member, eet_type)
(_my_conf_descriptor, My_Conf_Type, "subs", subs, _my_conf_sub_descriptor);
#undef MY_CONF_ADD_BASIC
#undef MY_CONF_SUB_ADD_BASIC
}
static void
_my_conf_descriptor_shutdown(void)
{
}
static My_Conf_Type *
_my_conf_new(void)
{
My_Conf_Type *my_conf = calloc(1, sizeof(My_Conf_Type));
My_Conf_Subtype *sub;
if (!my_conf)
{
fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
return NULL;
}
my_conf->version = 0x112233;
sub = calloc(1, sizeof(My_Conf_Subtype));
if (sub)
{
sub->port = 1234;
}
return my_conf;
}
static void
_my_conf_free(My_Conf_Type *my_conf)
{
My_Conf_Subtype *sub;
{
free(sub);
}
free(my_conf);
}
static My_Conf_Type *
_my_conf_load(const char *filename)
{
My_Conf_Type *my_conf;
if (!ef)
{
fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
return NULL;
}
my_conf =
eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
if (!my_conf)
goto end;
if (my_conf->version < 0x112233)
{
fprintf(stderr,
"WARNING: version %#x was too old, upgrading it to %#x\n",
my_conf->version, 0x112233);
my_conf->version = 0x112233;
}
end:
return my_conf;
}
_my_conf_save(const My_Conf_Type *my_conf,
const char *filename)
{
char tmp[PATH_MAX];
unsigned int i, len;
struct stat st;
if (len + 12 >= (int)sizeof(tmp))
{
fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
}
i = 0;
do
{
snprintf(tmp + len, 12, ".%u", i);
i++;
}
while (stat(tmp, &st) == 0);
if (!ef)
{
fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
}
(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf,
EINA_TRUE);
if (ret)
{
unlink(filename);
rename(tmp, filename);
}
return ret;
}
int
main(int argc,
char *argv[])
{
My_Conf_Type *my_conf;
const My_Conf_Subtype *sub;
int ret = 0;
if (argc != 3)
{
fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
return -1;
}
_my_conf_descriptor_init();
my_conf = _my_conf_load(argv[1]);
if (!my_conf)
{
printf("creating new configuration.\n");
my_conf = _my_conf_new();
if (!my_conf)
{
ret = -2;
goto end;
}
}
printf("My_Conf_Type:\n"
"\tversion: %#x\n"
"\tname...: '%s'\n"
"\tid.....: %d\n"
"\tenabled: %hhu\n"
"\tsubs...:\n",
my_conf->version,
my_conf->name ? my_conf->name : "",
my_conf->id,
my_conf->enabled);
printf("\t\tserver: '%s', port: %d\n",
sub->server ? sub->server : "",
sub->port);
if (!_my_conf_save(my_conf, argv[2]))
ret = -3;
_my_conf_free(my_conf);
end:
_my_conf_descriptor_shutdown();
return ret;
}