manager can query and list IKE_SA status (no layout yet)
[strongswan.git] / src / manager / lib / template.c
1 /**
2 * @file template.c
3 *
4 * @brief Implementation of template_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "template.h"
24
25 #include <ClearSilver/ClearSilver.h>
26
27 #include <library.h>
28
29 typedef struct private_template_t private_template_t;
30
31 /**
32 * private data of the task manager
33 */
34 struct private_template_t {
35
36 /**
37 * public functions
38 */
39 template_t public;
40
41 /**
42 * template file
43 */
44 char *file;
45
46 /**
47 * clearsilver HDF dataset
48 */
49 HDF *hdf;
50 };
51
52 /**
53 * clearsilver cs_render callback function
54 */
55 static NEOERR* render_cb(response_t *response, char *str)
56 {
57 response->print(response, str);
58 return NULL;
59 }
60
61 /**
62 * Implementation of template_t.render.
63 */
64 static void render(private_template_t *this, response_t *response)
65 {
66 NEOERR* err;
67 CSPARSE *parse;
68
69 err = cs_init(&parse, this->hdf);
70 if (!err)
71 {
72 err = cs_parse_file(parse, this->file);
73 if (!err)
74 {
75 err = cs_render(parse, response, (CSOUTFUNC)render_cb);
76 if (!err)
77 {
78 cs_destroy(&parse);
79 return;
80 }
81 }
82 cs_destroy(&parse);
83 }
84 nerr_log_error(err);
85 return;
86 }
87
88 /**
89 * Implementation of template_t.set.
90 */
91 static void set(private_template_t *this, char *key, char *value)
92 {
93 hdf_set_value(this->hdf, key, value);
94 }
95
96 /**
97 * Implementation of template_t.setf.
98 */
99 static void setf(private_template_t *this, char *format, ...)
100 {
101 va_list args;
102
103 va_start(args, format);
104 hdf_set_valuevf(this->hdf, format, args);
105 va_end(args);
106 }
107
108 /**
109 * Implementation of template_t.destroy
110 */
111 static void destroy(private_template_t *this)
112 {
113 hdf_destroy(&this->hdf);
114 free(this->file);
115 free(this);
116 }
117
118 /*
119 * see header file
120 */
121 template_t *template_create(char *file)
122 {
123 private_template_t *this = malloc_thing(private_template_t);
124
125 this->public.render = (void(*)(template_t*,response_t*))render;
126 this->public.set = (void(*)(template_t*, char *, char*))set;
127 this->public.setf = (void(*)(template_t*, char *format, ...))setf;
128 this->public.destroy = (void(*)(template_t*))destroy;
129
130 this->file = strdup(file);
131 this->hdf = NULL;
132
133 hdf_init(&this->hdf);
134 return &this->public;
135 }
136