Skip to content

Commit

Permalink
Make key_file_parse_with_callback work
Browse files Browse the repository at this point in the history
  • Loading branch information
PucklaJ committed Nov 23, 2023
1 parent 1e4c770 commit d125f4b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
5 changes: 3 additions & 2 deletions dynareadout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,14 @@ func TestKeyFile(t *testing.T) {

func TestKeyFileParseWithCallback(t *testing.T) {
warn, err := KeyFileParseWithCallback("test_data/key_file.k",
func(fileName string, lineNumber int, keywordName string, card *Card, cardIndex int) {
func(info KeyParseInfo, keywordName string, card *Card, cardIndex int) {
var cardString string
if card != nil {
cardString = fmt.Sprint("Card: ", card.ParseWholeNoTrim(), " CardIndex: ", cardIndex)
}

fmt.Println("Filename:", fileName, "Line:", lineNumber, "Keyword:", keywordName, cardString)
fmt.Println("Filename:", info.FileName(), "Line:", info.LineNumber(), "Keyword:", keywordName, cardString)

}, DefaultKeyFileParseConfig())
assert.Nil(t, warn)
if !assert.Nil(t, err) {
Expand Down
5 changes: 2 additions & 3 deletions header.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#define DYNAREADOUT_HEADER_H
#include "dynareadout/src/key.h"

void keyFileParseGoCallback(char *fileName, size_t lineNumber,
char *keywordName, card_t *card, size_t cardIndex,
void *userData);
void keyFileParseGoCallback(key_parse_info_t info, char *keywordName,
card_t *card, size_t cardIndex, void *userData);
int get_errno();

#endif
63 changes: 56 additions & 7 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"errors"
"fmt"
"math"
"sync"
"unsafe"
)

Expand All @@ -38,7 +39,7 @@ type Card struct {
handle *C.card_t
}

type KeyFileParseCallback func(string, int, string, *Card, int)
type KeyFileParseCallback func(KeyParseInfo, string, *Card, int)

type KeyFileParseConfig struct {
ParseIncludes bool
Expand All @@ -49,6 +50,10 @@ type KeyFileWarning struct {
warningString string
}

type KeyParseInfo struct {
handle C.key_parse_info_t
}

func (w *KeyFileWarning) Error() string {
return w.warningString
}
Expand Down Expand Up @@ -311,6 +316,30 @@ func (c Card) ParseGetTypeWidth(valueWidth int) int {
return int(C.card_parse_get_type_width(c.handle, C.uint8_t(valueWidth)))
}

func (i KeyParseInfo) FileName() string {
return C.GoString(i.handle.file_name)
}

func (i KeyParseInfo) LineNumber() int {
return int(i.handle.line_number)
}

func (i KeyParseInfo) IncludePaths() []string {
rv := make([]string, i.handle.num_include_paths)
for j := C.size_t(0); j < i.handle.num_include_paths; j++ {
pathC := (*C.char)(unsafe.Add(unsafe.Pointer(i.handle.include_paths), uintptr(j)*unsafe.Sizeof(*i.handle.include_paths)))
rv = append(rv, C.GoString(pathC))
}
return rv
}

func (i KeyParseInfo) RootFolder() string {
return C.GoString(i.handle.root_folder)
}

var keyFileCallbacks map[uintptr]KeyFileParseCallback
var keyFileCallbacksMtx sync.Mutex

func KeyFileParseWithCallback(fileName string, callback KeyFileParseCallback, parseConfig KeyFileParseConfig) (*KeyFileWarning, error) {
fileNameC := C.CString(fileName)
var cParseConfig C.key_parse_config_t
Expand All @@ -329,12 +358,30 @@ func KeyFileParseWithCallback(fileName string, callback KeyFileParseCallback, pa
cParseConfig.ignore_not_found_includes = 0
}

keyFileCallbacksMtx.Lock()
if keyFileCallbacks == nil {
keyFileCallbacks = make(map[uintptr]KeyFileParseCallback)
}
var callbackIndex uintptr
for callbackIndex = 0; ; callbackIndex++ {
if _, ok := keyFileCallbacks[callbackIndex]; !ok {
break
}
}
keyFileCallbacks[callbackIndex] = callback
keyFileCallbacksMtx.Unlock()
defer func() {
keyFileCallbacksMtx.Lock()
defer keyFileCallbacksMtx.Unlock()
delete(keyFileCallbacks, callbackIndex)
}()

C.key_file_parse_with_callback(fileNameC,
C.key_file_callback(C.keyFileParseGoCallback),
&cParseConfig,
&errorString,
&warningString,
unsafe.Pointer(&callback),
unsafe.Pointer(callbackIndex),
nil,
)
C.free(unsafe.Pointer(fileNameC))
Expand All @@ -355,11 +402,13 @@ func KeyFileParseWithCallback(fileName string, callback KeyFileParseCallback, pa
}

//export keyFileParseGoCallback
func keyFileParseGoCallback(fileNameC *C.char, lineNumberC C.size_t, keywordNameC *C.char, cardC *C.card_t, cardIndexC C.size_t, userData unsafe.Pointer) {
callback := *(*KeyFileParseCallback)(userData)
func keyFileParseGoCallback(infoC C.key_parse_info_t, keywordNameC *C.char, cardC *C.card_t, cardIndexC C.size_t, userData unsafe.Pointer) {
callbackIndex := uintptr(userData)
keyFileCallbacksMtx.Lock()
defer keyFileCallbacksMtx.Unlock()
callback := keyFileCallbacks[callbackIndex]

fileName := C.GoString(fileNameC)
lineNumber := int(lineNumberC)
info := KeyParseInfo{infoC}
keywordName := C.GoString(keywordNameC)
var card *Card
if cardC != nil {
Expand All @@ -373,5 +422,5 @@ func keyFileParseGoCallback(fileNameC *C.char, lineNumberC C.size_t, keywordName
cardIndex = int(cardIndexC)
}

callback(fileName, lineNumber, keywordName, card, cardIndex)
callback(info, keywordName, card, cardIndex)
}

0 comments on commit d125f4b

Please sign in to comment.