blob: 4aaffeb567f2fd8f7b90e8b1f372b3d4a78dd505 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include <stdio.h>
#include "fgetc.h"
/* ************************************************************************ */
/* Custom fgetc/ungetc implementation able to unget more than one character */
/* ************************************************************************ */
enum
{
GETC_BUFF_SIZE = 16
};
static char getc_buffer[GETC_BUFF_SIZE] = { '\0' };
static long next_buffer_pos = 0; /* next free position in the getc buffer */
/* ====================================== */
/* Get a (possibly pushed-back) character */
/* ====================================== */
int
my_fgetc(FILE * input)
{
return (next_buffer_pos > 0) ? getc_buffer[--next_buffer_pos] : fgetc(input);
}
/* ============================ */
/* Push character back on input */
/* ============================ */
void
my_ungetc(int c)
{
if (next_buffer_pos >= GETC_BUFF_SIZE)
fprintf(stderr, "Error: cannot push back more than %d characters\n",
GETC_BUFF_SIZE);
else
getc_buffer[next_buffer_pos++] = c;
}
|