2015-02-18 03:42:48 +00:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-02-18 05:08:26 +00:00
|
|
|
"fmt"
|
2015-02-18 03:42:48 +00:00
|
|
|
"bufio"
|
|
|
|
"strings"
|
2015-02-18 05:08:26 +00:00
|
|
|
"io/ioutil"
|
2015-02-18 03:42:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Row struct {
|
|
|
|
Data []string
|
|
|
|
}
|
|
|
|
|
2015-02-18 05:08:26 +00:00
|
|
|
func ReadData(path string) []byte {
|
|
|
|
d, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return []byte(``)
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2015-02-18 03:42:48 +00:00
|
|
|
func ReadRows(path, delimiter string) *[]Row {
|
|
|
|
f, _ := os.Open(path)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-02-18 05:08:26 +00:00
|
|
|
func WriteData(path string, data []byte) {
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE | os.O_RDWR | os.O_TRUNC, 0644)
|
2015-02-18 03:42:48 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2015-02-18 05:08:26 +00:00
|
|
|
_, err = f.Write(data)
|
2015-02-18 03:42:48 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WriteRows(path string, rows *[]Row, delimeter string) {
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE | os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2015-02-18 05:08:26 +00:00
|
|
|
for _, r := range *rows {
|
2015-02-18 03:42:48 +00:00
|
|
|
_, err = f.WriteString(strings.Join(r.Data, delimeter))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|