1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"
int text2array(int **dst, const char *r) {
int *a = malloc(sizeof *a);
int na = 0, sa = 1;
char *err;
int v;
for (;;) {
if (na == sa) {
int *tmp = realloc(a, (size_t)(2*sa)*sizeof *tmp);
if (!tmp) exit(EXIT_FAILURE);
a = tmp;
sa *= 2;
}
v = (int)strtol(r, &err, 10);
a[na++] = v;
if (!*err) break;
r = err;
}
*dst = a;
return na;
}
|
|
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#include <stdio.h>
#include <stdlib.h>
#include "aocutils.h"
int text2array(int **dst, const char *r) {
int *a = malloc(sizeof *a);
size_t na = 0, sa = 1;
char *err;
int v;
for (;;) {
if (na == sa) {
int *tmp = realloc(a, (2*sa) * sizeof *tmp);
if (!tmp) exit(EXIT_FAILURE);
a = tmp;
sa *= 2;
}
v = strtol(r, &err, 10);
a[na++] = v;
if (!*err) break;
r = err;
}
*dst = a;
return na;
}
|
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
size_t r = 0;
while ((ch = fgetc(h)) != EOF) {
if (r == s) {
//grow tmp
char *ttmp = realloc(tmp, (13*s)/8); // 13/8 is within 0.5% of the golden ratio
if (ttmp) {
tmp = ttmp;
s = (13*s)/8;
} else {
free(tmp);
return 0;
}
}
tmp[r++] = (char)ch;
}
fclose(h);
*dst = tmp;
return r;
}
|
|
|
|
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
size_t r = 0;
while ((ch = fgetc(h)) != EOF) {
if (r == s) {
//grow tmp
char *ttmp = realloc(tmp, (13*s)/8); // 13/8 is within 0.5% of the golden ratio
if (ttmp) {
tmp = ttmp;
s = (13*s) / 8;
} else {
free(tmp);
return 0;
}
}
tmp[r++] = ch;
}
fclose(h);
*dst = tmp;
return r;
}
|