Skip to content

Commit a56bc93

Browse files
committed
Remove the blocks for variable declaration #212
1 parent 72ca122 commit a56bc93

File tree

13 files changed

+1024
-1227
lines changed

13 files changed

+1024
-1227
lines changed

library/json/test/src/usage_tetengo.json.parsing_c.c

Lines changed: 100 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -24,59 +24,57 @@ void usage_tetengo_json_parsing()
2424
{
2525
// clang-format off
2626
static const char* const json_text =
27-
"{\n"
28-
" \"hoge\": 42,\n"
29-
" \"fuga\": [ \"foo\", \"bar\" ]\n"
30-
"}\n";
27+
"{\n"
28+
" \"hoge\": 42,\n"
29+
" \"fuga\": [ \"foo\", \"bar\" ]\n"
30+
"}\n";
3131
// clang-format on
3232
static const char* const json_file_path = "jsonParser_sample.json";
3333
if (!make_json_file(json_text, json_file_path))
3434
{
3535
return;
3636
}
37-
{
38-
// Creates a reader from a file path.
39-
tetengo_json_reader_t* const p_reader = tetengo_json_reader_createStreamReader(
40-
json_file_path, tetengo_json_reader_streamReaderDefaultBufferCapacity());
41-
42-
// Creates a JSON parser
43-
tetengo_json_jsonParser_t* const p_parser =
44-
tetengo_json_jsonParser_create(p_reader, tetengo_json_jsonParser_defaultBufferCapacity());
4537

46-
// Iteration.
47-
char element_list_string[384] = { 0 };
48-
while (tetengo_json_jsonParser_hasNext(p_parser))
49-
{
50-
// Obtains the current element.
51-
const tetengo_json_element_t* const p_element = tetengo_json_jsonParser_peek(p_parser);
38+
// Creates a reader from a file path.
39+
tetengo_json_reader_t* const p_reader =
40+
tetengo_json_reader_createStreamReader(json_file_path, tetengo_json_reader_streamReaderDefaultBufferCapacity());
5241

53-
strcat(element_list_string, to_string(p_element));
42+
// Creates a JSON parser
43+
tetengo_json_jsonParser_t* const p_parser =
44+
tetengo_json_jsonParser_create(p_reader, tetengo_json_jsonParser_defaultBufferCapacity());
5445

55-
// Moves to the next element.
56-
tetengo_json_jsonParser_next(p_parser);
57-
}
46+
// Iteration.
47+
char element_list_string[384] = { 0 };
48+
while (tetengo_json_jsonParser_hasNext(p_parser))
49+
{
50+
// Obtains the current element.
51+
const tetengo_json_element_t* const p_element = tetengo_json_jsonParser_peek(p_parser);
5852

59-
{
60-
// clang-format off
61-
static const char* const expected =
62-
"object:open:\n"
63-
"member:open:name=hoge:\n"
64-
"number:42\n"
65-
"member:close:\n"
66-
"member:open:name=fuga:\n"
67-
"array:open:\n"
68-
"string:foo\n"
69-
"string:bar\n"
70-
"array:close:\n"
71-
"member:close:\n"
72-
"object:close:\n";
73-
// clang-format on
74-
assert(strcmp(element_list_string, expected) == 0);
75-
}
53+
strcat(element_list_string, to_string(p_element));
7654

77-
// Destroys the JSON parser. The reader inside is also destroyed.
78-
tetengo_json_jsonParser_destroy(p_parser);
55+
// Moves to the next element.
56+
tetengo_json_jsonParser_next(p_parser);
7957
}
58+
59+
// clang-format off
60+
static const char* const expected =
61+
"object:open:\n"
62+
"member:open:name=hoge:\n"
63+
"number:42\n"
64+
"member:close:\n"
65+
"member:open:name=fuga:\n"
66+
"array:open:\n"
67+
"string:foo\n"
68+
"string:bar\n"
69+
"array:close:\n"
70+
"member:close:\n"
71+
"object:close:\n";
72+
// clang-format on
73+
assert(strcmp(element_list_string, expected) == 0);
74+
75+
// Destroys the JSON parser. The reader inside is also destroyed.
76+
tetengo_json_jsonParser_destroy(p_parser);
77+
8078
remove(json_file_path);
8179
}
8280

@@ -97,80 +95,75 @@ const char* to_string(const tetengo_json_element_t* const p_element)
9795
{
9896
static char result[32] = { 0 };
9997
result[0] = '\0';
98+
99+
// Obtains the element type name.
100+
const tetengo_json_element_type_t* const p_type = tetengo_json_element_type(p_element);
101+
if (p_type->name == tetengo_json_element_typeName_string())
100102
{
101-
// Obtains the element type name.
102-
const tetengo_json_element_type_t* const p_type = tetengo_json_element_type(p_element);
103-
if (p_type->name == tetengo_json_element_typeName_string())
104-
{
105-
strcat(result, "string:");
106-
}
107-
else if (p_type->name == tetengo_json_element_typeName_number())
108-
{
109-
strcat(result, "number:");
110-
}
111-
else if (p_type->name == tetengo_json_element_typeName_boolean())
112-
{
113-
strcat(result, "boolean:");
114-
}
115-
else if (p_type->name == tetengo_json_element_typeName_null())
116-
{
117-
strcat(result, "null:");
118-
}
119-
else if (p_type->name == tetengo_json_element_typeName_object())
120-
{
121-
strcat(result, "object:");
122-
}
123-
else if (p_type->name == tetengo_json_element_typeName_member())
124-
{
125-
strcat(result, "member:");
126-
}
127-
else
128-
{
129-
assert(p_type->name == tetengo_json_element_typeName_array());
130-
strcat(result, "array:");
131-
}
103+
strcat(result, "string:");
104+
}
105+
else if (p_type->name == tetengo_json_element_typeName_number())
106+
{
107+
strcat(result, "number:");
108+
}
109+
else if (p_type->name == tetengo_json_element_typeName_boolean())
110+
{
111+
strcat(result, "boolean:");
112+
}
113+
else if (p_type->name == tetengo_json_element_typeName_null())
114+
{
115+
strcat(result, "null:");
116+
}
117+
else if (p_type->name == tetengo_json_element_typeName_object())
118+
{
119+
strcat(result, "object:");
120+
}
121+
else if (p_type->name == tetengo_json_element_typeName_member())
122+
{
123+
strcat(result, "member:");
124+
}
125+
else
126+
{
127+
assert(p_type->name == tetengo_json_element_typeName_array());
128+
strcat(result, "array:");
129+
}
130+
131+
// Obtains the element type category.
132+
if (p_type->category == tetengo_json_element_typeCategory_primitive()) {}
133+
else if (p_type->category == tetengo_json_element_typeCategory_structureOpen())
134+
{
135+
strcat(result, "open:");
136+
}
137+
else
138+
{
139+
assert(p_type->category == tetengo_json_element_typeCategory_structureClose());
140+
strcat(result, "close:");
141+
}
132142

133-
// Obtains the element type category.
134-
if (p_type->category == tetengo_json_element_typeCategory_primitive()) {}
135-
else if (p_type->category == tetengo_json_element_typeCategory_structureOpen())
143+
// Obtains the element attributes.
144+
const size_t attribute_count = tetengo_json_element_attributeKeys(p_element, NULL);
145+
if (attribute_count > 0)
146+
{
147+
const char** const p_keys = (const char**)malloc(attribute_count * sizeof(const char*));
148+
if (!p_keys)
136149
{
137-
strcat(result, "open:");
150+
fprintf(stderr, "Failed to allocate a memory.");
151+
return result;
138152
}
139-
else
153+
tetengo_json_element_attributeKeys(p_element, p_keys);
154+
for (size_t i = 0; i < attribute_count; ++i)
140155
{
141-
assert(p_type->category == tetengo_json_element_typeCategory_structureClose());
142-
strcat(result, "close:");
156+
strcat(result, p_keys[i]);
157+
strcat(result, "=");
158+
strcat(result, tetengo_json_element_attributeValueOf(p_element, p_keys[i]));
159+
strcat(result, ":");
143160
}
161+
free((void*)p_keys);
162+
}
144163

145-
{
146-
// Obtains the element attributes.
147-
const size_t attribute_count = tetengo_json_element_attributeKeys(p_element, NULL);
148-
if (attribute_count > 0)
149-
{
150-
const char** const p_keys = (const char**)malloc(attribute_count * sizeof(const char*));
151-
if (!p_keys)
152-
{
153-
fprintf(stderr, "Failed to allocate a memory.");
154-
return result;
155-
}
156-
tetengo_json_element_attributeKeys(p_element, p_keys);
157-
{
158-
size_t i = 0;
159-
for (i = 0; i < attribute_count; ++i)
160-
{
161-
strcat(result, p_keys[i]);
162-
strcat(result, "=");
163-
strcat(result, tetengo_json_element_attributeValueOf(p_element, p_keys[i]));
164-
strcat(result, ":");
165-
}
166-
}
167-
free((void*)p_keys);
168-
}
169-
}
164+
// Obtains the element value.
165+
strcat(result, tetengo_json_element_value(p_element));
170166

171-
// Obtains the element value.
172-
strcat(result, tetengo_json_element_value(p_element));
173-
}
174167
strcat(result, "\n");
175168
return result;
176169
}

0 commit comments

Comments
 (0)