-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.h
123 lines (105 loc) · 2.15 KB
/
io.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <theosl/vec.h>
#ifndef __THEOSL_IO__
#define __THEOSL_IO__
DEF_VEC(char)
typedef Vec(char) string;
DEF_VEC(string)
DEF_VEC(int)
DEF_VEC(float)
/// @returns 100 * allocation fail OR -2 for invalid character
int iscan(Vec(int) * iv) {
char ch;
bool spacer = true;
string word;
{
int e = vec_char_init(&word);
if (e) {
return e * 100;
}
}
while ((ch = getc(stdin)) != EOF) {
switch (ch) {
case ' ':
case '\t':
case ',':
if (!spacer) {
spacer = true;
vec_char_rappend(&word, '\0');
vec_int_rappend(iv, atoi(word.data));
vec_char_clear(&word);
}
break;
case '\n':
goto rest;
default:
spacer = false;
if ((ch >= '0' && ch <= '9') || ch == '-') {
vec_char_rappend(&word, ch);
} else {
return -2;
}
break;
}
}
rest:
if (word.len != 0) {
// str_add(&word, '\0');
// str_arr_add(&arr, word.start);
vec_char_rappend(&word, '\0');
vec_int_rappend(iv, atoi(word.data));
}
vec_char_free(&word);
// for (size_t i = 0; i < arr.len; ++i) {
// // data[i] = atoi(arr.start[i]);
// // free(arr.start[i]);
// vec_int_rappend(iv, atoi(arr.data[i].data));
// vec_char_free(arr.data+i);
// }
// // free(arr.start);
// vec_string_free(&arr);
return 0;
}
int fscan(Vec(float) * fv) {
char ch;
bool spacer = true;
Vec(char) word;
{
int e = vec_char_init(&word);
if (e) {
return e * 100;
}
}
while ((ch = getc(stdin)) != EOF) {
switch (ch) {
case ' ':
case '\t':
if (!spacer) {
spacer = true;
vec_char_rappend(&word, '\0');
vec_float_rappend(fv, atof(word.data));
vec_char_clear(&word);
}
break;
case '\n':
goto rest;
default:
spacer = false;
if ((ch >= '0' && ch <= '9') || ch == '.') {
vec_char_rappend(&word, ch);
} else {
return -2;
}
break;
}
}
rest:
if (word.len != 0) {
vec_char_rappend(&word, '\0');
vec_float_rappend(fv, atof(word.data));
} else {
vec_char_free(&word);
}
return 0;
}
#endif