-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
86 lines (72 loc) · 1.87 KB
/
checker.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
package mlst
import (
"flag"
"fmt"
"os"
)
var args []string
func printError(err string) {
fmt.Fprintf(os.Stderr, err)
}
func printMessage(msg string) {
fmt.Fprintf(os.Stdout, msg)
}
func init() {
flag.Parse()
args = flag.Args()
}
func CheckInput() []EdgeSet {
var infile string
if len(args) < 1 {
infile = DefaultInputFile
} else {
infile = args[0]
}
file, err := os.Open(infile)
if err != nil {
printError(fmt.Sprintf("Cannot open '%s' (%s).\n", infile, err.Error()))
return nil
}
inReader := NewInFileReader(file)
if edgeSets, err := inReader.ReadInputFile(); err != nil {
printError("(" + infile + ") " + err.Error() + "\n")
} else {
printMessage(fmt.Sprintf("Input file '%s' has the correct format.\n", infile))
return edgeSets
}
return nil
}
func CheckOutput(checkOutputProgramName string) {
var outfile string
switch numArgs := len(args); numArgs {
case 0:
outfile = DefaultOutputFile
case 2:
outfile = args[1]
default:
printError(fmt.Sprintf("usage: %s [file.in file.out]\n\n"+
" Check the format of \"file.out\" against \"file.in\".\n"+
"Error: Must provide either two arguments, or zero arguments to use the default\n"+
"input \"%s\" and output \"%s\". (Number of arguments is %d)\n",
checkOutputProgramName, DefaultInputFile, DefaultOutputFile, numArgs))
return
}
edgeSets := CheckInput()
if edgeSets == nil {
return
}
file, err := os.Open(outfile)
if err != nil {
printError(fmt.Sprintf("Cannot open '%s' (%s).\n", outfile, err.Error()))
return
}
outReader := NewOutFileReader(file)
if NumLeaves, err := outReader.ReadOutputFile(edgeSets); err != nil {
printError("(" + outfile + ") " + err.Error() + "\n")
} else {
printMessage(fmt.Sprintf("Output file '%s' has the correct format.\n", outfile))
for i, v := range NumLeaves {
printMessage(fmt.Sprintf("Output tree %d has %d leaves.\n", i+1, v))
}
}
}