-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (70 loc) · 1.71 KB
/
main.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
)
func processFile(file io.Reader, processors []Processor) error {
fileReader := bufio.NewReader(file)
for {
line, isPrefix, err := fileReader.ReadLine()
if err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("error reading file")
}
for _, processor := range processors {
processor.process(line, isPrefix)
}
}
return nil
}
func main() {
countBytesFlag := flag.Bool("c", false, "Count number of bytes in the file")
countLinesFlag := flag.Bool("l", false, "Count number of lines in the file")
countWordFlag := flag.Bool("w", false, "Count number of words in the file")
flag.Parse()
input := flag.Args()
var file *os.File
defer file.Close()
var fileName string = ""
var err error
if len(input) == 0 {
file = os.Stdin
} else if len(input) == 1 {
fileName = input[0]
file, err = os.Open(input[0])
} else {
err = fmt.Errorf("invalid arguments")
}
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
var processors []Processor = make([]Processor, 0, 3)
if *countBytesFlag {
processors = append(processors, &ByteCountProcessor{})
}
if *countLinesFlag {
processors = append(processors, &LineCountProcessor{})
}
if *countWordFlag {
processors = append(processors, &WordCountProcessor{})
}
if !*countBytesFlag && !*countLinesFlag && !*countWordFlag {
processors = append(processors, &ByteCountProcessor{}, &WordCountProcessor{}, &LineCountProcessor{})
}
err = processFile(file, processors)
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
output := ""
for _, processor := range processors {
output += fmt.Sprintf("%d ", processor.getValue())
}
fmt.Printf("%s %s\n", output, fileName)
}