Browse code

Simplify input_delimiter handling

John Hawthorn authored on 16/08/2019 08:09:29
Showing 5 changed files

... ...
@@ -46,7 +46,7 @@ static void *safe_realloc(void *buffer, size_t size) {
46 46
 	return buffer;
47 47
 }
48 48
 
49
-void choices_fread(choices_t *c, FILE *file) {
49
+void choices_fread(choices_t *c, FILE *file, char input_delimiter) {
50 50
 	/* Save current position for parsing later */
51 51
 	size_t buffer_start = c->buffer_size;
52 52
 
... ...
@@ -75,7 +75,7 @@ void choices_fread(choices_t *c, FILE *file) {
75 75
 	const char *line_end = c->buffer + c->buffer_size;
76 76
 	char *line = c->buffer + buffer_start;
77 77
 	do {
78
-		char *nl = strchr(line, c->input_delimiter);
78
+		char *nl = strchr(line, input_delimiter);
79 79
 		if (nl)
80 80
 			*nl++ = '\0';
81 81
 
... ...
@@ -114,12 +114,6 @@ void choices_init(choices_t *c, options_t *options) {
114 114
 		c->worker_count = (int)sysconf(_SC_NPROCESSORS_ONLN);
115 115
 	}
116 116
 
117
-	if (options->read_null) {
118
-		c->input_delimiter = '\0';
119
-	} else {
120
-		c->input_delimiter = '\n';
121
-	}
122
-
123 117
 	choices_reset_search(c);
124 118
 }
125 119
 
... ...
@@ -25,11 +25,10 @@ typedef struct {
25 25
 	size_t selection;
26 26
 
27 27
 	unsigned int worker_count;
28
-	char input_delimiter;
29 28
 } choices_t;
30 29
 
31 30
 void choices_init(choices_t *c, options_t *options);
32
-void choices_fread(choices_t *c, FILE *file);
31
+void choices_fread(choices_t *c, FILE *file, char input_delimiter);
33 32
 void choices_destroy(choices_t *c);
34 33
 void choices_add(choices_t *c, const char *choice);
35 34
 size_t choices_available(choices_t *c);
... ...
@@ -27,11 +27,11 @@ int main(int argc, char *argv[]) {
27 27
 			fprintf(stderr, "Must specify -e/--show-matches with --benchmark\n");
28 28
 			exit(EXIT_FAILURE);
29 29
 		}
30
-		choices_fread(&choices, stdin);
30
+		choices_fread(&choices, stdin, options.input_delimiter);
31 31
 		for (int i = 0; i < options.benchmark; i++)
32 32
 			choices_search(&choices, options.filter);
33 33
 	} else if (options.filter) {
34
-		choices_fread(&choices, stdin);
34
+		choices_fread(&choices, stdin, options.input_delimiter);
35 35
 		choices_search(&choices, options.filter);
36 36
 		for (size_t i = 0; i < choices_available(&choices); i++) {
37 37
 			if (options.show_scores)
... ...
@@ -42,13 +42,13 @@ int main(int argc, char *argv[]) {
42 42
 		/* interactive */
43 43
 
44 44
 		if (isatty(STDIN_FILENO))
45
-			choices_fread(&choices, stdin);
45
+			choices_fread(&choices, stdin, options.input_delimiter);
46 46
 
47 47
 		tty_t tty;
48 48
 		tty_init(&tty, options.tty_filename);
49 49
 
50 50
 		if (!isatty(STDIN_FILENO))
51
-			choices_fread(&choices, stdin);
51
+			choices_fread(&choices, stdin, options.input_delimiter);
52 52
 
53 53
 		if (options.num_lines > choices.size)
54 54
 			options.num_lines = choices.size;
... ...
@@ -41,16 +41,16 @@ static struct option longopts[] = {{"show-matches", required_argument, NULL, 'e'
41 41
 
42 42
 void options_init(options_t *options) {
43 43
 	/* set defaults */
44
-	options->benchmark    = 0;
45
-	options->filter       = NULL;
46
-	options->init_search  = NULL;
47
-	options->show_scores  = 0;
48
-	options->scrolloff    = 1;
49
-	options->tty_filename = DEFAULT_TTY;
50
-	options->num_lines    = DEFAULT_NUM_LINES;
51
-	options->prompt       = DEFAULT_PROMPT;
52
-	options->workers      = DEFAULT_WORKERS;
53
-	options->read_null    = 0;
44
+	options->benchmark       = 0;
45
+	options->filter          = NULL;
46
+	options->init_search     = NULL;
47
+	options->show_scores     = 0;
48
+	options->scrolloff       = 1;
49
+	options->tty_filename    = DEFAULT_TTY;
50
+	options->num_lines       = DEFAULT_NUM_LINES;
51
+	options->prompt          = DEFAULT_PROMPT;
52
+	options->workers         = DEFAULT_WORKERS;
53
+	options->input_delimiter = '\n';
54 54
 }
55 55
 
56 56
 void options_parse(options_t *options, int argc, char *argv[]) {
... ...
@@ -66,7 +66,7 @@ void options_parse(options_t *options, int argc, char *argv[]) {
66 66
 				options->show_scores = 1;
67 67
 				break;
68 68
 			case '0':
69
-				options->read_null = 1;
69
+				options->input_delimiter = '\0';
70 70
 				break;
71 71
 			case 'q':
72 72
 				options->init_search = optarg;
... ...
@@ -11,7 +11,7 @@ typedef struct {
11 11
 	unsigned int scrolloff;
12 12
 	const char *prompt;
13 13
 	unsigned int workers;
14
-	int read_null;
14
+	char input_delimiter;
15 15
 } options_t;
16 16
 
17 17
 void options_init(options_t *options);