forked from ezrec/uv3dp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
152 lines (124 loc) · 2.7 KB
/
format.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
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
//
// Copyright (c) 2020 Jason S. McMullan <jason.mcmullan@gmail.com>
//
package uv3dp
import (
"fmt"
"io"
"os"
"sort"
"strings"
)
// Reader needs io.ReaderAt for archive/zip
type Reader interface {
io.Reader
io.ReaderAt
}
// Writer
type Writer interface {
io.Writer
}
// Printable file format
type Formatter interface {
Parse(args []string) (err error)
Parsed() bool
Args() (args []string)
NArg() int
PrintDefaults()
Decode(reader Reader, size int64) (printable Printable, err error)
Encode(writer Writer, printable Printable) (err error)
}
// Printable to file format
type NewFormatter func(suffix string) (formatter Formatter)
var formatterMap map[string]NewFormatter
func RegisterFormatter(suffix string, newFormatter NewFormatter) {
if formatterMap == nil {
formatterMap = make(map[string]NewFormatter)
}
formatterMap[suffix] = newFormatter
}
func FormatterUsage() {
if formatterMap != nil {
list := []string{}
for suffix := range formatterMap {
list = append(list, suffix)
}
sort.Strings(list)
for _, suffix := range list {
newFormatter := formatterMap[suffix]
fmt.Fprintln(os.Stderr)
fmt.Fprintf(os.Stderr, "Options for '%s':\n", suffix)
fmt.Fprintln(os.Stderr)
newFormatter(suffix).PrintDefaults()
}
}
}
type Format struct {
Formatter
Suffix string
Filename string
}
func NewFormat(filename string, args []string) (format *Format, err error) {
var formatter Formatter
var suffix string
var newFormatter NewFormatter
for suffix, newFormatter = range formatterMap {
if strings.HasSuffix(filename, suffix) {
// Get formatter, and parse arguments
formatter = newFormatter(suffix)
break
}
}
if formatter == nil {
err = fmt.Errorf("%s: File extension unknown", filename)
return
}
err = formatter.Parse(args)
if err != nil {
return
}
format = &Format{
Formatter: formatter,
Suffix: suffix,
Filename: filename,
}
return
}
func (format *Format) Printable() (printable Printable, err error) {
var reader *os.File
var filesize int64
if format.Suffix != "empty" {
reader, err = os.Open(format.Filename)
if err != nil {
return
}
defer func() { reader.Close() }()
filesize, err = reader.Seek(0, io.SeekEnd)
if err != nil {
return
}
_, err = reader.Seek(0, io.SeekStart)
if err != nil {
return
}
}
decoded, err := format.Decode(reader, filesize)
if err != nil {
return
}
printable = decoded
return
}
// Write writes a printable to the file format
func (format *Format) SetPrintable(printable Printable) (err error) {
writer, err := os.Create(format.Filename)
if err != nil {
return
}
defer func() { writer.Close() }()
err = format.Encode(writer, printable)
if err != nil {
return
}
return
}