-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
204 lines (158 loc) · 4.47 KB
/
Copy pathencode.go
File metadata and controls
204 lines (158 loc) · 4.47 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
)
// This is the format of a journal currently
type Journal struct {
Entries []string
Titles []string
Theme string
}
// This is used to change a journal entry, It can also be used to write titles
func changeJournal(journalName string, entry string, text string, title bool) error {
dat, err := getJournalRaw(journalName)
if err != nil {
return err
}
var j Journal
err = json.Unmarshal(dat, &j)
if err != nil {
fmt.Println("Error unmarshaling json")
return err
}
i, err := strconv.Atoi(entry)
i--
if i > 2499 || i < 0 {
fmt.Println("Wrote outside journal bounds")
return nil
}
if err != nil {
fmt.Println("Error converting number from string to integer")
return err
}
// Make sure that the array does not access out of range but doesn't create
// too many entries or titles
length := i + 1
if title {
if len(j.Titles) > length {
length = len(j.Titles)
}
titlearr := make([]string, length)
copy(titlearr, j.Titles)
titlearr[i] = text
j.Titles = titlearr
jsonvalue, _ := json.Marshal(j)
writeJournalRaw(journalName, jsonvalue)
return nil
}
if len(j.Entries) > length {
length = len(j.Entries)
}
entryarr := make([]string, length)
copy(entryarr, j.Entries)
entryarr[i] = text
j.Entries = entryarr
jsonvalue, _ := json.Marshal(j)
writeJournalRaw(journalName, jsonvalue)
return nil
}
// This will create a new journal
func newJournal(jorunalName string) error {
f, err := os.Create("./entries/" + jorunalName + ".json")
defer f.Close()
if err != nil {
fmt.Println("error creating file (" + jorunalName + ".json)")
return err;
}
var arr []string
j := Journal{arr, arr, "normal"}
jsondata, _ := json.Marshal(j)
ioutil.WriteFile("./entries/" + jorunalName + ".json", jsondata, 0777)
return nil
}
// Read an entry from a journal (Can also read titles if the title bit is set)
func readJournal(journalName string, entryNumber string, title bool) (string, error) {
dat, err := getJournalRaw(journalName)
if err != nil {
fmt.Println("Couldn't open a journal for reading")
return "Couldn't open journal", err
}
var j Journal
err = json.Unmarshal(dat, &j)
if err != nil {
fmt.Println("Couldn't convert JSON")
return "Couldn't convert json", err
}
i, err := strconv.Atoi(entryNumber)
i--
if err != nil {
fmt.Println("Entry number was invalid")
return "Entry number invalid", err
}
if i > 2499 {
return "Error reading journal entry", nil
}
// Send the user a generic title if the one they requested is blank
if len(j.Titles) - 1 < i && title {
if i == 0 {
changeJournal(journalName, entryNumber, "My Journal", true)
return "My Journal", nil
}
changeJournal(journalName, entryNumber, "Entry title", true)
return "Entry title", nil
}
// Send the user a generic entry if the one they requested is blank
if len(j.Entries) - 1 < i && !title {
if i == 0 {
// However, if it's also the first of the journla, read from a file.
// (This file _SHOULD_ contain a welcome message)
dat, _ := ioutil.ReadFile("./public/introtext.txt")
changeJournal(journalName, entryNumber, string(dat), false)
return string(dat), nil
}
changeJournal(journalName, entryNumber, "New planner entry", false)
return "New planner entry", nil
}
if title {
return j.Titles[i], nil
}
return j.Entries[i], nil
}
// Read a journal's theme really easily
func readTheme(journalName string) (string, error) {
dat, err := getJournalRaw(journalName)
if err != nil {
fmt.Println("Couldn't read a json file")
return "", err
}
var j Journal
err = json.Unmarshal(dat, &j)
if err != nil {
fmt.Println("Couldn't unmarshal data (" + journalName + ".json)")
}
return j.Theme, nil
}
// Set a journal's theme
func setTheme(journalName string, theme string) error {
dat, err := getJournalRaw(journalName)
if err != nil {
fmt.Println("Couldn't read a json file")
return err
}
// Unmarshal the JSON
var j Journal
err = json.Unmarshal(dat, &j)
if err != nil {
fmt.Println("Couldn't unmarshal data (" + journalName + ".json)")
}
j.Theme = theme
jsondata, _ := json.Marshal(j)
// Errors aren't handled here because we were able to read from the same file.
// If some error occurs, the theme won't be set.
writeJournalRaw(journalName, jsondata)
return nil
}