📄 record.c
字号:
record_is_default_changed (const Record *record, guint32 ftid){ FidsMap * map = NULL; g_return_val_if_fail(record, FALSE); g_return_val_if_fail(ftid != FIELD_TEMPLATE_ID_INVALID, FALSE); map = (FidsMap *)g_hash_table_lookup(record->fids_map, &ftid); if (map == NULL) { return FALSE; } return (map->def_changed);}static voidrecord_collect_fids_extend (gpointer key, gpointer value, gpointer user_data){ FidsMap * map = (FidsMap *)value; Iterator * iter = (Iterator *)user_data; gint i; for (i = 1; i < fids_map_size(map); i++) { guint32 tmp = g_array_index(map->fids, guint32, i); guint32 *fid = NULL; fid = g_new0(guint32, 1); *fid = tmp; iterator_insert(iter, (gpointer)fid); } return;}static voidrecord_collect_fids_default (gpointer key, gpointer value, gpointer user_data){ FidsMap * map = (FidsMap *)value; Iterator * iter = (Iterator *)user_data; guint32 *fid = NULL; gint i; fid = g_new0(guint32, 1); *fid = fids_map_get_default_fid(map); iterator_insert(iter, (gpointer)fid); return;}/** * @brief Retrieve the identifiers of field for the * specific field template in #Record object * * @param record the #Record object * @param ftid the identifier of specific field template or * FIELD_TEMPLATE_ID_INVALID to retrieve all * the identifiers of field in #Record object * @param flag only the default field identifiers will be retrieved * if set to FID_DEFAULT, or only the extend field * identifiers will be retrieved if set to FID_EXTEND, * or all the identifiers will be retrieved if * set to FID_ALL * * @return A #Iterator object contains all identifiers * user needs and user SHOULD free this #Iterator * object when it's no longer needed by iterator_free() call. */Iterator *record_get_fids (Record *record, guint32 ftid, guint32 flag){ Iterator * iter = NULL; g_return_val_if_fail(record, NULL); if (ftid == FIELD_TEMPLATE_ID_INVALID) { iter = iterator_new(); if (flag & FID_DEFAULT) {// g_print("%s(): collect default fids\n", __FUNCTION__); g_hash_table_foreach(record->fids_map, record_collect_fids_default, (gpointer)iter); } if (flag & FID_EXTEND) {// g_print("%s(): collect extend fids\n", __FUNCTION__); g_hash_table_foreach(record->fids_map, record_collect_fids_extend, (gpointer)iter); } } else { FidsMap * map = NULL; map = (FidsMap *)g_hash_table_lookup(record->fids_map, &ftid); if (map == NULL) { map = record_gen_base_fields(record, ftid); if (map == NULL) { return NULL; } } iter = iterator_new(); if (flag & FID_DEFAULT) { record_collect_fids_default(&ftid, map, iter); } if (flag & FID_EXTEND) { record_collect_fids_extend(&ftid, map, iter); } } return iter;}/** * @brief Returns field template identifiers * in #Record object. * * @param record the #Record object * * @return A #Iterator object contains all identifiers * user needs and user SHOULD free this #Iterator * object when it's no longer needed by iterator_free() call. */Iterator *record_get_ftids (const Record *record){ Iterator * iter = NULL; g_return_val_if_fail(record, NULL); iter = field_template_get_ftids(record->template); return iter;}/** * @brief Retrieve the default fid of specific * field template in #Record object. * * @param record the #Record object * @param ftid the identifier of specific field template * * @return default identifier of the specific field template */guint32record_get_default_fid (Record *record, guint32 ftid){ FidsMap * map = NULL; guint32 fid = FIELD_ID_INVALID; gint i; g_return_val_if_fail(record, FIELD_ID_INVALID); g_return_val_if_fail(ftid != FIELD_TEMPLATE_ID_INVALID, FIELD_ID_INVALID); map = (FidsMap *)g_hash_table_lookup(record->fids_map, &ftid); if (map == NULL) { map = record_gen_base_fields(record, ftid); } fid = fids_map_get_default_fid(map); return fid;}/** * @brief Set the default fid of specific * field template in #Record object. * * @param record the #Record object * @param ftid the identifier of specific field template * @param fid the identifier of the field to set * * @return identifier of original default field of * the field template */guint32record_set_default_fid (Record *record, guint32 ftid, guint32 fid){ FidsMap * map = NULL; guint32 oldfid; gint i; g_return_val_if_fail(record, FIELD_ID_INVALID); g_return_val_if_fail(ftid != FIELD_TEMPLATE_ID_INVALID, FIELD_ID_INVALID); map = (FidsMap *)g_hash_table_lookup(record->fids_map, &ftid); if (map == NULL) { map = record_gen_base_fields(record, ftid); } oldfid = fids_map_get_default_fid(map); if (fid < fids_map_get_min_fid(map) || fid > fids_map_get_max_fid(map)) { return oldfid; } for (i = 0; i < fids_map_size(map); i++) { guint32 tmp = fids_map_get_fid(map, i); if (tmp == fid) { fids_map_set_fid(map, i, oldfid); fids_map_set_fid(map, 0, tmp); map->def_changed = TRUE; break; } } record->extended = TRUE; return oldfid;}/** * @brief Retrieve the identifier of corresponding * field template for a specific field. * * @param record the #Record object * @param fid the identifier of the field * * @return identifier of corresponding field template */guint32 record_get_ftid (const Record *record, guint32 fid) { g_return_val_if_fail(record, FIELD_ID_INVALID); return FID_GET_FTID(fid);}/** * @brief Retrieve the template of #Record object * * A direct pointer to the memory block of template * is returned and users SHOULD NOT modify or free * this pointer. * * @param record the #Record object * * @return A pointer points to template of the * #Record object */const FieldTemplate * record_get_template (const Record *record){ g_return_val_if_fail(record, NULL); return record->template;}static voidfids_map_print_cb (gpointer key, gpointer value, gpointer user_data){ guint32 ftid = *(guint32 *)key; FidsMap * map = (FidsMap *)value; gint i; g_print("| |- FTID [%04d]/[%s] : ", ftid, (map->def_changed ? "Y" : "N")); for (i = 0; i < fids_map_size(map); i++) { g_print("%s%-6d", (i == 0 ? "*" : ""), g_array_index(map->fids, guint32, i)); } g_print("\n"); return;}static voidfields_print_cb (gpointer key, gpointer value, gpointer user_data){ guint32 fid = *(guint32 *)key; Field * field = (Field *)value; Record * record = (Record *)user_data; guint32 ftid = FIELD_TEMPLATE_ID_INVALID; const FieldDescriptor * fdesc = NULL; gconstpointer fval = NULL; const gchar * label = NULL; gint i; ftid = FID_GET_FTID(fid); fdesc = field_template_get(record->template, ftid); label = field_get_label(field); if (label == NULL) { const FieldLayout * layout = NULL; layout = field_descriptor_get_layout(fdesc, fid % FID_GENERATE_SEED); if (layout) { label = layout->label; } else { layout = field_descriptor_get_layout(fdesc, 0); if (layout) { label = layout->label; } } } g_print(" |- [%5d/%4d] %-25s : ", fid, ftid, (label ? label : "(null)")); fval = field_get_value(field); if (fval == NULL) { g_print("(null)"); } else { switch (fdesc->type) { case FIELD_TYPE_STRING: g_print("%s", (gchar *)fval); break; case FIELD_TYPE_BINARY: { gchar * value_str = NULL; value_str = utils_value_to_string(fval, field_get_size(field)); g_print("%s", value_str); g_free(value_str); break; } case FIELD_TYPE_INTEGER: g_print("%d", *(gint *)fval); break; case FIELD_TYPE_BOOLEAN: g_print("%s", (*(gboolean *)fval) ? "TRUE" : "FALSE"); break; case FIELD_TYPE_DATE: g_print("%d", *(gint *)fval); break; case FIELD_TYPE_FLOAT: g_print("%f", *(gfloat *)fval); break; } } g_print("[size = %d]\n", field_get_size(field)); return;}/** * @brief Print a #Record object. * * @param record the #Record object */voidrecord_print (const Record *record){ g_print("Record: [0x%08x]\n", (guint32)record); g_print("|- extended : %s\n", (record->extended == TRUE ? "TRUE" : "FASLE")); g_print("|- fid maps : [0x%08x]\n", (guint32)record->fids_map); if (g_hash_table_size(record->fids_map) != 0) { g_hash_table_foreach(record->fids_map, fids_map_print_cb, (gpointer)record); } g_print("| `->[END]\n"); g_print("`- fields : [0x%08x]\n", (guint32)record->fields); if (g_hash_table_size(record->fields) != 0) { g_hash_table_foreach(record->fields, fields_print_cb, (gpointer)record); } g_print(" `->[END]\n"); return;}gboolean record_is_default_field (Record *record, guint32 fid){ FidsMap * map = NULL; guint32 ftid = FIELD_TEMPLATE_ID_INVALID; g_return_val_if_fail(record, FALSE); g_return_val_if_fail(fid != FIELD_ID_INVALID, FALSE); ftid = FID_GET_FTID(fid); map = (FidsMap *)g_hash_table_lookup(record->fids_map, &ftid); if (map == NULL) { return FALSE; } return (fids_map_get_default_fid(map) == fid);}gboolean record_add_know_field (Record *record, guint32 fid){ FieldDescriptor * fdesc = NULL; FidsMap *map = NULL; guint32 ftid = FIELD_ID_INVALID; gint i; g_return_val_if_fail(record, FALSE); g_return_val_if_fail(fid != FIELD_ID_INVALID, FALSE); ftid = FID_GET_FTID(fid); fdesc = field_template_get(record->template, ftid); if (fdesc == NULL) { g_print("%s(): NULL Descriptor (%d).\n", __FUNCTION__, ftid); return FALSE; } else if(!(fdesc->attribute & FIELD_ATTR_MULTIPLE)) { g_print("%s(): CANNOT add field for template (%d). MULTI unset.\n", __FUNCTION__, ftid); return FALSE; } /* * retrieve existing fids map of specific * ftid. if no existing map, generate one with * default layout. */ map = (FidsMap *)g_hash_table_lookup(record->fids_map, &ftid);/* g_print("%s(): map = 0x%08x\n", __FUNCTION__, (guint32)map);*/ if (map == NULL) { map = record_gen_base_fields(record, ftid);/* guint32 * key = NULL; key = g_new0(guint32, 1); *key = ftid; map = fids_map_new(); g_hash_table_replace(record->fids_map, key, (gpointer)map);*/ } for (i = 0; i < fids_map_size(map); i++) {/* g_print("%s(): fid = %d, tmp = %d\n", __FUNCTION__, fid, fids_map_get_fid(map, i));*/ if (fid == fids_map_get_fid(map, i)) { break; } } if (i >= fids_map_size(map)) {/* g_print("%s(): append %d\n", __FUNCTION__, fid);*/ fids_map_append_fid(map, fid); } if (fid > fids_map_get_max_fid(map)) { fids_map_set_max_fid(map, fid); } /* g_print("%s(): fid = %d, max_fid = %d\n", __FUNCTION__, fid, fids_map_get_max_fid(map)); */ return TRUE;}/*vi:ts=2:nowrap:ai:expandtab */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -