Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,10 @@
|
|||
#include <stdio.h>
|
||||
#include "_cgo_export.h"
|
||||
|
||||
void Run()
|
||||
{
|
||||
char Buffer [1024];
|
||||
size_t Size = sizeof (Buffer);
|
||||
|
||||
if (0 == Query (Buffer, &Size))
|
||||
...
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package main
|
||||
|
||||
// #include <stdlib.h>
|
||||
// extern void Run();
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
func main() {
|
||||
C.Run()
|
||||
}
|
||||
|
||||
const msg = "Here am I"
|
||||
|
||||
//export Query
|
||||
func Query(cbuf *C.char, csiz *C.size_t) C.int {
|
||||
if int(*csiz) <= len(msg) {
|
||||
return 0
|
||||
}
|
||||
pbuf := uintptr(unsafe.Pointer(cbuf))
|
||||
for i := 0; i < len(msg); i++ {
|
||||
*((*byte)(unsafe.Pointer(pbuf))) = msg[i]
|
||||
pbuf++
|
||||
}
|
||||
*((*byte)(unsafe.Pointer(pbuf))) = 0
|
||||
*csiz = C.size_t(len(msg) + 1)
|
||||
return 1
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
// This buildmode requires the package to be main
|
||||
package main
|
||||
|
||||
// Import C so we can export the function to C and use C types
|
||||
|
||||
//#include <stdlib.h> // for size_t
|
||||
import "C"
|
||||
|
||||
// Import reflect and unsafe so we can wrap the C array in a Go slice
|
||||
import "reflect"
|
||||
import "unsafe"
|
||||
|
||||
// This buildmode also requires a main function, but it is never actually called
|
||||
func main() {}
|
||||
|
||||
// The message to copy into the buffer
|
||||
const msg = "Here am I"
|
||||
|
||||
// Here we declare the Query function using C types and export it to C
|
||||
|
||||
//export Query
|
||||
func Query(buffer *C.char, length *C.size_t) C.int {
|
||||
// Check there is enough space in the buffer
|
||||
if int(*length) < len(msg) {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Wrap the buffer in a slice to make it easier to copy into
|
||||
sliceHeader := reflect.SliceHeader {
|
||||
Data: uintptr(unsafe.Pointer(buffer)),
|
||||
Len: len(msg),
|
||||
Cap: len(msg),
|
||||
}
|
||||
bufferSlice := *(*[]byte)(unsafe.Pointer(&sliceHeader))
|
||||
|
||||
// Iterate through the message and copy it to the buffer, byte by byte
|
||||
for i:=0;i<len(msg);i++ {
|
||||
bufferSlice[i] = msg[i]
|
||||
}
|
||||
|
||||
// Set length to the amount of bytes we copied
|
||||
(*length) = C.size_t(len(msg))
|
||||
|
||||
return 1
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue