mirror of
https://github.com/johnkerl/miller.git
synced 2026-07-18 00:45:47 +00:00
[read performance iterate] byte-reader iterate
This commit is contained in:
parent
25451533e3
commit
0fea309c5b
4 changed files with 146 additions and 0 deletions
|
|
@ -3,7 +3,11 @@
|
|||
#include "input/byte_reader.h"
|
||||
|
||||
byte_reader_t* string_byte_reader_alloc();
|
||||
byte_reader_t* stdio_byte_reader_alloc();
|
||||
byte_reader_t* mmap_byte_reader_alloc();
|
||||
|
||||
void string_byte_reader_free(byte_reader_t* pbr);
|
||||
void stdio_byte_reader_free(byte_reader_t* pbr);
|
||||
void mmap_byte_reader_free(byte_reader_t* pbr);
|
||||
|
||||
#endif // BYTE_READERS_H
|
||||
|
|
|
|||
62
c/input/stdio_byte_reader.c
Normal file
62
c/input/stdio_byte_reader.c
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "input/byte_readers.h"
|
||||
#include "lib/mlr_globals.h"
|
||||
#include "lib/mlrutil.h"
|
||||
|
||||
typedef struct _stdio_byte_reader_state_t {
|
||||
char* filename;
|
||||
FILE* fp;
|
||||
} stdio_byte_reader_state_t;
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
static int stdio_byte_reader_open_func(struct _byte_reader_t* pbr, char* filename) {
|
||||
stdio_byte_reader_state_t* pstate = mlr_malloc_or_die(sizeof(stdio_byte_reader_state_t));
|
||||
pstate->filename = strdup(filename);
|
||||
pstate->fp = fopen(filename, "r");
|
||||
if (pstate->fp == NULL) {
|
||||
perror("fopen");
|
||||
fprintf(stderr, "%s: Couldn't open \"%s\" for read.\n", MLR_GLOBALS.argv0, filename);
|
||||
exit(1);
|
||||
}
|
||||
pbr->pvstate = pstate;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static int stdio_byte_reader_read_func(struct _byte_reader_t* pbr) {
|
||||
stdio_byte_reader_state_t* pstate = pbr->pvstate;
|
||||
int c = getc_unlocked(pstate->fp);
|
||||
if (c == EOF && ferror(pstate->fp)) {
|
||||
perror("fread");
|
||||
fprintf(stderr, "%s: Read error on file \"%s\".\n", MLR_GLOBALS.argv0, pstate->filename);
|
||||
exit(1);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
static void stdio_byte_reader_close_func(struct _byte_reader_t* pbr) {
|
||||
stdio_byte_reader_state_t* pstate = pbr->pvstate;
|
||||
fclose(pstate->fp);
|
||||
free(pstate);
|
||||
pbr->pvstate = NULL;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
byte_reader_t* stdio_byte_reader_alloc() {
|
||||
byte_reader_t* pbr = mlr_malloc_or_die(sizeof(byte_reader_t));
|
||||
|
||||
pbr->pvstate = NULL;
|
||||
pbr->popen_func = stdio_byte_reader_open_func;
|
||||
pbr->pread_func = stdio_byte_reader_read_func;
|
||||
pbr->pclose_func = stdio_byte_reader_close_func;
|
||||
|
||||
return pbr;
|
||||
}
|
||||
|
||||
void stdio_byte_reader_free(byte_reader_t* pbr) {
|
||||
stdio_byte_reader_state_t* pstate = pbr->pvstate;
|
||||
if (pstate != NULL) {
|
||||
free(pstate->filename); // null-ok semantics
|
||||
}
|
||||
free(pbr);
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ typedef struct _string_byte_reader_state_t {
|
|||
char* pend;
|
||||
} string_byte_reader_state_t;
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
static int string_byte_reader_open_func(struct _byte_reader_t* pbr, char* backing) {
|
||||
string_byte_reader_state_t* pstate = mlr_malloc_or_die(sizeof(string_byte_reader_state_t));
|
||||
pstate->backing = backing;
|
||||
|
|
@ -27,8 +28,10 @@ static int string_byte_reader_read_func(struct _byte_reader_t* pbr) {
|
|||
}
|
||||
|
||||
static void string_byte_reader_close_func(struct _byte_reader_t* pbr) {
|
||||
pbr->pvstate = NULL;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
byte_reader_t* string_byte_reader_alloc() {
|
||||
byte_reader_t* pbr = mlr_malloc_or_die(sizeof(byte_reader_t));
|
||||
|
||||
|
|
|
|||
77
c/input/test_stdio_byte_reader.c
Normal file
77
c/input/test_stdio_byte_reader.c
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "lib/mlrutil.h"
|
||||
#include "lib/minunit.h"
|
||||
#include "input/byte_readers.h"
|
||||
|
||||
#ifdef __TEST_STDIO_BYTE_READER_MAIN__
|
||||
int tests_run = 0;
|
||||
int tests_failed = 0;
|
||||
int assertions_run = 0;
|
||||
int assertions_failed = 0;
|
||||
|
||||
// xxx mkstemp
|
||||
// xxx tmpfile
|
||||
// xxx pop from buf
|
||||
// xxx take dirname from argv[1]?
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
static FILE* make_temp_file(char* contents) {
|
||||
xxx
|
||||
|
||||
int fd = mkstemp("/tmp/mlr-ut-XXXXXXXX");
|
||||
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
static char* test_it() {
|
||||
byte_reader_t* pbr = stdio_byte_reader_alloc();
|
||||
|
||||
char* filename = xxx;
|
||||
int ok = pbr->popen_func(pbr, filename);
|
||||
mu_assert_lf(ok == TRUE);
|
||||
mu_assert_lf(pbr->pread_func(pbr) == EOF);
|
||||
mu_assert_lf(pbr->pread_func(pbr) == EOF);
|
||||
mu_assert_lf(pbr->pread_func(pbr) == EOF);
|
||||
pbr->pclose_func(pbr);
|
||||
|
||||
ok = pbr->popen_func(pbr, "a");
|
||||
mu_assert_lf(ok == TRUE);
|
||||
mu_assert_lf(pbr->pread_func(pbr) == 'a');
|
||||
mu_assert_lf(pbr->pread_func(pbr) == EOF);
|
||||
mu_assert_lf(pbr->pread_func(pbr) == EOF);
|
||||
pbr->pclose_func(pbr);
|
||||
|
||||
ok = pbr->popen_func(pbr, "abc");
|
||||
mu_assert_lf(ok == TRUE);
|
||||
mu_assert_lf(pbr->pread_func(pbr) == 'a');
|
||||
mu_assert_lf(pbr->pread_func(pbr) == 'b');
|
||||
mu_assert_lf(pbr->pread_func(pbr) == 'c');
|
||||
mu_assert_lf(pbr->pread_func(pbr) == EOF);
|
||||
mu_assert_lf(pbr->pread_func(pbr) == EOF);
|
||||
pbr->pclose_func(pbr);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// ================================================================
|
||||
static char * run_all_tests() {
|
||||
mu_run_test(test_it);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char *result = run_all_tests();
|
||||
printf("\n");
|
||||
if (result != 0) {
|
||||
printf("Not all unit tests passed\n");
|
||||
}
|
||||
else {
|
||||
printf("TEST_STDIO_BYTE_READER: ALL UNIT TESTS PASSED\n");
|
||||
}
|
||||
printf("Tests passed: %d of %d\n", tests_run - tests_failed, tests_run);
|
||||
printf("Assertions passed: %d of %d\n", assertions_run - assertions_failed, assertions_run);
|
||||
|
||||
return result != 0;
|
||||
}
|
||||
#endif // __TEST_STDIO_BYTE_READER_MAIN__
|
||||
Loading…
Add table
Add a link
Reference in a new issue