Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bff7a6d

Browse files
committed
First commit
0 parents commit bff7a6d

File tree

9 files changed

+126
-0
lines changed

9 files changed

+126
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.idea

‎main.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"texttree/v1"
6+
)
7+
8+
func main() {
9+
tt, err := texttree.NewTextTree("sample", texttree.DefaultMaxFileSize)
10+
if err != nil {
11+
panic(err)
12+
}
13+
fmt.Println(tt.Entities())
14+
fmt.Println(tt.GetString("sample/a/hello"))
15+
fmt.Println(tt.GetString("sample/b/c/quack"))
16+
}

‎sample/a/hello.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello

‎sample/a/world.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
World

‎sample/b/c/quack‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
QUAACKKKK

‎sample/b/hello.html‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
olleH

‎sample/b/hello.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
H E L L O

‎sample/b/world.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dlorW

‎v1/loader.go‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package texttree
2+
3+
// AUTHOR: Daniil Furmanov
4+
// LICENSE: MIT
5+
// DESCRIPTION: TextTree is a file buffer that stores files content
6+
// in memory and allow access to it by path.
7+
// It is useful for working with localization trees.
8+
9+
import (
10+
"io/ioutil"
11+
"os"
12+
"path/filepath"
13+
)
14+
15+
const DefaultMaxFileSize = 16 << 10 // 16 KB
16+
17+
type TextTree struct {
18+
cache map[string]*Entity
19+
basePath string
20+
}
21+
22+
type Entity struct {
23+
Content string
24+
Filename string
25+
}
26+
27+
// Creates a new text tree by loading all the files from a specified directory
28+
// that are smaller than maximum file size
29+
func NewTextTree(path string, maxFileSize int64) (*TextTree, error) {
30+
cache := make(map[string]*Entity)
31+
// recursively walk through the path
32+
err := filepath.Walk(path,
33+
func(path string, info os.FileInfo, err error) error {
34+
if err != nil {
35+
return err
36+
}
37+
// check the file size
38+
if !info.IsDir() && info.Size() < maxFileSize {
39+
data, err := ioutil.ReadFile(path)
40+
if err != nil {
41+
return err
42+
}
43+
newEntity := &Entity{Content: string(data), Filename: info.Name()}
44+
dir, filename := filepath.Split(path)
45+
key := dir + filename[0:len(filename)-len(filepath.Ext(filename))]
46+
if entity, ok := cache[key]; ok {
47+
// if entity already exists, keep the key extension
48+
delete(cache, key)
49+
cache[path] = newEntity
50+
cache[dir+entity.Filename] = entity
51+
} else {
52+
cache[key] = newEntity
53+
}
54+
}
55+
return nil
56+
})
57+
if err != nil {
58+
return nil, err
59+
}
60+
// create a text tree object with buffered data
61+
return &TextTree{
62+
cache: cache,
63+
basePath: path,
64+
}, nil
65+
}
66+
67+
// Creates an array of file paths
68+
func (tt *TextTree) Entities() (entities []string) {
69+
entities = make([]string, len(tt.cache))
70+
i := 0
71+
for key := range tt.cache {
72+
entities[i] = key
73+
i++
74+
}
75+
return
76+
}
77+
78+
// Returns an entity
79+
func (tt *TextTree) Get(path string) *Entity {
80+
return tt.cache[path]
81+
}
82+
83+
// Returns an entity if it exists
84+
func (tt *TextTree) GetIfExists(path string) (*Entity, bool) {
85+
e, ok := tt.cache[path]
86+
return e, ok
87+
}
88+
89+
// Returns an entity's content
90+
func (tt *TextTree) GetString(path string) string {
91+
return tt.cache[path].Content
92+
}
93+
94+
// Returns an entity's content if it exists
95+
func (tt *TextTree) GetStringIfExists(path string) (string, bool) {
96+
e, ok := tt.cache[path]
97+
return e.Content, ok
98+
}
99+
100+
// Returns the path of a loaded directory
101+
func (tt *TextTree) GetBasePath() string {
102+
return tt.basePath
103+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /