]> git.ipfire.org Git - thirdparty/lldpd.git/blame - src/kv_writer.c
Update ISC license wording.
[thirdparty/lldpd.git] / src / kv_writer.c
CommitLineData
999509a3
VB
1/*
2 * Copyright (c) 2010 Andreas Hofmeister <andi@collax.com>
3 * 2010 Vincent Bernat <bernat@luffy.cx>
4 *
51434125 5 * Permission to use, copy, modify, and/or distribute this software for any
999509a3
VB
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "writer.h"
23#include "lldpd.h"
24
25struct kv_writer_private {
26 FILE * fh;
27 char * prefix;
28};
29
30void
31kv_start(struct writer *w , const char *tag, const char *descr)
32{
33 struct kv_writer_private *p = w->priv;
34 char *newprefix;
35 int s;
36
37 s = strlen(p->prefix) + 1 + strlen(tag);
38 if ((newprefix = malloc(s+1)) == NULL)
39 fatal(NULL);
40 if (strlen(p->prefix) > 0)
41 snprintf(newprefix, s+1, "%s.%s", p->prefix, tag);
42 else
43 snprintf(newprefix, s+1, "%s", tag);
44 free(p->prefix);
45 p->prefix = newprefix;
46}
47
48void
49kv_data(struct writer *w, const char *data)
50{
51 struct kv_writer_private *p = w->priv;
52 fprintf(p->fh, "%s=%s\n", p->prefix, data);
53}
54
55void
56kv_end(struct writer *w)
57{
58 struct kv_writer_private *p = w->priv;
59 char *dot;
60
61 if ((dot = strrchr(p->prefix, '.')) == NULL)
62 p->prefix[0] = '\0';
63 else
64 *dot = '\0';
65}
66
67void
68kv_attr(struct writer *w, const char *tag, const char *descr, const char *value)
69{
70 if (!strcmp(tag, "name") || !strcmp(tag, "type")) {
71 /* Special case for name, replace the last prefix */
72 kv_end(w);
73 kv_start(w, value, NULL);
74 } else {
75 kv_start(w, tag, NULL);
76 kv_data(w, value);
77 kv_end(w);
78 }
79}
80
81void
82kv_finish(struct writer *w)
83{
84 struct kv_writer_private *p = w->priv;
85
86 free(p->prefix);
87 free(w->priv);
88 w->priv = NULL;
89
90 free(w);
91}
92
93struct writer *
94kv_init(FILE *fh)
95{
96
97 struct writer *result;
98 struct kv_writer_private *priv;
99
100 if ((priv = malloc(sizeof(*priv))) == NULL)
101 fatal(NULL);
102
103 priv->fh = fh;
104 priv->prefix = strdup("");
105
106 if ((result = malloc(sizeof(struct writer))) == NULL)
107 fatal(NULL);
108
109 result->priv = priv;
110 result->start = kv_start;
111 result->attr = kv_attr;
112 result->data = kv_data;
113 result->end = kv_end;
114 result->finish= kv_finish;
115
116 return result;
117}