-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetpar.go
232 lines (208 loc) · 3.36 KB
/
getpar.go
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"bytes"
"fmt"
"os"
"strings"
)
type cvntab struct {
abrev string
full string
boolValue bool
intValue int
funcValue func(int)
}
var Yntab = []cvntab{
{abrev: "y", full: "yes", boolValue: true},
{abrev: "n", full: "no", boolValue: false},
}
func getynpar(s string) bool {
r := getcodpar(s, Yntab)
return r.boolValue
}
func getcodpar(s string, tab []cvntab) *cvntab {
flag := false
for {
f := testnl()
flag = flag || f
if flag {
fmt.Printf("%s: ", s)
}
if f {
stdin.ReadByte() /* throw out the newline */
}
skipchars(" \t;")
input, err := readToken(" \t;\n")
if err != nil {
panic(err)
}
if input == "?" {
c := 4
for _, t := range tab {
fmt.Printf("%14.14s", t.full)
c--
if c > 0 {
continue
}
c = 4
fmt.Println("")
}
if c != 4 {
fmt.Println("")
}
continue
}
// TODO: implement same logic as in original.
for _, t := range tab {
if (t.abrev != "" && t.abrev == input) || (t.full != "" && strings.HasPrefix(input, t.full)) {
return &t
}
}
fmt.Printf("invalid input; ? for valid inputs\n")
skiptonl()
}
}
func testnl() bool {
for {
c, _ := stdin.ReadByte()
if c == '\n' {
break
}
if (c >= '0' && c <= '9') || c == '.' || c == '!' ||
(c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z') || c == '-' {
stdin.UnreadByte()
return false
}
}
stdin.UnreadByte()
return true
}
func readToken(charset string) (string, error) {
var buf bytes.Buffer
for {
c, err := stdin.ReadByte()
if err != nil {
break
}
if strings.IndexByte(charset, c) >= 0 {
stdin.UnreadByte()
break
}
buf.WriteByte(c)
}
return buf.String(), nil
}
/**
** scan for newline
**/
func skiptonl() {
for {
c, err := stdin.ReadByte()
if err != nil {
return
}
if c == '\n' {
break
}
}
stdin.UnreadByte()
}
func getintpar(s string) int {
for {
if testnl() {
if s != "" {
fmt.Printf("%s: ", s)
}
stdin.ReadByte()
}
var n int
i, _ := fmt.Fscanf(stdin, "%d", &n)
if i > 0 && testterm() {
return n
}
fmt.Printf("invalid input; please enter an integer\n")
skiptonl()
}
}
func getfltpar(s string) float64 {
for {
if testnl() {
if s != "" {
fmt.Printf("%s: ", s)
}
stdin.ReadByte()
}
var d float64
i, err := fmt.Fscanf(stdin, "%f", &d)
if i < 0 || err != nil {
tracef("getfltpar: i = %d err = %v", i, err)
os.Exit(1)
}
stdin.UnreadByte()
if i > 0 && testterm() {
return d
}
fmt.Printf("invalid input; please enter a double\n")
skiptonl()
}
}
func testterm() bool {
c, _ := stdin.ReadByte()
if c == 0 {
return true
}
if c == '.' {
return false
}
if c == '\n' || c == ';' {
stdin.UnreadByte()
}
return true
}
func readdelim(d byte) bool {
for {
c, _ := stdin.ReadByte()
if c == 0 {
break
}
if c == d {
return true
}
if c == ' ' {
continue
}
stdin.UnreadByte()
break
}
return false
}
func getstrpar(s string) string {
for {
if testnl() {
if s != "" {
fmt.Printf("%s: ", s)
}
stdin.ReadByte()
}
answer, err := readToken("\t\n;")
if err != nil {
panic(err)
}
if answer != "" {
return answer
}
}
}
func skipchars(charset string) {
for {
c, err := stdin.ReadByte()
if c == 0 && err != nil {
os.Exit(1)
}
if strings.IndexByte(charset, c) < 0 {
stdin.UnreadByte()
return
}
}
}