| 1 |
/* |
| 2 |
* Copyright (C) 2009-2014 Karel Zak <[email protected]> |
| 3 |
* Copyright (C) 2014 Ondrej Oprala <[email protected]> |
| 4 |
* |
| 5 |
* This file may be redistributed under the terms of the |
| 6 |
* GNU Lesser General Public License. |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* SECTION: iter |
| 11 |
* @title: Iterator |
| 12 |
* @short_description: unified iterator |
| 13 |
* |
| 14 |
* The iterator keeps the direction and the last position |
| 15 |
* for access to the internal library tables/lists. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <string.h> |
| 19 |
#include <stdlib.h> |
| 20 |
|
| 21 |
#include "smartcolsP.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* scols_new_iter: |
| 25 |
* @direction: SCOLS_INTER_{FOR,BACK}WARD direction |
| 26 |
* |
| 27 |
* Returns: newly allocated generic libmount iterator. |
| 28 |
*/ |
| 29 |
struct libscols_iter *scols_new_iter(int direction) |
| 30 |
{ |
| 31 |
struct libscols_iter *itr = calloc(1, sizeof(*itr)); |
| 32 |
if (!itr) |
| 33 |
return NULL; |
| 34 |
itr->direction = direction; |
| 35 |
return itr; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* scols_free_iter: |
| 40 |
* @itr: iterator pointer |
| 41 |
* |
| 42 |
* Deallocates the iterator. |
| 43 |
*/ |
| 44 |
void scols_free_iter(struct libscols_iter *itr) |
| 45 |
{ |
| 46 |
free(itr); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* scols_reset_iter: |
| 51 |
* @itr: iterator pointer |
| 52 |
* @direction: SCOLS_INTER_{FOR,BACK}WARD or -1 to keep the direction unchanged |
| 53 |
* |
| 54 |
* Resets the iterator. |
| 55 |
*/ |
| 56 |
void scols_reset_iter(struct libscols_iter *itr, int direction) |
| 57 |
{ |
| 58 |
if (direction == -1) |
| 59 |
direction = itr->direction; |
| 60 |
|
| 61 |
memset(itr, 0, sizeof(*itr)); |
| 62 |
itr->direction = direction; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* scols_iter_get_direction: |
| 67 |
* @itr: iterator pointer |
| 68 |
* |
| 69 |
* Returns: SCOLS_INTER_{FOR,BACK}WARD |
| 70 |
*/ |
| 71 |
int scols_iter_get_direction(const struct libscols_iter *itr) |
| 72 |
{ |
| 73 |
return itr->direction; |
| 74 |
} |