-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.go
38 lines (34 loc) · 1.04 KB
/
lib.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
package fiftyonedegrees
/*
#include "51Degrees.h"
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
type FiftyoneDegrees struct {
dataSet *C.fiftyoneDegreesDataSet
}
func NewFiftyoneDegrees(fileName, properties string) (*FiftyoneDegrees, error) {
item := &FiftyoneDegrees{dataSet: new(C.fiftyoneDegreesDataSet)}
status := C.fiftyoneDegreesInitWithPropertyString(C.CString(fileName), item.dataSet, C.CString(properties))
if status != 0 {
return nil, errors.New(fmt.Sprintln("InitWithPropertyString Error,Status:", status))
}
return item, nil
}
func (this *FiftyoneDegrees) Close() {
C.fiftyoneDegreesDestroy(this.dataSet)
}
func (this *FiftyoneDegrees) Parse(userAgent string) string {
ws:=C.fiftyoneDegreesCreateWorkset(this.dataSet)
defer C.fiftyoneDegreesFreeWorkset(ws)
C.fiftyoneDegreesMatch(ws, C.CString(userAgent))
resultLength := 50000
buff := make([]byte, resultLength)
length := int32(C.fiftyoneDegreesProcessDeviceJSON(ws, (*C.char)(unsafe.Pointer(&buff[0])), C.int32_t(resultLength)))
result := buff[:length]
return string(result)
}