| 1 |
/* |
| 2 |
* Copyright (C) 2009 Karel Zak <[email protected]> |
| 3 |
* |
| 4 |
* This file may be redistributed under the terms of the |
| 5 |
* GNU Lesser General Public License. |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* SECTION: iter |
| 10 |
* @title: Iterator |
| 11 |
* @short_description: unified iterator |
| 12 |
* |
| 13 |
* The iterator keeps the direction and the last position for access to the |
| 14 |
* internal library tables/lists. |
| 15 |
* |
| 16 |
* It's very unusual to use the same iterator on multiple places in your |
| 17 |
* application or share the same iterator, for this purpose libfdisk does not |
| 18 |
* provide reference counting for this object. It's recommended to initialize |
| 19 |
* the iterator by fdisk_new_iter() at begin of your function and then |
| 20 |
* fdisk_free_iter() before you return from the function. |
| 21 |
* |
| 22 |
* Don't forget to call fdisk_reset_iter() if you want to use the iterator more |
| 23 |
* than once. |
| 24 |
*/ |
| 25 |
#include <stdio.h> |
| 26 |
#include <string.h> |
| 27 |
#include <stdlib.h> |
| 28 |
|
| 29 |
#include "fdiskP.h" |
| 30 |
|
| 31 |
/** |
| 32 |
* fdisk_new_iter: |
| 33 |
* @direction: FDISK_INTER_{FOR,BACK}WARD direction |
| 34 |
* |
| 35 |
* Returns: newly allocated generic libmount iterator. |
| 36 |
*/ |
| 37 |
struct fdisk_iter *fdisk_new_iter(int direction) |
| 38 |
{ |
| 39 |
struct fdisk_iter *itr = calloc(1, sizeof(*itr)); |
| 40 |
if (!itr) |
| 41 |
return NULL; |
| 42 |
itr->direction = direction; |
| 43 |
return itr; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* fdisk_free_iter: |
| 48 |
* @itr: iterator pointer |
| 49 |
* |
| 50 |
* Deallocates the iterator. |
| 51 |
*/ |
| 52 |
void fdisk_free_iter(struct fdisk_iter *itr) |
| 53 |
{ |
| 54 |
free(itr); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* fdisk_reset_iter: |
| 59 |
* @itr: iterator pointer |
| 60 |
* @direction: FDISK_ITER_{FOR,BACK}WARD or -1 to keep the direction unchanged |
| 61 |
* |
| 62 |
* Resets the iterator. |
| 63 |
*/ |
| 64 |
void fdisk_reset_iter(struct fdisk_iter *itr, int direction) |
| 65 |
{ |
| 66 |
if (direction == -1) |
| 67 |
direction = itr->direction; |
| 68 |
|
| 69 |
memset(itr, 0, sizeof(*itr)); |
| 70 |
itr->direction = direction; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* fdisk_iter_get_direction: |
| 75 |
* @itr: iterator pointer |
| 76 |
* |
| 77 |
* Returns: FDISK_INTER_{FOR,BACK}WARD |
| 78 |
*/ |
| 79 |
int fdisk_iter_get_direction(struct fdisk_iter *itr) |
| 80 |
{ |
| 81 |
return itr->direction; |
| 82 |
} |