Lines Matching defs:lr
59 struct lafe_line_reader *lr;
61 lr = calloc(1, sizeof(*lr));
62 if (lr == NULL)
65 lr->nullSeparator = nullSeparator;
66 lr->pathname = strdup(pathname);
69 lr->f = stdin;
71 lr->f = fopen(pathname, "r");
72 if (lr->f == NULL)
74 lr->buff_length = 8192;
75 lr->line_start = lr->line_end = lr->buff_end = lr->buff = NULL;
77 return (lr);
81 lafe_line_reader_find_eol(struct lafe_line_reader *lr)
84 lr->line_end += strcspn(lr->line_end,
85 lr->nullSeparator ? "" : "\x0d\x0a");
86 *lr->line_end = '\0'; /* Noop if line_end == buff_end */
90 lafe_line_reader_next(struct lafe_line_reader *lr)
97 while (lr->line_end < lr->buff_end) {
98 line_start = lr->line_start;
99 lr->line_start = ++lr->line_end;
100 lafe_line_reader_find_eol(lr);
102 if (lr->nullSeparator || line_start[0] != '\0')
107 if (lr->f == NULL) {
108 if (lr->line_start == lr->buff_end)
110 line_start = lr->line_start;
111 lr->line_start = lr->buff_end;
116 if (lr->line_start > lr->buff) {
118 memmove(lr->buff, lr->line_start,
119 lr->buff_end - lr->line_start);
120 lr->buff_end -= lr->line_start - lr->buff;
121 lr->line_end -= lr->line_start - lr->buff;
122 lr->line_start = lr->buff;
125 new_buff_size = lr->buff_length * 2;
126 if (new_buff_size <= lr->buff_length)
128 "Line too long in %s", lr->pathname);
129 lr->buff_length = new_buff_size;
134 p = realloc(lr->buff, new_buff_size + 1);
137 "Line too long in %s", lr->pathname);
138 lr->buff_end = p + (lr->buff_end - lr->buff);
139 lr->line_end = p + (lr->line_end - lr->buff);
140 lr->line_start = lr->buff = p;
144 bytes_wanted = lr->buff + lr->buff_length - lr->buff_end;
145 bytes_read = fread(lr->buff_end, 1, bytes_wanted, lr->f);
146 lr->buff_end += bytes_read;
147 *lr->buff_end = '\0'; /* Always terminate buffer */
148 lafe_line_reader_find_eol(lr);
150 if (ferror(lr->f))
151 lafe_errc(1, errno, "Can't read %s", lr->pathname);
152 if (feof(lr->f)) {
153 if (lr->f != stdin)
154 fclose(lr->f);
155 lr->f = NULL;
161 lafe_line_reader_free(struct lafe_line_reader *lr)
163 free(lr->buff);
164 free(lr->pathname);
165 free(lr);