-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloaded_text_files.go
121 lines (110 loc) · 2.91 KB
/
loaded_text_files.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
// -----------------------------------------------------------------------------
// CMDX Utilities Suite cmdx/[loaded_text_files.go]
// (c) balarabe@protonmail.com License: GPLv3
// -----------------------------------------------------------------------------
package main
import (
"os"
"sort"
"sync"
"github.com/balacode/zr"
)
// LoadedTextFiles _ _
type LoadedTextFiles struct {
m map[string]*TextFile
}
// GetFile returns the named file stored in LoadedTextFiles
func (ob *LoadedTextFiles) GetFile(filename string) *TextFile {
if ob.m == nil {
env.Error("No loaded files.")
return &TextFile{}
}
return ob.m[filename]
}
// GetAllFilenames returns a list of all loaded file names
func (ob *LoadedTextFiles) GetAllFilenames() []string {
if ob.m == nil {
env.Error("No loaded files.")
return []string{}
}
var ret []string
for key := range ob.m {
ret = append(ret, key)
}
return ret
}
// LoadAll loads all text files in 'path' into a map in memory.
// On repeated calls it reloads only files that changed after the last call.
// Returns a list of added or changed files (but not deleted files).
func (ob *LoadedTextFiles) LoadAll(
path string,
fsMx *sync.RWMutex,
) (
changedFiles []string,
) {
exts := append(env.TextFileExts(), ExtraTextFileExts...)
paths := env.GetFilePaths(path, exts...)
for _, filename := range paths {
_, changed := ob.LoadFile(filename, fsMx)
if changed {
changedFiles = append(changedFiles, filename)
}
}
ob.SortListByModTime(changedFiles)
return changedFiles
}
// LoadFile reads a file into the map of loaded files.
// The contents of the file are retained in memory.
func (ob *LoadedTextFiles) LoadFile(
filename string,
fsMx *sync.RWMutex,
) (
file *TextFile,
changed bool,
) {
fsMx.Lock()
defer fsMx.Unlock()
if ob.m == nil {
ob.m = make(map[string]*TextFile, 1000)
}
var inMap bool
file, inMap = ob.m[filename]
if !inMap {
file = &TextFile{Path: filename}
ob.m[filename] = file
}
if !env.FileExists(filename) {
delete(ob.m, filename)
return nil, false
}
info, err := os.Stat(filename)
if err != nil {
zr.Error("Stat failed on file", filename)
delete(ob.m, filename)
return nil, false
}
if file.Size == info.Size() && file.ModTime == info.ModTime() {
return file, false
}
file.Size = info.Size()
file.ModTime = info.ModTime()
file.Lines = env.ReadFileLines(file.Path)
return file, true
}
// SortListByModTime sorts a list of file names by time modified.
// 'modFiles' is the slice of file names being sorted.
func (ob *LoadedTextFiles) SortListByModTime(modFiles []string) {
sort.Slice(modFiles, func(i, j int) bool {
a := ob.m[modFiles[i]]
b := ob.m[modFiles[j]]
//
// this should never occur
if a == nil || b == nil {
zr.Error(zr.ENil)
return true // push front
}
return a.ModTime.After(b.ModTime)
})
}
// TODO: GLOBAL: All doc. comments should not exceed 76 columns
// end