Adds untested file write and read
This commit is contained in:
parent
56e668c7c4
commit
63eb0df708
|
@ -0,0 +1,84 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
Sheet rune = 29
|
||||
Row rune = 30
|
||||
Field rune = 31
|
||||
End rune = 3
|
||||
)
|
||||
|
||||
func WriteFile(path string) {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
defer f.Close()
|
||||
f.WriteString("tss")
|
||||
for _, s := range wb.sheets {
|
||||
writeSheet(f, s)
|
||||
}
|
||||
f.WriteString(string(End))
|
||||
}
|
||||
|
||||
func writeSheet(f *os.File, sh sheet) {
|
||||
var o strings.Builder
|
||||
o.WriteRune(Sheet)
|
||||
|
||||
// Sheet header data
|
||||
fmt.Fprintf(&o, "%s%c%d%c%d%c%d", sh.name, Field, sh.cols, Field, sh.rows, Field, sh.zoom)
|
||||
for _, row := range sh.cells {
|
||||
o.WriteRune(Row)
|
||||
for c, cell := range row {
|
||||
o.WriteString(cell.rawVal)
|
||||
if c < len(row) - 1 {
|
||||
o.WriteRune(Field)
|
||||
}
|
||||
}
|
||||
}
|
||||
f.WriteString(o.String())
|
||||
}
|
||||
|
||||
func LoadFile(path string) {
|
||||
fbytes, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
fmt.Fprint(os.Stderr, "Unable to read file\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
f := string(fbytes)
|
||||
if !strings.HasPrefix(f, "tss") || !strings.HasSuffix(f, string(End)) {
|
||||
fmt.Fprint(os.Stderr, "Invalid file type\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
wb = makeWorkbook(path)
|
||||
sheets := strings.Split(f[3:len(f)-1], string(Sheet))
|
||||
for _, sh := range sheets {
|
||||
rows := strings.Split(sh, string(Row))
|
||||
s := makeSheet("")
|
||||
for i, row := range rows {
|
||||
fields := strings.Split(row, string(Field))
|
||||
if i == 0 {
|
||||
s.name = fields[0]
|
||||
s.cols, _ = strconv.Atoi(fields[1])
|
||||
s.rows, _ = strconv.Atoi(fields[2])
|
||||
s.zoom, _ = strconv.Atoi(fields[3])
|
||||
continue
|
||||
}
|
||||
s.cells = append(s.cells, make([]cell, 0, s.cols))
|
||||
for _, field := range fields {
|
||||
c := cell{}
|
||||
c.Update(field)
|
||||
s.cells[i-1] = append(s.cells[i-1], c)
|
||||
}
|
||||
}
|
||||
s.Recalculate()
|
||||
wb.sheets = append(wb.sheets, s)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue