v2 / vlib / builtin / character_inout.c.v
43 lines · 41 sloc · 1.1 KB · c79f84d4067c566dead5284c98e670e6f3e8b9c9
Raw
1module builtin
2
3// input_character gives back a single character, read from the standard input.
4// It returns -1 on error (when the input is finished (EOF), on a broken pipe etc).
5pub fn input_character() int {
6 mut ch := 0
7 $if freestanding {
8 // TODO
9 return -1
10 } $else $if vinix {
11 // TODO
12 return -1
13 } $else {
14 ch = C.getchar()
15 if ch == C.EOF {
16 return -1
17 }
18 }
19 return ch
20}
21
22// print_character writes the single character `ch` to the standard output.
23// It returns -1 on error (when the output is closed, on a broken pipe, etc).
24// Note: this function does not allocate memory, unlike `print(ch.ascii_str())`
25// which does, and is thus cheaper to call, which is important, if you have
26// to output many characters one by one. If you instead want to print entire
27// strings at once, use `print(your_string)`.
28pub fn print_character(ch u8) int {
29 $if android && !termux {
30 C.android_print(C.stdout, c'%.*s', 1, voidptr(&ch))
31 } $else $if freestanding {
32 bare_print(voidptr(&ch), u64(1))
33 } $else $if vinix {
34 // TODO
35 return 0
36 } $else {
37 x := C.putchar(ch)
38 if x == C.EOF {
39 return -1
40 }
41 }
42 return ch
43}
44