-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
45 lines (43 loc) · 1023 Bytes
/
common.h
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
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int getdelim(char **linep, size_t *n, int delim, FILE *fp){
int ch;
int i = 0;
if(!linep || !n || !fp){
errno = EINVAL;
return -1;
}
if(*linep == NULL){
if(NULL==(*linep = malloc(*n=128))){
*n = 0;
errno = ENOMEM;
return -1;
}
}
while((ch = fgetc(fp)) != EOF){
if(i + 1 >= *n){
char *temp = realloc(*linep, *n + 128);
if(!temp){
errno = ENOMEM;
return -1;
}
*n += 128;
*linep = temp;
}
(*linep)[i++] = ch;
if(ch == delim)
break;
}
(*linep)[i] = '\0';
return !i && ch == EOF ? -1 : i;
}
int getline(char **linep, size_t *n, FILE *fp){
return getdelim(linep, n, '\n', fp);
}
int streq(const char* a, const char* b) {
return strcmp(a, b) == 0;
}