-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoclib.c
More file actions
49 lines (42 loc) · 1.23 KB
/
oclib.c
File metadata and controls
49 lines (42 loc) · 1.23 KB
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
// $Id: oclib.c,v 1.96 2018-11-26 13:03:09-08 - - $
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define __OCLIB_C__
#include "oclib.h"
typedef int (*ctype_fn) (int);
cstring scan (ctype_fn skip_over, ctype_fn stop_at) {
int byte = 0;
do {
byte = getchar();
if (byte == EOF) return NULL;
} while (skip_over != NULL && skip_over (byte));
size_t buf_size = 16;
cstring buffer = malloc (buf_size);
assert (buffer != NULL);
size_t end_pos = 0;
do {
buffer[end_pos++] = byte;
if (end_pos >= buf_size) {
buf_size *= 2;
buffer = realloc (buffer, buf_size);
assert (buffer != NULL);
}
buffer[end_pos] = '\0';
byte = getchar();
}while (byte != EOF && !stop_at (byte));
return buffer;
}
int isnl (int byte) { return byte == '\n'; }
void putint (int val) { printf ("%d", val); }
void putstr (const cstring s) { printf ("%s", s); }
cstring getword (void) { return scan (isspace, isspace); }
cstring getln (void) { return scan (NULL, isnl); }
void endl() { putchar ('\n'); }
void* xcalloc (int nelem, int size) {
void* result = calloc (nelem, size);
assert (result != NULL);
return result;
}