aboutsummaryrefslogtreecommitdiffstats
path: root/src/manager/lib/template.c
blob: 3ae7c87a3cccacf3585dcaa6349ef16e6d153ada (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * @file template.c
 *
 * @brief Implementation of template_t.
 *
 */

/*
 * Copyright (C) 2007 Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "template.h"

#include <ClearSilver/ClearSilver.h>

#include <library.h>

typedef struct private_template_t private_template_t;

/**
 * private data of the task manager
 */
struct private_template_t {

	/**
	 * public functions
	 */
	template_t public;
	
	/**
	 * template file
	 */
	char *file;
	
	/**
	 * clearsilver HDF dataset
	 */
	HDF *hdf;
};

/**
 * clearsilver cs_render callback function
 */
static NEOERR* render_cb(response_t *response, char *str)
{
	response->print(response, str);
	return NULL;
}

/**
 * Implementation of template_t.render.
 */
static void render(private_template_t *this, response_t *response)
{
	NEOERR* err;
	CSPARSE *parse;
	
	err = cs_init(&parse, this->hdf);
	if (!err)
	{
		err = cs_parse_file(parse, this->file);
		if (!err) 
		{
			err = cs_render(parse, response, (CSOUTFUNC)render_cb);
			if (!err)
			{
				cs_destroy(&parse);
				return;
			}
		}
		cs_destroy(&parse);
	}
	nerr_log_error(err);
	return;
}

/**
 * Implementation of template_t.set.
 */
static void set(private_template_t *this, char *key, char *value)
{
	hdf_set_value(this->hdf, key, value);
}

/**
 * Implementation of template_t.setf.
 */
static void setf(private_template_t *this, char *format, ...)
{
	va_list args;

	va_start(args, format);
	hdf_set_valuevf(this->hdf, format, args);
	va_end(args);
}

/**
 * Implementation of template_t.destroy
 */
static void destroy(private_template_t *this)
{
	hdf_destroy(&this->hdf);
	free(this->file);
	free(this);
}

/*
 * see header file
 */
template_t *template_create(char *file)
{	
	private_template_t *this = malloc_thing(private_template_t);

	this->public.render = (void(*)(template_t*,response_t*))render;
	this->public.set = (void(*)(template_t*, char *, char*))set;
	this->public.setf = (void(*)(template_t*, char *format, ...))setf;
	this->public.destroy = (void(*)(template_t*))destroy;

	this->file = strdup(file);
	this->hdf = NULL;
	
	hdf_init(&this->hdf);
	return &this->public;
}