mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-28 13:10:19 +00:00
Add helper 'for_each_dive()' dive iterator
It's an easy thing to do, but the for-loop ends up being pretty ugly, so hide it behind the macro. It would be even prettier with one of the (few) useful C99 features: local for-loop variables. However, gcc needs special command line options, and other compilers may not do it at all. So instead of doing #define for_each_dive(_x) \ for (int _i = 0; ((_x) = get_dive(_i)) != NULL; _i++) we require that the user declare the index iterator too, and the use syntax becomes for_each_dive(idx, dive) { ... use idx/dive here ... } And hey, maybe somebody actually will want to use the index, so maybe that's not all bad. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
e46688d694
commit
666538ec77
4 changed files with 13 additions and 3 deletions
10
dive.h
10
dive.h
|
@ -295,6 +295,16 @@ static inline struct dive *get_dive(unsigned int nr)
|
||||||
return dive_table.dives[nr];
|
return dive_table.dives[nr];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Iterate over each dive, with the first parameter being the index
|
||||||
|
* iterator variable, and the second one being the dive one.
|
||||||
|
*
|
||||||
|
* I don't think anybody really wants the index, and we could make
|
||||||
|
* it local to the for-loop, but that would make us requires C99.
|
||||||
|
*/
|
||||||
|
#define for_each_dive(_i,_x) \
|
||||||
|
for ((_i) = 0; ((_x) = get_dive(_i)) != NULL; (_i)++)
|
||||||
|
|
||||||
extern void parse_xml_init(void);
|
extern void parse_xml_init(void);
|
||||||
extern void parse_xml_buffer(const char *url, const char *buf, int size, GError **error);
|
extern void parse_xml_buffer(const char *url, const char *buf, int size, GError **error);
|
||||||
extern void set_filename(const char *filename);
|
extern void set_filename(const char *filename);
|
||||||
|
|
|
@ -85,7 +85,7 @@ void dump_selection(void)
|
||||||
struct dive *dive;
|
struct dive *dive;
|
||||||
|
|
||||||
printf("currently selected are %d dives:", amount_selected);
|
printf("currently selected are %d dives:", amount_selected);
|
||||||
for (i = 0; (dive = get_dive(i)) != NULL; i++) {
|
for_each_dive(i, dive) {
|
||||||
if (dive->selected)
|
if (dive->selected)
|
||||||
printf(" %d", i);
|
printf(" %d", i);
|
||||||
}
|
}
|
||||||
|
|
2
info.c
2
info.c
|
@ -519,7 +519,7 @@ int edit_multi_dive_info(struct dive *single_dive)
|
||||||
int i;
|
int i;
|
||||||
struct dive *dive;
|
struct dive *dive;
|
||||||
|
|
||||||
for (i = 0; (dive = get_dive(i)) != NULL; i++) {
|
for_each_dive(i, dive) {
|
||||||
if (dive == master || !dive->selected)
|
if (dive == master || !dive->selected)
|
||||||
continue;
|
continue;
|
||||||
/* copy all "info" fields */
|
/* copy all "info" fields */
|
||||||
|
|
|
@ -151,7 +151,7 @@ void process_selected_dives(void)
|
||||||
memset(&stats_selection, 0, sizeof(stats_selection));
|
memset(&stats_selection, 0, sizeof(stats_selection));
|
||||||
|
|
||||||
nr = 0;
|
nr = 0;
|
||||||
for (i = 0; (dive = get_dive(i)) != NULL; ++i) {
|
for_each_dive(i, dive) {
|
||||||
if (dive->selected) {
|
if (dive->selected) {
|
||||||
process_dive(dive, &stats_selection);
|
process_dive(dive, &stats_selection);
|
||||||
nr++;
|
nr++;
|
||||||
|
|
Loading…
Reference in a new issue