static read_method_decl(filtered) { struct filtered_istream *fs = &(st->u.filtered); size_t filled = 0; while (sz) { /* do we already have filtered output? */ if (fs->o_ptr < fs->o_end) { size_t to_move = fs->o_end - fs->o_ptr; if (sz < to_move) to_move = sz; memcpy(buf + filled, fs->obuf + fs->o_ptr, to_move); fs->o_ptr += to_move; sz -= to_move; filled += to_move; continue; } fs->o_end = fs->o_ptr = 0; /* do we have anything to feed the filter with? */ if (fs->i_ptr < fs->i_end) { size_t to_feed = fs->i_end - fs->i_ptr; size_t to_receive = FILTER_BUFFER; if (stream_filter(fs->filter, fs->ibuf + fs->i_ptr, &to_feed, fs->obuf, &to_receive)) return -1; fs->i_ptr = fs->i_end - to_feed; fs->o_end = FILTER_BUFFER - to_receive; continue; } /* tell the filter to drain upon no more input */ if (fs->input_finished) { size_t to_receive = FILTER_BUFFER; if (stream_filter(fs->filter, NULL, NULL, fs->obuf, &to_receive)) return -1; fs->o_end = FILTER_BUFFER - to_receive; if (!fs->o_end) break; continue; } fs->i_end = fs->i_ptr = 0; /* refill the input from the upstream */ if (!fs->input_finished) { fs->i_end = read_istream(fs->upstream, fs->ibuf, FILTER_BUFFER); if (fs->i_end < 0) return -1; if (fs->i_end) continue; } fs->input_finished = 1; } return filled; }
void test_stream_filter(void) { stream_t *st; long count = 0; puts("Testing even filter (0-10):"); st = stream_range(0, 10, 1); while(!stream_empty(st)) { st = stream_filter(st, stream_generic_filter_even, NULL); if(!st) break; printf("\t%p\n", (void *)stream_head(st)); CU_ASSERT((void *)stream_head(st) == (void *)count); count += 2; st = stream_tail(st); } return; }
/* * Filter a stream of struct Schedule to contain schedules that contain a * specific date. * * @param s Stream * @param t time_t to filter against * @return EXIT_FAILURE or EXIT_SUCCESS */ int tt_filter_schedule_contains_date(Stream *s, time_t *t) { return stream_filter(s, filter, t, NULL); }