-
Notifications
You must be signed in to change notification settings - Fork 0
/
operations.go
69 lines (56 loc) · 1.43 KB
/
operations.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
package ergotool
import (
"bufio"
"fmt"
"os"
"sort"
)
func ReadDoc(filename string) (*Doc, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
doc, err := NewDoc(bufio.NewReader(f))
return doc, err
}
func WriteDoc(filename string, doc *Doc) error {
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(doc.String())
return err
}
// func BackupFile(filename string) error {
// // get the non-extension portion of the filename
// ext := filepath.Ext(filename)
// base := strings.TrimSuffix(filename, ext) + "." + time.Now().Format("2006-01-02 03:04:05PM") + ".bak" + ext
// // err := os.Rename(filename, filename+".bak")
// return nil
// }
func UpdateFootprintLocations(src *Doc, dest *Doc) error {
srcFootprints, err := src.EnumerateFootprints()
if err != nil {
return err
}
destFootprints, err := dest.EnumerateFootprints()
if err != nil {
return err
}
keys := make([]string, 0, len(destFootprints))
for k, _ := range destFootprints {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
destFp := destFootprints[k]
if srcFp, has := srcFootprints[destFp.Ref().Value()]; has {
destFp.UpdateLocation(srcFp.At().X(), srcFp.At().Y(), srcFp.At().R())
} else {
fmt.Printf("failed to update footprint '%s': not found in source", destFp.Ref().Value())
}
}
return nil
}