summaryrefslogtreecommitdiff
path: root/util.c
blob: 8073340bdd07ac764995e729ef21d6eac17fe878 (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
38
#include <stdio.h>
#include <stdlib.h>
#include "talk.h"

char *readupto(FILE *f, char end){
	char c;
	char *out = malloc(128);
	int counter = 0;
	while ((c = fgetc(f)) != EOF){
		out[counter] = c;
		if (c == end) break;
		counter++;
		if (counter > 128) out = realloc(out, counter + 128);
	}
	if (c == EOF && end != EOF) return NULL;

	out[counter] = 0;
	out = realloc(out, counter);

	return out;
}

char *readnchars(FILE *f, int n){
	char c;
	char *out = malloc(128);
	int counter = 0;
	while ((c = fgetc(f)) != EOF && counter < n){
		out[counter] = c;
		counter++;
		if (counter > 128) out = realloc(out, counter + 128);
	}
	if (c == EOF) return NULL;

	out[counter] = 0;
	out = realloc(out, counter);

	return out;
}