📄 template.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 <stdlib.h>#include <string.h>#include <glib.h>#include <template.h>#include "internal.h"/** * @brief Create a #FieldTemplate object. * * @return newly-created #FieldTemplate object * or NULL if failed. */FieldTemplate *field_template_new (void){ FieldTemplate * templ = NULL; templ = g_new0(FieldTemplate, 1); if (templ == NULL) { return NULL; } templ->fdescs = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, (GDestroyNotify)field_descriptor_free); return templ;}#define MAX_LINE_TOKEN 7/** * @brief Create a #FieldTemplate object * from plain-text file. * * @param templ the text file name of template * * @return newly-created #FieldTemplate object * or NULL if failed. */FieldTemplate * field_template_new_from_file (const gchar *fname){ FieldTemplate * templ = NULL; templ = field_template_new(); field_template_append_from_file(templ, fname); return templ;}gbooleanfield_template_append_from_file (FieldTemplate *templ, const gchar *fname){ GIOChannel * io = NULL; GIOStatus status = G_IO_STATUS_NORMAL; g_return_val_if_fail(templ, FALSE); g_return_val_if_fail(fname, FALSE); io = g_io_channel_new_file(fname, "r", NULL); if (io == NULL) { g_print("%s(): file NOT exists.[%s]\n", __FUNCTION__, fname); return FALSE; } do { gchar * buffer = NULL; gchar ** strs = NULL; GError * error = NULL; gint i; status = g_io_channel_read_line(io, &buffer, NULL, NULL, &error); if (error) { g_print("%s(): read fiel error: %s\n", __FUNCTION__, error->message); g_error_free(error); continue; } if (buffer == NULL || strlen(buffer) == 0) { g_free(buffer); continue; } if (buffer[0] == '#') { g_free(buffer); continue; } strs = g_strsplit(buffer, ",", MAX_LINE_TOKEN); for (i = 0; strs[i] != NULL; i++) { g_strstrip(strs[i]);// g_print("[%s]\t", strs[i]); }// g_print("i = %d\n", i); if (i >= MAX_LINE_TOKEN) { gchar *value = NULL; gint id; gchar *label = NULL; gchar *desc = NULL; gint type; gint attribute; gint size; gboolean def; def = (g_strcasecmp(strs[0], "D") == 0) ? TRUE : FALSE; id = atoi(strs[1]); label = strs[2]; desc = strs[3]; value = strs[4]; if (g_strcasecmp(value, "BOOLEAN") == 0) { type = FIELD_TYPE_BOOLEAN; } else if (g_strcasecmp(value, "INTEGER") == 0) { type = FIELD_TYPE_INTEGER; } else if (g_strcasecmp(value, "FLOAT") == 0) { type = FIELD_TYPE_FLOAT; } else if (g_strcasecmp(value, "STRING") == 0) { type = FIELD_TYPE_STRING; } else if (g_strcasecmp(value, "DATE") == 0) { type = FIELD_TYPE_DATE; } else { type = FIELD_TYPE_BINARY; } value = strs[5]; if (g_strcasecmp(value, "REQUIRED") == 0) { attribute = FIELD_ATTR_REQUIRED; } else if (g_strcasecmp(value, "PRIMARY") == 0) { attribute = FIELD_ATTR_PRIMARY; } else if (g_strcasecmp(value, "STANDARD") == 0) { attribute = FIELD_ATTR_STANDARD; } else if (g_strcasecmp(value, "MULTI") == 0) { attribute = FIELD_ATTR_MULTIPLE; } else { attribute = FIELD_ATTR_NONE; }/* g_print("%s(): ATTR = %s ,attr = 0x%08x\n", __FUNCTION__, value, attribute);*/ size = atoi(strs[6]); if (def) { FieldDescriptor * fdesc = NULL; FieldLayout * layout = NULL; fdesc = field_descriptor_new(id, type, attribute, size); layout = field_layout_new(label, desc); field_descriptor_add_layout(fdesc, layout, TRUE); field_template_add(templ, fdesc); } else { FieldDescriptor * fdesc = NULL; FieldLayout * layout = NULL; fdesc = field_template_get(templ, id); if (fdesc == NULL) { g_print("%s(): no Descriptor defined before\ extension layout definition. FTID %d\n", __FUNCTION__, id); continue; } if (!(fdesc->attribute & FIELD_ATTR_MULTIPLE)) { g_print("%s(): CANNOT add layout on descriptor (%d). MULTI unset.\n", __FUNCTION__, fdesc->identifier); } else { layout = field_layout_new(label, desc); field_descriptor_add_layout(fdesc, layout, FALSE); } } } g_strfreev(strs); g_free(buffer); } while(status == G_IO_STATUS_NORMAL); g_io_channel_shutdown(io, TRUE, NULL); g_io_channel_unref(io); return TRUE;}/** * @brief Frees the memory allocated for the * #FieldTemplate object. * * @param templ the #FieldTemplate object */voidfield_template_free (FieldTemplate *templ){ g_return_if_fail(templ); g_hash_table_destroy(templ->fdescs); g_free(templ); return;}/** * @brief Add a descriptor to the #FieldTemplate object. * * @param templ the #FieldTemplate object * @param fdesc the #FieldDescriptor object to be added */void field_template_add (FieldTemplate *templ, FieldDescriptor *fdesc){ guint32 * ftid = NULL; g_return_if_fail(templ); ftid = g_new0(guint32, 1); *ftid = fdesc->identifier; g_hash_table_replace(templ->fdescs, (gpointer)ftid, (gpointer)fdesc); return;}/** * @brief Remove a descriptor from the * #FieldTemplate object. * * Removed #FieldDescriptor object is returned to user * and SHOULD be freed when no longer needed. * * @param templ the #FieldDescriptor object * @param ftid the template id of descriptor to be removed * * @return A pointer points to #FieldDescriptor object which is * removed from #FieldTemplate object. */FieldDescriptor *field_template_remove (FieldTemplate *templ, guint32 ftid){ FieldDescriptor * fdesc = NULL; g_return_if_fail(templ); fdesc = (FieldDescriptor *)g_hash_table_lookup(templ->fdescs, &ftid); g_hash_table_remove(templ->fdescs, &ftid); return fdesc;}/** * @brief Retrieve a #FieldDescriptor from the * #FieldTemplate object. * * A direct pointer to the memory block of #FieldDescriptor * object is returned and users SHOULD NOT modify * or free this pointer. * * @param templ the #FieldDescriptor object * @param ftid the template id of descriptor to be retrieve * * @return A pointer points to #FieldDescriptor object. */FieldDescriptor * field_template_get (const FieldTemplate *templ, guint32 ftid){ FieldDescriptor * desc = NULL; g_return_if_fail(templ); desc = (FieldDescriptor *)g_hash_table_lookup(templ->fdescs, &ftid); return desc;}static void _add_ftid (gpointer key, gpointer value, gpointer user_data){ Iterator * iter = NULL; guint32 * ftid = NULL; iter = (Iterator *)user_data; ftid = g_new(guint32, 1); *ftid = *(guint32 *)key; iterator_insert(iter, (gpointer)ftid); return;}/** * @brief Retrieve all template identifiers from the * #FieldTemplate object. * * @param templ the #FieldTemplate object * * @return A #Iterator object contains all template * identifiers in #FieldTemplate object and user * SHOULD free this #Iterator object when it's * no longer needed by iterator_free() call. */Iterator * field_template_get_ftids (const FieldTemplate *templ){ Iterator * iter = NULL; g_return_if_fail(templ); iter = iterator_new(); g_hash_table_foreach(templ->fdescs, _add_ftid, iter); return iter;}gboolean field_template_is_vaild_ftid (const FieldTemplate *templ, guint32 ftid){ g_return_if_fail(templ); if (g_hash_table_lookup(templ->fdescs, &ftid) == NULL) { return TRUE; } return FALSE;}/** * @brief Retrieve Number of field descriptors * in #FieldTemplate object. * * @param templ the #FieldTemplate object * * @return Number of field descriptors in #FieldTemplate * object */gint field_template_size (const FieldTemplate *templ){ g_return_if_fail(templ); return g_hash_table_size(templ->fdescs);}static void _print_field_desc (gpointer key, gpointer value, gpointer user_data){ FieldDescriptor * fdesc = (FieldDescriptor *)value; field_descriptor_print(fdesc); return;}/** * @brief Print a #FieldTemplate object. * * @param fdesc the #FieldTemplate object */voidfield_template_print (const FieldTemplate *templ){ g_return_if_fail(templ); g_print("Field Template [0x%08x]\n", (guint32)templ); g_print("|- Count : %d\n", g_hash_table_size(templ->fdescs)); g_print("`- FDecs : [0x%08x]\n", (guint32)(templ->fdescs)); g_print("--> Details <--\n", __FUNCTION__); g_hash_table_foreach(templ->fdescs, _print_field_desc, NULL); return;}/*vi:ts=2:nowrap:ai:expandtab*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -