File: json.c

package info (click to toggle)
nvme-cli 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,664 kB
  • sloc: ansic: 80,727; sh: 2,257; python: 975; makefile: 70; ruby: 25
file content (153 lines) | stat: -rw-r--r-- 3,159 bytes parent folder | download
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
// SPDX-License-Identifier: GPL-2.0-or-later
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>

#include "json.h"
#include "types.h"
#include "cleanup.h"

struct json_object *util_json_object_new_double(long double d)
{
	struct json_object *obj;
	char *str;

	if (asprintf(&str, "%Lf", d) < 0)
		return NULL;

	obj = json_object_new_string(str);

	free(str);
	return obj;

}

struct json_object *util_json_object_new_uint64(uint64_t i)
{
	struct json_object *obj;
	char *str;

	if (asprintf(&str, "%" PRIu64, i) < 0)
		return NULL;

	obj = json_object_new_string(str);

	free(str);
	return obj;

}

static int util_json_object_string_to_number(struct json_object *jso,
					     struct printbuf *pb, int level,
					     int flags)
{
	ssize_t len = json_object_get_string_len(jso);

	printbuf_memappend(pb, json_object_get_string(jso), len);

	return 0;
}

struct json_object *util_json_object_new_uint128(nvme_uint128_t  val)
{
	struct json_object *obj;

	obj = json_object_new_string(uint128_t_to_string(val));
	json_object_set_serializer(obj, util_json_object_string_to_number, NULL, NULL);

	return obj;
}

uint64_t util_json_object_get_uint64(struct json_object *obj)
{
	uint64_t val = 0;

	if (json_object_is_type(obj, json_type_string)) {
		char *end = NULL;
		const char *buf;

		buf = json_object_get_string(obj);
		val = strtoull(buf, &end, 10);
		if ((val == 0 && errno != 0) || (end == buf))
			return 0;
	}

	return val;
}

void json_object_add_uint_02x(struct json_object *o, const char *k, __u32 v)
{
	json_object_add_uint_0nx(o, k, v, 2);
}

void json_object_add_uint_0x(struct json_object *o, const char *k, __u32 v)
{
	char str[STR_LEN];

	sprintf(str, "0x%x", v);
	json_object_add_value_string(o, k, str);
}

void json_object_add_byte_array(struct json_object *o, const char *k, unsigned char *buf, int len)
{
	int i;

	_cleanup_free_ char *value = NULL;

	if (!buf || !len) {
		json_object_add_value_string(o, k, "No information provided");
		return;
	}

	value = calloc(1, (len + 1) * 2 + 1);

	if (!value) {
		json_object_add_value_string(o, k, "Could not allocate string");
		return;
	}

	sprintf(value, "0x");
	for (i = 1; i <= len; i++)
		sprintf(&value[i * 2], "%02x", buf[len - i]);

	json_object_add_value_string(o, k, value);
}

void json_object_add_nprix64(struct json_object *o, const char *k, uint64_t v)
{
	char str[STR_LEN];

	sprintf(str, "%#"PRIx64"", v);
	json_object_add_value_string(o, k, str);
}

void json_object_add_uint_0nx(struct json_object *o, const char *k, __u32 v, int width)
{
	char str[STR_LEN];

	sprintf(str, "0x%0*x", width, v);
	json_object_add_value_string(o, k, str);
}

void json_object_add_0nprix64(struct json_object *o, const char *k, uint64_t v, int width)
{
	char str[STR_LEN];

	sprintf(str, "0x%0*"PRIx64"", width, v);
	json_object_add_value_string(o, k, str);
}

void json_object_add_string(struct json_object *o, const char *k, const char *format, ...)
{
	_cleanup_free_ char *value = NULL;
	va_list ap;

	va_start(ap, format);

	if (vasprintf(&value, format, ap) < 0)
		value = NULL;

	json_object_add_value_string(o, k, value ? value : "Could not allocate string");

	va_end(ap);
}