-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabellchen.go
174 lines (162 loc) · 3.61 KB
/
tabellchen.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
package tabellchen
import (
"bufio"
"fmt"
"os"
"strings"
)
type ReadConfig struct {
FilePath string
Separator rune
CommentChar rune
HasHeader bool
}
type WriteConfig struct {
File *os.File
Separator rune
}
type Table struct {
Header []string
Rows [][]string
}
// The method NewTable creates a table with specified header and rows.
func NewTable(header []string, rows [][]string) Table {
t := Table{
Header: header,
Rows: rows,
}
return t
}
func (t Table) ColIdByName(colname string) (int, error) {
id := -1
for i, h := range t.Header {
if h == colname {
id = i
break
}
}
var err error = nil
if id == -1 {
err = fmt.Errorf("ColIdByName: "+
"column '%s' not found\n", colname)
}
return id, err
}
// The method WriteTable writes a Table into an os.File.
func (t Table) WriteTable(config WriteConfig) error {
file := config.File
separator := config.Separator
var writeErr error
writer := bufio.NewWriter(file)
defer writer.Flush()
if len(t.Header) > 0 {
stringHeader := strings.Join(t.Header, string(separator))
_, err := writer.WriteString(stringHeader + "\n")
if err != nil {
writeErr := fmt.Errorf("WriteTable: failed "+
"to write header: %v\n", err)
return writeErr
}
}
rn := 0
for _, row := range t.Rows {
stringRow := strings.Join(row, string(separator))
_, err := writer.WriteString(stringRow + "\n")
if err != nil {
writeErr := fmt.Errorf("WriteTable: failed "+
"to write row %d: %v\n", rn, err)
return writeErr
}
rn += 1
}
return writeErr
}
// The function ReadTable reads data from a file and populates a Table. It accepts a ReadConfig struct.
func ReadTable(config ReadConfig) (Table, error) {
path := config.FilePath
separator := config.Separator
commentChar := config.CommentChar
hasHeader := config.HasHeader
file, err := os.Open(path)
if err != nil {
return Table{},
fmt.Errorf("Failed to open file: %v\n", err)
}
defer file.Close()
table := Table{}
numFields := 0
currentLine := 0
scanner := bufio.NewScanner(file)
firstLine := true
for scanner.Scan() {
line := scanner.Text()
currentLine += 1
if len(line) == 0 {
continue
}
if rune(line[0]) == commentChar {
continue
}
fields := strings.FieldsFunc(line, func(c rune) bool {
return c == separator
})
if firstLine {
numFields = len(fields)
if hasHeader {
table.Header = fields
} else {
table.Header = []string{}
table.Rows = append(table.Rows, fields)
}
firstLine = false
continue
}
if len(fields) != numFields {
return Table{},
fmt.Errorf("Line %d has %d fields, expected %d\n",
currentLine,
len(fields),
numFields)
}
table.Rows = append(table.Rows, fields)
}
if err := scanner.Err(); err != nil {
return Table{},
fmt.Errorf("Error reading file: %v\n", err)
}
return table, nil
}
// The function Filter returns rows of a Table that satisfy a condition.
func Filter(t Table,
col interface{},
cond func(string) bool) (Table, error) {
colId := -1
switch v := col.(type) {
case int:
colId = v
nc := len(t.Rows[0])
if colId > nc {
return t,
fmt.Errorf("Filter: tried to access "+
"column %d, but there are "+
"only %d columns\n", colId, nc)
}
case string:
var errColName error = nil
colId, errColName = t.ColIdByName(v)
if errColName != nil {
return t, errColName
}
default:
return t,
fmt.Errorf("Filter: can't handle column "+
"index of type %v\n", v)
}
filteredRows := [][]string{}
for _, row := range t.Rows {
if cond(row[colId]) {
filteredRows = append(filteredRows, row)
}
}
return Table{Header: t.Header, Rows: filteredRows}, nil
}