-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.go
65 lines (54 loc) · 1.39 KB
/
io.go
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
package comicinfo
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"os"
)
// Read reads the ComicInfo spec from the specified reader
func Read(r io.Reader) (*ComicInfo, error) {
var ci ComicInfo
err := xml.NewDecoder(r).Decode(&ci)
if err != nil {
return nil, fmt.Errorf("error unmarshalling ComicInfo: %w", err)
}
return &ci, nil
}
// Write writes the ComicInfo spec to the specified writter
func Write(ci *ComicInfo, w io.Writer) error {
contents, err := xml.Marshal(ci)
if err != nil {
return fmt.Errorf("error marshalling ComicInfo: %w", err)
}
_, err = w.Write(bytes.Join([][]byte{xmlHeader, contents}, []byte("")))
if err != nil {
return fmt.Errorf("error writing ComicInfo to file: %w", err)
}
return nil
}
// Read reads the ComicInfo spec from the specified path.
func Open(path string) (*ComicInfo, error) {
f, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("error reading file: %w", err)
}
var ci ComicInfo
err = xml.Unmarshal(f, &ci)
if err != nil {
return nil, fmt.Errorf("error unmarshalling ComicInfo: %w", err)
}
return &ci, nil
}
// Save writes the ComicInfo spec to the specified path.
func Save(ci *ComicInfo, path string) error {
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("error creating file: %w", err)
}
err = Write(ci, f)
if err != nil {
return fmt.Errorf("error writing ComicInfo to file: %w", err)
}
return nil
}