2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * @defgroup enumerator enumerator
18 * @{ @ingroup collections
24 typedef struct enumerator_t enumerator_t
;
26 #include <utils/utils.h>
29 * Enumerator interface, allows enumeration over collections.
34 * Enumerate collection.
36 * The enumerate function takes a variable argument list containing
37 * pointers where the enumerated values get written.
39 * @param ... variable list of enumerated items, implementation dependent
40 * @return TRUE if pointers returned
42 bool (*enumerate
)(enumerator_t
*this, ...);
45 * Destroy a enumerator instance.
47 void (*destroy
)(enumerator_t
*this);
51 * Create an enumerator which enumerates over nothing
53 * @return an enumerator over no values
55 enumerator_t
* enumerator_create_empty();
58 * Create an enumerator which enumerates over a single item
60 * @param item item to enumerate
61 * @param cleanup cleanup function called on destroy with the item
62 * @return an enumerator over a single value
64 enumerator_t
*enumerator_create_single(void *item
, void (*cleanup
)(void *item
));
67 * Create an enumerator over files/subdirectories in a directory.
69 * This enumerator_t.enumerate() function returns a (to the directory) relative
70 * filename (as a char*), an absolute filename (as a char*) and a file status
71 * (to a struct stat), which all may be NULL. "." and ".." entries are
79 e = enumerator_create_directory("/tmp");
82 while (e->enumerate(e, &rel, &abs, &st))
84 if (S_ISDIR(st.st_mode) && *rel != '.')
93 * @param path path of the directory
94 * @return the directory enumerator, NULL on failure
96 enumerator_t
* enumerator_create_directory(const char *path
);
99 * Create an enumerator over tokens of a string.
101 * Tokens are separated by one of the characters in sep and trimmed by the
102 * characters in trim.
104 * @param string string to parse
105 * @param sep separator characters
106 * @param trim characters to trim from tokens
107 * @return enumerator over char* tokens
109 enumerator_t
* enumerator_create_token(const char *string
, const char *sep
,
113 * Creates an enumerator which enumerates over enumerated enumerators :-).
115 * The variable argument list of enumeration values is limit to 5.
117 * @param outer outer enumerator
118 * @param inner_constructor constructor to inner enumerator
119 * @param data data to pass to each inner_constructor call
120 * @param destroy_data destructor to pass to data
121 * @return the nested enumerator
123 enumerator_t
*enumerator_create_nested(enumerator_t
*outer
,
124 enumerator_t
*(*inner_constructor
)(void *outer
, void *data
),
125 void *data
, void (*destroy_data
)(void *data
));
128 * Creates an enumerator which filters output of another enumerator.
130 * The filter function receives the user supplied "data" followed by a
131 * unfiltered enumeration item, followed by an output pointer where to write
132 * the filtered data. Then the next input/output pair follows.
133 * It returns TRUE to deliver the
134 * values to the caller of enumerate(), FALSE to filter this enumeration.
136 * The variable argument list of enumeration values is limit to 5.
138 * @param unfiltered unfiltered enumerator to wrap, gets destroyed
139 * @param filter filter function
140 * @param data user data to supply to filter
141 * @param destructor destructor function to clean up data after use
142 * @return the filtered enumerator
144 enumerator_t
*enumerator_create_filter(enumerator_t
*unfiltered
,
145 bool (*filter
)(void *data
, ...),
146 void *data
, void (*destructor
)(void *data
));
149 * Create an enumerator wrapper which does a cleanup on destroy.
151 * @param wrapped wrapped enumerator
152 * @param cleanup cleanup function called on destroy
153 * @param data user data to supply to cleanup
154 * @return the enumerator with cleanup
156 enumerator_t
*enumerator_create_cleaner(enumerator_t
*wrapped
,
157 void (*cleanup
)(void *data
), void *data
);
159 #endif /** ENUMERATOR_H_ @}*/