-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvcell.go
More file actions
153 lines (116 loc) · 4.03 KB
/
csvcell.go
File metadata and controls
153 lines (116 loc) · 4.03 KB
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
/*
MIT License
Copyright (c) 2025 Elliot Michael Keavney
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package csvcell
import (
"bufio"
"log"
"os"
"strings"
)
// Function to read a CSV file
func ReadCSV(rootDirPath string, fileName string) (result [][18278]string) {
// Go introduced OpenRoot in version 1.24, it restricts file operations to a single directory
rootDir, err := os.OpenRoot(rootDirPath)
if err != nil {
panic("Directory path does not exist")
}
defer rootDir.Close()
// Open the file
file, err := rootDir.Open(fileName)
if err != nil {
log.Fatal("File " + fileName + " cannot be opened or does not exist")
}
defer file.Close()
scanner := bufio.NewScanner(file)
// For loop to scan lines in the file and add to a slice
for scanner.Scan() {
line := scanner.Text()
// Create a slice and split each line into an index based on the , delimiter
var splitSlice = []string{}
splitSlice = strings.Split(line, ",")
// Convert splitSlice into an array named splitArray
splitArray := [18278]string{}
copy(splitArray[:], splitSlice)
// Append to array
result = append(result, splitArray)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return result
}
// Function to write to a CSV file
func WriteCSV(rootDirPath string, fileName string, commaPrepend int, csvRow string, commaAppend int) {
// Go introduced OpenRoot in version 1.24, it restricts file operations to a single directory
rootDir, err := os.OpenRoot(rootDirPath)
if err != nil {
panic("Directory path does not exist")
}
defer rootDir.Close()
// Open/create the file
file, err := rootDir.OpenFile(fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal("File " + fileName + " cannot be opened or does not exist")
}
defer file.Close()
// Write the string to the file and add optional commas at the beginning or end of the row
_, err = file.WriteString(strings.Repeat(",", commaPrepend) + csvRow + strings.Repeat(",", commaAppend) + "\n")
if err != nil {
panic(err)
}
}
// Function to retrieve all contents from a plain text file
func FileData(rootDirPath string, fileName string) (result string) {
// Go introduced OpenRoot in version 1.24, it restricts file operations to a single directory
rootDir, err := os.OpenRoot(rootDirPath)
if err != nil {
panic("Directory path does not exist")
}
defer rootDir.Close()
file, err := rootDir.Open(fileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
var fileSlice []string
for scanner.Scan() {
line := scanner.Text()
fileSlice = append(fileSlice, line)
}
result = strings.Join(fileSlice, "")
return result
}
// Function to retrieve all file and directory names from a specific directory
func DirList(rootDirPath string) (result []string) {
rootDir, err := os.Open(rootDirPath)
if err != nil {
panic("Directory path does not exist")
}
defer rootDir.Close()
fileList, err := rootDir.ReadDir(-1)
if err != nil {
log.Fatal(err)
}
for _, fileListLoop := range fileList {
result = append(result, fileListLoop.Name())
}
return result
}