hdata_t hdata_clone(const hdata_class_t *data_class,hdata_t data,InvokeTickDeclare){ if(data_class && data){ hdata_type_code type = hdata_type(data_class,data); hdata_t result = NULL; hint32 c,i; hdata_clone_param_t param; if(type == HDATA_TYPE_NULL){ result = hdata_null_alloc(InvokeTickArg); } else if(type == HDATA_TYPE_INT16){ result = hdata_int16_alloc( hdata_int16(data_class,data,0),InvokeTickArg); } else if(type == HDATA_TYPE_INT32){ result = hdata_int32_alloc( hdata_int32(data_class,data,0),InvokeTickArg); } else if(type == HDATA_TYPE_INT64){ result = hdata_int64_alloc( hdata_int64(data_class,data,0),InvokeTickArg); } else if(type == HDATA_TYPE_BOOL){ result = hdata_boolean_alloc( hdata_boolean(data_class,data,0),InvokeTickArg); } else if(type == HDATA_TYPE_DOUBLE){ result = hdata_double_alloc( hdata_double(data_class,data,0),InvokeTickArg); } else if(type == HDATA_TYPE_STRING){ result = hdata_string_alloc( hdata_string(data_class,data,NULL),InvokeTickArg); } else if(type == HDATA_TYPE_ARRAY){ c = hdata_array_size(data_class, data); result = hdata_array_alloc( c>0?c:10, 20,InvokeTickArg); for(i=0;i<c;i++){ hdata_array_add(result,hdata_clone( data_class,hdata_array(data_class,data,i),InvokeTickArg),InvokeTickArg); } } else if(type == HDATA_TYPE_OBJECT){ result = hdata_object_alloc(InvokeTickArg); param.result = result; param.data_class = data_class; hdata_object_each(data_class, data, hdata_clone_object_each,¶m); } return result; } return NULL; }
char * eval_hdata_get_value (struct t_hdata *hdata, void *pointer, const char *path) { char *value, *old_value, *var_name, str_value[128], *pos; const char *ptr_value, *hdata_name; int type; struct t_hashtable *hashtable; value = NULL; var_name = NULL; /* NULL pointer? return empty string */ if (!pointer) return strdup (""); /* no path? just return current pointer as string */ if (!path || !path[0]) { snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)pointer); return strdup (str_value); } /* * look for name of hdata, for example in "window.buffer.full_name", the * hdata name is "window" */ pos = strchr (path, '.'); if (pos > path) var_name = string_strndup (path, pos - path); else var_name = strdup (path); if (!var_name) goto end; /* search type of variable in hdata */ type = hdata_get_var_type (hdata, var_name); if (type < 0) goto end; /* build a string with the value or variable */ switch (type) { case WEECHAT_HDATA_CHAR: snprintf (str_value, sizeof (str_value), "%c", hdata_char (hdata, pointer, var_name)); value = strdup (str_value); break; case WEECHAT_HDATA_INTEGER: snprintf (str_value, sizeof (str_value), "%d", hdata_integer (hdata, pointer, var_name)); value = strdup (str_value); break; case WEECHAT_HDATA_LONG: snprintf (str_value, sizeof (str_value), "%ld", hdata_long (hdata, pointer, var_name)); value = strdup (str_value); break; case WEECHAT_HDATA_STRING: case WEECHAT_HDATA_SHARED_STRING: ptr_value = hdata_string (hdata, pointer, var_name); value = (ptr_value) ? strdup (ptr_value) : NULL; break; case WEECHAT_HDATA_POINTER: pointer = hdata_pointer (hdata, pointer, var_name); snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)pointer); value = strdup (str_value); break; case WEECHAT_HDATA_TIME: snprintf (str_value, sizeof (str_value), "%ld", (long)hdata_time (hdata, pointer, var_name)); value = strdup (str_value); break; case WEECHAT_HDATA_HASHTABLE: pointer = hdata_hashtable (hdata, pointer, var_name); if (pos) { /* * for a hashtable, if there is a "." after name of hdata, * get the value for this key in hashtable */ hashtable = pointer; ptr_value = hashtable_get (hashtable, pos + 1); if (ptr_value) { switch (hashtable->type_values) { case HASHTABLE_INTEGER: snprintf (str_value, sizeof (str_value), "%d", *((int *)ptr_value)); value = strdup (str_value); break; case HASHTABLE_STRING: value = strdup (ptr_value); break; case HASHTABLE_POINTER: case HASHTABLE_BUFFER: snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)ptr_value); value = strdup (str_value); break; case HASHTABLE_TIME: snprintf (str_value, sizeof (str_value), "%ld", (long)(*((time_t *)ptr_value))); value = strdup (str_value); break; case HASHTABLE_NUM_TYPES: break; } } } else { snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)pointer); value = strdup (str_value); } break; } /* * if we are on a pointer and that something else is in path (after "."), * go on with this pointer and remaining path */ if ((type == WEECHAT_HDATA_POINTER) && pos) { hdata_name = hdata_get_var_hdata (hdata, var_name); if (!hdata_name) goto end; hdata = hook_hdata_get (NULL, hdata_name); old_value = value; value = eval_hdata_get_value (hdata, pointer, (pos) ? pos + 1 : NULL); if (old_value) free (old_value); } end: if (var_name) free (var_name); return value; }
void _hdata_write_binary(const hdata_class_t *data_class,hdata_t data,hbuffer_t buffer,hbyte * value_bytes,InvokeTickDeclare){ hbuffer_t object_buffer; hint32 length ,i; hdata_type_code type; _hdata_write_binary_object_each_arg_t arg; if(data_class && DATA_STRUCT_ASSET(data) && buffer && value_bytes){ type = hdata_type(data_class,data,InvokeTickArg); memset(value_bytes,0,HTYPE_MAX_BYTES); buffer_append(buffer, value_bytes, type_byte_to_bytes(value_bytes,type)); if(type == HDATA_TYPE_INT16){ memset(value_bytes,0,HTYPE_MAX_BYTES); buffer_append(buffer, value_bytes, type_int16_to_bytes(value_bytes,hdata_int16(data_class,data,0,InvokeTickArg))); } else if(type == HDATA_TYPE_INT32){ memset(value_bytes,0,HTYPE_MAX_BYTES); buffer_append(buffer, value_bytes, type_int32_to_bytes(value_bytes,hdata_int32(data_class,data,0,InvokeTickArg))); } else if(type == HDATA_TYPE_INT64){ memset(value_bytes,0,HTYPE_MAX_BYTES); buffer_append(buffer, value_bytes, type_int64_to_bytes(value_bytes,hdata_int64(data_class,data,0,InvokeTickArg))); } else if(type == HDATA_TYPE_DOUBLE){ memset(value_bytes,0,HTYPE_MAX_BYTES); buffer_append(buffer, value_bytes, type_double_to_bytes(value_bytes,hdata_double(data_class,data,0.0,InvokeTickArg))); } else if(type == HDATA_TYPE_BOOL){ memset(value_bytes,0,HTYPE_MAX_BYTES); buffer_append(buffer, value_bytes, type_bool_to_bytes(value_bytes,hdata_boolean(data_class, data, 0,InvokeTickArg))); } else if(type == HDATA_TYPE_STRING){ memset(value_bytes,0,HTYPE_MAX_BYTES); length = (hint32)str_len(hdata_string(data_class,data,"",InvokeTickArg)) +1; buffer_append(buffer, value_bytes, type_int32_to_bytes(value_bytes,length)); buffer_append(buffer,(hany) hdata_string(data_class,data,"",InvokeTickArg),length); } else if(type == HDATA_TYPE_ARRAY){ length = hdata_array_size(data_class,data,InvokeTickArg); object_buffer = buffer_alloc(2048, 1024); for(i=0;i<length;i++){ _hdata_write_binary(data_class,hdata_array(data_class,data,i,InvokeTickArg),object_buffer,value_bytes,InvokeTickArg); } memset(value_bytes,0,HTYPE_MAX_BYTES); buffer_append(buffer, value_bytes, type_int32_to_bytes(value_bytes,buffer_length(object_buffer))); buffer_append(buffer, value_bytes, type_int32_to_bytes(value_bytes,length)); buffer_append(buffer, buffer_data(object_buffer),buffer_length(object_buffer)); buffer_dealloc(object_buffer); } else if(type == HDATA_TYPE_OBJECT){ object_buffer = buffer_alloc(2048,1024); arg.buffer = object_buffer; arg.value_bytes = value_bytes; arg.data_class = data_class; hdata_object_each(data_class, data, _hdata_write_binary_object_each, &arg,InvokeTickArg); length = buffer_length(object_buffer); memset(value_bytes,0,HTYPE_MAX_BYTES); buffer_append(buffer, value_bytes, type_int32_to_bytes(value_bytes,length)); buffer_append(buffer, buffer_data(object_buffer),length); buffer_dealloc(object_buffer); } else if(type == HDATA_TYPE_BYTES){ memset(value_bytes,0,HTYPE_MAX_BYTES); length = hdata_bytes_size(data_class,data,InvokeTickArg); buffer_append(buffer, value_bytes, type_int32_to_bytes(value_bytes,length)); buffer_append(buffer,(hany) hdata_bytes(data_class,data,InvokeTickArg),length); } } }
void _hdata_write(const hdata_class_t * data_class,hdata_t data,hbuffer_t buffer,hbool pattern,hint32 level,InvokeTickDeclare){ hint32 c,i; _hdata_write_object_each_arg_t arg; hdata_type_code type; if(data && data_class && buffer){ type = hdata_type(data_class,data,InvokeTickArg); if(type == HDATA_TYPE_NULL){ buffer_append_str(buffer, "null"); } else if(type == HDATA_TYPE_INT16 ){ buffer_append_format(buffer, "%d",InvokeTickArg, hdata_int16(data_class,data,0,InvokeTickArg)); } else if(type == HDATA_TYPE_INT32){ buffer_append_format(buffer, "%ld",InvokeTickArg,hdata_int32(data_class,data,0,InvokeTickArg)); } else if(type == HDATA_TYPE_INT64){ buffer_append_format(buffer, "%lld",InvokeTickArg,hdata_int64(data_class,data,0,InvokeTickArg)); } else if(type == HDATA_TYPE_DOUBLE){ buffer_append_format(buffer, "%f",InvokeTickArg,hdata_double(data_class,data,0,InvokeTickArg)); } else if(type == HDATA_TYPE_BOOL){ if(hdata_boolean(data_class,data,hbool_false,InvokeTickArg)){ buffer_append_str(buffer, "true"); } else{ buffer_append_str(buffer, "false"); } } else if(type == HDATA_TYPE_STRING){ if(hdata_string(data_class,data,NULL,InvokeTickArg)){ hdata_json_write_string(hdata_string(data_class,data,"",InvokeTickArg),buffer,InvokeTickArg); } else{ buffer_append_str(buffer, "null"); } } else if(type ==HDATA_TYPE_ARRAY){ buffer_append_str(buffer, "["); c = hdata_array_size(data_class,data,InvokeTickArg); for(i=0;i<c;i++){ if(i==0 && pattern){ buffer_append_str(buffer, "\n"); _hdata_json_write_level(buffer,level+1,InvokeTickArg); } else if(pattern){ _hdata_json_write_level(buffer,level+1,InvokeTickArg); } _hdata_write(data_class,hdata_array(data_class,data,i,InvokeTickArg),buffer,pattern,level+1,InvokeTickArg); if(i != c-1){ buffer_append_str(buffer, ","); } if(pattern){ buffer_append_str(buffer, "\n"); } } if(pattern && c>0){ _hdata_json_write_level(buffer,level,InvokeTickArg); buffer_append_str(buffer, "]"); } else{ buffer_append_str(buffer, "]"); } } else if(type ==HDATA_TYPE_OBJECT){ buffer_append_str(buffer, "{"); arg.buffer = buffer; arg.pattern = pattern; arg.level = level+1; arg.first = hbool_true; arg.data_class = data_class; hdata_object_each(data_class,data, _hdata_json_write_object_echo,&arg,InvokeTickArg); if(pattern && *((hchar *)buffer_data(buffer) + buffer_length(buffer) -1) != '{'){ _hdata_json_write_level(buffer,level,InvokeTickArg); buffer_append_str(buffer, "}"); } else{ buffer_append_str(buffer, "}"); } } else if(type == HDATA_TYPE_BYTES){ buffer_append_str(buffer, "\""); buffer_append_str(buffer, HDATA_BASE64_TAG); hbase64_encode(hdata_bytes(data_class, data,InvokeTickArg),hdata_bytes_size(data_class, data,InvokeTickArg),buffer); buffer_append_str(buffer, "\""); } } }