1 /* ################################################################### */
2 /* Copyright 2015, Pierre Gentile (p.gen.progs@gmail.com) */
4 /* This Source Code Form is subject to the terms of the Mozilla Public */
5 /* License, v. 2.0. If a copy of the MPL was not distributed with this */
6 /* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 /* ################################################################### */
9 /* ************************************************************************* */
10 /* Custom fgetc/ungetc implementation able to unget more than one character. */
11 /* ************************************************************************* */
21 static unsigned char getc_buffer
[GETC_BUFF_SIZE
] = { '\0' };
23 static int next_buffer_pos
= 0; /* Next free position in the getc buffer. */
25 /* ======================================== */
26 /* Gets a (possibly pushed-back) character. */
27 /* ======================================== */
31 if (next_buffer_pos
> 0)
32 return getc_buffer
[--next_buffer_pos
];
40 while (c
== EOF
&& errno
== EAGAIN
)
50 /* =============================== */
51 /* Pushes character back on input. */
52 /* =============================== */
54 my_ungetc(int c
, FILE *input
)
58 if (next_buffer_pos
>= GETC_BUFF_SIZE
)
62 rc
= getc_buffer
[next_buffer_pos
++] = (unsigned char)c
;
65 clearerr(input
); /* No more EOF. */