2007-04-23 10:23:40 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "context.h"
|
2007-05-02 20:42:12 +02:00
|
|
|
#include "filedata.h"
|
2007-04-23 10:23:40 +02:00
|
|
|
#include "list.h"
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
static LIST_HEAD(context_list);
|
|
|
|
|
2007-05-02 20:42:12 +02:00
|
|
|
struct context * create_context(const char *filename)
|
2007-04-23 10:23:40 +02:00
|
|
|
{
|
|
|
|
struct context *ctx = malloc(sizeof(struct context));
|
|
|
|
if (ctx == NULL) {
|
|
|
|
log_print(LOG_WARN, "create_context(): out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(ctx, 0, sizeof(struct context));
|
|
|
|
|
2007-05-02 20:42:12 +02:00
|
|
|
ctx->file = get_filedata(filename);
|
|
|
|
if (ctx->file == NULL) {
|
|
|
|
free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-04-23 10:23:40 +02:00
|
|
|
list_add(&ctx->list, &context_list);
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
int destroy_context(struct context *ctx)
|
|
|
|
{
|
|
|
|
list_del(&ctx->list);
|
|
|
|
|
|
|
|
if (ctx->dev_close != NULL)
|
|
|
|
ctx->dev_close(ctx);
|
|
|
|
|
2007-05-02 20:42:12 +02:00
|
|
|
free(ctx->file);
|
2007-04-23 10:23:40 +02:00
|
|
|
free(ctx);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int context_close(void)
|
|
|
|
{
|
|
|
|
struct context *ctx, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(ctx, tmp, &context_list, list)
|
|
|
|
destroy_context(ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|