-
Notifications
You must be signed in to change notification settings - Fork 4
/
stringport.c
50 lines (45 loc) · 857 Bytes
/
stringport.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
/* see LICENSE file for copyright and license details */
#include <stdio.h>
#include <stdlib.h>
#include "stringport.h"
#include "reporting.h"
int
port_peek(string_port *port)
{
int c;
switch (port->kind) {
case STRPORT_CHAR:
return port->text[port->place];
case STRPORT_FILE:
c = fgetc(port->fptr);
ungetc(c, port->fptr);
return c;
}
reporterr("port set to wrong kind");
}
int
port_eof(string_port *port)
{
switch (port->kind) {
case STRPORT_CHAR:
return port->text[port->place] == '\0';
case STRPORT_FILE:
return feof(port->fptr);
}
reporterr("port set to wrong kind");
}
int
port_getc(string_port *port)
{
int c;
switch (port->kind) {
case STRPORT_CHAR:
c = port->text[port->place];
if (c != '\0')
port->place++;
return c;
case STRPORT_FILE:
return fgetc(port->fptr);
}
reporterr("port set to wrong kind");
}