-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
154 lines (133 loc) · 2.88 KB
/
utils.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
153
154
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/macdylan/SMFix/fix"
)
type empty struct{}
func humanReadableSize(size int64) string {
const unit = 1024
if size < unit {
return fmt.Sprintf("%d B", size)
}
div, exp := int64(unit), 0
for n := size / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(size)/float64(div), "KMGTPE"[exp])
}
var reFilename = regexp.MustCompile(`^[\.\/\\~]+`)
func normalizedFilename(filename string) string {
return reFilename.ReplaceAllString(filename, "")
}
/*
func postProcessFile(file_path string) (out []byte, err error) {
var r *os.File
if r, err = os.Open(file_path); err != nil {
return
}
defer r.Close()
return postProcess(r)
}
*/
func postProcess(r io.Reader) (out []byte, err error) {
var (
isFixed = false
nl = []byte("\n")
headers = [][]byte{}
gcodes = []*fix.GcodeBlock{}
sc = bufio.NewScanner(r)
)
for sc.Scan() {
line := sc.Text()
if !isFixed && strings.HasPrefix(line, "; Postprocessed by smfix") {
isFixed = true
}
g, err := fix.ParseGcodeBlock(line)
if err == nil {
if g.Is("G4") {
var s int
if err := g.GetParam('S', &s); err == nil && s == 0 {
continue
}
}
gcodes = append(gcodes, g)
continue
}
if err != fix.ErrEmptyString {
return nil, err
}
}
if !isFixed {
funcs := []fix.GcodeModifier{}
if !noTrim {
// funcs = append(funcs, fix.GcodeTrimLines)
}
if !noShutoff {
funcs = append(funcs, fix.GcodeFixShutoff)
}
// if !noPreheat {
// funcs = append(funcs, fix.GcodeFixPreheat)
// }
if !noReplaceTool {
funcs = append(funcs, fix.GcodeReplaceToolNum)
}
// if !noReinforceTower {
// funcs = append(funcs, fix.GcodeReinforceTower)
// }
funcs = append(funcs, fix.GcodeFixOrcaToolUnload)
for _, fn := range funcs {
gcodes = fn(gcodes)
}
if headers, err = fix.ExtractHeader(gcodes); err != nil {
return nil, err
}
}
var buf bytes.Buffer
for _, h := range headers {
buf.Write(h)
buf.Write(nl)
}
for _, gcode := range gcodes {
buf.WriteString(gcode.String())
buf.Write(nl)
}
return buf.Bytes(), nil
}
func shouldBeFix(fpath string) bool {
ext := strings.ToLower(filepath.Ext(fpath))
return SmFixExtensions[ext]
}
func parseIntEnv(key string, defaultValue int) int {
if value, ok := os.LookupEnv(key); ok {
if v, err := strconv.Atoi(value); err == nil {
return v
}
}
return defaultValue
}
func parseBoolEnv(key string, defaultValue bool) bool {
if value, ok := os.LookupEnv(key); ok {
if v, err := strconv.ParseBool(value); err == nil {
return v
}
}
return defaultValue
}
func parseDurationEnv(key string, defaultValue time.Duration) time.Duration {
if value, ok := os.LookupEnv(key); ok {
if v, err := time.ParseDuration(value); err == nil {
return v
}
}
return defaultValue
}