// Get the keys and the value from the string bool GetKeyAndValue(char *str, char *key, UINT key_size, char *value, UINT value_size, char *split_str) { UINT mode = 0; UINT wp1 = 0, wp2 = 0; UINT i, len; char *key_tmp, *value_tmp; bool ret = false; if (split_str == NULL) { split_str = default_spliter; } len = StrLen(str); key_tmp = ZeroMalloc(len + 1); value_tmp = ZeroMalloc(len + 1); for (i = 0;i < len;i++) { char c = str[i]; switch (mode) { case 0: if (IsSplitChar(c, split_str) == false) { mode = 1; key_tmp[wp1] = c; wp1++; } break; case 1: if (IsSplitChar(c, split_str) == false) { key_tmp[wp1] = c; wp1++; } else { mode = 2; } break; case 2: if (IsSplitChar(c, split_str) == false) { mode = 3; value_tmp[wp2] = c; wp2++; } break; case 3: value_tmp[wp2] = c; wp2++; break; } } if (mode != 0) { ret = true; StrCpy(key, key_size, key_tmp); StrCpy(value, value_size, value_tmp); } Free(key_tmp); Free(value_tmp); return ret; }
int json_solve_str(void ** root, char *str) { JSON_NODE * root_node; JSON_NODE * father_node; JSON_NODE * curr_node; JSON_VALUE * curr_value; int value_type; char value_buffer[1024]; char * tempstr; int i; int offset=0; int state=0; int ret; // give the root node value root_node=get_json_node(NULL); if(root_node==NULL) return -ENOMEM; father_node=NULL; curr_node=root_node; curr_node->layer=0; root_node->elem_type=JSON_ELEM_INIT; curr_node->solve_state=SOLVE_INIT; while(str[offset]!='\0') { switch(curr_node->solve_state) { case SOLVE_INIT: while(str[offset]!=0) { if(!IsSplitChar(str[offset])) break; offset++; } if(str[offset]!='{') return -EINVAL; // get an object node,then switch to the SOLVE_OBJECT father_node=curr_node; curr_node=get_json_node(father_node); curr_node->elem_type=JSON_ELEM_MAP; curr_node->solve_state=SOLVE_MAP; offset++; break; case SOLVE_MAP: while(str[offset]!=0) { if(!IsSplitChar(str[offset])) break; offset++; } if(str[offset]!='\"'){ // if this map is empty,then finish this MAP if(str[offset]=='}') { offset++; curr_node->solve_state=SOLVE_UPGRADE; break; } // if we should to find another elem if(str[offset]==',') { offset++; break; } else return -EINVAL; } // we should build a name:value json node father_node=curr_node; curr_node=get_json_node(father_node); curr_node->elem_str=str+offset; offset++; curr_node->solve_state=SOLVE_NAME; break; case SOLVE_NAME: ret=get_json_strvalue(value_buffer,str+offset); if(ret<0) return ret; if(ret>=DIGEST_SIZE*2) return ret; offset+=ret; { int len=strlen(value_buffer); if(len<=DIGEST_SIZE*2) memcpy(curr_node->name,value_buffer,len+1); else memcpy(curr_node->name,value_buffer,DIGEST_SIZE*2); offset++; } while(str[offset]!=0) { if(!IsSplitChar(str[offset])) break; offset++; } if(str[offset]!=':') return -EINVAL; offset++; curr_node->solve_state=SOLVE_VALUE; break; case SOLVE_VALUE: while(str[offset]!=0) { if(!IsSplitChar(str[offset])) break; offset++; } if(str[offset]=='{') { // get an object node,then switch to the SOLVE_MAP curr_node->elem_type=JSON_ELEM_MAP; offset++; curr_node->solve_state=SOLVE_MAP; break; } if(str[offset]=='[') { // get an array node,then switch to the SOLVE_ARRAY curr_node->elem_type=JSON_ELEM_ARRAY; offset++; curr_node->solve_state=SOLVE_ARRAY; break; } if(str[offset]=='\"') // value is JSON_STRING { offset++; i=get_json_strvalue(value_buffer,str+offset); if(i>=0) { offset+=i+1; curr_node->elem_type=JSON_ELEM_STRING; } else return -EINVAL; } else { i=get_json_numvalue(value_buffer,str+offset); if(i>0) { offset+=i; curr_node->elem_type=JSON_ELEM_NUM; } else { i=get_json_boolvalue(value_buffer,str+offset); if(i>0) { offset+=i; curr_node->elem_type=JSON_ELEM_BOOL; } else { i=get_json_nullvalue(value_buffer,str+offset); if(i>0) { offset+=i; curr_node->elem_type=JSON_ELEM_NULL; } else return -EINVAL; } } } curr_node->value_str=dup_str(value_buffer,0); curr_node->solve_state=SOLVE_UPGRADE; break; case SOLVE_ARRAY: while(str[offset]!=0) { if(!IsSplitChar(str[offset])) break; offset++; } if(str[offset]==']') { offset++; curr_node->solve_state=SOLVE_UPGRADE; break; } // if we should to find another elem if(str[offset]==',') { offset++; break; } // we should build a name:value json node father_node=curr_node; curr_node=get_json_node(father_node); curr_node->elem_str=str+offset; // offset++; curr_node->solve_state=SOLVE_VALUE; break; case SOLVE_UPGRADE: // get value process curr_node->solve_state=SOLVE_FINISH; if(father_node->elem_type==JSON_ELEM_INIT) break; curr_node=father_node; father_node=curr_node->father; break; default: return -EINVAL; } if(curr_node->solve_state==SOLVE_FINISH) break; } *root=curr_node; return offset; }