-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
158 lines (126 loc) · 3 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
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"github.com/spf13/pflag"
)
const programName = "crossjoin"
const programVersion = "1.1.0"
const programDescription = `Generate a cross join, also known as a Cartesian product, from the lines of the
specified files. If standard input (stdin) is provided, the program will use it
as the first input.`
type arguments struct {
help bool
version bool
files []string
}
func parseArguments() arguments {
args := arguments{}
pflag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s file1 file2 file3 [...fileN]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Flags:\n")
pflag.PrintDefaults()
}
flags := pflag.NewFlagSet("", pflag.ExitOnError)
flags.BoolVarP(&args.help, "help", "h", false, "Show help")
flags.BoolVarP(&args.version, "version", "v", false, "Show version")
pflag.CommandLine.AddFlagSet(flags)
pflag.Parse()
if args.help {
usage()
os.Exit(0)
}
if args.version {
fmt.Println(programName, programVersion)
os.Exit(0)
}
args.files = pflag.Args()
return args
}
func usage() {
fmt.Fprintf(os.Stderr, "%s %s\n", programName, programVersion)
fmt.Fprintf(os.Stderr, "%s\n\n", programDescription)
pflag.Usage()
}
func main() {
args := parseArguments()
if err := process(args.files); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
}
}
func process(filenames []string) error {
readerCount := len(filenames)
if hasStdin() {
readerCount++
}
if readerCount == 0 {
return fmt.Errorf("no input specified")
}
readers := make([]io.ReadSeeker, readerCount)
scanners := make([]*bufio.Scanner, readerCount)
lines := make([][]byte, readerCount)
// Initialize stdin
inputIndex := 0
if hasStdin() {
readers[inputIndex] = os.Stdin
inputIndex++
}
// Initialize files
for _, filename := range filenames {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
readers[inputIndex] = file
inputIndex++
}
// Initialize scanners and get first line from each
for i := 0; i < readerCount; i++ {
scanner := bufio.NewScanner(readers[i])
scanner.Scan()
scanners[i] = scanner
lines[i] = scanner.Bytes()
}
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
for {
// Print current line combination
for _, line := range lines {
writer.Write(line)
}
writer.WriteByte('\n')
// Update combination starting from the last file
for i := readerCount - 1; i >= 0; i-- {
if scanners[i].Scan() {
lines[i] = scanners[i].Bytes()
break
} else {
// Check if we've cycled through all combinations
if i == 0 {
return nil
}
// Reached the end of this file, rewind and continue with the next one
if _, err := readers[i].Seek(0, 0); err != nil {
return err
}
scanners[i] = bufio.NewScanner(readers[i])
scanners[i].Scan()
lines[i] = scanners[i].Bytes()
}
}
}
}
func hasStdin() bool {
fi, err := os.Stdin.Stat()
if err != nil {
return false
}
if fi.Mode()&os.ModeNamedPipe == 0 {
return false
}
return true
}