📄 iterator.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 <iterator.h>#include "internal.h"voiditerator_element_free (void *element){ g_free(element); return;}Iterator *iterator_new (void){ Iterator * iter = NULL; iter = g_new0(Iterator, 1); if (!iter) { return NULL; } iter->array = g_ptr_array_new(); if (!iter->array) { g_free(iter); return NULL; } iter->index = -1; iter->element_free = iterator_element_free; return iter;}Iterator *iterator_new_full (IterFreeFunc func){ Iterator * iter = NULL; iter = iterator_new(); if (!iter) { return NULL; } iter->element_free = func; return iter;}gpointer *iterator_free (Iterator *iter, gboolean free_segment){ gpointer * segment = NULL; int i, count; g_return_if_fail(iter); if (free_segment) { count = iter->array->len; for (i = 0; i < count; i++) { gpointer * element; element = (gpointer)g_ptr_array_remove_index_fast(iter->array, 0); iter->element_free(element); } g_ptr_array_free(iter->array, TRUE); } else { iterator_insert(iter, NULL); segment = g_ptr_array_free(iter->array, FALSE); } g_free(iter); return segment;}gbooleaniterator_size (const Iterator *iter){ g_return_if_fail(iter); return iter->array->len;}gbooleaniterator_at_first (const Iterator * iter){ g_return_if_fail(iter); if (iter->index != 0) { return (gint)FALSE; } return (gint)TRUE;}gbooleaniterator_at_last (const Iterator *iter){ g_return_if_fail(iter); if (iter->array->len == 0) { return (gint)TRUE; } if (iter->index != iter->array->len) { return (gint)FALSE; } return (gint)TRUE;}void iterator_to_first (Iterator *iter){ g_return_if_fail(iter);// g_assert(iter->array->len != 0); if (iter->array->len == 0) { return; } iter->index = 0; return;}voiditerator_to_last (Iterator *iter){ g_return_if_fail(iter);// g_assert(iter->array->len != 0); if (iter->array->len == 0) { return; } iter->index = iter->array->len - 1; return;}gconstpointeriterator_current (const Iterator *iter){ g_return_if_fail(iter);// g_assert(iter->array->len != 0); if (iter->index < 0 || iter->index == iter->array->len) { return NULL; } return (gpointer)g_ptr_array_index(iter->array, iter->index);}voiditerator_next (Iterator *iter){ g_return_if_fail(iter);// g_assert(iter->array->len != 0); if (iter->array->len == 0) { return; } if (iter->index < iter->array->len) { iter->index++; } return;}/** * Private functions */voiditerator_insert (Iterator *iter, gconstpointer element){ g_return_if_fail(iter); if (iter->index == -1) { iter->index = 0; } g_ptr_array_add(iter->array, (gpointer)element); return;}gpointer iterator_remove_curr (Iterator *iter){ gpointer data; g_return_val_if_fail(iter, NULL); if (iter->index < 0 || iter->index == iter->array->len) { return NULL; } data = g_ptr_array_remove_index (iter->array, iter->index); return data;}/*vi:ts=2:nowrap:ai:expandtab*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -