4 /* FIXME: this is small for testing */
5 #define BUFFER_SIZE (8)
7 #define LEN(X, I) ((X)->p[(I)].one_past_end - (X)->p[(I)].start)
8 #define EMPTY(X) ((X)->n_bufs == 1 && LEN (X, 0) == 0)
10 #define ONE_PAST_END(X, I) ((X)->p[(I)].one_past_end)
17 typedef struct Line_ptr Line_ptr
;
31 typedef struct Buf Buf
;
34 buf_init_from_stdin (Buf
*x
, int eol_byte
)
36 int last_byte_is_eol_byte
= 1;
39 #define OBS (&(x->obs))
44 char *buf
= (char *) malloc (BUFFER_SIZE
);
49 /* Fall back on the code that relies on a temporary file.
50 Write all buffers to that file and free them. */
55 bytes_read
= full_read (STDIN_FILENO
, buf
, BUFFER_SIZE
);
57 error (1, errno
, _("read error"));
62 bp
.one_past_end
= buf
+ bytes_read
;
63 obstack_grow (OBS
, &bp
, sizeof (bp
));
67 last_byte_is_eol_byte
= (buf
[bytes_read
- 1] == eol_byte
);
69 if (bytes_read
< BUFFER_SIZE
)
75 /* If the file was non-empty and lacked an EOL_BYTE at its end,
76 then add a buffer containing just that one byte. */
77 if (!last_byte_is_eol_byte
)
79 char *buf
= malloc (1);
82 /* FIXME: just like above */
90 bp
.one_past_end
= buf
+ 1;
91 obstack_grow (OBS
, &bp
, sizeof (bp
));
96 x
->n_bufs
= obstack_object_size (OBS
) / sizeof (x
->p
[0]);
97 x
->p
= (struct B_pair
*) obstack_finish (OBS
);
99 /* If there are two or more buffers and the last buffer is empty,
100 then free the last one and decrement the buffer count. */
102 && x
->p
[x
->n_bufs
- 1].start
== x
->p
[x
->n_bufs
- 1].one_past_end
)
103 free (x
->p
[--(x
->n_bufs
)].start
);
112 for (i
= 0; i
< x
->n_bufs
; i
++)
113 free (x
->p
[i
].start
);
114 obstack_free (OBS
, NULL
);
118 line_ptr_decrement (const Buf
*x
, const Line_ptr
*lp
)
122 if (lp
->ptr
> x
->p
[lp
->i
].start
)
125 lp_new
.ptr
= lp
->ptr
- 1;
130 lp_new
.i
= lp
->i
- 1;
131 lp_new
.ptr
= ONE_PAST_END (x
, lp
->i
- 1) - 1;
137 line_ptr_increment (const Buf
*x
, const Line_ptr
*lp
)
141 assert (lp
->ptr
<= ONE_PAST_END (x
, lp
->i
) - 1);
142 if (lp
->ptr
< ONE_PAST_END (x
, lp
->i
) - 1)
145 lp_new
.ptr
= lp
->ptr
+ 1;
149 assert (lp
->i
< x
->n_bufs
- 1);
150 lp_new
.i
= lp
->i
+ 1;
151 lp_new
.ptr
= x
->p
[lp
->i
+ 1].start
;
157 find_bol (const Buf
*x
,
158 const Line_ptr
*last_bol
, Line_ptr
*new_bol
, char eol_byte
)
164 if (last_bol
->ptr
== x
->p
[0].start
)
167 tmp
= line_ptr_decrement (x
, last_bol
);
168 last_bol_ptr
= tmp
.ptr
;
172 char *nl
= memrchr (x
->p
[i
].start
, last_bol_ptr
, eol_byte
);
178 *new_bol
= line_ptr_increment (x
, &nl_pos
);
186 last_bol_ptr
= ONE_PAST_END (x
, i
);
189 /* If last_bol->ptr didn't point at the first byte of X, then reaching
190 this point means that we're about to return the line that is at the
192 if (last_bol
->ptr
!= x
->p
[0].start
)
195 new_bol
->ptr
= x
->p
[0].start
;
203 print_line (FILE *out_stream
, const Buf
*x
,
204 const Line_ptr
*bol
, const Line_ptr
*bol_next
)
207 for (i
= bol
->i
; i
<= bol_next
->i
; i
++)
209 char *a
= (i
== bol
->i
? bol
->ptr
: x
->p
[i
].start
);
210 char *b
= (i
== bol_next
->i
? bol_next
->ptr
: ONE_PAST_END (x
, i
));
211 fwrite (a
, 1, b
- a
, out_stream
);
220 char eol_byte
= '\n';
223 fail
= buf_init_from_stdin (&x
, eol_byte
);
230 /* Special case the empty file. */
234 /* Initially, point at one past the last byte of the file. */
235 bol
.i
= x
.n_bufs
- 1;
236 bol
.ptr
= ONE_PAST_END (&x
, bol
.i
);
241 int found
= find_bol (&x
, &bol
, &new_bol
, eol_byte
);
244 print_line (stdout
, &x
, &new_bol
, &bol
);