⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 field-desc.c

📁 linux下的电话本的最底层
💻 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 <glib.h>#include <field-desc.h>#include <utils.h>/** * @brief		Create a #FieldLayout object, initialized with * 					the given information. * * @param		label		field layout label * @param		desc		field layout description  * * @return	newly-created #FieldLayout object or NULL if failed. */FieldLayout *field_layout_new (const gchar	*label,									const gchar	*desc){	FieldLayout * layout = NULL;		layout = g_new0(FieldLayout, 1);		g_return_val_if_fail(layout, NULL);	layout->label	= (label ? g_strdup(label) : NULL);	layout->desc	= (desc ? g_strdup(desc) : NULL);		return layout;}/** * @brief		Frees the memory allocated for the  * 					#FieldLayout object. * * @param		fdesc		the #FieldLayout object */voidfield_layout_free (FieldLayout * layout){	g_return_if_fail(layout);		g_free(layout->label);	g_free(layout->desc);	g_free(layout);		return;}/** * @brief		Duplicate a #FieldLayout object. *  * If str is NULL it returns NULL. The returned string  * SHOULD be freed when no longer needed. * * @param		src		the #FieldLayout object to  * 								be duplicated *  * @return	a newly-allocated copy of #FieldLayout object. */FieldLayout *	field_layout_copy (const FieldLayout	*src){	g_return_val_if_fail(src, NULL);		return field_layout_new(src->label,													src->desc);}/** * @brief		Print a #FieldLayout object. * * @param		fdesc		the #FieldLayout object */voidfield_layout_print (const FieldLayout	*layout){	g_return_if_fail(layout);		g_print("Layout : [0x%08x]\n", (guint32)layout);	g_print("|- Label : %s\n",					(layout->label ? layout->label : "(null)"));	g_print("`- Desc  : %s\n",					(layout->desc ? layout->desc : "(null)"));	return;}/** * @brief		Create a #FieldDescriptor object, initialized with * 					the given information. * * @param		id			field descriptor identifier * @param		label		field label * @param		desc		field description  * @param		value		field type * @param		value		field attribute * @param		size		field size limitation * * @return	newly-created #FieldDescriptor object or NULL if failed. */FieldDescriptor *field_descriptor_new (guint32				 id,											FeildType			 type,											guint32				 attr,											FieldSize			 msize){	FieldDescriptor * fdesc = NULL;	fdesc = g_new0(FieldDescriptor, 1);		g_return_val_if_fail(fdesc, NULL);	fdesc->identifier	= id;	fdesc->type				= type;	fdesc->attribute	= attr;	fdesc->msize			= msize;		fdesc->layouts = NULL;	return fdesc;}																							 /** * @brief		Frees the memory allocated for the  * 					#FieldDescriptor object. * * @param		fdesc		the #FieldDescriptor object */voidfield_descriptor_free (FieldDescriptor	*fdesc){	g_return_if_fail(fdesc);	if (fdesc->layouts)	{		utils_g_slist_free_custom(fdesc->layouts,															(UtilsFreeFunc)field_layout_free);	}	g_free(fdesc);	return;}/** * @brief		Duplicate a #FieldDescriptor object. *  * If str is NULL it returns NULL. The returned string  * SHOULD be freed when no longer needed. * * @param		src		the #FieldDescriptor object to  * 								be duplicated *  * @return	a newly-allocated copy of #FieldDescriptor object. */FieldDescriptor *field_descriptor_copy (const FieldDescriptor	*src){	FieldDescriptor * dest = NULL;	g_return_val_if_fail(src, NULL);	dest = field_descriptor_new(src->identifier,															src->type,															src->attribute,															src->msize);	if (src->layouts)	{		GSList * iter = NULL;				for (iter = src->layouts; iter; iter = g_slist_next(iter))		{			FieldLayout * layout = (FieldLayout *)iter->data;			 			dest->layouts = g_slist_append(dest->layouts,																		 field_layout_copy(layout));		}	}		return dest;}/** * @brief		Print a #FieldDescriptor object. * * @param		fdesc		the #FieldDescriptor object */voidfield_descriptor_print (const FieldDescriptor	*fdesc){	g_return_if_fail(fdesc);	g_print("Field Descriptor: [0x%08x]\n",					(guint32)fdesc);	g_print("|- Identifer   : %d\n",					fdesc->identifier);						switch (fdesc->type)	{		case FIELD_TYPE_BOOLEAN:			g_print("|- Type        : BOOLEAN\n");			break;		case FIELD_TYPE_INTEGER:			g_print("|- Type        : INTEGER\n");			break;		case FIELD_TYPE_FLOAT:			g_print("|- Type        : FLOAT\n");			break;		case FIELD_TYPE_STRING:			g_print("|- Type        : STRING\n");			break;		case FIELD_TYPE_DATE:			g_print("|- Type        : DATE\n");			break;		case FIELD_TYPE_BINARY:			g_print("|- Type        : BINARY\n");			break;	}	g_print("|- Attribute   : 0x%08x\n",					fdesc->attribute);	g_print("|- Size Limit  : %d\n",					fdesc->msize);		g_print("`- Layouts     : [0x%08x], %d\n",					(guint32)fdesc->layouts,					g_slist_length(fdesc->layouts));						if (fdesc->layouts)	{		GSList * iter = NULL;		gint i;		gint count;				count = g_slist_length(fdesc->layouts);		for (i = 0, iter = fdesc->layouts; iter; iter = g_slist_next(iter), i++)		{			FieldLayout * layout = (FieldLayout *)iter->data;			g_print("   %c- [%03d]    : [0x%08x], %s\n",							(i == count - 1) ? '`' : '|',							i,							(gpointer)iter,							(i == 0) ? "*Default*" : "");			g_print("   %c  |- Label : %s\n",							(i == count - 1) ? ' ' : '|',							(layout->label) ? layout->label : "(null)");			g_print("   %c  `- Desc  : %s\n",							(i == count - 1) ? ' ' : '|',							(layout->desc) ? layout->desc : "(null)");		}	}	return;}/** * @brief		Add a layout to the #FieldDescriptor object. *  * @param		fdesc		the #FieldDescriptor object * @param		layout	the #FieldLayout object to be added * @param		def			if TRUE the layout is added as a primary * 									layout of the descriptor */voidfield_descriptor_add_layout	(FieldDescriptor	*fdesc,														 FieldLayout			*layout,														 gboolean					 def){	g_return_if_fail(fdesc);	g_return_if_fail(layout);		if (def)	{		fdesc->layouts = g_slist_prepend(fdesc->layouts,																		 layout);	}	else	{		fdesc->layouts = g_slist_append(fdesc->layouts,																		layout);	}						return;}/** * @brief		Remove a layout from the #FieldDescriptor object. *  * Removed #FieldLayout object is returned to user and SHOULD * be freed when no longer needed.  *  * @param		fdesc		the #FieldDescriptor object * @param		index		the index of layout to be removed *  * @return	A pointer points to #FieldLayout object which is * 					removed from #FieldDescriptor object. */FieldLayout *			field_descriptor_remove_layout (FieldDescriptor	*fdesc,																guint32					 index){	FieldLayout * layout = NULL;		g_return_val_if_fail(fdesc && fdesc->layouts, NULL);		if (index < 0 ||			index >= g_slist_length(fdesc->layouts))	{		return NULL;	}	layout = (FieldLayout *)g_slist_nth_data(fdesc->layouts,																					 index);		fdesc->layouts = g_slist_remove(fdesc->layouts,																	layout);	return layout;}/** * @brief		Retrieve a #FieldLayout from the  * 					#FieldDescriptor object. *  * A direct pointer to the memory block of #FieldLayout  * object is returned and users SHOULD NOT modify  * or free this pointer. *  * @param		fdesc		the #FieldDescriptor object * @param		index		the index of layout to be retrieve *  * @return	A pointer points to #FieldLayout object. */const FieldLayout *			field_descriptor_get_layout	(const FieldDescriptor	*fdesc,														 guint32								 index){	FieldLayout * layout = NULL;		g_return_val_if_fail(fdesc && fdesc->layouts, NULL);		if (index < 0 ||			index >= g_slist_length(fdesc->layouts))	{		return NULL;	}		layout = (FieldLayout *)g_slist_nth_data(fdesc->layouts,																					 index);		return layout;}/** * @brief		Retrieve all #FieldLayout objects from the  * 					#FieldDescriptor object. *  * @param		fdesc		the #FieldDescriptor object *  * @return	A #Iterator object contains all #FieldLayout * 					objects in #FieldDescriptor object and user * 					SHOULD free this #Iterator object when it's * 					no longer needed by iterator_free() call. */Iterator *	field_descriptor_get_layouts (const FieldDescriptor	*fdesc){	Iterator * iterator = NULL;		g_return_val_if_fail(fdesc, NULL);		iterator = iterator_new_full((IterFreeFunc)field_layout_free);		if (fdesc->layouts)	{		GSList * iter = NULL;				for (iter = fdesc->layouts; iter; iter = g_slist_next(iter))		{			FieldLayout * layout = NULL;						layout = field_layout_copy((FieldLayout *)iter->data);			iterator_insert(iterator, layout);	 		}	}		return iterator;}/*vi:ts=2:nowrap:ai:expandtab */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -