1
0
mirror of https://github.com/thebaer/tildes.git synced 2018-07-20 07:15:21 +00:00
tildes/store/store.go

72 lines
1.2 KiB
Go
Raw Normal View History

package store
import (
"bufio"
2015-02-24 00:06:28 +00:00
"fmt"
"io/ioutil"
2015-02-24 00:06:28 +00:00
"os"
"strings"
)
type Row struct {
Data []string
}
func ReadData(path string) []byte {
d, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
return []byte(``)
}
return d
}
func ReadRows(path, delimiter string) *[]Row {
f, _ := os.Open(path)
2015-02-24 00:06:28 +00:00
defer f.Close()
rows := []Row{}
s := bufio.NewScanner(f)
s.Split(bufio.ScanLines)
for s.Scan() {
data := strings.Split(s.Text(), delimiter)
r := &Row{Data: data}
rows = append(rows, *r)
}
return &rows
}
func WriteData(path string, data []byte) {
2015-02-24 00:06:28 +00:00
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
fmt.Println(err)
}
2015-02-24 00:06:28 +00:00
// TODO: check for Close() errors
// https://github.com/ncw/swift/blob/master/swift.go#L170
defer f.Close()
_, err = f.Write(data)
if err != nil {
fmt.Println(err)
}
}
func WriteRows(path string, rows *[]Row, delimeter string) {
2015-02-24 00:06:28 +00:00
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
fmt.Println(err)
}
defer f.Close()
for _, r := range *rows {
_, err = f.WriteString(strings.Join(r.Data, delimeter))
if err != nil {
fmt.Println(err)
}
}
}