-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.c
89 lines (82 loc) · 1.98 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <assert.h>
#include <gc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
char* long_long_to_string(long long num) {
char* str;
size_t len;
if (num == 0) {
return string("0");
} else {
len = num_char_len(num);
str = GC_MALLOC(len + 1);
snprintf(str, len + 1, "%lli", num);
return str;
}
}
// note: there is a formula using log10 to calculate a number length,
// but it uses the math lib which yelled at me for using TinyCC :-/
size_t num_char_len(long long num) {
if (num < 0) {
return 1 + num_char_len(llabs(num));
} else if (num < 10) {
return 1;
} else if (num < 100) {
return 2;
} else if (num < 1000) {
return 3;
} else if (num < 10000) {
return 4;
} else if (num < 100000) {
return 5;
} else if (num < 1000000) {
return 6;
} else if (num < 1000000000) {
return 9;
} else if (num < 1000000000000) {
return 12;
} else if (num < 1000000000000000) {
return 15;
} else if (num < 1000000000000000000) {
return 18;
} else { // up to 128 bits
return 40;
}
}
char* string(char *str) {
size_t len = strlen(str);
char *copy = GC_MALLOC(len + 1);
snprintf(copy, len + 1, "%s", str);
return copy;
}
char* substring(char *orig, size_t start, size_t len) {
size_t orig_len = strlen(orig);
assert(start < orig_len);
assert(start + len <= orig_len);
char *buffer = GC_MALLOC(len + 1);
snprintf(buffer, len + 1, "%s", orig + start);
return buffer;
}
MalType* program_arguments_as_vector(int argc, char *argv[]) {
MalType *arg_vec = mal_vector();
for (int i=1; i<argc; i++) {
mal_vector_push(arg_vec, mal_string(argv[i]));
}
return arg_vec;
}
MalType* read_file(char *filename) {
MalType *content = mal_string("");
FILE *fp = fopen(filename, "r");
if(!fp) {
printf("Error opening file %s\n", filename);
exit(1);
}
char buffer[100];
while (fgets(buffer, 100, fp)) {
mal_string_append(content, buffer);
}
fclose(fp);
return content;
}