/
files.go
186 lines (159 loc) · 3.85 KB
/
files.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
package lizt
import (
"bufio"
"errors"
"fmt"
"math/rand"
"os"
"strings"
)
// OpenFile opens a file
func OpenFile(filename string) (*os.File, error) {
f, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("os.Open(): %s -> %w", filename, err)
}
return f, nil
}
// ReadFromFile reads a file into a slice of strings
func ReadFromFile(filename string) ([]string, error) {
lc, _ := FileLineCount(filename)
idx := 0
lines := make([]string, lc)
file, err := OpenFile(filename)
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines[idx] = strings.TrimSpace(scanner.Text())
idx++
}
if scanner.Err() != nil {
return nil, fmt.Errorf("ReadFromFile(): %s -> %w", filename, scanner.Err())
}
return lines, nil
}
// DeleteFile takes a path and deletes the file
func DeleteFile(path string) error {
err := os.Remove(path)
if err != nil {
return fmt.Errorf("os.Remove(): %s -> %w", path, err)
}
return nil
}
func FileToMap(pathname string) (map[string]struct{}, error) {
lines, err := ReadFromFile(pathname)
if err != nil {
return nil, err
}
m := make(map[string]struct{}, len(lines))
for _, l := range lines {
m[l] = struct{}{}
}
return m, nil
}
// FileLineCount returns the number of lines in a file
func FileLineCount(filename string) (int, error) {
file, err := OpenFile(filename)
if err != nil {
return 0, err
}
scanner := bufio.NewScanner(file)
count := 0
for scanner.Scan() {
count++
}
if scanner.Err() != nil {
return 0, fmt.Errorf("FileLineCount(): %s -> %w", filename, scanner.Err())
}
return count, nil
}
// RepeatLines repeats lines in a file
func RepeatLines(lines []string, times int) []string {
res := make([]string, len(lines)*times)
idx := 0
for i := 0; i < times; i++ {
for j := i + 1; j < len(lines); j++ {
res[idx] = lines[i]
idx++
}
}
return res
}
// GetDuplicateLines returns duplicate lines in a file
func GetDuplicateLines(lines []string) []string {
var duplicates []string
for i := 0; i < len(lines); i++ {
for j := i + 1; j < len(lines); j++ {
if lines[i] == lines[j] {
duplicates = append(duplicates, lines[i])
}
}
}
return duplicates
}
// WriteToFile writes a string to a file
func WriteToFile(lines []string, filename string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("os.Create(): %s -> %w", filename, err)
}
defer file.Close()
writer := bufio.NewWriter(file)
sb := &strings.Builder{}
for _, line := range lines {
sb.WriteString(line + "\n")
}
_, err = writer.WriteString(sb.String())
if err != nil {
return fmt.Errorf("writer.WriteString(): %s -> %w", filename, err)
}
if err = writer.Flush(); err != nil {
return fmt.Errorf("writer.Flush(): %s -> %w", filename, err)
}
return nil
}
// Shuffle shuffles a slice of strings
func Shuffle(lines []string) []string {
res := make([]string, len(lines))
perm := rand.Perm(len(lines))
for i, v := range perm {
res[v] = lines[i]
}
return res
}
// ReadFileToString reads a file into a string
func ReadFileToString(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", fmt.Errorf("os.Open(): %s -> %w", filename, err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return "", fmt.Errorf("file.Stat(): %s -> %w", filename, err)
}
bs := make([]byte, stat.Size())
_, err = file.Read(bs)
if err != nil {
return "", fmt.Errorf("file.Read(): %s -> %w", filename, err)
}
return string(bs), nil
}
// DoesFileExist checks if a file exists
func DoesFileExist(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// GetCurrentDir returns the current directory
func GetCurrentDir() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", errors.New("failed to get current directory")
}
return dir, nil
}