-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.c
59 lines (50 loc) · 1.03 KB
/
util.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
51
52
53
54
55
56
57
58
59
#include "util.h"
#include <stdlib.h>
#include <string.h>
char peek(FILE *in){
char c = getc(in);
ungetc(c, in);
return c;
}
void eat_ws(FILE *in)
{
int c;
while((c = getc(in)) != EOF){
if (isspace(c)) continue;
else if (c == ';'){ /* comment - ignore */
while((c = getc(in)) != '\n' && c != EOF);
continue;
}
ungetc(c, in);
break;
}
}
void eat_expected_str(FILE *in, char *str)
{
char c;
while(*str != '\0'){
c = getc(in);
if(c != *str++){
fprintf(stderr, "Unexpected character: expecting %s, got %c\n", str, c);
exit(1);
}
}
}
#define INT2STR_BUFLEN 12 /* a 32 bit integer is never longer than 12 digits in decimal */
char *int_to_string(int num)
{
char buf[INT2STR_BUFLEN];
snprintf(buf, INT2STR_BUFLEN, "%d", num);
return strdup(buf);
}
char *str_append(char* first, char *second){
char *buf = malloc(strlen(first) + strlen(second) + 1); /* +1 for null character */
char *ret;
if(buf == NULL)
return NULL;
strcpy(buf, first);
strcat(buf, second);
ret = strdup(buf);
free(buf);
return ret;
}