/
ledger.go
175 lines (156 loc) · 4.58 KB
/
ledger.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
//import "fmt"
import "time"
import "os"
import "bufio"
import "utf8"
import "unicode"
//import "strings"
import "strconv"
import "regexp"
type t_account struct {
name string // как в файле Траты:Вовка:Стрижка
lastname string // Стрижка
fullName string //Траты:Вовка:Стрижка:Валюта
valuta string // валюта
parent *t_account // выше по уровню
}
type t_ledger_item struct {
account *t_account
summa int64
}
type t_ledger_tran struct {
date time.Time
description string
items []*t_ledger_item
}
type t_ledger_file struct {
Tran []*t_ledger_tran
Accounts map[string] *t_account
curT *t_ledger_tran
curY int64
curM int
}
func (lf *t_ledger_file) findOrCreateAccount(s string) *t_account{
var acc *t_account
acc = lf.Accounts[s]
if acc!=nil {// нашли
return acc
}
r,err := regexp.Compile(":")
a := r.FindStringSubmatch(s)
println(a,err)
// создаем
acc = new(t_account)
return acc
}
func DecodeDate(s string, dt* time.Time) int { // cлайс на входе
S := utf8.NewString(s)
var err os.Error
if S.RuneCount()==2 { // тока день
dt.Day, err = strconv.Atoi(S.Slice(0,2))
if err==os.ERANGE {return(-1)}
return(0)
}
if S.RuneCount()==5 { // тока месяц день
dt.Day, err = strconv.Atoi(S.Slice(3,5))
if err==os.ERANGE {return(-1)}
dt.Month, err = strconv.Atoi(S.Slice(0,2))
if err==os.ERANGE {return(-1)}
return(0)
}
if S.RuneCount()==10 { // все
dt.Year, err = strconv.Atoi64(S.Slice(0,4))
if err==os.ERANGE {return(-1)}
dt.Month, err = strconv.Atoi(S.Slice(5,7))
if err==os.ERANGE {return(-1)}
dt.Day, err = strconv.Atoi(S.Slice(8,10))
if err==os.ERANGE {return(-1)}
return(0)
}
return(-10)
}
func (lf *t_ledger_file) ParseTran(s string) *t_ledger_tran{
lf.curT = new(t_ledger_tran)
lf.curT.items = make([]*t_ledger_item,0)
lf.curT.date.Year = lf.curY
lf.curT.date.Month = lf.curM
S := utf8.NewString(s)
var i int
for i=0; i<S.RuneCount(); i++{
if unicode.IsSpace(S.At(i)){ // конец даты
DecodeDate(S.Slice(0,i), &lf.curT.date)
break
}
}
lf.curT.description = S.Slice(i,S.RuneCount())
print("add tran: ", lf.curT.date.String() ,lf.curT.description)
lf.Tran = append(lf.Tran,lf.curT)
return(lf.curT)
}
func (lf *t_ledger_file) ParseAccount(s string) *t_account{
var acc *t_account // вычислить счет найти если нет создать
return (acc)
}
func ParseSumma(s string) int64{
S := utf8.NewString(s)
var i int
for i=0; i<S.RuneCount(); i++{
if unicode.IsSpace(S.At(i)){ // конец даты
//DecodeDate(S.Slice(0,i), &lf.curT.date)
break
}
}
return(0)
}
func (lf *t_ledger_file) ParseTranItem(s string) *t_ledger_item{
item := new(t_ledger_item)
lf.curT.items = append(lf.curT.items,item)
item.account = lf.ParseAccount(s) // чистый аккаунт сюда
item.summa = ParseSumma(s) // чистая сумма
return(item)
}
func ledgerParseFile(fileName string) *t_ledger_file {
f, err := os.Open(fileName, os.O_RDONLY, 0)
if err != nil {
return nil
}
defer f.Close()
lf := new(t_ledger_file)
lf.Tran = make([]*t_ledger_tran,0)
lf.Accounts = make(map[string]*t_account,0)
var s string
//var n int = 1
fr := bufio.NewReader(f)
s, err = fr.ReadString('\n')
for s!="" {
S := utf8.NewString(s)
if S.At(0)==int(';') {
println("// ",s)
}
if S.At(0)==int('Y') { // текущий год
lf.curY, err = strconv.Atoi64(S.Slice(1,5))
}
if S.At(0)==int('M') { // текущий месяц
lf.curM, err = strconv.Atoi(S.Slice(1,3))
}
if unicode.IsDigit(S.At(0)) { // tran begin
lf.ParseTran(s)
}
if unicode.IsSpace(S.At(0)) { // tran item
lf.ParseTranItem(s)
}
s, err = fr.ReadString('\n')
}
println(lf.Tran)
return lf
}
func main() {
ledgerParseFile("test.txt")
println("End")
s := "Ghbdtn dctv: n bgf n :juJприВет:34261"
// r,err := regexp.Compile(":([123456789]+)")
r,err := regexp.Compile(":([a-zA-Z0-9а-яА-Я]+)")
a := r.FindString(s)
println(a,err)
}