1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "stringport.h"
- // from stackoverflow.com/questions/2082743/c-equivalent-to-fstreams-peek
- int fpeek(FILE *stream)
- {
- int c;
- c = fgetc(stream);
- ungetc(c, stream);
- return c;
- }
- int port_peek(string_port *port)
- {
- switch(port->kind) {
-
- case STRPORT_CHAR:
- return port->text[port->place];
- break;
- case STRPORT_FILE:
- return fpeek(port->fptr);
- default:
- exit(-1);
- }
- }
- int port_eof(string_port *port)
- {
- switch(port->kind) {
-
- case STRPORT_CHAR:
- return port->text[port->place] == '\0';
- break;
- case STRPORT_FILE:
- return feof(port->fptr);
- default:
- exit(-1);
- }
- }
- 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);
- default:
- exit(-1);
- }
- }
|