build: make the "rpm" rule work once again
[iwhd.git] / template.c
blob2d49e2cd71ecd9426ca3b23c47bf0834ff2cf65c
1 /* Copyright (C) 2010 Red Hat, 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 static const char xml_root_header[] = "\
24 <api service=\"%s\" version=\"%s\">\
27 static const char xml_root_entry[] = "\
28 \n\
29 <link rel=\"%s\" href=\"http://%s/\%s\"/>\
32 static const char xml_root_footer[] = "\
33 \n\
34 </api>\n\
37 static const char xml_prov_header[] = "\
38 <providers>\
41 static const 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 static const char xml_prov_footer[] = "\
53 \n\
54 </providers>\n\
57 static const char xml_list_header[] = "\
58 <objects>\
61 static const char xml_list_entry[] = "\
62 \n\
63 <object>\n\
64 <bucket>%s</bucket>\n\
65 <key>%s</key>\n\
66 </object>\
69 static const char xml_list_footer[] = "\
70 \n\
71 </objects>\n\
74 static const char xml_obj_header[] = "\
75 <object>\n\
76 <object_body path=\"http://%s/%s/%s/body\"/>\n\
77 <object_attr_list path=\"http://%s/%s/%s/attrs\"/>\
80 static const char xml_obj_entry[] = "\
81 \n\
82 <object_attr name=\"%s\" path=\"http:/%s/%s/%s/attr_%s\"\
85 static const char xml_obj_footer[] = "\
86 \n\
87 </object>\n\
90 static const tmpl_format_t xml_format = {
91 .root_header = xml_root_header,
92 .root_entry = xml_root_entry,
93 .root_footer = xml_root_footer,
94 .prov_header = xml_prov_header,
95 .prov_entry = xml_prov_entry,
96 .prov_footer = xml_prov_footer,
97 .list_header = xml_list_header,
98 .list_entry = xml_list_entry,
99 .list_footer = xml_list_footer,
100 .obj_header = xml_obj_header,
101 .obj_entry = xml_obj_entry,
102 .obj_footer = xml_obj_footer,
103 .z_offset = 0
106 static const char json_root_header[] = "\
107 {\n\
108 \"service\": \"%s\",\n\
109 \"version\": \"%s\",\n\
113 static const char json_root_entry[] = "\
114 ,\n\
115 {\n\
116 \"rel\": \"%s\",\n\
117 \"link\": \"http://%s/%s\"\n\
121 static const char json_root_footer[] = "\
123 ]\n\
124 }\n\
127 static const char json_prov_header[] = "\
131 static const char json_prov_entry[] = "\
132 ,\n\
133 {\n\
134 \"name\": \"%s\",\n\
135 \"type\": \"%s\",\n\
136 \"host\": \"%s\",\n\
137 \"port\": %d,\n\
138 \"username\": \"%s\",\n\
139 \"password\": \"%s\"\n\
143 static const char json_prov_footer[] = "\
145 ]\n\
148 static const char json_list_header[] = "\
152 static const char json_list_entry[] = "\
153 ,\n\
154 {\n\
155 \"bucket\": \"%s\",\n\
156 \"key\": \"%s\"\n\
160 static const char json_list_footer[] = "\
162 ]\n\
165 static char json_obj_header[] = "\
166 {\n\
167 \"object_body\": \"http://%s/%s/%s/body\"\n\
168 \"object_attr_list\": \"http://%s/%s/%s/attrs\"\
171 static char json_obj_entry[] = "\
172 ,\n\
173 \"attr_%s\": \"http://%s/%s/%s/attr_%s\"\
176 static char json_obj_footer[] = "\
178 }\n\
181 static const tmpl_format_t json_format = {
182 .root_header = json_root_header,
183 .root_entry = json_root_entry,
184 .root_footer = json_root_footer,
185 .prov_header = json_prov_header,
186 .prov_entry = json_prov_entry,
187 .prov_footer = json_prov_footer,
188 .list_header = json_list_header,
189 .list_entry = json_list_entry,
190 .list_footer = json_list_footer,
191 .obj_header = json_obj_header,
192 .obj_entry = json_obj_entry,
193 .obj_footer = json_obj_footer,
194 .z_offset = 1
197 tmpl_ctx_t *
198 tmpl_get_ctx (const char *type)
200 tmpl_ctx_t *tmp;
202 tmp = malloc(sizeof(*tmp));
203 if (tmp) {
204 if (type && strstr(type,"/json")) {
205 tmp->format = &json_format;
207 else {
208 tmp->format = &xml_format;
210 tmp->index = 0;
212 return tmp;
215 size_t
216 tmpl_root_header (tmpl_ctx_t *ctx, const char *name, const char *version)
218 int size;
219 const tmpl_format_t *fmt = ctx->format;
221 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->root_header,
222 name,version);
223 if (size >= TMPL_BUF_SIZE || size < 0) {
224 return 0;
226 ctx->buf = ctx->raw_buf;
228 return size;
231 size_t
232 tmpl_root_entry (tmpl_ctx_t *ctx, const char *rel, const char *link)
234 int size;
235 const tmpl_format_t *fmt = ctx->format;
237 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->root_entry,
238 rel, ctx->base, link);
239 if (size >= TMPL_BUF_SIZE || size < 0) {
240 return 0;
242 ctx->buf = ctx->raw_buf;
244 if (size && (ctx->index == 0)) {
245 ctx->buf += fmt->z_offset;
246 size -= fmt->z_offset;
249 ++(ctx->index);
250 return size;
253 size_t
254 tmpl_root_footer (tmpl_ctx_t *ctx)
256 ctx->buf = ctx->format->root_footer;
257 return strlen(ctx->buf);
260 size_t
261 tmpl_prov_header (tmpl_ctx_t *ctx)
263 ctx->buf = ctx->format->prov_header;
264 return strlen(ctx->buf);
267 size_t
268 tmpl_prov_entry (tmpl_ctx_t *ctx,
269 const char *name, const char *type,
270 const char *host, int port,
271 const char *user, const char *pass)
273 int size;
274 const tmpl_format_t *fmt = ctx->format;
276 if (!name) name = "";
277 if (!type) type = "";
278 if (!host) host = "";
279 if (!user) user = "";
280 if (!pass) pass = "";
282 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->prov_entry,
283 name, type, host, port, user, pass);
284 if (size >= TMPL_BUF_SIZE || size < 0) {
285 return 0;
287 ctx->buf = ctx->raw_buf;
289 if (size && (ctx->index == 0)) {
290 ctx->buf += fmt->z_offset;
291 size -= fmt->z_offset;
294 ++(ctx->index);
295 return size;
298 size_t
299 tmpl_prov_footer (tmpl_ctx_t *ctx)
301 ctx->buf = ctx->format->prov_footer;
302 return strlen(ctx->buf);
305 size_t
306 tmpl_list_header (tmpl_ctx_t *ctx)
308 ctx->buf = ctx->format->list_header;
309 return strlen(ctx->buf);
312 size_t
313 tmpl_list_entry (tmpl_ctx_t *ctx, const char *bucket, const char *key)
315 int size;
316 const tmpl_format_t *fmt = ctx->format;
318 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->list_entry,bucket,key);
319 if (size >= TMPL_BUF_SIZE || size < 0) {
320 return 0;
322 ctx->buf = ctx->raw_buf;
324 if (size && (ctx->index == 0)) {
325 ctx->buf += fmt->z_offset;
326 size -= fmt->z_offset;
329 ++(ctx->index);
330 return size;
333 size_t
334 tmpl_list_footer (tmpl_ctx_t *ctx)
336 ctx->buf = ctx->format->list_footer;
337 return strlen(ctx->buf);
340 size_t
341 tmpl_obj_header (tmpl_ctx_t *ctx, const char *bucket, const char *key)
343 int size;
344 const tmpl_format_t *fmt = ctx->format;
346 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->obj_header,
347 ctx->base, bucket, key, /* once for the body... */
348 ctx->base, bucket, key); /* ...and once for the attrs */
349 if (size >= TMPL_BUF_SIZE || size < 0) {
350 return 0;
352 ctx->buf = ctx->raw_buf;
354 ++(ctx->index);
355 return size;
358 size_t
359 tmpl_obj_entry (tmpl_ctx_t *ctx, const char *bucket, const char *key,
360 const char *attr)
362 int size;
363 const tmpl_format_t *fmt = ctx->format;
365 size = snprintf(ctx->raw_buf,TMPL_BUF_SIZE,fmt->obj_entry,
366 attr, ctx->base, bucket, key, attr);
367 if (size >= TMPL_BUF_SIZE || size < 0) {
368 return 0;
370 ctx->buf = ctx->raw_buf;
372 if (size && (ctx->index == 0)) {
373 ctx->buf += fmt->z_offset;
374 size -= fmt->z_offset;
377 ++(ctx->index);
378 return size;
381 size_t
382 tmpl_obj_footer (tmpl_ctx_t *ctx)
384 ctx->buf = ctx->format->obj_footer;
385 return strlen(ctx->buf);