-
Notifications
You must be signed in to change notification settings - Fork 0
/
proconio.c.v
129 lines (117 loc) · 2.69 KB
/
proconio.c.v
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
124
125
126
127
128
129
// (c) 2023 lemoncmd.
module proconio
fn C.scanf(&char, ...voidptr) int
fn read_i64() i64 {
number := i64(0)
unsafe { C.scanf(c'%lld', &number) }
return number
}
fn read_u64() u64 {
number := u64(0)
unsafe { C.scanf(c'%llu', &number) }
return number
}
fn read_f32() f32 {
number := f32(0)
unsafe { C.scanf(c'%f', &number) }
return number
}
fn read_f64() f64 {
number := f64(0)
unsafe { C.scanf(c'%lf', &number) }
return number
}
fn read_rune() rune {
number := u8(0)
unsafe { C.scanf(c'%c', &number) }
if number < 128 {
return rune(number)
}
mut buf := [number]
for _ in 0 .. utf8_char_len(number) - 1 {
rest := u8(0)
unsafe { C.scanf(c'%c', &rest) }
buf << rest
}
return buf.utf8_to_utf32() or { rune(number) }
}
fn read_string(buf_size int) string {
buf := []u8{len: buf_size}
unsafe { C.scanf(c'%s', &buf[0]) }
return unsafe { tos_clone(&buf[0]) }
}
fn try_read_i64() !i64 {
number := i64(0)
err := unsafe { C.scanf(c'%lld', &number) }
if err > 0 {
return number
}
if err == 0 {
return new_input_error('Unable to read number', 'signed integer')
}
return new_input_error('Reached EOF', 'signed integer')
}
fn try_read_u64() !u64 {
number := u64(0)
err := unsafe { C.scanf(c'%llu', &number) }
if err > 0 {
return number
}
if err == 0 {
return new_input_error('Unable to read number', 'unsigned integer')
}
return new_input_error('Reached EOF', 'unsigned integer')
}
fn try_read_f32() !f32 {
number := f32(0)
err := unsafe { C.scanf(c'%f', &number) }
if err > 0 {
return number
}
if err == 0 {
return new_input_error('Unable to read number', 'unsigned integer')
}
return new_input_error('Reached EOF', 'unsigned integer')
}
fn try_read_f64() !f64 {
number := f64(0)
err := unsafe { C.scanf(c'%lf', &number) }
if err > 0 {
return number
}
if err == 0 {
return new_input_error('Unable to read number', 'unsigned integer')
}
return new_input_error('Reached EOF', 'unsigned integer')
}
fn try_read_rune() !rune {
number := u8(0)
mut err := unsafe { C.scanf(c'%c', &number) }
if err > 0 && number < 128 {
return rune(number)
}
if err == 0 {
return new_input_error('Unable to read character', 'rune')
}
if err < 0 {
return new_input_error('Reached EOF', 'rune')
}
mut buf := [number]
for _ in 0 .. utf8_char_len(number) - 1 {
rest := u8(0)
err = unsafe { C.scanf(c'%c', &rest) }
if rest < 128 {
return new_input_error('Could not decode character as UTF-8', 'rune')
}
if err == 0 {
return new_input_error('Unable to read character', 'rune')
}
if err < 0 {
return new_input_error('Reached EOF', 'rune')
}
buf << rest
}
return buf.utf8_to_utf32() or {
return new_input_error('Could not decode character as UTF-8', 'rune')
}
}