-
Notifications
You must be signed in to change notification settings - Fork 50
/
fgetc.c
69 lines (56 loc) · 1.73 KB
/
fgetc.c
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* ################################################################### */
/* Copyright 2015, Pierre Gentile ([email protected]) */
/* */
/* This Source Code Form is subject to the terms of the Mozilla Public */
/* License, v. 2.0. If a copy of the MPL was not distributed with this */
/* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/* ################################################################### */
/* ************************************************************************* */
/* Custom fgetc/ungetc implementation able to unget more than one character. */
/* ************************************************************************* */
#include <stdio.h>
#include "fgetc.h"
enum
{
GETC_BUFF_SIZE = 16
};
static unsigned char getc_buffer[GETC_BUFF_SIZE] = { '\0' };
static long next_buffer_pos = 0; /* Next free position in the getc buffer. */
/* ======================================== */
/* Gets a (possibly pushed-back) character. */
/* ======================================== */
int
my_fgetc(FILE *input)
{
if (next_buffer_pos > 0)
return getc_buffer[--next_buffer_pos];
else
{
int c;
errno = 0;
c = fgetc(input);
while (c == EOF && errno == EAGAIN)
{
errno = 0;
c = fgetc(input);
}
return c;
}
}
/* =============================== */
/* Pushes character back on input. */
/* =============================== */
int
my_ungetc(int c, FILE *input)
{
int rc;
if (next_buffer_pos >= GETC_BUFF_SIZE)
rc = EOF;
else
{
rc = getc_buffer[next_buffer_pos++] = (unsigned char)c;
if (feof(input))
clearerr(input); /* No more EOF. */
}
return rc;
}