This repository was archived by the owner on Oct 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
81 lines (65 loc) · 1.56 KB
/
util.c
File metadata and controls
81 lines (65 loc) · 1.56 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "gary.h"
#include "compiler.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int get_file_size(FILE *fp) {
fseek(fp, 0, SEEK_END);
int size = ftell(fp);
fseek(fp, 0, SEEK_SET);
return size;
}
char *load_file(FILE *fp) {
size_t size = get_file_size(fp);
char *content = malloc(size + 1);
char *start = content;
char c;
while((c = getc(fp)) != EOF) {
*content = c;
content++;
}
*content = '\0';
return start;
}
int uid() {
static int id = 0;
return id++;
}
void Stack_init(struct Stack *s) {
s->length = 0;
}
void Stack_push(struct Stack *s, int n) {
if(s->length == 32) {
perror("Gary: stack size limit reached.");
return;
}
s->data[s->length] = n;
s->length++;
}
int Stack_pop(struct Stack *s) {
s->length--;
return s->data[s->length];
}
void DynStrArray_init(struct DynStrArray *dsa) {
dsa->items = malloc(sizeof(char *));
dsa->used = 0;
dsa->size = 1;
}
void DynStrArray_add(struct DynStrArray *dsa, char *item) {
if(dsa->used == dsa->size) {
dsa->size = dsa->size * 2;
dsa->items = realloc(dsa->items, dsa->size * sizeof(char *));
}
dsa->items[dsa->used] = malloc(strlen(item)+1); // \0
memcpy(dsa->items[dsa->used], item, strlen(item)+1);
dsa->used++;
}
bool DynStrArray_contains(struct DynStrArray *dsa, char *str) {
for(int i = 0; i < dsa->used; i++) {
char *cmp = dsa->items[i];
if(strcmp(cmp, str) == 0) {
return true;
}
}
return false;
}