forked from vivien/i3blocks
/
json.c
155 lines (129 loc) · 3.09 KB
/
json.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* json.c - basic JSON parsing and printing
* Copyright (C) 2014 Vivien Didelot
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "block.h"
#include "log.h"
static inline int
is_number(const char *str)
{
char *end;
strtoul(str, &end, 10);
/* not a valid number if end is not a null character */
return !(*str == 0 || *end != 0);
}
static inline void
print_string(const char *str)
{
fprintf(stdout, "\"");
while (*str) {
switch (*str) {
case '"':
case '\\':
fprintf(stdout, "\\%c", *str);
break;
default:
fprintf(stdout, "%c", *str);
}
str++;
}
fprintf(stdout, "\"");
}
static inline void
print_number(const char *str)
{
fprintf(stdout, "%s", str);
}
static inline void
print_boolean(const char *str)
{
/* FIXME useful? May have been checked during ini parsing */
fprintf(stdout, strcmp(str, "true") == 0 ? "true" : "false");
}
static inline void
print_string_or_number(const char *str)
{
is_number(str) ? print_number(str) : print_string(str);
}
static void
block_to_json(struct block *block)
{
int first = true;
#define JSON(_name, _size, _type) \
if (*block->_name) { \
if (!first) fprintf(stdout, ","); \
else first = false; \
fprintf(stdout, "\"" #_name "\":"); \
print_##_type(block->_name); \
}
fprintf(stdout, "{");
PROTOCOL_KEYS(JSON);
fprintf(stdout, "}");
#undef JSON
}
/*
* Parse the <json> input for the key <name> and store the start of its value
* into <start> and its size into <len>.
*
* <start> set to 0 means that the key was not present.
*/
void
json_parse(const char *json, const char *name, int *start, int *len)
{
char *here = strstr(json, name);
*start = *len = 0;
if (here) {
here += strlen(name) + 2;
if (*here == '"') {
/* string */
here++;
*start = here - json;
while (*here++ != '"')
*len += 1;
} else {
/* number */
*start = here - json;
while (isdigit(*here++))
*len += 1;
}
}
}
void
json_print_status_line(struct status_line *status)
{
bool first = true;
int i = 0;
fprintf(stdout, ",[");
for (i = 0; i < status->num; ++i) {
struct block *block = status->updated_blocks + i;
/* full_text is the only mandatory key, skip if empty */
if (!*block->full_text) {
bdebug(block, "no text to display, skipping");
continue;
}
if (!first) fprintf(stdout, ",");
else first = false;
bdebug(block, "print json");
block_to_json(block);
}
fprintf(stdout, "]\n");
fflush(stdout);
}