-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (50 loc) · 1 KB
/
main.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
package main
/*
#include <stdlib.h>
*/
import "C"
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"github.com/erh/gonmea/analyzer"
)
//export ParseData
func ParseData(byteArr []byte) *C.char {
fmt.Println(string(byteArr))
in := io.NopCloser(bytes.NewReader(byteArr))
defer in.Close()
parser, err := analyzer.NewParser()
if err != nil {
return C.CString(fmt.Sprintf("Error: %v", err))
}
reader := bufio.NewReader(in)
for {
line, _, err := reader.ReadLine()
if err != nil {
if errors.Is(err, io.EOF) {
return nil
}
return C.CString(fmt.Sprintf("Error: %v", err))
}
line = []byte(strings.TrimSpace(string(line)))
if len(line) == 0 {
continue
}
msg, err := parser.ParseMessage(line)
if err != nil {
return C.CString(fmt.Sprintf("Error: %v", err))
}
md, err := json.MarshalIndent(msg, "", " ")
if err != nil {
return C.CString(fmt.Sprintf("Error: %v", err))
}
fmt.Println(string(md))
return C.CString(string(md))
}
}
func main() {}