-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread.go
42 lines (35 loc) · 875 Bytes
/
read.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
package stl
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
// From creates a Solid from the input.
// It handles both ASCII and binary formats.
func From(r io.Reader) (s Solid, err error) {
// Use a buffered reader. Default size is 4096 (4KB).
br := bufio.NewReader(r)
// Read first 6 bytes to get file type indicator.
indicator, err := br.Peek(6)
if err != nil {
return Solid{}, fmt.Errorf("input has no content")
}
// If indicator is "solid " then it is an ASCII file. Otherwise binary.
if string(indicator) == "solid " {
return fromASCII(br)
}
return fromBinary(br)
}
// FromFile creates a Solid from a file
// See stl.From for more info
func FromFile(filename string) (Solid, error) {
// Open file for reading
file, err := os.Open(strings.TrimSpace(filename))
if err != nil {
return Solid{}, err
}
defer file.Close()
return From(file)
}