forked from bensema/gotdx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblock.go
More file actions
119 lines (103 loc) · 2.86 KB
/
Copy pathblock.go
File metadata and controls
119 lines (103 loc) · 2.86 KB
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
package gotdx
import (
"fmt"
"github.com/bensema/gotdx/proto"
)
type BlockFlatItem struct {
BlockName string
BlockType uint16
CodeIndex int
Code string
}
type BlockGroup struct {
BlockName string
BlockType uint16
StockCount int
Codes []string
}
func ParseBlockFlat(data []byte) ([]BlockFlatItem, error) {
if len(data) < 386 {
return nil, fmt.Errorf("invalid block data length: %d", len(data))
}
pos := 384
total := int(uint16(data[pos]) | uint16(data[pos+1])<<8)
pos += 2
result := make([]BlockFlatItem, 0, total)
for i := 0; i < total; i++ {
if pos+13 > len(data) {
return nil, fmt.Errorf("invalid block header %d", i)
}
blockName := proto.Utf8ToGbk(data[pos : pos+9])
pos += 9
stockCount := int(uint16(data[pos]) | uint16(data[pos+1])<<8)
blockType := uint16(data[pos+2]) | uint16(data[pos+3])<<8
pos += 4
blockStockBegin := pos
for codeIndex := 0; codeIndex < stockCount; codeIndex++ {
if pos+7 > len(data) {
return nil, fmt.Errorf("invalid block code %d:%d", i, codeIndex)
}
result = append(result, BlockFlatItem{
BlockName: blockName,
BlockType: blockType,
CodeIndex: codeIndex,
Code: proto.Utf8ToGbk(data[pos : pos+7]),
})
pos += 7
}
pos = blockStockBegin + 2800
}
return result, nil
}
func ParseBlockGroups(data []byte) ([]BlockGroup, error) {
if len(data) < 386 {
return nil, fmt.Errorf("invalid block data length: %d", len(data))
}
pos := 384
total := int(uint16(data[pos]) | uint16(data[pos+1])<<8)
pos += 2
result := make([]BlockGroup, 0, total)
for i := 0; i < total; i++ {
if pos+13 > len(data) {
return nil, fmt.Errorf("invalid block header %d", i)
}
blockName := proto.Utf8ToGbk(data[pos : pos+9])
pos += 9
stockCount := int(uint16(data[pos]) | uint16(data[pos+1])<<8)
blockType := uint16(data[pos+2]) | uint16(data[pos+3])<<8
pos += 4
blockStockBegin := pos
group := BlockGroup{
BlockName: blockName,
BlockType: blockType,
StockCount: stockCount,
Codes: make([]string, 0, stockCount),
}
for codeIndex := 0; codeIndex < stockCount; codeIndex++ {
if pos+7 > len(data) {
return nil, fmt.Errorf("invalid block code %d:%d", i, codeIndex)
}
group.Codes = append(group.Codes, proto.Utf8ToGbk(data[pos:pos+7]))
pos += 7
}
result = append(result, group)
pos = blockStockBegin + 2800
}
return result, nil
}
// GetParsedBlockFile 获取并解析板块文件
func (client *Client) GetParsedBlockFile(filename string) ([]BlockFlatItem, error) {
content, err := client.GetBlockFile(filename)
if err != nil {
return nil, err
}
return ParseBlockFlat(content)
}
// GetGroupedBlockFile 获取并按板块分组解析板块文件
func (client *Client) GetGroupedBlockFile(filename string) ([]BlockGroup, error) {
content, err := client.GetBlockFile(filename)
if err != nil {
return nil, err
}
return ParseBlockGroups(content)
}