1package appdata
 2
 3import (
 4	"errors"
 5	"fmt"
 6	"io/ioutil"
 7	"log"
 8	"os"
 9	"strconv"
10)
11
12var DataPath string
13
14func checkError(error error) {
15	if error != nil {
16		log.Fatal(error)
17	}
18}
19
20func SaveBookData(bookHash string, bookPosition int) {
21	_ = os.Remove(DataPath + bookHash)
22	f, err := os.Create(DataPath + bookHash)
23	checkError(err)
24	defer f.Close()
25	f.WriteString(fmt.Sprintf("%d", bookPosition))
26	fmt.Printf("Saving current position : %d\n", bookPosition)
27}
28
29func ReadBookData(bookHash string) int {
30	if _, err := os.Stat(DataPath + bookHash); errors.Is(err, os.ErrNotExist) {
31		SaveBookData(bookHash, 0)
32		return 0
33	}
34	content, err := ioutil.ReadFile(DataPath + bookHash)
35	checkError(err)
36	i, err := strconv.Atoi(string(content))
37	checkError(err)
38	return i
39}