Merge branch 'gitee-master' into gitlab-upstream

This commit is contained in:
zhengshuxin 2024-08-28 18:02:44 +08:00
commit 99b28700c2
33 changed files with 1466 additions and 202 deletions

View File

@ -28,6 +28,9 @@ struct ACL_JSON_NODE {
#define ACL_JSON_T_A_NULL (1 << 3)
#define ACL_JSON_T_A_DOUBLE (1 << 4)
#define ACL_JSON_T_A_TYPES (ACL_JSON_T_A_NULL | ACL_JSON_T_A_BOOL | \
ACL_JSON_T_A_NUMBER | ACL_JSON_T_A_DOUBLE | ACL_JSON_T_A_STRING)
#define ACL_JSON_T_STRING (1 << 5)
#define ACL_JSON_T_NUMBER (1 << 6)
#define ACL_JSON_T_BOOL (1 << 7)
@ -49,6 +52,7 @@ struct ACL_JSON_NODE {
unsigned char right_ch; /**< 本节点的最后一个字符: } or ] */
unsigned backslash:1; /**< 转义字符 \ */
unsigned part_word:1; /**< 半个汉字的情况处理标志位 */
unsigned disabled:1; /**< 该节点是否被禁止 */
ACL_JSON *json; /**< json 对象 */
ACL_RING node; /**< 当前节点 */
@ -126,6 +130,35 @@ ACL_API ACL_JSON_NODE *acl_json_node_alloc(ACL_JSON *json);
*/
ACL_API int acl_json_node_delete(ACL_JSON_NODE *node);
/**
* json json
* @param node {ACL_JSON_NODE*} json
* @return {ACL_JSON_NODE*} json
*/
ACL_API ACL_JSON_NODE *acl_json_node_erase(ACL_JSON_NODE *node, ACL_ITER *it);
/**
* json json
* @param node {ACL_JSON_NODE*} json
* @return {ACL_JSON_NODE*} json
*/
ACL_API ACL_JSON_NODE *acl_json_node_rerase(ACL_JSON_NODE *node, ACL_ITER *it);
/**
* / json json json
* 便
* @param node {ACL_JSON_NODE*} json
* @param yes {int} json
*/
ACL_API void acl_json_node_disable(ACL_JSON_NODE *node, int yes);
/**
* json
* @param node {ACL_JSON_NODE*} json
* @return {int} 0
*/
ACL_API int acl_json_node_disabled(ACL_JSON_NODE *node);
/**
* json ( json )
* @param node1 {ACL_JSON_NODE*} json
@ -138,8 +171,7 @@ ACL_API void acl_json_node_append(ACL_JSON_NODE *node1, ACL_JSON_NODE *node2);
* @param parent {ACL_JSON_NODE*}
* @param child {ACL_JSON_NODE*}
*/
ACL_API void acl_json_node_add_child(
ACL_JSON_NODE *parent, ACL_JSON_NODE *child);
ACL_API void acl_json_node_add_child(ACL_JSON_NODE *parent, ACL_JSON_NODE *child);
/**
* JSON JSON JSON JSON
@ -148,8 +180,7 @@ ACL_API void acl_json_node_add_child(
* @param from {ACL_JSON_NODE*} JSON JSON
* @return {ACL_JSON_NODE*}
*/
ACL_API ACL_JSON_NODE *acl_json_node_duplicate(
ACL_JSON *json, ACL_JSON_NODE *from);
ACL_API ACL_JSON_NODE *acl_json_node_duplicate(ACL_JSON *json, ACL_JSON_NODE *from);
/**
* json
@ -172,6 +203,14 @@ ACL_API ACL_JSON_NODE *acl_json_node_next(ACL_JSON_NODE *node);
*/
ACL_API ACL_JSON_NODE *acl_json_node_prev(ACL_JSON_NODE *node);
/**
* Json json json
* @param json {ACL_JSON*} json
* @param it {ACL_ITER*} json
* @return {ACL_JSON_NODE*} json
*/
ACL_API ACL_JSON_NODE *acl_json_erase(ACL_JSON *json, ACL_ITER *it);
/**
* json
* @return {ACL_JSON*} json

View File

@ -0,0 +1,3 @@
base_path = ../../..
include ../../Makefile_cpp.in
PROG = json

View File

@ -0,0 +1,33 @@
{
"name1": "value1",
"name3": "value3",
"name4": "value4",
"name5": [ "value51", "value52", "value53" ],
"name6": "value6",
"name7": { "name71": "value71", "name72": "value72", "name73": "value73" },
"name8": "value8",
"name10": "value10",
"name11": {
"name111": {
"name1111": "value1111",
"name1112": "value1112",
"name1113": "value1113" },
"name112": "value112",
"name113": [
{"name1131": "value1131"},
{"name1132": "value1132"},
{"name1133": "value1133"}
],
"name114": "value114",
"name116": "value116",
"name117": { "name1171":{ "name11711": { "name117111": "value11711" }}},
"name118": "value118",
"name119": {
"name1191":{
"name11911": { "name119111": "value11911"},
"name11912": [{ "name119121": "value119121" }, { "name119122": "value119122" }]
}
},
"name1110": "value1110"
}
}

View File

@ -0,0 +1,13 @@
{
"name113": [
{"name1131": "value1131"},
{"name1132": "value1132"},
{"name1133": "value1133"}
],
"name117": { "name1171":{ "name11711": { "name117111": "value11711" }}},
"name118": [
{ "name1181": { "name11811": "value11811", "name11812": "value11812", "name11813": "value11813" }},
{ "name1182": "value1182" },
{ "name1183": "value1183" }
]
}

View File

@ -0,0 +1,27 @@
{ "menu name": {
"file": "file",
"value": "File",
"popup": {
"menuitem1": [
{"value": "New1", "onclick": "CreateNewDoc"},
{"value": "Open1", "onclick": "OpenDoc"},
{"value": "Close1", "onclick": "CloseDoc"}
],
"menuname": "hello world",
"inner": { "value" : "new2", "value" : "open2"},
"menuitem2": [
{"value": "New3", "onclick": "CreateNewDoc"},
{"value": "Open3", "onclick": "OpenDoc"},
{"value": "Close3", "onclick": "CloseDoc"},
{"value": "Help3", "onclick": "Help"}
]
}
},
"help": "hello world",
"menuitem2": [
{"value": "New4", "onclick": "CreateNewDoc"},
{"value": "Open4", "onclick": "OpenDoc"},
{"value": "Close4", "onclick": "CloseDoc"},
[{"value": "Save4", "onclick": "SaveDoc"}]
]
}

View File

@ -0,0 +1,94 @@
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include "lib_acl.h"
static void test_json(const char *data, ACL_ARGV *names)
{
ACL_JSON *json = acl_json_alloc();
ACL_VSTRING *buf = acl_vstring_alloc(1024);
ACL_ITER iter, iter2;
acl_json_update(json, data);
if (!acl_json_finish(json)) {
printf("Invalid json!\r\n");
return;
}
#define STR acl_vstring_str
#define LEN ACL_VSTRING_LEN
#define EQ !strcmp
acl_foreach(iter, json) {
ACL_JSON_NODE* node = (ACL_JSON_NODE*) iter.data;
if (LEN(node->ltag) == 0) {
continue;
}
acl_foreach(iter2, names) {
const char *name = (const char*) iter2.data;
if (EQ(STR(node->ltag), name)) {
acl_json_node_disable(node, 1);
}
}
}
acl_json_build(json, buf);
printf("%s\r\n", STR(buf));
acl_json_free(json);
acl_vstring_free(buf);
}
static void usage(const char *procname)
{
printf("usage: %s -h [help] -f filename -n tag_names\r\n", procname);
}
int main(int argc, char *argv[])
{
ACL_ARGV *names;
char name[512], filename[256], *data;
int ch;
snprintf(name, sizeof(name), "name");
snprintf(filename, sizeof(filename), "json.txt");
while ((ch = getopt(argc, argv, "hf:n:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'f':
snprintf(filename, sizeof(filename), "%s", optarg);
break;
case 'n':
snprintf(name, sizeof(name), "%s", optarg);
break;
default:
break;
}
}
if (name[0] == 0 || filename[0] == 0) {
usage(argv[0]);
return 0;
}
data = acl_vstream_loadfile(filename);
if (data == NULL || *data == 0) {
printf("load %s error %s\r\n", filename, acl_last_serror());
return 1;
}
names = acl_argv_split(name, ",; \t");
test_json(data, names);
acl_argv_free(names);
acl_myfree(data);
#ifdef WIN32
getchar();
#endif
return (0);
}

144
lib_acl/samples/json/json7/t.sh Executable file
View File

@ -0,0 +1,144 @@
#!/bin/sh
./json -n name1|jq
echo "------------------------name1-------------------------------------------"
read ch
./json -n name3|jq
echo "------------------------name3-------------------------------------------"
read ch
./json -n name4|jq
echo "------------------------name4-------------------------------------------"
read ch
./json -n name5|jq
echo "------------------------name5-------------------------------------------"
read ch
./json -n name6|jq
echo "------------------------name6-------------------------------------------"
read ch
./json -n name7|jq
echo "------------------------name7-------------------------------------------"
read ch
./json -n name71|jq
echo "------------------------name71------------------------------------------"
read ch
./json -n name72|jq
echo "------------------------name72------------------------------------------"
read ch
./json -n name73|jq
echo "------------------------name73------------------------------------------"
read ch
./json -n name8|jq
echo "------------------------name8-------------------------------------------"
read ch
./json -n name10|jq
echo "------------------------name10------------------------------------------"
read ch
./json -n name11|jq
echo "------------------------name11------------------------------------------"
read ch
./json -n name111|jq
echo "------------------------name111-----------------------------------------"
read ch
./json -n name1111|jq
echo "------------------------name1111----------------------------------------"
read ch
./json -n name1112|jq
echo "------------------------name1112----------------------------------------"
read ch
./json -n name1113|jq
echo "------------------------name1113----------------------------------------"
read ch
./json -n name112|jq
echo "------------------------name112-----------------------------------------"
read ch
./json -n name113|jq
echo "------------------------name113-----------------------------------------"
read ch
./json -n name1131|jq
echo "------------------------name1131----------------------------------------"
read ch
./json -n name1132|jq
echo "------------------------name1132----------------------------------------"
read ch
./json -n name1133|jq
echo "------------------------name1133----------------------------------------"
read ch
./json -n name114|jq
echo "------------------------name114-----------------------------------------"
read ch
./json -n name116|jq
echo "------------------------name116-----------------------------------------"
read ch
./json -n name117|jq
echo "------------------------name117-----------------------------------------"
read ch
./json -n name1171|jq
echo "------------------------name1171----------------------------------------"
read ch
./json -n name11711|jq
echo "------------------------name11711---------------------------------------"
read ch
./json -n name118|jq
echo "------------------------name118-----------------------------------------"
read ch
./json -n name119|jq
echo "------------------------name119-----------------------------------------"
read ch
./json -n name1191|jq
echo "------------------------name1191----------------------------------------"
read ch
./json -n name11911|jq
echo "------------------------name11911---------------------------------------"
read ch
./json -n name119111|jq
echo "------------------------name119111--------------------------------------"
read ch
./json -n name11912|jq
echo "------------------------name11912---------------------------------------"
read ch
./json -n name119121|jq
echo "------------------------name119121--------------------------------------"
read ch
./json -n name119122|jq
echo "------------------------name119122--------------------------------------"
read ch
./json -n name1110|jq
echo "------------------------name1110----------------------------------------"
read ch
./json -n name1111|jq
echo "------------------------name1111----------------------------------------"

View File

@ -0,0 +1,72 @@
#!/bin/sh
./json -f json1.txt -n name113|jq
echo "------------------------name113-----------------------------------------"
read ch
./json -f json1.txt -n name1131|jq
echo "------------------------name1131----------------------------------------"
read ch
./json -f json1.txt -n name1132|jq
echo "------------------------name1132----------------------------------------"
read ch
./json -f json1.txt -n name1133|jq
echo "------------------------name1133----------------------------------------"
read ch
./json -f json1.txt -n name117|jq
echo "------------------------name117-----------------------------------------"
read ch
./json -f json1.txt -n name1171|jq
echo "------------------------name1171----------------------------------------"
read ch
./json -f json1.txt -n name11711|jq
echo "------------------------name11711---------------------------------------"
read ch
./json -f json1.txt -n name117111|jq
echo "------------------------name117111--------------------------------------"
read ch
./json -f json1.txt -n "name1131,name117111"|jq
echo "------------------------name1131,name117111-----------------------------"
read ch
./json -f json1.txt -n "name1131,name1132"|jq
echo "------------------------name1131,name1132-------------------------------"
read ch
./json -f json1.txt -n "name118"|jq
echo "------------------------name118-----------------------------------------"
read ch
./json -f json1.txt -n "name1181"|jq
echo "------------------------name1181----------------------------------------"
read ch
./json -f json1.txt -n "name1182"|jq
echo "------------------------name1183----------------------------------------"
read ch
./json -f json1.txt -n "name1183"|jq
echo "------------------------name1183----------------------------------------"
read ch
./json -f json1.txt -n "name1181,name1182"|jq
echo "------------------------name1181,name1182-------------------------------"
read ch
./json -f json1.txt -n "name1181,name1183"|jq
echo "------------------------name1181,name1183-------------------------------"
read ch
./json -f json1.txt -n "name1183,name1182"|jq
echo "------------------------name1183,name1182-------------------------------"
read ch
./json -f json1.txt -n "name1181,name1182,name1183"|jq
echo "------------------------name1181,name1182,name1183----------------------"

View File

@ -0,0 +1,3 @@
#!/bin/sh
valgrind --tool=memcheck --leak-check=yes -v ./json

View File

@ -1,5 +1,4 @@
#include "StdAfx.h"
#include <stdio.h>
#ifndef ACL_PREPARE_COMPILE
#include "stdlib/acl_define.h"
#include "stdlib/acl_mymalloc.h"
@ -105,6 +104,7 @@ ACL_JSON_NODE *acl_json_node_alloc(ACL_JSON *json)
node->right_ch = 0;
node->backslash = 0;
node->part_word = 0;
node->disabled = 0;
node->json = json;
node->iter_head = node_iter_head;
@ -133,6 +133,36 @@ int acl_json_node_delete(ACL_JSON_NODE *node)
return n;
}
ACL_JSON_NODE *acl_json_node_erase(ACL_JSON_NODE *node, ACL_ITER *it)
{
ACL_JSON_NODE *curr = (ACL_JSON_NODE*) it->data;
ACL_JSON_NODE *next = node->iter_next(it, node);
if (curr) {
acl_json_node_delete(curr);
}
return next;
}
ACL_JSON_NODE *acl_json_node_rerase(ACL_JSON_NODE *node, ACL_ITER *it)
{
ACL_JSON_NODE *curr = (ACL_JSON_NODE*) it->data;
ACL_JSON_NODE *next = node->iter_prev(it, node);
if (curr) {
acl_json_node_delete(curr);
}
return next;
}
void acl_json_node_disable(ACL_JSON_NODE *node, int yes)
{
node->disabled = yes ? 1 : (unsigned) 0;
}
int acl_json_node_disabled(ACL_JSON_NODE *node)
{
return node->disabled != (unsigned) 0;
}
void acl_json_node_append(ACL_JSON_NODE *node1, ACL_JSON_NODE *node2)
{
acl_ring_append(&node1->node, &node2->node);
@ -155,12 +185,14 @@ ACL_JSON_NODE *acl_json_node_next(ACL_JSON_NODE *node)
ACL_RING *ring_ptr = acl_ring_succ(&node->node);
ACL_JSON_NODE *parent;
if (ring_ptr == &node->node)
if (ring_ptr == &node->node) {
return NULL;
}
parent = node->parent;
acl_assert(parent != NULL);
if (ring_ptr == &parent->children)
if (ring_ptr == &parent->children) {
return NULL;
}
return acl_ring_to_appl(ring_ptr, ACL_JSON_NODE, node);
}
@ -169,12 +201,14 @@ ACL_JSON_NODE *acl_json_node_prev(ACL_JSON_NODE *node)
ACL_RING *ring_ptr = acl_ring_pred(&node->node);
ACL_JSON_NODE *parent;
if (ring_ptr == &node->node)
if (ring_ptr == &node->node) {
return NULL;
}
parent = node->parent;
acl_assert(parent != NULL);
if (ring_ptr == &parent->children)
if (ring_ptr == &parent->children) {
return NULL;
}
return acl_ring_to_appl(ring_ptr, ACL_JSON_NODE, node);
}
@ -234,15 +268,16 @@ static ACL_JSON_NODE *json_iter_next(ACL_ITER *it, ACL_JSON *json)
}
/* 当前节点的兄弟节点遍历完毕,最后遍历当前节点的父节点的兄弟节点 */
do {
if (parent == json->root)
if (parent == json->root) {
break;
}
ring_ptr = acl_ring_succ(&parent->node);
parent = acl_json_node_parent(parent);
if (parent == NULL)
if (parent == NULL) {
acl_msg_fatal("%s(%d): parent null", __FILE__, __LINE__);
}
if (ring_ptr != &parent->children) {
it->i++;
@ -253,7 +288,6 @@ static ACL_JSON_NODE *json_iter_next(ACL_ITER *it, ACL_JSON *json)
} while (ring_ptr != &json->root->children);
/* 遍历完所有节点 */
it->ptr = it->data = NULL;
return NULL;
}
@ -310,8 +344,9 @@ static ACL_JSON_NODE *json_iter_prev(ACL_ITER *it, ACL_JSON *json)
/* 当前节点的兄弟节点遍历完毕,最后遍历当前节点的父节点的兄弟节点 */
do {
if (parent == json->root)
if (parent == json->root) {
break;
}
ring_ptr = acl_ring_pred(&parent->node);
parent = acl_json_node_parent(parent);
if (parent == NULL)
@ -331,6 +366,51 @@ static ACL_JSON_NODE *json_iter_prev(ACL_ITER *it, ACL_JSON *json)
return NULL;
}
ACL_JSON_NODE *acl_json_erase(ACL_JSON *json, ACL_ITER *it)
{
ACL_RING *ring_ptr;
ACL_JSON_NODE *node, *parent;
node = (ACL_JSON_NODE*) it->data;
parent = acl_json_node_parent(node);
/* 查找当前节点的下一个兄弟节点 */
ring_ptr = acl_ring_succ(&node->node);
if (ring_ptr != &parent->children) {
it->i++;
it->ptr = acl_ring_to_appl(ring_ptr, ACL_JSON_NODE, node);
it->data = it->ptr;
acl_json_node_delete(node); /* 从 json 中删除当前节点 */
return it->ptr;
}
acl_json_node_delete(node); /* 从 json 中删除当前节点 */
/* 当前节点的兄弟节点遍历完毕,最后遍历当前节点的父节点的兄弟节点 */
do {
if (parent == json->root) {
break;
}
ring_ptr = acl_ring_succ(&parent->node);
parent = acl_json_node_parent(parent);
if (parent == NULL) {
acl_msg_fatal("%s(%d): parent null", __FILE__, __LINE__);
}
if (ring_ptr != &parent->children) {
it->i++;
it->ptr = acl_ring_to_appl(ring_ptr, ACL_JSON_NODE, node);
it->data = it->ptr;
return it->ptr;
}
} while (ring_ptr != &json->root->children);
/* 遍历完所有节点 */
it->ptr = it->data = NULL;
return NULL;
}
ACL_JSON *acl_json_alloc(void)
{
return acl_json_dbuf_alloc(NULL);
@ -388,6 +468,7 @@ ACL_JSON_NODE *acl_json_node_duplicate(ACL_JSON *json, ACL_JSON_NODE *from)
to->right_ch = from->right_ch;
to->type = from->type;
to->depth = from->depth; /* XXX? */
to->disabled = from->disabled;
acl_vstring_strcpy(to->ltag, STR(from->ltag));
acl_vstring_strcpy(to->text, STR(from->text));
@ -395,8 +476,9 @@ ACL_JSON_NODE *acl_json_node_duplicate(ACL_JSON *json, ACL_JSON_NODE *from)
child_from = acl_ring_to_appl(iter.ptr, ACL_JSON_NODE, node);
child_to = acl_json_node_duplicate(json, child_from);
acl_json_node_add_child(to, child_to);
if (from->tag_node == child_from)
if (from->tag_node == child_from) {
to->tag_node = child_to;
}
}
return to;
@ -418,7 +500,7 @@ ACL_JSON *acl_json_dbuf_create(ACL_DBUF_POOL *dbuf, ACL_JSON_NODE *node)
json = (ACL_JSON*) acl_dbuf_pool_calloc(dbuf, sizeof(ACL_JSON));
json->dbuf = dbuf;
json->dbuf_inner = NULL;
json->dbuf_inner = NULL;
/* 如果传入的节点为 root 节点,则直接赋值创建 root 即可 */
if (node == root) {
@ -463,14 +545,16 @@ void acl_json_foreach_init(ACL_JSON *json, ACL_JSON_NODE *node)
void acl_json_free(ACL_JSON *json)
{
if (json->dbuf_inner)
if (json->dbuf_inner) {
acl_dbuf_pool_destroy(json->dbuf_inner);
}
}
void acl_json_reset(ACL_JSON *json)
{
if (json->dbuf_inner != NULL)
if (json->dbuf_inner != NULL) {
acl_dbuf_pool_reset(json->dbuf, json->dbuf_keep);
}
json->root = acl_json_node_alloc(json);
#if 0

View File

@ -20,8 +20,9 @@ ACL_JSON_NODE *acl_json_getFirstElementByTagName(
acl_foreach(iter, json) {
ACL_JSON_NODE *node = (ACL_JSON_NODE*) iter.data;
if (strcasecmp(tag, STR(node->ltag)) == 0)
if (strcasecmp(tag, STR(node->ltag)) == 0) {
return node;
}
}
return NULL;
@ -257,9 +258,8 @@ void acl_json_node_append_child(ACL_JSON_NODE *parent, ACL_JSON_NODE *child)
const char *myname = "acl_json_node_append_child";
if (parent->type != ACL_JSON_T_ARRAY
&& parent->type != ACL_JSON_T_OBJ
&& parent != parent->json->root)
{
&& parent->type != ACL_JSON_T_OBJ
&& parent != parent->json->root) {
acl_msg_fatal("%s(%d): parent's type not array or obj",
myname, __LINE__);
}
@ -299,6 +299,26 @@ static void json_escape_append(ACL_VSTRING *buf, const char *src)
ACL_VSTRING_TERMINATE(buf);
}
static void child_end(ACL_JSON *json, ACL_JSON_NODE *node, ACL_VSTRING *buf)
{
/* 当本节点为叶节点且后面没有兄弟节点时,需要一级一级回溯
*
*
*/
while (acl_json_node_next(node) == NULL) {
if (node->parent == json->root) {
break;
}
node = node->parent;
/* right_ch: '}' or ']' */
if (node->right_ch != 0) {
ACL_VSTRING_ADDCH(buf, node->right_ch);
}
}
}
void acl_json_building(ACL_JSON *json, size_t length,
int (*callback)(ACL_JSON *, ACL_VSTRING *, void *), void *ctx)
{
@ -325,8 +345,9 @@ void acl_json_building(ACL_JSON *json, size_t length,
json->root->right_ch = '}';
}
if (json->root->left_ch > 0)
if (json->root->left_ch > 0) {
ACL_VSTRING_ADDCH(buf, json->root->left_ch);
}
acl_foreach(iter, json) {
if (ACL_VSTRING_LEN(buf) >= length && callback != NULL) {
@ -341,10 +362,11 @@ void acl_json_building(ACL_JSON *json, size_t length,
node = (ACL_JSON_NODE*) iter.data;
prev = acl_json_node_prev(node);
if (prev != NULL) {
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE))
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE)) {
acl_vstring_strcat(buf, ", ");
else
} else {
acl_vstring_strcat(buf, ",");
}
}
/* 只有当标签的对应值为 JSON 对象或数组对象时 tag_node 非空 */
@ -352,21 +374,24 @@ void acl_json_building(ACL_JSON *json, size_t length,
if (LEN(node->ltag) > 0) {
json_escape_append(buf, STR(node->ltag));
ACL_VSTRING_ADDCH(buf, ':');
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE))
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE)) {
ACL_VSTRING_ADDCH(buf, ' ');
}
}
/* '{' or '[' */
if (node->left_ch != 0)
if (node->left_ch != 0) {
ACL_VSTRING_ADDCH(buf, node->left_ch);
}
}
/* 当节点有标签名时 */
else if (LEN(node->ltag) > 0) {
json_escape_append(buf, STR(node->ltag));
ACL_VSTRING_ADDCH(buf, ':');
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE))
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE)) {
ACL_VSTRING_ADDCH(buf, ' ');
}
switch (node->type & ~ACL_JSON_T_LEAF) {
case ACL_JSON_T_NULL:
@ -384,9 +409,8 @@ void acl_json_building(ACL_JSON *json, size_t length,
}
/* 当节点为数组的成员时 */
else if (LEN(node->text) > 0 && node->parent
&& node->parent->left_ch != 0)
{
else if (node->parent && node->parent->type == ACL_JSON_T_ARRAY
&& (node->type & ACL_JSON_T_A_TYPES)) {
switch (node->type & ~ACL_JSON_T_LEAF) {
case ACL_JSON_T_A_NULL:
acl_vstring_strcat(buf, "null");
@ -417,8 +441,9 @@ void acl_json_building(ACL_JSON *json, size_t length,
if (acl_ring_size(&node->children) > 0) {
continue;
} else if (acl_json_node_next(node) != NULL) {
if (node->right_ch > 0)
if (node->right_ch > 0) {
ACL_VSTRING_ADDCH(buf, node->right_ch);
}
continue;
}
@ -430,20 +455,12 @@ void acl_json_building(ACL_JSON *json, size_t length,
*
*
*/
while (acl_json_node_next(node) == NULL) {
if (node->parent == json->root)
break;
node = node->parent;
/* right_ch: '}' or ']' */
if (node->right_ch != 0)
ACL_VSTRING_ADDCH(buf, node->right_ch);
}
child_end(json, node, buf);
}
if (json->root->right_ch > 0)
if (json->root->right_ch > 0) {
ACL_VSTRING_ADDCH(buf, json->root->right_ch);
}
ACL_VSTRING_TERMINATE(buf);
if (ACL_VSTRING_LEN(buf) > 0 && callback != NULL) {
@ -456,8 +473,29 @@ void acl_json_building(ACL_JSON *json, size_t length,
acl_vstring_free(buf);
/* 将第二个参数置 NULL 表示处理完毕 */
if (callback != NULL)
if (callback != NULL) {
(void) callback(json, NULL, ctx);
}
}
static int is_parents_disabled(ACL_JSON_NODE *node)
{
while ((node = node->parent)) {
if (node->disabled) {
return 1;
}
}
return 0;
}
static int is_first_node(ACL_JSON_NODE *node)
{
while ((node = acl_json_node_prev(node))) {
if (!node->disabled) {
return 0;
}
}
return 1;
}
ACL_VSTRING *acl_json_build(ACL_JSON *json, ACL_VSTRING *buf)
@ -466,8 +504,9 @@ ACL_VSTRING *acl_json_build(ACL_JSON *json, ACL_VSTRING *buf)
ACL_ITER iter;
ACL_RING *ring_ptr = acl_ring_succ(&json->root->children);
if (buf == NULL)
if (buf == NULL) {
buf = acl_vstring_alloc(256);
}
/* 为了兼容历史的BUG所以此处只能如此处理了--zsx, 2021.3.27 */
@ -487,17 +526,28 @@ ACL_VSTRING *acl_json_build(ACL_JSON *json, ACL_VSTRING *buf)
json->root->right_ch = '}';
}
if (json->root->left_ch > 0)
if (json->root->left_ch > 0) {
ACL_VSTRING_ADDCH(buf, json->root->left_ch);
}
acl_foreach(iter, json) {
node = (ACL_JSON_NODE*) iter.data;
if (node->disabled) { // 跳过被禁止的 json 节点
child_end(json, node, buf);
continue;
}
if (is_parents_disabled(node)) {
continue;
}
prev = acl_json_node_prev(node);
if (prev != NULL) {
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE))
if (prev != NULL && (!prev->disabled || !is_first_node(node))) {
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE)) {
acl_vstring_strcat(buf, ", ");
else
} else {
acl_vstring_strcat(buf, ",");
}
}
/* 只有当标签的对应值为 JSON 对象或数组对象时 tag_node 非空 */
@ -505,21 +555,24 @@ ACL_VSTRING *acl_json_build(ACL_JSON *json, ACL_VSTRING *buf)
if (LEN(node->ltag) > 0) {
json_escape_append(buf, STR(node->ltag));
ACL_VSTRING_ADDCH(buf, ':');
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE))
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE)) {
ACL_VSTRING_ADDCH(buf, ' ');
}
}
/* '{' or '[' */
if (node->left_ch != 0)
if (node->left_ch != 0) {
ACL_VSTRING_ADDCH(buf, node->left_ch);
}
}
/* 当节点有标签名时 */
else if (LEN(node->ltag) > 0) {
json_escape_append(buf, STR(node->ltag));
ACL_VSTRING_ADDCH(buf, ':');
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE))
if ((json->flag & ACL_JSON_FLAG_ADD_SPACE)) {
ACL_VSTRING_ADDCH(buf, ' ');
}
switch (node->type & ~ACL_JSON_T_LEAF) {
case ACL_JSON_T_NULL:
@ -537,22 +590,8 @@ ACL_VSTRING *acl_json_build(ACL_JSON *json, ACL_VSTRING *buf)
}
/* 当节点为数组的成员时 */
#if 0
else if (LEN(node->text) > 0 && node->parent
/* 应该依据父节点类型来确定当前节点是否为数组节点
* && node->parent->left_ch != 0)
*/
&& node->parent->type == ACL_JSON_T_ARRAY)
#elif 0
else if (node->parent && node->parent->type == ACL_JSON_T_ARRAY
&& (LEN(node->text) > 0 || (node->type & ACL_JSON_T_A_STRING)))
#else
else if (node->parent && node->parent->type == ACL_JSON_T_ARRAY
&& (node->type & (ACL_JSON_T_A_NULL
| ACL_JSON_T_A_BOOL | ACL_JSON_T_A_NUMBER
| ACL_JSON_T_A_DOUBLE | ACL_JSON_T_A_STRING)))
#endif
{
&& (node->type & ACL_JSON_T_A_TYPES)) {
switch (node->type & ~ACL_JSON_T_LEAF) {
case ACL_JSON_T_A_NULL:
acl_vstring_strcat(buf, "null");
@ -582,35 +621,29 @@ ACL_VSTRING *acl_json_build(ACL_JSON *json, ACL_VSTRING *buf)
/* 当本节点有子节点或虽为叶节点,但该节点的下一个兄弟节点
*
*/
if (acl_ring_size(&node->children) > 0)
if (acl_ring_size(&node->children) > 0) {
continue;
else if (acl_json_node_next(node) != NULL) {
if (node->right_ch > 0)
} else if (acl_json_node_next(node) != NULL) {
if (node->right_ch > 0) {
ACL_VSTRING_ADDCH(buf, node->right_ch);
}
continue;
}
if (node->right_ch > 0)
if (node->right_ch > 0) {
ACL_VSTRING_ADDCH(buf, node->right_ch);
}
/* 当本节点为叶节点且后面没有兄弟节点时,需要一级一级回溯
*
*
*/
while (acl_json_node_next(node) == NULL) {
if (node->parent == json->root)
break;
node = node->parent;
/* right_ch: '}' or ']' */
if (node->right_ch != 0)
ACL_VSTRING_ADDCH(buf, node->right_ch);
}
child_end(json, node, buf);
}
if (json->root->right_ch > 0)
if (json->root->right_ch > 0) {
ACL_VSTRING_ADDCH(buf, json->root->right_ch);
}
ACL_VSTRING_TERMINATE(buf);
return buf;
@ -621,13 +654,15 @@ ACL_VSTRING *acl_json_node_build(ACL_JSON_NODE *node, ACL_VSTRING *buf)
ACL_JSON *json = acl_json_alloc();
ACL_JSON_NODE *first;
if (buf == NULL)
if (buf == NULL) {
buf = acl_vstring_alloc(256);
}
if (node == node->json->root && node->tag_node != NULL)
if (node == node->json->root && node->tag_node != NULL) {
node = node->tag_node;
else
} else {
json->root->left_ch = json->root->right_ch = 0;
}
first = acl_json_node_duplicate(json, node);
acl_json_node_add_child(json->root, first);

View File

@ -33,27 +33,27 @@ public:
* @return {const char*} json
*
*/
const char* tag_name(void) const;
const char* tag_name() const;
/**
* json
*
* @return {const char*}
*/
const char* get_text(void) const;
const char* get_text() const;
/**
* json json json
* @return {const json_node*} NULL
* get_text get_obj NULL
*/
json_node* get_obj(void) const;
json_node* get_obj() const;
/**
* json
* @return {const char*} NULL
*/
const char* get_string(void) const;
const char* get_string() const;
/**
* json
@ -62,68 +62,68 @@ public:
#if defined(_WIN32) || defined(_WIN64)
const __int64* get_int64(void) const;
#else
const long long int* get_int64(void) const;
const long long int* get_int64() const;
#endif
/**
* json
* @return {const double*} NULL
*/
const double *get_double(void) const;
const double *get_double() const;
/**
* json
* @return {bool*} NULL
*/
const bool* get_bool(void) const;
const bool* get_bool() const;
/**
*
* @return {bool}
*/
bool is_string(void) const;
bool is_string() const;
/**
*
* @return {bool}
*/
bool is_number(void) const;
bool is_number() const;
/**
*
* @return {bool}
*/
bool is_double(void) const;
bool is_double() const;
/**
*
* @return {bool}
*/
bool is_bool(void) const;
bool is_bool() const;
/**
* null
* @return {bool}
*/
bool is_null(void) const;
bool is_null() const;
/**
*
* @return {bool}
*/
bool is_object(void) const;
bool is_object() const;
/**
*
* @return {bool}
*/
bool is_array(void) const;
bool is_array() const;
/**
*
* @return {const char*}
*/
const char* get_type(void) const;
const char* get_type() const;
/**
* json
@ -285,8 +285,7 @@ public:
* @return {json_node&} return_child true
* json
*/
json_node& add_array_text(const char* text,
bool return_child = false);
json_node& add_array_text(const char* text, bool return_child = false);
/**
* json json
@ -299,8 +298,7 @@ public:
json_node& add_array_number(__int64 value,
bool return_child = false);
#else
json_node& add_array_number(long long int value,
bool return_child = false);
json_node& add_array_number(long long int value, bool return_child = false);
#endif
/**
@ -333,7 +331,7 @@ public:
* @return {json_node&} json
*
*/
json_node& get_parent(void) const;
json_node& get_parent() const;
/////////////////////////////////////////////////////////////////////
@ -342,14 +340,22 @@ public:
* @return {json_node*}
* delete
*/
json_node* first_child(void);
json_node* first_child();
/**
*
* @return {json_node*}
* delete
*/
json_node* next_child(void);
json_node* next_child();
/**
*
* @param child {json_node*} first_child/next_child/remove_child
* json Json
* @return {json_node*}
*/
json_node* free_child(json_node* child);
/**
* json json
@ -362,19 +368,32 @@ public:
* json json
* @return {int}
*/
int depth(void) const;
int depth() const;
/**
* json
* @return {int} >= 0
*/
int children_count(void) const;
int children_count() const;
/**
* json json
* @return {int}
*/
int detach(void);
int detach();
/**
* json json
* json
* @param yes {bool} json false true
*/
void disable(bool yes);
/**
* json
* @return {bool}
*/
bool disabled() const;
/**
* json json_node
@ -382,19 +401,19 @@ public:
* next_child json_node
* 访
*/
void clear(void);
void clear();
/**
* json
* @return {json&}
*/
json& get_json(void) const;
json& get_json() const;
/**
* ACL json
* @return {ACL_JSON_NODE*}
*/
ACL_JSON_NODE* get_json_node(void) const;
ACL_JSON_NODE* get_json_node() const;
private:
friend class json;
@ -409,7 +428,7 @@ private:
/**
*
*/
~json_node(void);
~json_node();
/**
* json
@ -459,7 +478,7 @@ public:
*/
json(const json_node& node, dbuf_guard* dbuf = NULL);
~json(void);
~json();
/**
*
@ -482,14 +501,14 @@ public:
*
* @return {bool}
*/
bool finish(void);
bool finish();
/**
* json json json
* 使 json
* json
*/
void reset(void);
void reset();
/**
* json
@ -563,7 +582,7 @@ public:
* @return {ACL_JSON*}
*
*/
ACL_JSON* get_json(void) const;
ACL_JSON* get_json() const;
/////////////////////////////////////////////////////////////////////
@ -677,7 +696,7 @@ public:
* json
* reset json_node
*/
json_node& create_array_null(void);
json_node& create_array_null();
/**
* json_node ,
@ -688,7 +707,7 @@ public:
* reset json_node
*/
json_node& create_node(bool as_array = false);
json_node& create_array(void);
json_node& create_array();
/**
* json_node tag_name: {}
@ -744,7 +763,7 @@ public:
*
* @return {json_node&}
*/
json_node& get_root(void);
json_node& get_root();
/**
* json
@ -752,7 +771,7 @@ public:
*
*
*/
json_node* first_node(void);
json_node* first_node();
/**
* json json
@ -760,7 +779,14 @@ public:
*
*
*/
json_node* next_node(void);
json_node* next_node();
/**
* json json
* @param curr {json_node*} first_node/next_node/free_node
* @return {json_node*} json
*/
json_node* free_node(json_node* curr);
/**
* json
@ -781,7 +807,7 @@ public:
*
* @return {dbuf_guard*}
*/
dbuf_guard* get_dbuf(void) const {
dbuf_guard* get_dbuf() const {
return dbuf_;
}
@ -790,7 +816,7 @@ public:
virtual int push_pop(const char* in, size_t len,
string* out, size_t max = 0);
virtual int pop_end(string* out, size_t max = 0);
virtual void clear(void);
virtual void clear();
private:
// 内存池管理对象,适合管理大量小内存

View File

@ -1369,6 +1369,23 @@ bool operator == (const acl::string& l, const std::string& r);
// std::cout << s << std::endl;
std::ostream& operator << (std::ostream& o, const acl::string& s);
/**
*
* @param str {const char*}
* @param sep {const char*}
* @param out {std::vector<std::string>&}
*/
void split(const char* str, const char* sep, std::vector<std::string>& out);
/**
*
* @param str {const char*}
* @param sep {const char*}
* @param out {std::list<std::string>&}
* @return {size_t}
*/
size_t split(const char* str, const char* sep, std::list<std::string>& out);
} // namespce acl
#if __cplusplus >= 201103L // Support c++11 ?

View File

@ -14,27 +14,25 @@ class token_tree;
/**
* 256
*/
class ACL_CPP_API token_node : public noncopyable
{
class ACL_CPP_API token_node : public noncopyable {
public:
/**
*
* @return {const char*}
*/
const char* get_key(void) const;
const char* get_key() const;
/**
*
* @return {void*}
*/
void* get_ctx(void) const;
void* get_ctx() const;
/**
*
* @return {token_tree*}
*/
token_tree* get_tree(void) const
{
token_tree* get_tree() const {
return tree_;
}
@ -42,16 +40,15 @@ public:
* C
* @return {ACL_TOKEN*}
*/
ACL_TOKEN* get_token(void) const
{
ACL_TOKEN* get_token() const {
return me_;
}
private:
friend class token_tree; // 仅允许 token_tree 构造/析构本类对象
token_node(void);
~token_node(void);
token_node();
~token_node();
void set_node(ACL_TOKEN* token, token_tree* tree);
@ -66,11 +63,10 @@ private:
* 256 ()
* 256
*/
class ACL_CPP_API token_tree : public noncopyable
{
class ACL_CPP_API token_tree : public noncopyable {
public:
token_tree(void);
~token_tree(void);
token_tree();
~token_tree();
/**
*
@ -129,20 +125,19 @@ public:
* 256
* @return {token_node*}
*/
const token_node* first_node(void);
const token_node* first_node();
/**
* 256
* @return {token_node*}
*/
const token_node* next_node(void);
const token_node* next_node();
/**
* C 256
* @return {ACL_TOKEN*}
*/
ACL_TOKEN* get_tree(void) const
{
ACL_TOKEN* get_tree() const {
return tree_;
}

View File

@ -13,8 +13,7 @@ struct URL_NV
char* value;
};
class ACL_CPP_API url_coder : public dbuf_obj
{
class ACL_CPP_API url_coder : public dbuf_obj {
public:
/**
*
@ -30,7 +29,7 @@ public:
*/
url_coder(const url_coder& coder, dbuf_guard* dbuf = NULL);
~url_coder(void);
~url_coder();
/**
* params_ url
@ -43,7 +42,7 @@ public:
*
* @return {const string&}
*/
const string& to_string(void) const;
const string& to_string() const;
/**
* URL
@ -94,8 +93,7 @@ public:
*
* @return {std::vector<URL_NV*>&}
*/
const std::vector<URL_NV*>& get_params(void) const
{
const std::vector<URL_NV*>& get_params() const {
return params_;
}
@ -109,7 +107,7 @@ public:
/**
*
*/
void reset(void);
void reset();
private:
bool nocase_;

View File

@ -0,0 +1,3 @@
base_path = ../../..
PROG = json
include ../../Makefile.in

View File

@ -0,0 +1,86 @@
#include "stdafx.h"
#include <getopt.h>
static bool matched(const std::vector<std::string>& tokens, const char* name) {
for (std::vector<std::string>::const_iterator cit = tokens.begin();
cit != tokens.end(); ++cit) {
if (*cit == name) {
return true;
}
}
return false;
}
static void usage(const char* procname) {
printf("usage: %s -h [help[ -f json_file -n names -D [remove if matched]\r\n", procname);
}
int main(int argc, char* argv[]) {
acl::string file;
acl::string names;
bool use_remove = false;
int ch;
while ((ch = getopt(argc, argv, "hf:n:D")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 'f':
file = optarg;
break;
case 'n':
names = optarg;
break;
case 'D':
use_remove = true;
break;
default:
break;
}
}
if (file.empty() || names.empty()) {
usage(argv[0]);
return 1;
}
acl::string buf;
if (!acl::ifstream::load(file, &buf)) {
printf("load %s error %s\r\n", file.c_str(), acl::last_serror());
return 1;
}
std::vector<std::string> tokens;
acl::split(names.c_str(), ",; \t", tokens);
acl::json json(buf);
if (!json.finish()) {
printf("invalid json: %s\r\n", buf.c_str());
return 1;
}
acl::json_node* node = json.first_node();
while (node) {
const char* tag = node->tag_name();
if (tag == NULL || *tag == 0) {
node = json.next_node();
continue;
}
if (matched(tokens, tag)) {
if (use_remove) {
node = json.free_node(node);
} else {
node->disable(true);
node = json.next_node();
}
} else {
node = json.next_node();
}
}
printf("%s\r\n", json.to_string(NULL, true).c_str());
return 0;
}

View File

@ -0,0 +1,33 @@
{
"name1": "value1",
"name3": "value3",
"name4": "value4",
"name5": [ "value51", "value52", "value53" ],
"name6": "value6",
"name7": { "name71": "value71", "name72": "value72", "name73": "value73" },
"name8": "value8",
"name10": "value10",
"name11": {
"name111": {
"name1111": "value1111",
"name1112": "value1112",
"name1113": "value1113" },
"name112": "value112",
"name113": [
{"name1131": "value1131"},
{"name1132": "value1132"},
{"name1133": "value1133"}
],
"name114": "value114",
"name116": "value116",
"name117": { "name1171":{ "name11711": { "name117111": "value11711" }}},
"name118": "value118",
"name119": {
"name1191":{
"name11911": { "name119111": "value11911"},
"name11912": [{ "name119121": "value119121" }, { "name119122": "value119122" }]
}
},
"name1110": "value1110"
}
}

View File

@ -0,0 +1,13 @@
{
"name113": [
{"name1131": "value1131"},
{"name1132": "value1132"},
{"name1133": "value1133"}
],
"name117": { "name1171":{ "name11711": { "name117111": "value11711" }}},
"name118": [
{ "name1181": { "name11811": "value11811", "name11812": "value11812", "name11813": "value11813" }},
{ "name1182": "value1182" },
{ "name1183": "value1183" }
]
}

View File

@ -0,0 +1,27 @@
{ "menu name": {
"file": "file",
"value": "File",
"popup": {
"menuitem1": [
{"value": "New1", "onclick": "CreateNewDoc"},
{"value": "Open1", "onclick": "OpenDoc"},
{"value": "Close1", "onclick": "CloseDoc"}
],
"menuname": "hello world",
"inner": { "value" : "new2", "value" : "open2"},
"menuitem2": [
{"value": "New3", "onclick": "CreateNewDoc"},
{"value": "Open3", "onclick": "OpenDoc"},
{"value": "Close3", "onclick": "CloseDoc"},
{"value": "Help3", "onclick": "Help"}
]
}
},
"help": "hello world",
"menuitem2": [
{"value": "New4", "onclick": "CreateNewDoc"},
{"value": "Open4", "onclick": "OpenDoc"},
{"value": "Close4", "onclick": "CloseDoc"},
[{"value": "Save4", "onclick": "SaveDoc"}]
]
}

View File

@ -0,0 +1,5 @@
[
{ "name1": "value1" },
{ "name2": "value2" },
{ "name3": "value3" }
]

View File

@ -0,0 +1,3 @@
[
{}, {}, {}
]

View File

@ -0,0 +1,144 @@
#!/bin/sh
./json -D -f json.txt -n name1|jq
echo "------------------------name1-------------------------------------------"
read ch
./json -D -f json.txt -n name3|jq
echo "------------------------name3-------------------------------------------"
read ch
./json -D -f json.txt -n name4|jq
echo "------------------------name4-------------------------------------------"
read ch
./json -D -f json.txt -n name5|jq
echo "------------------------name5-------------------------------------------"
read ch
./json -D -f json.txt -n name6|jq
echo "------------------------name6-------------------------------------------"
read ch
./json -D -f json.txt -n name7|jq
echo "------------------------name7-------------------------------------------"
read ch
./json -D -f json.txt -n name71|jq
echo "------------------------name71------------------------------------------"
read ch
./json -D -f json.txt -n name72|jq
echo "------------------------name72------------------------------------------"
read ch
./json -D -f json.txt -n name73|jq
echo "------------------------name73------------------------------------------"
read ch
./json -D -f json.txt -n name8|jq
echo "------------------------name8-------------------------------------------"
read ch
./json -D -f json.txt -n name10|jq
echo "------------------------name10------------------------------------------"
read ch
./json -D -f json.txt -n name11|jq
echo "------------------------name11------------------------------------------"
read ch
./json -D -f json.txt -n name111|jq
echo "------------------------name111-----------------------------------------"
read ch
./json -D -f json.txt -n name1111|jq
echo "------------------------name1111----------------------------------------"
read ch
./json -D -f json.txt -n name1112|jq
echo "------------------------name1112----------------------------------------"
read ch
./json -D -f json.txt -n name1113|jq
echo "------------------------name1113----------------------------------------"
read ch
./json -D -f json.txt -n name112|jq
echo "------------------------name112-----------------------------------------"
read ch
./json -D -f json.txt -n name113|jq
echo "------------------------name113-----------------------------------------"
read ch
./json -D -f json.txt -n name1131|jq
echo "------------------------name1131----------------------------------------"
read ch
./json -D -f json.txt -n name1132|jq
echo "------------------------name1132----------------------------------------"
read ch
./json -D -f json.txt -n name1133|jq
echo "------------------------name1133----------------------------------------"
read ch
./json -D -f json.txt -n name114|jq
echo "------------------------name114-----------------------------------------"
read ch
./json -D -f json.txt -n name116|jq
echo "------------------------name116-----------------------------------------"
read ch
./json -D -f json.txt -n name117|jq
echo "------------------------name117-----------------------------------------"
read ch
./json -D -f json.txt -n name1171|jq
echo "------------------------name1171----------------------------------------"
read ch
./json -D -f json.txt -n name11711|jq
echo "------------------------name11711---------------------------------------"
read ch
./json -D -f json.txt -n name118|jq
echo "------------------------name118-----------------------------------------"
read ch
./json -D -f json.txt -n name119|jq
echo "------------------------name119-----------------------------------------"
read ch
./json -D -f json.txt -n name1191|jq
echo "------------------------name1191----------------------------------------"
read ch
./json -D -f json.txt -n name11911|jq
echo "------------------------name11911---------------------------------------"
read ch
./json -D -f json.txt -n name119111|jq
echo "------------------------name119111--------------------------------------"
read ch
./json -D -f json.txt -n name11912|jq
echo "------------------------name11912---------------------------------------"
read ch
./json -D -f json.txt -n name119121|jq
echo "------------------------name119121--------------------------------------"
read ch
./json -D -f json.txt -n name119122|jq
echo "------------------------name119122--------------------------------------"
read ch
./json -D -f json.txt -n name1110|jq
echo "------------------------name1110----------------------------------------"
read ch
./json -D -f json.txt -n name1111|jq
echo "------------------------name1111----------------------------------------"

View File

@ -0,0 +1,8 @@
// stdafx.cpp : 只包括标准包含文件的源文件
// json.pch 将成为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: 在 STDAFX.H 中
//引用任何所需的附加头文件,而不是在此文件中引用

View File

@ -0,0 +1,41 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是常用但不常更改的项目特定的包含文件
//
#pragma once
//#include <iostream>
//#include <tchar.h>
// TODO: 在此处引用程序要求的附加头文件
#include "acl_cpp/lib_acl.hpp"
#include "lib_acl.h"
#ifdef WIN32
#include <io.h>
#define snprintf _snprintf
#endif
#ifdef DEBUG
// 以下宏定义用来帮助检查变参中的参数类型是否合法
#undef logger
#define logger printf
#undef logger_error
#define logger_error printf
#undef logger_warn
#define logger_warn printf
#undef logger_fatal
#define logger_fatal printf
#undef logger_panic
#define logger_panic printf
extern void __attribute__((format(printf,3,4))) \
dummy_debug(int, int, const char*, ...);
#undef logger_debug
#define logger_debug dummy_debug
#endif

View File

@ -0,0 +1,144 @@
#!/bin/sh
./json -f json.txt -n name1|jq
echo "------------------------name1-------------------------------------------"
read ch
./json -f json.txt -n name3|jq
echo "------------------------name3-------------------------------------------"
read ch
./json -f json.txt -n name4|jq
echo "------------------------name4-------------------------------------------"
read ch
./json -f json.txt -n name5|jq
echo "------------------------name5-------------------------------------------"
read ch
./json -f json.txt -n name6|jq
echo "------------------------name6-------------------------------------------"
read ch
./json -f json.txt -n name7|jq
echo "------------------------name7-------------------------------------------"
read ch
./json -f json.txt -n name71|jq
echo "------------------------name71------------------------------------------"
read ch
./json -f json.txt -n name72|jq
echo "------------------------name72------------------------------------------"
read ch
./json -f json.txt -n name73|jq
echo "------------------------name73------------------------------------------"
read ch
./json -f json.txt -n name8|jq
echo "------------------------name8-------------------------------------------"
read ch
./json -f json.txt -n name10|jq
echo "------------------------name10------------------------------------------"
read ch
./json -f json.txt -n name11|jq
echo "------------------------name11------------------------------------------"
read ch
./json -f json.txt -n name111|jq
echo "------------------------name111-----------------------------------------"
read ch
./json -f json.txt -n name1111|jq
echo "------------------------name1111----------------------------------------"
read ch
./json -f json.txt -n name1112|jq
echo "------------------------name1112----------------------------------------"
read ch
./json -f json.txt -n name1113|jq
echo "------------------------name1113----------------------------------------"
read ch
./json -f json.txt -n name112|jq
echo "------------------------name112-----------------------------------------"
read ch
./json -f json.txt -n name113|jq
echo "------------------------name113-----------------------------------------"
read ch
./json -f json.txt -n name1131|jq
echo "------------------------name1131----------------------------------------"
read ch
./json -f json.txt -n name1132|jq
echo "------------------------name1132----------------------------------------"
read ch
./json -f json.txt -n name1133|jq
echo "------------------------name1133----------------------------------------"
read ch
./json -f json.txt -n name114|jq
echo "------------------------name114-----------------------------------------"
read ch
./json -f json.txt -n name116|jq
echo "------------------------name116-----------------------------------------"
read ch
./json -f json.txt -n name117|jq
echo "------------------------name117-----------------------------------------"
read ch
./json -f json.txt -n name1171|jq
echo "------------------------name1171----------------------------------------"
read ch
./json -f json.txt -n name11711|jq
echo "------------------------name11711---------------------------------------"
read ch
./json -f json.txt -n name118|jq
echo "------------------------name118-----------------------------------------"
read ch
./json -f json.txt -n name119|jq
echo "------------------------name119-----------------------------------------"
read ch
./json -f json.txt -n name1191|jq
echo "------------------------name1191----------------------------------------"
read ch
./json -f json.txt -n name11911|jq
echo "------------------------name11911---------------------------------------"
read ch
./json -f json.txt -n name119111|jq
echo "------------------------name119111--------------------------------------"
read ch
./json -f json.txt -n name11912|jq
echo "------------------------name11912---------------------------------------"
read ch
./json -f json.txt -n name119121|jq
echo "------------------------name119121--------------------------------------"
read ch
./json -f json.txt -n name119122|jq
echo "------------------------name119122--------------------------------------"
read ch
./json -f json.txt -n name1110|jq
echo "------------------------name1110----------------------------------------"
read ch
./json -f json.txt -n name1111|jq
echo "------------------------name1111----------------------------------------"

View File

@ -0,0 +1,72 @@
#!/bin/sh
./json -f json1.txt -n name113|jq
echo "------------------------name113-----------------------------------------"
read ch
./json -f json1.txt -n name1131|jq
echo "------------------------name1131----------------------------------------"
read ch
./json -f json1.txt -n name1132|jq
echo "------------------------name1132----------------------------------------"
read ch
./json -f json1.txt -n name1133|jq
echo "------------------------name1133----------------------------------------"
read ch
./json -f json1.txt -n name117|jq
echo "------------------------name117-----------------------------------------"
read ch
./json -f json1.txt -n name1171|jq
echo "------------------------name1171----------------------------------------"
read ch
./json -f json1.txt -n name11711|jq
echo "------------------------name11711---------------------------------------"
read ch
./json -f json1.txt -n name117111|jq
echo "------------------------name117111--------------------------------------"
read ch
./json -f json1.txt -n "name1131,name117111"|jq
echo "------------------------name1131,name117111-----------------------------"
read ch
./json -f json1.txt -n "name1131,name1132"|jq
echo "------------------------name1131,name1132-------------------------------"
read ch
./json -f json1.txt -n "name118"|jq
echo "------------------------name118-----------------------------------------"
read ch
./json -f json1.txt -n "name1181"|jq
echo "------------------------name1181----------------------------------------"
read ch
./json -f json1.txt -n "name1182"|jq
echo "------------------------name1183----------------------------------------"
read ch
./json -f json1.txt -n "name1183"|jq
echo "------------------------name1183----------------------------------------"
read ch
./json -f json1.txt -n "name1181,name1182"|jq
echo "------------------------name1181,name1182-------------------------------"
read ch
./json -f json1.txt -n "name1181,name1183"|jq
echo "------------------------name1181,name1183-------------------------------"
read ch
./json -f json1.txt -n "name1183,name1182"|jq
echo "------------------------name1183,name1182-------------------------------"
read ch
./json -f json1.txt -n "name1181,name1182,name1183"|jq
echo "------------------------name1181,name1182,name1183----------------------"

View File

@ -0,0 +1,12 @@
#!/bin/sh
./json -f json3.txt -n name1|jq
echo "------------------------name1-----------------------------------------"
read ch
./json -f json3.txt -n name2|jq
echo "------------------------name2-----------------------------------------"
read ch
./json -f json3.txt -n name3|jq
echo "------------------------name3-----------------------------------------"

View File

@ -0,0 +1,3 @@
#!/bin/sh
valgrind --tool=memcheck --leak-check=yes -v ./json

View File

@ -5,8 +5,7 @@
#include "acl_cpp/stdlib/json.hpp"
#endif
namespace acl
{
namespace acl {
json_node::json_node(ACL_JSON_NODE* node, json* json_ptr)
: node_me_(node)
@ -21,12 +20,12 @@ json_node::json_node(ACL_JSON_NODE* node, json* json_ptr)
acl_assert(dbuf_);
}
json_node::~json_node(void)
json_node::~json_node()
{
delete buf_;
}
const char* json_node::tag_name(void) const
const char* json_node::tag_name() const
{
if (node_me_->ltag && ACL_VSTRING_LEN(node_me_->ltag) > 0) {
return acl_vstring_str(node_me_->ltag);
@ -35,7 +34,7 @@ const char* json_node::tag_name(void) const
}
}
const char* json_node::get_text(void) const
const char* json_node::get_text() const
{
if (node_me_->text) {
if (ACL_VSTRING_LEN(node_me_->text) > 0) {
@ -48,7 +47,7 @@ const char* json_node::get_text(void) const
}
}
json_node* json_node::get_obj(void) const
json_node* json_node::get_obj() const
{
if (obj_ != NULL) {
return obj_;
@ -62,12 +61,12 @@ json_node* json_node::get_obj(void) const
return obj_;
}
const char* json_node::get_string(void) const
const char* json_node::get_string() const
{
return get_text();
}
const acl_int64* json_node::get_int64(void) const
const acl_int64* json_node::get_int64() const
{
if (!is_number()) {
return NULL;
@ -80,7 +79,7 @@ const acl_int64* json_node::get_int64(void) const
return &node_val_.n;
}
const double* json_node::get_double(void) const
const double* json_node::get_double() const
{
if (!is_double()) {
return NULL;
@ -93,7 +92,7 @@ const double* json_node::get_double(void) const
return &node_val_.d;
}
const bool* json_node::get_bool(void) const
const bool* json_node::get_bool() const
{
if (!is_bool()) {
return NULL;
@ -107,18 +106,18 @@ const bool* json_node::get_bool(void) const
return &node_val_.b;
}
bool json_node::is_string(void) const
bool json_node::is_string() const
{
return (node_me_->type & ACL_JSON_T_A_STRING)
|| (node_me_->type & ACL_JSON_T_STRING);
}
bool json_node::is_number(void) const
bool json_node::is_number() const
{
return is_double();
}
bool json_node::is_double(void) const
bool json_node::is_double() const
{
return (node_me_->type & ACL_JSON_T_A_DOUBLE) ||
(node_me_->type & ACL_JSON_T_DOUBLE) ||
@ -126,19 +125,19 @@ bool json_node::is_double(void) const
(node_me_->type & ACL_JSON_T_NUMBER);
}
bool json_node::is_bool(void) const
bool json_node::is_bool() const
{
return (node_me_->type & ACL_JSON_T_A_BOOL)
|| (node_me_->type & ACL_JSON_T_BOOL);
}
bool json_node::is_null(void) const
bool json_node::is_null() const
{
return (node_me_->type & ACL_JSON_T_A_NULL)
|| (node_me_->type & ACL_JSON_T_NULL);
}
bool json_node::is_object(void) const
bool json_node::is_object() const
{
if (node_me_->type & ACL_JSON_T_OBJ) {
return true;
@ -156,7 +155,7 @@ bool json_node::is_object(void) const
*/
}
bool json_node::is_array(void) const
bool json_node::is_array() const
{
if (node_me_->type & ACL_JSON_T_ARRAY) {
return true;
@ -165,7 +164,7 @@ bool json_node::is_array(void) const
}
}
const char* json_node::get_type(void) const
const char* json_node::get_type() const
{
if (is_string()) {
return "string";
@ -347,12 +346,22 @@ json_node& json_node::add_array_null(bool return_child /* = false */)
return add_child(json_->create_array_null(), return_child);
}
int json_node::detach(void)
int json_node::detach()
{
return acl_json_node_delete(node_me_);
}
json_node& json_node::get_parent(void) const
void json_node::disable(bool yes)
{
acl_json_node_disable(node_me_, yes ? 1 : 0);
}
bool json_node::disabled() const
{
return acl_json_node_disabled(node_me_) != 0;
}
json_node& json_node::get_parent() const
{
if (parent_) {
return *parent_;
@ -367,7 +376,7 @@ json_node& json_node::get_parent(void) const
return *parent_;
}
json_node* json_node::first_child(void)
json_node* json_node::first_child()
{
if (iter_ == NULL) {
iter_ = (ACL_ITER*) dbuf_->dbuf_alloc(sizeof(ACL_ITER));
@ -382,9 +391,11 @@ json_node* json_node::first_child(void)
return child;
}
json_node* json_node::next_child(void)
json_node* json_node::next_child()
{
acl_assert(iter_);
if (iter_ == NULL) {
return NULL;
}
ACL_JSON_NODE* node = node_me_->iter_next(iter_, node_me_);
if (node == NULL) {
@ -395,6 +406,21 @@ json_node* json_node::next_child(void)
return child;
}
json_node* json_node::free_child(json_node* child)
{
if (iter_ == NULL || child == NULL) {
return NULL;
}
ACL_JSON_NODE* node = acl_json_node_erase(node_me_, iter_);
if (node == NULL) {
return NULL;
}
json_node* next = dbuf_->create<json_node>(node, json_);
return next;
}
json_node* json_node::operator[](const char* tag)
{
json_node* iter = first_child();
@ -409,24 +435,24 @@ json_node* json_node::operator[](const char* tag)
return NULL;
}
int json_node::depth(void) const
int json_node::depth() const
{
return node_me_->depth;
}
int json_node::children_count(void) const
int json_node::children_count() const
{
return acl_ring_size(&node_me_->children);
}
void json_node::clear(void)
void json_node::clear()
{
if (buf_) {
buf_->clear();
}
}
json& json_node::get_json(void) const
json& json_node::get_json() const
{
return *json_;
}
@ -498,7 +524,7 @@ const char* json::update(const char* data)
return acl_json_update(json_, data);
}
bool json::finish(void)
bool json::finish()
{
return acl_json_finish(json_) == 0 ? false : true;
}
@ -581,7 +607,7 @@ json_node* json::getFirstElementByTags(const char* tags) const
return node;
}
ACL_JSON* json::get_json(void) const
ACL_JSON* json::get_json() const
{
return json_;
}
@ -649,7 +675,7 @@ json_node& json::create_array_bool(bool value)
return *n;
}
json_node& json::create_array_null(void)
json_node& json::create_array_null()
{
ACL_JSON_NODE* node = acl_json_create_array_null(json_);
json_node* n = dbuf_->create<json_node>(node, this);
@ -667,7 +693,7 @@ json_node& json::create_node(bool as_array /* = false */)
return *n;
}
json_node& json::create_array(void)
json_node& json::create_array()
{
ACL_JSON_NODE* node = acl_json_create_array(json_);
json_node* n = dbuf_->create<json_node>(node, this);
@ -718,7 +744,7 @@ json_node& json::duplicate_node(const json_node& node)
return *n;
}
json_node& json::get_root(void)
json_node& json::get_root()
{
if (root_) {
root_->node_me_ = json_->root;
@ -728,7 +754,7 @@ json_node& json::get_root(void)
return *root_;
}
json_node* json::first_node(void)
json_node* json::first_node()
{
if (iter_ == NULL) {
iter_ = (ACL_ITER*) dbuf_->dbuf_alloc(sizeof(ACL_ITER));
@ -741,9 +767,12 @@ json_node* json::first_node(void)
return n;
}
json_node* json::next_node(void)
json_node* json::next_node()
{
acl_assert(iter_);
if (iter_ == NULL) {
return NULL;
}
ACL_JSON_NODE* node = json_->iter_next(iter_, json_);
if (node == NULL) {
return NULL;
@ -753,6 +782,20 @@ json_node* json::next_node(void)
return n;
}
json_node* json::free_node(acl::json_node* curr)
{
if (iter_ == NULL || curr == NULL) {
return NULL;
}
ACL_JSON_NODE* node = acl_json_erase(json_, iter_);
if (node == NULL) {
return NULL;
}
json_node* next = dbuf_->create<json_node>(node, this);
return next;
}
const string& json::to_string(string* out /* = NULL */,
bool add_space /* = false */) const
{
@ -788,7 +831,7 @@ void json::build_json(string& out, bool add_space /* = false */) const
(void) acl_json_build(json_, buf);
}
void json::reset(void)
void json::reset()
{
clear();
dbuf_->dbuf_reset();
@ -818,7 +861,7 @@ int json::pop_end(string* out acl_unused, size_t max /* = 0 */ acl_unused)
return 0;
}
void json::clear(void)
void json::clear()
{
if (buf_) {
buf_->clear();

View File

@ -1986,4 +1986,50 @@ std::ostream& operator << (std::ostream& o, const acl::string& s) {
return o;
}
void split(const char* str, const char* sep, std::vector<std::string>& out) {
const char* ptr = str, *start = str;
while (*ptr) {
if (strchr(sep, (int) (*ptr)) != NULL) {
if (start < ptr) {
size_t n = ptr - start;
std::string buf;
buf.assign(start, n);
out.push_back(buf);
}
start = ptr + 1;
}
ptr++;
}
if (*start) {
std::string buf = start;
out.push_back(start);
}
}
size_t split(const char* str, const char* sep, std::list<std::string>& out) {
size_t cnt = 0;
const char* ptr = str, *start = str;
while (*ptr) {
if (strchr(sep, (int) (*ptr)) != NULL) {
if (start < ptr) {
size_t n = ptr - start;
std::string buf;
buf.assign(start, n);
out.push_back(buf);
cnt++;
}
start = ptr + 1;
}
ptr++;
}
if (*start) {
out.push_back(start);
cnt++;
}
return cnt;
}
} // namespace acl

View File

@ -3,17 +3,16 @@
#include "acl_cpp/stdlib/token_tree.hpp"
#endif
namespace acl
{
namespace acl {
token_node::token_node(void)
token_node::token_node()
: me_(NULL)
, tree_(NULL)
, dirty_(false)
{
}
token_node::~token_node(void) {}
token_node::~token_node() {}
void token_node::set_node(ACL_TOKEN* token, token_tree* tree)
{
@ -22,7 +21,7 @@ void token_node::set_node(ACL_TOKEN* token, token_tree* tree)
dirty_ = true;
}
const char* token_node::get_key(void) const
const char* token_node::get_key() const
{
if (me_ == NULL) {
return "";
@ -34,20 +33,20 @@ const char* token_node::get_key(void) const
return key_.c_str();
}
void* token_node::get_ctx(void) const
void* token_node::get_ctx() const
{
return me_->ctx;
}
//////////////////////////////////////////////////////////////////////////////
token_tree::token_tree(void)
token_tree::token_tree()
: iter_(NULL)
{
tree_ = acl_token_new();
}
token_tree::~token_tree(void)
token_tree::~token_tree()
{
acl_token_tree_destroy(tree_);
if (iter_) {
@ -105,7 +104,7 @@ void token_tree::free_delimiters_tab(char* delimiters_tab)
acl_token_delim_tab_free(delimiters_tab);
}
const token_node* token_tree::first_node(void)
const token_node* token_tree::first_node()
{
if (iter_ == NULL) {
iter_ = (ACL_ITER *) acl_mymalloc(sizeof(ACL_ITER));
@ -119,7 +118,7 @@ const token_node* token_tree::first_node(void)
return &node_;
}
const token_node* token_tree::next_node(void)
const token_node* token_tree::next_node()
{
if (iter_ == NULL) {
logger_error("call first_node first!");

View File

@ -6,8 +6,7 @@
#include "acl_cpp/stdlib/url_coder.hpp"
#endif
namespace acl
{
namespace acl {
void url_coder::init_dbuf(dbuf_guard* dbuf)
{
@ -42,7 +41,7 @@ url_coder::url_coder(const url_coder& coder, dbuf_guard* dbuf /* = NULL */)
}
}
url_coder::~url_coder(void)
url_coder::~url_coder()
{
reset();
@ -63,7 +62,7 @@ const url_coder& url_coder::operator =(const url_coder& coder)
return *this;
}
void url_coder::reset(void)
void url_coder::reset()
{
params_.clear();
buf_->clear();
@ -96,7 +95,7 @@ void url_coder::encode(string& buf, bool clean /* = true */) const
}
}
const string& url_coder::to_string(void) const
const string& url_coder::to_string() const
{
encode(*buf_);
return *buf_;