static void handle_fd (DskPattern *pattern, const char *prefix_filename, unsigned buffer_size, int fd) { DskBuffer in = DSK_BUFFER_INIT; DskBuffer out = DSK_BUFFER_INIT; for (;;) { int rv = dsk_buffer_readv (&in, fd); if (rv == 0) break; char *line; while ((line = dsk_buffer_read_line (&in)) != NULL) { if (dsk_pattern_match (pattern, line)) { if (prefix_filename) { dsk_buffer_append_string (&out, prefix_filename); dsk_buffer_append_string (&out, ": "); } dsk_buffer_append_string (&out, line); dsk_buffer_append_byte (&out, '\n'); while (out.size > buffer_size) dsk_buffer_writev (&out, STDOUT_FILENO); } dsk_free (line); } } while (out.size > 0) dsk_buffer_writev (&out, STDOUT_FILENO); }
void decount(DskBuffer* buf, int start, int end) { char b[1024]; while (start <= end) { char *rv; sprintf (b, "%d", start++); rv = dsk_buffer_read_line (buf); dsk_assert (rv != NULL); dsk_assert (strcmp (b, rv) == 0); dsk_free (rv); } }
int main(int argc, char** argv) { DskBuffer gskbuffer; char buf[1024]; char *str; dsk_cmdline_init ("test dsk-buffer code", "test DskBuffer", NULL, 0); dsk_cmdline_process_args (&argc, &argv); dsk_buffer_init (&gskbuffer); dsk_assert (gskbuffer.size == 0); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_assert (gskbuffer.size == 5); dsk_assert (dsk_buffer_read (&gskbuffer, sizeof (buf), buf) == 5); dsk_assert (memcmp (buf, "hello", 5) == 0); dsk_assert (gskbuffer.size == 0); dsk_buffer_clear (&gskbuffer); dsk_buffer_init (&gskbuffer); count (&gskbuffer, 1, 100000); decount (&gskbuffer, 1, 100000); dsk_assert (gskbuffer.size == 0); dsk_buffer_clear (&gskbuffer); dsk_buffer_init (&gskbuffer); dsk_buffer_append_string (&gskbuffer, "hello\na\nb"); str = dsk_buffer_read_line (&gskbuffer); dsk_assert (str); dsk_assert (strcmp (str, "hello") == 0); dsk_free (str); str = dsk_buffer_read_line (&gskbuffer); dsk_assert (str); dsk_assert (strcmp (str, "a") == 0); dsk_free (str); dsk_assert (gskbuffer.size == 1); dsk_assert (dsk_buffer_read_line (&gskbuffer) == NULL); dsk_buffer_append_byte (&gskbuffer, '\n'); str = dsk_buffer_read_line (&gskbuffer); dsk_assert (str); dsk_assert (strcmp (str, "b") == 0); dsk_free (str); dsk_assert (gskbuffer.size == 0); dsk_buffer_clear (&gskbuffer); dsk_buffer_init (&gskbuffer); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_buffer_append_foreign (&gskbuffer, 4, "test", NULL, NULL); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_assert (gskbuffer.size == 14); dsk_assert (dsk_buffer_read (&gskbuffer, sizeof (buf), buf) == 14); dsk_assert (memcmp (buf, "hellotesthello", 14) == 0); dsk_assert (gskbuffer.size == 0); /* Test that the foreign data really is not being stored in the DskBuffer */ { char test_str[5]; strcpy (test_str, "test"); dsk_buffer_init (&gskbuffer); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_buffer_append_foreign (&gskbuffer, 4, test_str, NULL, NULL); dsk_buffer_append (&gskbuffer, 5, "hello"); dsk_assert (gskbuffer.size == 14); dsk_assert (dsk_buffer_peek (&gskbuffer, sizeof (buf), buf) == 14); dsk_assert (memcmp (buf, "hellotesthello", 14) == 0); test_str[1] = '3'; dsk_assert (gskbuffer.size == 14); dsk_assert (dsk_buffer_read (&gskbuffer, sizeof (buf), buf) == 14); dsk_assert (memcmp (buf, "hellot3sthello", 14) == 0); dsk_buffer_clear (&gskbuffer); } /* Test str_index_of */ { DskBuffer buffer = DSK_BUFFER_INIT; dsk_buffer_append_foreign (&buffer, 3, "abc", NULL, NULL); dsk_buffer_append_foreign (&buffer, 3, "def", NULL, NULL); dsk_buffer_append_foreign (&buffer, 3, "gad", NULL, NULL); #if 0 dsk_assert (dsk_buffer_str_index_of (&buffer, "cdefg") == 2); dsk_assert (dsk_buffer_str_index_of (&buffer, "ad") == 7); dsk_assert (dsk_buffer_str_index_of (&buffer, "ab") == 0); dsk_assert (dsk_buffer_str_index_of (&buffer, "a") == 0); dsk_assert (dsk_buffer_str_index_of (&buffer, "g") == 6); #endif dsk_buffer_clear (&buffer); } static const char *before_strs[] = { "", "foo", NULL, NULL }; before_strs[2] = generate_str (100, 1000); before_strs[3] = generate_str (10000, 100000); static const char *placeholder_strs[] = { "", "bar", NULL, NULL, NULL }; placeholder_strs[2] = generate_str (100, 1000); placeholder_strs[3] = generate_str (10000, 100000); placeholder_strs[4] = generate_str (100000, 1000000); static const char *after_strs[] = { "", "foo", NULL, NULL }; after_strs[2] = generate_str (100, 1000); after_strs[3] = generate_str (10000, 100000); unsigned bi, pi, ai; for (bi = 0; bi < DSK_N_ELEMENTS (before_strs); bi++) for (pi = 0; pi < DSK_N_ELEMENTS (placeholder_strs); pi++) for (ai = 0; ai < DSK_N_ELEMENTS (after_strs); ai++) { DskBuffer buffer = DSK_BUFFER_INIT; const char *pi_str = placeholder_strs[pi]; DskBufferPlaceholder placeholder; dsk_buffer_append_string (&buffer, before_strs[bi]); dsk_buffer_append_placeholder (&buffer, strlen (pi_str), &placeholder); dsk_buffer_append_string (&buffer, after_strs[ai]); dsk_buffer_placeholder_set (&placeholder, pi_str); dsk_assert (try_initial_remove (&buffer, before_strs[bi])); dsk_assert (try_initial_remove (&buffer, pi_str)); dsk_assert (try_initial_remove (&buffer, after_strs[ai])); dsk_assert (buffer.size == 0); } return 0; }