More const changes.
[iwhd.git] / template.c
blobe9809b6df80b9578a2f31d08b9aa8b64122a900d
1 /* Copyright (C) 2010 Free Software Foundation, Inc.
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16 #include <config.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include "template.h"
23 char xml_root_header[] = "\
24 <api service=\"%s\" version=\"%s\">\
27 char xml_root_entry[] = "\
28 \n\
29 <link rel=\"%s\" href=\"http://%s/\%s\"/>\
32 char xml_root_footer[] = "\
33 \n\
34 </api>\n\
37 char xml_prov_header[] = "\
38 <providers>\
41 char xml_prov_entry[] = "\
42 \n\
43 <provider name=\"%s\">\n\
44 <type>%s</type>\n\
45 <host>%s</host>\n\
46 <port>%d</port>\n\
47 <username>%s</username>\n\
48 <password>%s</password>\n\
49 </provider>\
52 char xml_prov_footer[] = "\
53 \n\
54 </providers>\n\
57 char xml_obj_header[] = "\
58 <objects>\
61 char xml_obj_entry[] = "\
62 \n\
63 <object>\n\
64 <bucket>%s</bucket>\n\
65 <key>%s</key>\n\
66 </object>\
69 char xml_obj_footer[] = "\
70 \n\
71 </objects>\n\
75 tmpl_format_t xml_format = {
76 .root_header = xml_root_header,
77 .root_entry = xml_root_entry,
78 .root_footer = xml_root_footer,
79 .prov_header = xml_prov_header,
80 .prov_entry = xml_prov_entry,
81 .prov_footer = xml_prov_footer,
82 .obj_header = xml_obj_header,
83 .obj_entry = xml_obj_entry,
84 .obj_footer = xml_obj_footer,
85 .z_offset = 0
88 char json_root_header[] = "\
89 {\n\
90 \"service\": \"%s\",\n\
91 \"version\": \"%s\",\n\
95 char json_root_entry[] = "\
96 ,\n\
97 {\n\
98 \"rel\": \"%s\",\n\
99 \"link\": \"http://%s/%s\"\n\
103 char json_root_footer[] = "\
105 ]\n\
106 }\n\
109 char json_prov_header[] = "\
113 char json_prov_entry[] = "\
114 ,\n\
115 {\n\
116 \"name\": \"%s\",\n\
117 \"type\": \"%s\",\n\
118 \"host\": \"%s\",\n\
119 \"port\": %d,\n\
120 \"username\": \"%s\",\n\
121 \"password\": \"%s\"\n\
125 char json_prov_footer[] = "\
127 ]\n\
130 char json_obj_header[] = "\
134 char json_obj_entry[] = "\
135 ,\n\
136 {\n\
137 \"bucket\": \"%s\",\n\
138 \"key\": \"%s\"\n\
142 char json_obj_footer[] = "\
144 ]\n\
147 tmpl_format_t json_format = {
148 .root_header = json_root_header,
149 .root_entry = json_root_entry,
150 .root_footer = json_root_footer,
151 .prov_header = json_prov_header,
152 .prov_entry = json_prov_entry,
153 .prov_footer = json_prov_footer,
154 .obj_header = json_obj_header,
155 .obj_entry = json_obj_entry,
156 .obj_footer = json_obj_footer,
157 .z_offset = 1
160 tmpl_ctx_t *
161 tmpl_get_ctx (const char *type)
163 tmpl_ctx_t *tmp;
165 tmp = malloc(sizeof(*tmp));
166 if (tmp) {
167 if (type && strstr(type,"/json")) {
168 tmp->format = &json_format;
170 else {
171 tmp->format = &xml_format;
173 tmp->index = 0;
175 return tmp;
179 tmpl_root_header (tmpl_ctx_t *ctx, const char *name, const char *version)
181 int size;
182 tmpl_format_t *fmt = ctx->format;
184 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->root_header,
185 name,version);
186 if (size >= TMPL_BUF_SIZE) {
187 return 0;
189 ctx->buf = ctx->raw_buf;
191 return size;
195 tmpl_root_entry (tmpl_ctx_t *ctx, const char *rel, const char *link)
197 int size;
198 tmpl_format_t *fmt = ctx->format;
200 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->root_entry,
201 rel, ctx->base, link);
202 if (size >= TMPL_BUF_SIZE) {
203 return 0;
205 ctx->buf = ctx->raw_buf;
207 if (size && (ctx->index == 0)) {
208 ctx->buf += fmt->z_offset;
209 size -= fmt->z_offset;
212 ++(ctx->index);
213 return size;
217 tmpl_root_footer (tmpl_ctx_t *ctx)
219 ctx->buf = ctx->format->root_footer;
220 return strlen(ctx->buf);
224 tmpl_prov_header (tmpl_ctx_t *ctx)
226 ctx->buf = ctx->format->prov_header;
227 return strlen(ctx->buf);
231 tmpl_prov_entry (tmpl_ctx_t *ctx,
232 const char *name, const char *type,
233 const char *host, int port,
234 const char *user, const char *pass)
236 int size;
237 tmpl_format_t *fmt = ctx->format;
239 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->prov_entry,
240 name, type, host, port, user, pass);
241 if (size >= TMPL_BUF_SIZE) {
242 return 0;
244 ctx->buf = ctx->raw_buf;
246 if (size && (ctx->index == 0)) {
247 ctx->buf += fmt->z_offset;
248 size -= fmt->z_offset;
251 ++(ctx->index);
252 return size;
256 tmpl_prov_footer (tmpl_ctx_t *ctx)
258 ctx->buf = ctx->format->prov_footer;
259 return strlen(ctx->buf);
263 tmpl_obj_header (tmpl_ctx_t *ctx)
265 ctx->buf = ctx->format->obj_header;
266 return strlen(ctx->buf);
270 tmpl_obj_entry (tmpl_ctx_t *ctx, const char *bucket, const char *key)
272 int size;
273 tmpl_format_t *fmt = ctx->format;
275 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->obj_entry,bucket,key);
276 if (size >= TMPL_BUF_SIZE) {
277 return 0;
279 ctx->buf = ctx->raw_buf;
281 if (size && (ctx->index == 0)) {
282 ctx->buf += fmt->z_offset;
283 size -= fmt->z_offset;
286 ++(ctx->index);
287 return size;
291 tmpl_obj_footer (tmpl_ctx_t *ctx)
293 ctx->buf = ctx->format->obj_footer;
294 return strlen(ctx->buf);