From 06176f4900be33f034aba95e90966a62c2081d29 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sat, 24 Feb 2024 17:32:42 +0900 Subject: [PATCH] resolved the issue that go wrapper code stores the file name path in memory allocated with C.malloc --- go/cb.go | 43 +++++++++++++++---------------------------- go/hcl.go | 16 ++++++++-------- 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/go/cb.go b/go/cb.go index 5bd468a..7c8221b 100644 --- a/go/cb.go +++ b/go/cb.go @@ -10,7 +10,6 @@ import "C" import ( "bufio" - "fmt" "io" "os" "path" @@ -95,53 +94,48 @@ func hcl_go_cci_handler(c *C.hcl_t, cmd C.hcl_io_cmd_t, arg unsafe.Pointer) C.in var ( ioarg *C.hcl_io_cciarg_t name string - includer_name string fd int tptr unsafe.Pointer tlen C.size_t ) ioarg = (*C.hcl_io_cciarg_t)(arg) - if ioarg.name == nil { // main stream when it's not feed based. - name = "" - } else { - var k []rune = ucstr_to_rune_slice(ioarg.name) - name = string(k) - } if ioarg.includer == nil /* || ioarg.includer.name == nil */ { - includer_name = g.io.cci_main - name = "" + // main stream + name = g.io.cci_main } else { + // actual included stream + var includer_name string + var k []rune = ucstr_to_rune_slice(ioarg.name) + + name = string(k) + //var k []rune = ucstr_to_rune_slice(ioarg.includer.name) //var k []rune = ucstr_to_rune_slice(ioarg.includer.handle.remembered_path) tptr = ioarg.includer.handle tlen = *(*C.size_t)(unsafe.Pointer(uintptr(tptr) + unsafe.Sizeof(fd))) includer_name = C.GoStringN((*C.char)(unsafe.Pointer(uintptr(tptr)+unsafe.Sizeof(fd)+unsafe.Sizeof(tlen))), C.int(tlen)) - - fmt.Printf("xname.... [%s] ccimain [%s] [%s]\n", name, g.io.cci_main, includer_name) name = filepath.Join(path.Dir(includer_name), name) - fmt.Printf("name.... [%s]\n", name) } - // len(name) is the number of bytes in the string - tlen = C.size_t(len(name)) + tlen = C.size_t(len(name)) // number of bytes in the string tptr = C.malloc(C.size_t(unsafe.Sizeof(fd)) + C.size_t(unsafe.Sizeof(tlen)) + tlen) if tptr == nil { g.set_errmsg(C.HCL_ESYSMEM, "memory allocation failure for cci name") return -1 } - if name != "" { - fd, err = g.io.cci.Open(g, name, "") + if ioarg.includer == nil { + fd = -1 + } else { + fd, err = g.io.cci.Open(g, name) if err != nil { g.set_errmsg(C.HCL_EIOERR, err.Error()) C.free(tptr) return -1 } - } else { - fd = -1 } C.memcpy(tptr, unsafe.Pointer(&fd), C.size_t(unsafe.Sizeof(fd))) @@ -335,7 +329,7 @@ type CciFileHandler struct { g *HCL } -func (p *CciFileHandler) Open(g *HCL, name string, includer_name string) (int, error) { +func (p *CciFileHandler) Open(g *HCL, name string) (int, error) { var ( f *os.File r *bufio.Reader @@ -346,14 +340,7 @@ func (p *CciFileHandler) Open(g *HCL, name string, includer_name string) (int, e if name == "" { f = os.Stdin } else { - var dir string - - dir = path.Dir(includer_name) - if dir != "/" && dir != "" { - dir = dir + "/" - } - - f, err = os.Open(dir + name) + f, err = os.Open(name) if err != nil { return -1, err } diff --git a/go/hcl.go b/go/hcl.go index f4d467a..56072a1 100644 --- a/go/hcl.go +++ b/go/hcl.go @@ -5,17 +5,17 @@ package hcl #include #include // for C.free -extern int hcl_go_cci_handler (hcl_t hcl, hcl_io_cmd_t cmd, void* arg); -extern int hcl_go_udi_handler (hcl_t hcl, hcl_io_cmd_t cmd, void* arg); -extern int hcl_go_udo_handler (hcl_t hcl, hcl_io_cmd_t cmd, void* arg); +extern int hcl_go_cci_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg); +extern int hcl_go_udi_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg); +extern int hcl_go_udo_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg); -int hcl_cci_Handler_for_go (hcl_t hcl, hcl_io_cmd_t cmd, void* arg) { +int hcl_cci_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) { return hcl_go_cci_handler(hcl, cmd, arg); } -int hcl_udi_handler_for_go (hcl_t hcl, hcl_io_cmd_t cmd, void* arg) { +int hcl_udi_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) { return hcl_go_udi_handler(hcl, cmd, arg); } -int hcl_udo_handler_for_go (hcl_t hcl, hcl_io_cmd_t cmd, void* arg) { +int hcl_udo_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) { return hcl_go_udo_handler(hcl, cmd, arg); } */ @@ -31,7 +31,7 @@ import ( ) type CciImpl interface { - Open(g *HCL, name string, includer_name string) (int, error) + Open(g *HCL, name string) (int, error) Close(fd int) Read(fd int, buf []rune) (int, error) } @@ -221,7 +221,7 @@ func (hcl *HCL) AttachCCIO(cci CciImpl, main_cci_name string) error { hcl.io.cci = cci hcl.io.cci_main = main_cci_name - x = C.hcl_attachccio(hcl.c, C.hcl_io_impl_t(C.hcl_cci_Handler_for_go)) + x = C.hcl_attachccio(hcl.c, C.hcl_io_impl_t(C.hcl_cci_handler_for_go)) if x <= -1 { // restore the io handler set due to attachment failure hcl.io.cci_main = old_cci_name