/
account.go
76 lines (63 loc) · 1.8 KB
/
account.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
package main
/*
#cgo LDFLAGS: -lktoblzcheck
#include <ktoblzcheck.h>
*/
import "C"
import (
"fmt"
"strconv"
)
type AccountNumberCheck struct {
ptr *C.struct_AccountNumberCheck
}
type AccountNumberResult int
type Record struct {
BankID string
Name string
Location string
}
type UnknownRecord string
func (f UnknownRecord) Error() string {
return fmt.Sprintf("unknown BLZ: %v", string(f))
}
func (check *AccountNumberCheck) FindBank(bankID string) (Record, error) {
bank := C.AccountNumberCheck_findBank(check.ptr, C.CString(bankID))
if bank == nil {
return Record{}, UnknownRecord(bankID)
}
var record = Record{
BankID: strconv.Itoa(int(C.AccountNumberCheck_Record_bankId(bank))),
}
if StringEncoding() == "UTF-8" {
record.Name = C.GoString(C.AccountNumberCheck_Record_bankName(bank))
record.Location = C.GoString(C.AccountNumberCheck_Record_location(bank))
} else {
record.Name = toUtf8([]byte(C.GoString(C.AccountNumberCheck_Record_bankName(bank))))
record.Location = toUtf8([]byte(C.GoString(C.AccountNumberCheck_Record_location(bank))))
}
return record, nil
}
func (check *AccountNumberCheck) Check(bankID string, accountID string) AccountNumberResult {
return AccountNumberResult(C.AccountNumberCheck_check(check.ptr, C.CString(bankID), C.CString(accountID)))
}
func StringEncoding() string {
return C.GoString(C.AccountNumberCheck_stringEncoding())
}
func NewAccountNumberCheck(dataDir string) AccountNumberCheck {
return AccountNumberCheck{
ptr: C.AccountNumberCheck_new_file(C.CString(dataDir)),
}
}
func NewDefaultAccountNumberCheck() AccountNumberCheck {
return AccountNumberCheck{
ptr: C.AccountNumberCheck_new(),
}
}
func toUtf8(iso8859Buf []byte) string {
buf := make([]rune, len(iso8859Buf))
for i, b := range iso8859Buf {
buf[i] = rune(b)
}
return string(buf)
}