tb_object_ref_t tb_object_seek(tb_object_ref_t object, tb_char_t const* path, tb_bool_t bmacro) { // check tb_assert_and_check_return_val(object, tb_null); // null? tb_check_return_val(path, object); // done tb_object_ref_t root = object; tb_char_t const* p = path; tb_char_t const* e = path + tb_strlen(path); while (p < e && object) { // done seek switch (*p) { case '.': { // check tb_assert_and_check_return_val(tb_object_type(object) == TB_OBJECT_TYPE_DICTIONARY, tb_null); // skip p++; // read the key name tb_char_t key[4096] = {0}; tb_char_t* kb = key; tb_char_t* ke = key + 4095; for (; p < e && kb < ke && *p && (*p != '.' && *p != '[' && *p != ']'); p++, kb++) { if (*p == '\\') p++; *kb = *p; } // trace tb_trace_d("key: %s", key); // the value object = tb_object_dictionary_value(object, key); } break; case '[': { // check tb_assert_and_check_return_val(tb_object_type(object) == TB_OBJECT_TYPE_ARRAY, tb_null); // skip p++; // read the item index tb_char_t index[32] = {0}; tb_char_t* ib = index; tb_char_t* ie = index + 31; for (; p < e && ib < ie && *p && tb_isdigit10(*p); p++, ib++) *ib = *p; // trace tb_trace_d("index: %s", index); // check tb_size_t i = tb_atoi(index); tb_assert_and_check_return_val(i < tb_object_array_size(object), tb_null); // the value object = tb_object_array_item(object, i); } break; case ']': default: p++; break; } // is macro? done it if be enabled if ( object && bmacro && tb_object_type(object) == TB_OBJECT_TYPE_STRING && tb_object_string_size(object) && tb_object_string_cstr(object)[0] == '$') { // the next path path = tb_object_string_cstr(object) + 1; // continue to seek it object = tb_object_seek(root, path, bmacro); } } // ok? return object; }
static tb_object_ref_t tb_object_bin_reader_func_dictionary(tb_object_bin_reader_t* reader, tb_size_t type, tb_uint64_t size) { // check tb_assert_and_check_return_val(reader && reader->stream && reader->list, tb_null); // empty? if (!size) return tb_object_dictionary_init(TB_OBJECT_DICTIONARY_SIZE_MICRO, tb_false); // init dictionary tb_object_ref_t dictionary = tb_object_dictionary_init(0, tb_false); tb_assert_and_check_return_val(dictionary, tb_null); // walk tb_size_t i = 0; tb_size_t n = (tb_size_t)size; for (i = 0; i < n; i++) { // read key tb_object_ref_t key = tb_null; do { // the type & size tb_size_t type = 0; tb_uint64_t size = 0; tb_object_reader_bin_type_size(reader->stream, &type, &size); // trace tb_trace_d("key: type: %lu, size: %llu", type, size); // is index? if (!type) { // the object index tb_size_t index = (tb_size_t)size; // check tb_assert_and_check_break(index < tb_vector_size(reader->list)); // the item key = (tb_object_ref_t)tb_iterator_item(reader->list, index); } else { // check tb_assert_and_check_break(type == TB_OBJECT_TYPE_STRING); // the reader func tb_object_bin_reader_func_t func = tb_object_bin_reader_func(type); tb_assert_and_check_break(func); // read it key = func(reader, type, size); tb_assert_and_check_break(key); // save it tb_vector_insert_tail(reader->list, key); // refn-- tb_object_dec(key); } } while (0); // check tb_assert_and_check_break(key && tb_object_type(key) == TB_OBJECT_TYPE_STRING); tb_assert_and_check_break(tb_object_string_size(key) && tb_object_string_cstr(key)); // read val tb_object_ref_t val = tb_null; do { // the type & size tb_size_t type = 0; tb_uint64_t size = 0; tb_object_reader_bin_type_size(reader->stream, &type, &size); // trace tb_trace_d("val: type: %lu, size: %llu", type, size); // is index? if (!type) { // the object index tb_size_t index = (tb_size_t)size; // check tb_assert_and_check_break(index < tb_vector_size(reader->list)); // the item val = (tb_object_ref_t)tb_iterator_item(reader->list, index); // refn++ if (val) tb_object_inc(val); } else { // the reader func tb_object_bin_reader_func_t func = tb_object_bin_reader_func(type); tb_assert_and_check_break(func); // read it val = func(reader, type, size); // save it if (val) tb_vector_insert_tail(reader->list, val); } } while (0); // check tb_assert_and_check_break(val); // set key => val tb_object_dictionary_set(dictionary, tb_object_string_cstr(key), val); } // failed? if (i != n) { if (dictionary) tb_object_exit(dictionary); dictionary = tb_null; } // ok? return dictionary; }