This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tclt_format.c
128 lines (120 loc) · 3.51 KB
/
tclt_format.c
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
/*
* Copyright (c) 2012 Florent Tribouilloy <tribou_f AT epitech DOT net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "tclt_format.h"
#include <yajl/yajl_tree.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
size_t tclt_get_size(yajl_val node, int *ok)
{
size_t len = 0;
unsigned int i = 0;
if (node == NULL)
{
*ok = 1;
return len;
}
if (YAJL_IS_STRING(node))
{
len = 2; /* for '"' and '"' */
len += strlen(node->u.string);
}
else if (YAJL_IS_OBJECT(node))
{
len = 2; /* for '{' and '}' */
for (i = 0; i < node->u.object.len; ++i)
{
len += 3; /* for the double quote of the key and the semicolon*/
len += strlen(node->u.object.keys[i]);
len += tclt_get_size(node->u.object.values[i], ok);
if (i + 1 != node->u.object.len)
len += 1; /* for the ',' */
}
}
else if (YAJL_IS_ARRAY(node))
{
len = 2; /* for '[' and ']' */
for (i = 0; i < node->u.array.len; ++i)
{
len += tclt_get_size(node->u.array.values[i], ok);
if (i + 1 != node->u.array.len)
len += 1; /* for the ',' */
}
}
else
{
*ok = 1;
}
return len;
}
void make_string(yajl_val node, char *str)
{
unsigned int i;
if (YAJL_IS_STRING(node))
{
strncat(str, "\"", 1);
strncat(str, node->u.string, strlen(node->u.string));
strncat(str, "\"", 1);
}
else if (YAJL_IS_OBJECT(node))
{
strncat(str, "{", 1);
for (i = 0; i < node->u.object.len; ++i)
{
strncat(str, "\"", 1);
strncat(str, node->u.object.keys[i], strlen(node->u.object.keys[i]));
strncat(str, "\":", 2);
make_string(node->u.object.values[i], str);
if (i + 1 != node->u.object.len)
strncat(str, ",", 1);
}
strncat(str, "}", 1);
}
else if (YAJL_IS_ARRAY(node))
{
strncat(str, "[", 1);
for (i = 0; i < node->u.array.len; ++i)
{
make_string(node->u.array.values[i], str);
if (i + 1 != node->u.array.len)
strncat(str, ",", 1);
}
strncat(str, "]", 1);
}
}
char *tclt_format(yajl_val node)
{
size_t len = 1;
char *str_made = NULL;
int ok = 0;
if (node == NULL)
return NULL;
len += tclt_get_size(node, &ok);
if (ok == 1)
{
fprintf(stderr, "tclt_format: error in the type node\n");
return NULL;
}
str_made = malloc(len * sizeof(*str_made));
str_made[0] = '\0';
if (str_made == NULL)
{
fprintf(stderr, "tclt_format: not enough memory\n");
return str_made;
}
make_string(node, str_made);
return str_made;
}