1 /*
2  * libInstPatch
3  * Copyright (C) 1999-2014 Element Green <element@elementsofsound.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; version 2.1
8  * of the License only.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA or on the web at http://www.gnu.org.
19  */
20 /**
21  * SECTION: IpatchIter
22  * @short_description: Iterator instance
23  * @see_also:
24  * @stability: Stable
25  *
26  * A boxed type (structure) used for abstracting manipulation of object lists.
27  */
28 #include <glib.h>
29 #include "IpatchIter.h"
30 
31 /* IpatchIter methods for GSList type lists */
32 IpatchIterMethods ipatch_iter_GSList_methods =
33 {
34     ipatch_iter_GSList_get,
35     ipatch_iter_GSList_next,
36     ipatch_iter_GSList_first,
37     ipatch_iter_GSList_last,
38     ipatch_iter_GSList_index,
39     ipatch_iter_GSList_insert,
40     ipatch_iter_GSList_remove,
41     ipatch_iter_GSList_count
42 };
43 
44 /* IpatchIter methods for GList type lists */
45 IpatchIterMethods ipatch_iter_GList_methods =
46 {
47     ipatch_iter_GList_get,
48     ipatch_iter_GList_next,
49     ipatch_iter_GList_first,
50     ipatch_iter_GList_last,
51     ipatch_iter_GList_index,
52     ipatch_iter_GList_insert,
53     ipatch_iter_GList_remove,
54     ipatch_iter_GList_count
55 };
56 
57 /* IpatchIter methods for arrays */
58 IpatchIterMethods ipatch_iter_array_methods =
59 {
60     ipatch_iter_array_get,
61     ipatch_iter_array_next,
62     ipatch_iter_array_first,
63     ipatch_iter_array_last,
64     ipatch_iter_array_index,
65     ipatch_iter_array_insert,
66     ipatch_iter_array_remove,
67     ipatch_iter_array_count
68 };
69 
70 /**
71  * ipatch_iter_get_type:
72  *
73  * Gets the GBoxed derived type for #IpatchIter structures.
74  *
75  * Returns: GType of #IpatchIter structures.
76  */
77 GType
ipatch_iter_get_type(void)78 ipatch_iter_get_type(void)
79 {
80     static GType type = 0;
81 
82     if(!type)
83         type = g_boxed_type_register_static("IpatchIter",
84                                             (GBoxedCopyFunc)ipatch_iter_duplicate,
85                                             (GBoxedFreeFunc)ipatch_iter_free);
86 
87     return (type);
88 }
89 
90 /**
91  * ipatch_iter_alloc: (skip)
92  *
93  * Allocates an item iterator. This function is seldom used since
94  * #IpatchIter structures are usually allocated on the stack.
95  *
96  * Returns: Newly allocated item iterator. Should be freed with
97  *   ipatch_iter_free() when finished with it.
98  */
99 IpatchIter *
ipatch_iter_alloc(void)100 ipatch_iter_alloc(void)
101 {
102     IpatchIter *iter;
103     iter = g_new0(IpatchIter, 1);
104     return (iter);
105 }
106 
107 /**
108  * ipatch_iter_free: (skip)
109  * @iter: Item iterator
110  *
111  * Frees an item iterator that was allocated with ipatch_iter_alloc().
112  * Seldom used since #IpatchIter structures are usually allocated on the
113  * stack.
114  */
115 void
ipatch_iter_free(IpatchIter * iter)116 ipatch_iter_free(IpatchIter *iter)
117 {
118     g_free(iter);
119 }
120 
121 /**
122  * ipatch_iter_duplicate: (skip)
123  * @iter: Patch iterator to duplicate
124  *
125  * Duplicates a patch iterator. Seldom used since #IpatchIter
126  * structures are usually allocated on the stack and can be copied
127  * directly.
128  *
129  * Returns: Newly allocated patch iter identical to @iter. Free it with
130  * ipatch_iter_free() when finished.
131  */
132 IpatchIter *
ipatch_iter_duplicate(IpatchIter * iter)133 ipatch_iter_duplicate(IpatchIter *iter)
134 {
135     IpatchIter *newiter;
136 
137     newiter = ipatch_iter_alloc();
138     *newiter = *iter;
139     return (newiter);
140 }
141 
142 /**
143  * ipatch_iter_GSList_init: (skip)
144  * @iter: Iterator to initialize
145  * @list: Pointer to root GSList pointer to initialize iterator to
146  *
147  * Initialize an iterator to iterate over a GSList.
148  */
149 void
ipatch_iter_GSList_init(IpatchIter * iter,GSList ** list)150 ipatch_iter_GSList_init(IpatchIter *iter, GSList **list)
151 {
152     g_return_if_fail(iter != NULL);
153     g_return_if_fail(list != NULL);
154 
155     iter->methods = &ipatch_iter_GSList_methods;
156     IPATCH_ITER_GSLIST_SET_LIST(iter, list);
157     IPATCH_ITER_GSLIST_SET_POS(iter, NULL);
158 }
159 
160 /**
161  * ipatch_iter_GSList_get: (skip)
162  * @iter: Item iterator initialized with a GSList
163  *
164  * GSList item iterator method to get the current item.
165  *
166  * Returns: Current item or %NULL if no current item.
167  */
168 gpointer
ipatch_iter_GSList_get(IpatchIter * iter)169 ipatch_iter_GSList_get(IpatchIter *iter)
170 {
171     GSList *pos;
172 
173     g_return_val_if_fail(iter != NULL, NULL);
174 
175     pos = IPATCH_ITER_GSLIST_GET_POS(iter);
176     return (pos ? (gpointer)(pos->data) : NULL);
177 }
178 
179 /**
180  * ipatch_iter_GSList_next: (skip)
181  * @iter: Item iterator initialized with a GSList
182  *
183  * GSList item iterator method to get the next item and advance the
184  * iterator's position.
185  *
186  * Returns: Next item or %NULL if no more items.
187  */
188 gpointer
ipatch_iter_GSList_next(IpatchIter * iter)189 ipatch_iter_GSList_next(IpatchIter *iter)
190 {
191     GSList *pos;
192 
193     g_return_val_if_fail(iter != NULL, NULL);
194 
195     pos = IPATCH_ITER_GSLIST_GET_POS(iter);
196 
197     if(pos)
198     {
199         pos = g_slist_next(pos);
200     }
201 
202     IPATCH_ITER_GSLIST_SET_POS(iter, pos);  /* set current position */
203     return (pos ? (gpointer)(pos->data) : NULL);
204 }
205 
206 /**
207  * ipatch_iter_GSList_first: (skip)
208  * @iter: Item iterator initialized with a GSList
209  *
210  * GSList item iterator method to get the first item and set the
211  * iterator's position to it.
212  *
213  * Returns: First item or %NULL if GSList is empty.
214  */
215 gpointer
ipatch_iter_GSList_first(IpatchIter * iter)216 ipatch_iter_GSList_first(IpatchIter *iter)
217 {
218     GSList **list, *pos;
219 
220     g_return_val_if_fail(iter != NULL, NULL);
221 
222     list = IPATCH_ITER_GSLIST_GET_LIST(iter);  /* list pointer */
223     g_return_val_if_fail(list != NULL, NULL);
224 
225     pos = *list;
226     IPATCH_ITER_GSLIST_SET_POS(iter, pos);  /* set position */
227     return (pos ? (gpointer)(pos->data) : NULL);
228 }
229 
230 /**
231  * ipatch_iter_GSList_last: (skip)
232  * @iter: Item iterator initialized with a GSList
233  *
234  * GSList item iterator method to get the last item and set the
235  * iterator's position to it.
236  *
237  * Returns: Last item or %NULL if GSList is empty.
238  */
239 gpointer
ipatch_iter_GSList_last(IpatchIter * iter)240 ipatch_iter_GSList_last(IpatchIter *iter)
241 {
242     GSList **list, *pos;
243 
244     g_return_val_if_fail(iter != NULL, NULL);
245 
246     list = IPATCH_ITER_GSLIST_GET_LIST(iter);
247     g_return_val_if_fail(list != NULL, NULL);
248 
249     pos = g_slist_last(*list);
250     IPATCH_ITER_GSLIST_SET_POS(iter, pos);  /* set current position */
251     return (pos ? (gpointer)(pos->data) : NULL);
252 }
253 
254 /**
255  * ipatch_iter_GSList_index: (skip)
256  * @iter: Item iterator initialized with a GSList
257  * @index: Index, from 0, of item to get
258  *
259  * GSList item iterator method to get an item at a given index and set the
260  * iterator's position to it.
261  *
262  * Returns: item at the @index position or %NULL if index is off
263  * the end of the GSList.
264  */
265 gpointer
ipatch_iter_GSList_index(IpatchIter * iter,int index)266 ipatch_iter_GSList_index(IpatchIter *iter, int index)
267 {
268     GSList **list, *pos;
269 
270     g_return_val_if_fail(iter != NULL, NULL);
271 
272     list = IPATCH_ITER_GSLIST_GET_LIST(iter);
273     g_return_val_if_fail(list != NULL, NULL);
274 
275     pos = g_slist_nth(*list, index);
276     IPATCH_ITER_GSLIST_SET_POS(iter, pos);  /* set current position */
277     return (pos ? (gpointer)(pos->data) : NULL);
278 }
279 
280 /**
281  * ipatch_iter_GSList_insert: (skip)
282  * @iter: Item iterator initialized with a GSList
283  * @item: Pointer to insert
284  *
285  * GSList item iterator method to insert an item pointer.
286  */
287 void
ipatch_iter_GSList_insert(IpatchIter * iter,gpointer item)288 ipatch_iter_GSList_insert(IpatchIter *iter, gpointer item)
289 {
290     GSList **list, *pos;
291 
292     g_return_if_fail(iter != NULL);
293 
294     if((pos = IPATCH_ITER_GSLIST_GET_POS(iter)))   /* position set? */
295     {
296         pos = g_slist_insert(pos, item, 1);  /* insert after position */
297         IPATCH_ITER_GSLIST_SET_POS(iter, g_slist_next(pos));   /* update pos */
298     }
299     else				/* position not set */
300     {
301         list = IPATCH_ITER_GSLIST_GET_LIST(iter);
302         g_return_if_fail(list != NULL);
303 
304         pos = g_slist_prepend(*list, item);  /* prepend */
305         IPATCH_ITER_GSLIST_SET_POS(iter, pos);  /* set current position */
306         *list = pos;		/* set root of list */
307     }
308 }
309 
310 /**
311  * ipatch_iter_GSList_remove: (skip)
312  * @iter: Item iterator initialized with a GSList
313  *
314  * GSList item iterator method to remove the current item and advance
315  * the current position.
316  */
317 void
ipatch_iter_GSList_remove(IpatchIter * iter)318 ipatch_iter_GSList_remove(IpatchIter *iter)
319 {
320     GSList **list, *pos;
321 
322     g_return_if_fail(iter != NULL);
323 
324     list = IPATCH_ITER_GSLIST_GET_LIST(iter);
325     g_return_if_fail(list != NULL);
326 
327     /* advance current position if set */
328     pos = IPATCH_ITER_GSLIST_GET_POS(iter);
329 
330     if(pos)
331     {
332         IPATCH_ITER_GSLIST_SET_POS(iter, g_slist_next(pos));
333         *list = g_slist_delete_link(*list, pos);
334     }
335 }
336 
337 /**
338  * ipatch_iter_GSList_count: (skip)
339  * @iter: Item iterator initialized with a GSList
340  *
341  * GSList item iterator method to get the count of items.
342  *
343  * Returns: Count of items in GSList iterator.
344  */
345 int
ipatch_iter_GSList_count(IpatchIter * iter)346 ipatch_iter_GSList_count(IpatchIter *iter)
347 {
348     GSList **list;
349 
350     g_return_val_if_fail(iter != NULL, 0);
351 
352     list = IPATCH_ITER_GSLIST_GET_LIST(iter);
353     g_return_val_if_fail(list != NULL, 0);
354 
355     return (g_slist_length(*list));
356 }
357 
358 
359 /**
360  * ipatch_iter_GList_init: (skip)
361  * @iter: Iterator to initialize
362  * @list: Pointer to root GList pointer to initialize iterator to
363  *
364  * Initialize an iterator to iterate over a GList.
365  */
366 void
ipatch_iter_GList_init(IpatchIter * iter,GList ** list)367 ipatch_iter_GList_init(IpatchIter *iter, GList **list)
368 {
369     g_return_if_fail(iter != NULL);
370 
371     iter->methods = &ipatch_iter_GList_methods;
372     IPATCH_ITER_GLIST_SET_LIST(iter, list);
373     IPATCH_ITER_GLIST_SET_POS(iter, NULL);
374 }
375 
376 /**
377  * ipatch_iter_GList_get: (skip)
378  * @iter: Item iterator initialized with a GList
379  *
380  * GList item iterator method to get the current item.
381  *
382  * Returns: Current item or %NULL if no current item.
383  */
384 gpointer
ipatch_iter_GList_get(IpatchIter * iter)385 ipatch_iter_GList_get(IpatchIter *iter)
386 {
387     GList *pos;
388 
389     g_return_val_if_fail(iter != NULL, NULL);
390 
391     pos = IPATCH_ITER_GLIST_GET_POS(iter);
392     return (pos ? (gpointer)(pos->data) : NULL);
393 }
394 
395 /**
396  * ipatch_iter_GList_next: (skip)
397  * @iter: Item iterator initialized with a GList
398  *
399  * GList item iterator method to get the next item and advance the
400  * iterator's position.
401  *
402  * Returns: Next item or %NULL if no more items.
403  */
404 gpointer
ipatch_iter_GList_next(IpatchIter * iter)405 ipatch_iter_GList_next(IpatchIter *iter)
406 {
407     GList *pos;
408 
409     g_return_val_if_fail(iter != NULL, NULL);
410 
411     pos = IPATCH_ITER_GLIST_GET_POS(iter);
412 
413     if(pos)
414     {
415         pos = g_list_next(pos);
416     }
417 
418     IPATCH_ITER_GLIST_SET_POS(iter, pos);  /* set current position */
419     return (pos ? (gpointer)(pos->data) : NULL);
420 }
421 
422 /**
423  * ipatch_iter_GList_first: (skip)
424  * @iter: Item iterator initialized with a GList
425  *
426  * GList item iterator method to get the first item and set the
427  * iterator's position to it.
428  *
429  * Returns: First item or %NULL if GList is empty.
430  */
431 gpointer
ipatch_iter_GList_first(IpatchIter * iter)432 ipatch_iter_GList_first(IpatchIter *iter)
433 {
434     GList **list, *pos;
435 
436     g_return_val_if_fail(iter != NULL, NULL);
437 
438     list = IPATCH_ITER_GLIST_GET_LIST(iter);  /* list pointer */
439     g_return_val_if_fail(list != NULL, NULL);
440 
441     pos = *list;
442     IPATCH_ITER_GLIST_SET_POS(iter, pos);  /* set position */
443     return (pos ? (gpointer)(pos->data) : NULL);
444 }
445 
446 /**
447  * ipatch_iter_GList_last: (skip)
448  * @iter: Item iterator initialized with a GList
449  *
450  * GList item iterator method to get the last item and set the
451  * iterator's position to it.
452  *
453  * Returns: Last item or %NULL if GList is empty.
454  */
455 gpointer
ipatch_iter_GList_last(IpatchIter * iter)456 ipatch_iter_GList_last(IpatchIter *iter)
457 {
458     GList **list, *pos;
459 
460     g_return_val_if_fail(iter != NULL, NULL);
461 
462     list = IPATCH_ITER_GLIST_GET_LIST(iter);
463     g_return_val_if_fail(list != NULL, NULL);
464 
465     pos = g_list_last(*list);
466     IPATCH_ITER_GLIST_SET_POS(iter, pos);  /* set current position */
467     return (pos ? (gpointer)(pos->data) : NULL);
468 }
469 
470 /**
471  * ipatch_iter_GList_index: (skip)
472  * @iter: Item iterator initialized with a GList
473  * @index: Index, from 0, of item to get
474  *
475  * GList item iterator method to get an item at a given index and set the
476  * iterator's position to it.
477  *
478  * Returns: item at the @index position or %NULL if index is off
479  * the end of the GList.
480  */
481 gpointer
ipatch_iter_GList_index(IpatchIter * iter,int index)482 ipatch_iter_GList_index(IpatchIter *iter, int index)
483 {
484     GList **list, *pos;
485 
486     g_return_val_if_fail(iter != NULL, NULL);
487 
488     list = IPATCH_ITER_GLIST_GET_LIST(iter);
489     g_return_val_if_fail(list != NULL, NULL);
490 
491     pos = g_list_nth(*list, index);
492     IPATCH_ITER_GLIST_SET_POS(iter, pos);  /* set current position */
493     return (pos ? (gpointer)(pos->data) : NULL);
494 }
495 
496 /**
497  * ipatch_iter_GList_insert: (skip)
498  * @iter: Item iterator initialized with a GList
499  * @item: Pointer to insert
500  *
501  * GList item iterator method to insert an item pointer.
502  */
503 void
ipatch_iter_GList_insert(IpatchIter * iter,gpointer item)504 ipatch_iter_GList_insert(IpatchIter *iter, gpointer item)
505 {
506     GList **list, *pos;
507 
508     g_return_if_fail(iter != NULL);
509 
510     if((pos = IPATCH_ITER_GLIST_GET_POS(iter)))   /* position set? */
511     {
512         pos = g_list_insert(pos, item, 1);  /* insert after position */
513         IPATCH_ITER_GLIST_SET_POS(iter, g_list_next(pos));   /* update pos */
514     }
515     else				/* position not set */
516     {
517         list = IPATCH_ITER_GLIST_GET_LIST(iter);
518         g_return_if_fail(list != NULL);
519 
520         pos = g_list_prepend(*list, item);  /* prepend */
521         IPATCH_ITER_GLIST_SET_POS(iter, pos);  /* set current position */
522         *list = pos;		/* set root of list */
523     }
524 }
525 
526 /**
527  * ipatch_iter_GList_remove: (skip)
528  * @iter: Item iterator initialized with a GList
529  *
530  * GList item iterator method to remove the current item and advance
531  * the current position.
532  */
533 void
ipatch_iter_GList_remove(IpatchIter * iter)534 ipatch_iter_GList_remove(IpatchIter *iter)
535 {
536     GList **list, *pos;
537 
538     g_return_if_fail(iter != NULL);
539 
540     list = IPATCH_ITER_GLIST_GET_LIST(iter);
541     g_return_if_fail(list != NULL);
542 
543     /* advance current position if set */
544     pos = IPATCH_ITER_GLIST_GET_POS(iter);
545 
546     if(pos)
547     {
548         IPATCH_ITER_GLIST_SET_POS(iter, g_list_next(pos));
549         *list = g_list_delete_link(*list, pos);
550     }
551 }
552 
553 /**
554  * ipatch_iter_GList_count: (skip)
555  * @iter: Item iterator initialized with a GList
556  *
557  * GList item iterator method to get the count of items.
558  *
559  * Returns: Count of items in GList iterator.
560  */
561 int
ipatch_iter_GList_count(IpatchIter * iter)562 ipatch_iter_GList_count(IpatchIter *iter)
563 {
564     GList **list;
565 
566     g_return_val_if_fail(iter != NULL, 0);
567 
568     list = IPATCH_ITER_GLIST_GET_LIST(iter);
569     g_return_val_if_fail(list != NULL, 0);
570 
571     return (g_list_length(*list));
572 }
573 
574 
575 /**
576  * ipatch_iter_array_init: (skip)
577  * @iter: Iterator to initialize
578  * @array: Pointer to an array of pointers
579  * @size: Count of elements in @array.
580  *
581  * Initialize an iterator to iterate over an array (read only).
582  */
583 void
ipatch_iter_array_init(IpatchIter * iter,gpointer * array,guint size)584 ipatch_iter_array_init(IpatchIter *iter, gpointer *array, guint size)
585 {
586     g_return_if_fail(iter != NULL);
587     g_return_if_fail(array != NULL);
588 
589     iter->methods = &ipatch_iter_array_methods;
590     IPATCH_ITER_ARRAY_SET_ARRAY(iter, array);
591     IPATCH_ITER_ARRAY_SET_SIZE(iter, size);
592     IPATCH_ITER_ARRAY_SET_POS(iter, -1);		/* init to no position */
593 }
594 
595 /**
596  * ipatch_iter_array_get: (skip)
597  * @iter: Item iterator initialized with an array
598  *
599  * Array item iterator method to get the current item.
600  *
601  * Returns: Current item or %NULL if no current item.
602  */
603 gpointer
ipatch_iter_array_get(IpatchIter * iter)604 ipatch_iter_array_get(IpatchIter *iter)
605 {
606     gpointer *array;
607     int pos;
608 
609     g_return_val_if_fail(iter != NULL, NULL);
610 
611     array = IPATCH_ITER_ARRAY_GET_ARRAY(iter);
612     g_return_val_if_fail(array != NULL, NULL);
613 
614     pos = IPATCH_ITER_ARRAY_GET_POS(iter);
615     return ((pos != -1) ? array[pos] : NULL);
616 }
617 
618 /**
619  * ipatch_iter_array_next: (skip)
620  * @iter: Item iterator initialized with an array
621  *
622  * Array item iterator method to get the next item and advance the
623  * iterator's position.
624  *
625  * Returns: Next item or %NULL if no more items.
626  */
627 gpointer
ipatch_iter_array_next(IpatchIter * iter)628 ipatch_iter_array_next(IpatchIter *iter)
629 {
630     gpointer *array;
631     guint pos, size;
632 
633     g_return_val_if_fail(iter != NULL, NULL);
634 
635     array = IPATCH_ITER_ARRAY_GET_ARRAY(iter);
636     g_return_val_if_fail(array != NULL, NULL);
637 
638     pos = (guint)IPATCH_ITER_ARRAY_GET_POS(iter);
639     size = IPATCH_ITER_ARRAY_GET_SIZE(iter);
640 
641     if(pos >= 0 && (pos + 1) < size)
642     {
643         pos++;
644     }
645     else
646     {
647         pos = -1;
648     }
649 
650     IPATCH_ITER_ARRAY_SET_POS(iter, pos);  /* update position */
651 
652     return ((pos != -1) ? array[pos] : NULL);
653 }
654 
655 /**
656  * ipatch_iter_array_first: (skip)
657  * @iter: Item iterator initialized with an array
658  *
659  * Array item iterator method to get the first item and set the
660  * iterator's position to it.
661  *
662  * Returns: First item or %NULL if array is empty.
663  */
664 gpointer
ipatch_iter_array_first(IpatchIter * iter)665 ipatch_iter_array_first(IpatchIter *iter)
666 {
667     gpointer *array;
668     int pos = 0;
669     guint size;
670 
671     g_return_val_if_fail(iter != NULL, NULL);
672 
673     array = IPATCH_ITER_ARRAY_GET_ARRAY(iter);
674     g_return_val_if_fail(array != NULL, NULL);
675 
676     size = IPATCH_ITER_ARRAY_GET_SIZE(iter);
677 
678     if(size == 0)
679     {
680         pos = -1;
681     }
682 
683     IPATCH_ITER_ARRAY_SET_POS(iter, pos);
684 
685     return ((pos != -1) ? array[pos] : NULL);
686 }
687 
688 /**
689  * ipatch_iter_array_last: (skip)
690  * @iter: Item iterator initialized with an array
691  *
692  * Array item iterator method to get the last item and set the
693  * iterator's position to it.
694  *
695  * Returns: Last item or %NULL if array is empty.
696  */
697 gpointer
ipatch_iter_array_last(IpatchIter * iter)698 ipatch_iter_array_last(IpatchIter *iter)
699 {
700     gpointer *array;
701     int pos;
702     guint size;
703 
704     g_return_val_if_fail(iter != NULL, NULL);
705 
706     array = IPATCH_ITER_ARRAY_GET_ARRAY(iter);
707     g_return_val_if_fail(array != NULL, NULL);
708 
709     size = IPATCH_ITER_ARRAY_GET_SIZE(iter);
710 
711     if(size > 0)
712     {
713         pos = size - 1;
714     }
715     else
716     {
717         pos = -1;
718     }
719 
720     IPATCH_ITER_ARRAY_SET_POS(iter, pos);
721 
722     return ((pos != -1) ? array[pos] : NULL);
723 }
724 
725 /**
726  * ipatch_iter_array_index: (skip)
727  * @iter: Item iterator initialized with an array
728  * @index: Index, from 0, of item to get
729  *
730  * Array item iterator method to get an item at a given index and set the
731  * iterator's position to it.
732  *
733  * Returns: item at the @index position or %NULL if index is off
734  * the end of the array.
735  */
736 gpointer
ipatch_iter_array_index(IpatchIter * iter,int index)737 ipatch_iter_array_index(IpatchIter *iter, int index)
738 {
739     gpointer *array;
740     int size;
741 
742     g_return_val_if_fail(iter != NULL, NULL);
743 
744     array = IPATCH_ITER_ARRAY_GET_ARRAY(iter);
745     g_return_val_if_fail(array != NULL, NULL);
746 
747     size = (int)IPATCH_ITER_ARRAY_GET_SIZE(iter);
748 
749     if(index < 0 || index >= size)
750     {
751         index = -1;
752     }
753 
754     IPATCH_ITER_ARRAY_SET_POS(iter, index);
755 
756     return ((index != -1) ? array[index] : NULL);
757 }
758 
759 /**
760  * ipatch_iter_array_insert: (skip)
761  * @iter: Item iterator initialized with a array
762  * @item: Pointer to insert
763  *
764  * array item iterator method to insert an item pointer.
765  */
766 void
ipatch_iter_array_insert(IpatchIter * iter,gpointer item)767 ipatch_iter_array_insert(IpatchIter *iter, gpointer item)
768 {
769     g_return_if_reached();
770 }
771 
772 /**
773  * ipatch_iter_array_remove: (skip)
774  * @iter: Item iterator initialized with a array
775  *
776  * array item iterator method to remove the current item and advance
777  * the current position.
778  */
779 void
ipatch_iter_array_remove(IpatchIter * iter)780 ipatch_iter_array_remove(IpatchIter *iter)
781 {
782     g_return_if_reached();
783 }
784 
785 /**
786  * ipatch_iter_array_count: (skip)
787  * @iter: Item iterator initialized with a array
788  *
789  * array item iterator method to get the count of items.
790  *
791  * Returns: Count of items in array iterator.
792  */
793 int
ipatch_iter_array_count(IpatchIter * iter)794 ipatch_iter_array_count(IpatchIter *iter)
795 {
796     guint size;
797 
798     g_return_val_if_fail(iter != NULL, 0);
799 
800     size = IPATCH_ITER_ARRAY_GET_SIZE(iter);
801     return (size);
802 }
803