📄 field.c
字号:
/* librecord2 - Record Object manipulation and storage library 2 * * Authors: YE Nan <nan.ye@orange-ftgroup.com> * * This software and associated documentation files (the "Software") * are copyright (C) 2005 LiPS Linux Phone Standards Forum [FranceTelecom] * All Rights Reserved. * * A copyright license is hereby granted for redistribution and use of * the Software in source and binary forms, with or without modification, * provided that the following conditions are met: * - Redistributions of source code must retain the above copyright notice, * this copyright license and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this copyright license and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - Neither the name of LiPS nor the names of its Members may be used * to endorse or promote products derived from the Software without * specific prior written permission. * * A patent license for any Necessary Claims owned by Members of LiPS Forum * to make, have made, use, import, offer to sell, lease and sell or otherwise * distribute any implementation compliant with the any specification adopted * by the LiPS Forumcan be obtained from the respective Members on reasonable * and non-discriminatory terms and conditions and under reciprocity, as * regulated in more detail in the Internal Policy of the LiPS Forum. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER, ITS MEMBERS AND CONTRIBUTORS * "AS IS", AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER, * ITS MEMBERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */#include <stdio.h>#include <string.h>#include <glib.h>#include <field.h>#include <field-desc.h>#include <utils.h>#include "internal.h"/** * @brief Create a #Field object. * * @return newly-created #Field object or NULL if failed. */Field * field_new (FieldDescriptor *fdesc){ Field * field = NULL; g_return_val_if_fail(fdesc, NULL); field = g_new0(Field, 1); field->fid = FIELD_ID_INVALID; field->label = NULL; field->desc = NULL; field->data = NULL; field->fdesc = fdesc; return field;}/** * @brief Create a #Field object, initialized with * the given information. * * @param fid field identifier * @param label field label * @param desc field description * @param value field value * @param size size of field value * * @return newly-created #Field object or NULL if failed. */Field * field_new_full (FieldDescriptor *fdesc, guint32 fid, const gchar *label, const gchar *desc, gconstpointer value, FieldSize size){ Field * field = NULL; g_return_val_if_fail(fdesc, NULL); field = g_new0(Field, 1); field->fid = fid; field->label = (label ? g_strdup(label) : NULL); field->desc = (desc ? g_strdup(desc) : NULL); field->fdesc = fdesc; field_set_value(field, value, size); return field;}/** * @brief Frees the memory allocated for the #Field object. * * @param field the #Field object */void field_free (Field * field){ g_return_if_fail(field); g_free(field->desc); g_free(field->label); g_free(field->data); g_free(field); return;}/** * @brief Set value of a #Field object. * * #Field object makes its own copy of value. * * @param field the #Field object * @param value field value to set * @param size size of the value */void field_set_value (Field *field, gconstpointer value, FieldSize size){ FieldDescriptor * fdesc = NULL; g_return_if_fail(field); if (field->data) { g_free(field->data); field->data = NULL; } if (value == NULL) { return; } fdesc = field->fdesc; switch (fdesc->type) { case FIELD_TYPE_STRING: { gint dsize = 0; if (fdesc->msize < strlen(value)) { gchar * pch = (gchar *)value; while(pch) { gchar * pnext = g_utf8_next_char(pch); if (pnext != NULL) { if ((dsize + (pnext - pch)) > fdesc->msize) { break; } dsize += (pnext - pch); } pch = pnext; } } else { dsize = strlen(value); } field->data = g_strndup((gchar *)value, dsize); break; } case FIELD_TYPE_INTEGER: case FIELD_TYPE_FLOAT: case FIELD_TYPE_BOOLEAN: case FIELD_TYPE_DATE: { gint dsize = fdesc->msize; field->data = g_malloc0(dsize); memcpy(field->data, value, dsize); break; } case FIELD_TYPE_BINARY: default: { gint dsize = 0; dsize = (size == FIELD_SIZE_AUTO ? fdesc->msize : size); field->data = g_malloc0(sizeof(FieldSize) + size); *((FieldSize *)field->data) = size; memcpy(((guint8 *)field->data) + sizeof(FieldSize), value, dsize); break; } } return;}/** * @brief Retrieve value of a #Field object. * * A direct pointer to the memory block of * #Field object value. Users SHOULD NOT modify * or free this pointer. * * @param field the #Field object. * * @return a pointer directly points to the * memory block of value of #Field object. */gconstpointer field_get_value (const Field *field){ FieldDescriptor * fdesc = NULL; gpointer value = NULL; g_return_val_if_fail(field, NULL); if (field->data == NULL) { return NULL; } fdesc = field->fdesc; switch (fdesc->type) { case FIELD_TYPE_BINARY: value = (gpointer)(((guint8 *)field->data) + sizeof(FieldSize)); break; case FIELD_TYPE_STRING: case FIELD_TYPE_INTEGER: case FIELD_TYPE_FLOAT: case FIELD_TYPE_BOOLEAN: case FIELD_TYPE_DATE: default: value = field->data; break; } return value;}/** * @brief Retrieve size of value of a #Field object. * * A direct pointer to the memory block of * #Field object value. Users SHOULD NOT modify * or free this pointer. * * @param field the #Field object * * @return the size of value of #Field object. */FieldSizefield_get_size (const Field *field){ FieldDescriptor * fdesc = NULL; FieldSize size = 0; g_return_val_if_fail(field, 0); fdesc = field->fdesc; if (field->data == NULL) { return 0; } switch (fdesc->type) { case FIELD_TYPE_BINARY: size = (*(FieldSize *)field->data); break; case FIELD_TYPE_STRING: size = strlen(field->data); break; case FIELD_TYPE_INTEGER: case FIELD_TYPE_FLOAT: case FIELD_TYPE_BOOLEAN: case FIELD_TYPE_DATE: default: size = fdesc->msize; break; } return size;}/** * @brief Set label of a #Field object. * * #Field object makes its own copy of label. * * @param field the #Field object * @param label the #Field label */void field_set_label (Field *field, const gchar *label){ g_return_if_fail(field); if (field->label) { g_free(field->label); } field->label = (label ? g_strdup(label) : NULL); return;}/** * @brief Set description of a #Field object. * * #Field object makes its own copy of description. * * @param field the #Field object * @param label the #Field description */voidfield_set_desc (Field *field, const gchar *desc){ g_return_if_fail(field); if (field->desc) { g_free(field->desc); } field->desc = (desc ? g_strdup(desc) : NULL); return;}/** * @brief Retrieve the label of #Field object. * * A direct pointer to the memory block of * #Field object label. Users SHOULD NOT modify * or free this pointer. * * @param field the #Field object * * @return the label of #Field object. */const gchar *field_get_label (const Field *field){ g_return_val_if_fail(field, NULL); return field->label;}/** * @brief Retrieve the description of #Field object. * * A direct pointer to the memory block of * #Field object description. Users SHOULD NOT * modify or free this pointer. * * @param field the #Field object * * @return the description of #Field object. */const gchar *field_get_desc (const Field *field){ g_return_val_if_fail(field, NULL); return field->desc;}/** * @brief Print a #Field object. * * @param field the #Field object */voidfield_print (const Field *field){ g_return_if_fail(field); g_print("Field: [0x%08x]\n", (guint32)field); g_print("|- ID : %03d\n", field->fid); g_print("|- Label : %s\n", (field->label ? field->label : "(null)")); g_print("|- Desc : %s\n", (field->desc ? field->desc : "(null)")); g_print("`- FDesc : [0x%08x]\n", (guint32)field->fdesc); g_print("`- Data : [0x%08x]\n", (guint32)field->data); if (field->data) { gconstpointer value = NULL; gchar * valstr = NULL; FieldSize size; value = field_get_value(field); size = field_get_size(field); valstr = utils_value_to_string(value, size); g_print(" |- value : [%s]\n", valstr); g_free(valstr); g_print(" `- size : %03d\n", size); } g_print("\n"); return;}/*vi:ts=2:nowrap:ai:expandtab */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -