Fixes of STRING/BYTES LENGTH typemaps.

Fix Java STRING LENGTH typemap.
Use string type in static typed languages (Java, C#, D and Go).

Add BYTES LENGTH typemap and apply it for binary data.
Use byte type in static typed languages.

Add li_cdata_cpp, li_cdata and char_binary
 tests for most of languages(apart from R and experimental).

Fix the director_binary_string test and add it to C#, D, Go,
 Perl, PHP, Python, Ruby and octave.

Update documents.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
This commit is contained in:
Erez Geva 2023-05-23 23:39:15 +02:00
parent feaabb0d2a
commit 1bf59e0bbc
80 changed files with 1774 additions and 419 deletions

View File

@ -446,7 +446,7 @@
<li><a href="Library.html#Library_nn8">C string handling</a>
<ul>
<li><a href="Library.html#Library_nn9">Default string handling</a>
<li><a href="Library.html#Library_nn10">Passing binary data</a>
<li><a href="Library.html#Library_nn10">Passing a string with length</a>
<li><a href="Library.html#Library_nn11">Using %newobject to release memory</a>
<li><a href="Library.html#Library_nn12">cstring.i</a>
</ul>

View File

@ -114,7 +114,6 @@
<li><a href="#Java_simple_pointers">Simple pointers</a>
<li><a href="#Java_c_arrays">Wrapping C arrays with Java arrays</a>
<li><a href="#Java_unbounded_c_arrays">Unbounded C Arrays</a>
<li><a href="#Java_binary_char">Binary data vs Strings</a>
<li><a href="#Java_heap_allocations">Overriding new and delete to allocate from Java heap</a>
</ul>
<li><a href="#Java_typemaps">Java typemaps</a>
@ -5512,51 +5511,7 @@ well suited for applications in which you need to create buffers,
package binary data, etc.
</p>
<H3><a name="Java_binary_char">27.8.5 Binary data vs Strings</a></H3>
<p>
By default SWIG handles <tt>char *</tt> as a string but there is a handy multi-argument typemap available as mentioned in <a href="Library.html#Library_nn10">Passing binary data</a>.
The following simple example demonstrates using a byte array instead of passing the default string type and length to the wrapped function.
</p>
<div class="code">
<pre>
%apply (char *STRING, size_t LENGTH) { (const char data[], size_t len) }
%inline %{
void binaryChar1(const char data[], size_t len) {
printf("len: %d data: ", len);
for (size_t i=0; i&lt;len; ++i)
printf("%x ", data[i]);
printf("\n");
}
%}
</pre>
</div>
<p>
Calling from Java requires just the byte array to be passed in as the multi-argument typemap being applied reduces the number of arguments in the target language to one, from the original two:
</p>
<div class="code">
<pre>
byte[] data = "hi\0jk".getBytes();
example.binaryChar1(data);
</pre>
</div>
<p>
resulting in the output
</p>
<div class="code"><pre>
$ java runme
len: 5 data: 68 69 0 6a 6b
</pre></div>
<H3><a name="Java_heap_allocations">27.8.6 Overriding new and delete to allocate from Java heap</a></H3>
<H3><a name="Java_heap_allocations">27.8.5 Overriding new and delete to allocate from Java heap</a></H3>
<p>

View File

@ -23,7 +23,7 @@
<li><a href="#Library_nn8">C string handling</a>
<ul>
<li><a href="#Library_nn9">Default string handling</a>
<li><a href="#Library_nn10">Passing binary data</a>
<li><a href="#Library_nn10">Passing a string with length</a>
<li><a href="#Library_nn11">Using %newobject to release memory</a>
<li><a href="#Library_nn12">cstring.i</a>
</ul>
@ -719,11 +719,18 @@ Now, in a script:
<p>
The <tt>cdata.i</tt> module defines functions for converting raw C data to and from strings
in the target language. The primary applications of this module would be packing/unpacking of
The <tt>cdata.i</tt> module defines functions for converting raw C data to and
from the target language. Static typed target languages (C#, Java, go, D)
uses array of bytes, while dynamic typed languages uses string.
The primary applications of this module would be packing/unpacking of
binary data structures---for instance, if you needed to extract data from a buffer.
The target language must support strings with embedded binary data
in order for this to work.
Target dynamic typed language must support strings with embedded binary data
in order for benefit from this module, and should support a 'pack' and 'unpack'
functions to convert string to array of numbers and via versa.
Most static typed languages uses string with defined encoding,
which means the content of the String will be different from the original raw C data.
Yet most dynamic typed language do not support arrays of bytes and
do not have a defined encoding of strings, which make them proper for holding raw data.
</p>
<p>
@ -731,7 +738,7 @@ in order for this to work.
</p>
<div class="indent"><p>
Converts <tt>nbytes</tt> of data at <tt>ptr</tt> into a string. <tt>ptr</tt> can be any
Converts <tt>nbytes</tt> of data at <tt>ptr</tt> into a string or array bytes when using static typed languages. <tt>ptr</tt> can be any
pointer.
</p></div>
@ -740,7 +747,7 @@ pointer.
</p>
<div class="indent"><p>
Copies all of the string data in <tt>s</tt> into the memory pointed to by
Copies all of the string data or array bytes when using static typed languages in <tt>s</tt> into the memory pointed to by
<tt>ptr</tt>. The string may contain embedded NULL bytes.
This is actually a wrapper to the standard C library <tt>memmove</tt> function, which is
declared as
@ -878,11 +885,11 @@ interpreter and lead to a crash). Furthermore, the default behavior does
not work well with binary data. Instead, strings are assumed to be NULL-terminated.
</p>
<H3><a name="Library_nn10">12.3.2 Passing binary data</a></H3>
<H3><a name="Library_nn10">12.3.2 Passing a string with length</a></H3>
<p>
If you have a function that expects binary data,
If you have a function that expects string with a length,
</p>
<div class="code">
@ -905,7 +912,7 @@ size_t parity(char *str, size_t len, size_t initial);
</div>
<p>
Now, in the target language, you can use binary string data like this:
Now, in the target language, you can use the string like this:
</p>
<div class="targetlang">

View File

@ -4471,7 +4471,7 @@ just such a typemap is already defined. Just do this:
<div class="code">
<pre>
%apply (char *STRING, int LENGTH) { (char *data, int size) };
%apply (char *STRING, size_t LENGTH) { (char *data, size_t size) };
...
int parity(char *data, int size, int initial);
</pre>

View File

@ -0,0 +1,52 @@
using System;
using char_binaryNamespace;
public class char_binary_runme {
public static void Main() {
Test t = new Test();
string hile = "hile";
string hil0 = "hil\0";
if (t.strlen(hile) != 4)
throw new Exception("bad multi-arg typemap");
if (t.strlen(hil0) != 4)
throw new Exception("bad multi-arg typemap");
if (t.ustrlen(hile) != 4)
throw new Exception("bad multi-arg typemap");
if (t.ustrlen(hil0) != 4)
throw new Exception("bad multi-arg typemap");
SWIGTYPE_p_char pc = char_binary.new_pchar(5);
char_binary.pchar_setitem(pc, 0, 'h');
char_binary.pchar_setitem(pc, 1, 'o');
char_binary.pchar_setitem(pc, 2, 'l');
char_binary.pchar_setitem(pc, 3, 'a');
char_binary.pchar_setitem(pc, 4, '\0');
/* FIXME: pc is NOT a string
if (t.strlen(pc) != 4)
throw new Exception("bad multi-arg typemap");
if (t.ustrlen(pc) != 4)
throw new Exception("bad multi-arg typemap");
*/
/* FIXME: pc is NOT a string
char_binary.var_pchar = pc;
*/
char_binary.var_pchar = "hola";
if (char_binary.var_pchar != "hola")
throw new Exception("bad pointer case");
/* FIXME: pc is NOT a string
char_binary.var_namet = pc;
*/
char_binary.var_namet = "hola";
if (char_binary.var_namet != "hola")
throw new Exception("bad pointer case");
char_binary.delete_pchar(pc);
}
}

View File

@ -0,0 +1,56 @@
using System;
using director_binary_stringNamespace;
public class director_binary_string_runme
{
static void Main()
{
Caller caller = new Caller();
Callback callback = new DirectorBinaryStringCallback();
caller.setCallback(callback);
int sum = caller.call();
int sumData = caller.callWriteData();
caller.delCallback();
if (sum != 9*2*8 + 13*3*5)
throw new Exception("Unexpected sum: " + sum);
if (sumData != 9*2*8)
throw new Exception("Unexpected sumData: " + sumData);
new Callback().run(null, null);
callback = new DirectorBinaryStringCallback();
caller.setCallback(callback);
caller.call_null();
}
}
public class DirectorBinaryStringCallback : Callback {
public DirectorBinaryStringCallback() : base() {}
public override int run(string dataBufferAA, string dataBufferBB)
{
int ret = 0;
if (dataBufferAA != null) {
for (int i = 0; i < dataBufferAA.Length; i++)
ret += (int)dataBufferAA[i] * 2;
}
if (dataBufferBB != null) {
for (int i = 0; i < dataBufferBB.Length; i++) {
ret += (int)dataBufferBB[i] * 3;
}
}
return ret;
}
public override int writeData(string dataBufferAA)
{
int ret = 0;
if (dataBufferAA != null) {
for (int i = 0; i < dataBufferAA.Length; i++)
ret += (int)dataBufferAA[i] * 2;
}
return ret;
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Text;
using li_cdata_cppNamespace;
public class li_cdata_cpp_runme {
public static void Main() {
Encoding utf8 = Encoding.UTF8;
string s0 = "ABC\0abc";
byte[] s = utf8.GetBytes(s0);
SWIGTYPE_p_void m = li_cdata_cpp.malloc(256);
li_cdata_cpp.memmove(m, s);
byte[] ss = li_cdata_cpp.cdata(m, 7);
string ss_string = utf8.GetString(ss);
if (!ss_string.Equals("ABC\0abc"))
throw new Exception("failed got: " + ss_string);
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Text;
using li_cdataNamespace;
public class li_cdata_runme {
public static void Main() {
Encoding utf8 = Encoding.UTF8;
string s0 = "ABC\0abc";
byte[] s = utf8.GetBytes(s0);
SWIGTYPE_p_void m = li_cdata.malloc(256);
li_cdata.memmove(m, s);
byte[] ss = li_cdata.cdata(m, 7);
string ss_string = utf8.GetString(ss);
if (!ss_string.Equals("ABC\0abc"))
throw new Exception("failed got: " + ss_string);
}
}

View File

@ -23,5 +23,31 @@ void main() {
pchar_setitem(pc, 2, 'l');
pchar_setitem(pc, 3, 'a');
pchar_setitem(pc, 4, 0);
/* FIXME: pc is not string
if (t.strlen(pc) != 4) {
throw new Exception("bad multi-arg typemap");
}
if (t.ustrlen(pc) != 4) {
throw new Exception("bad multi-arg typemap");
}
*/
/* FIXME: pc is not string
var_pchar = pc;
*/
var_pchar = "hola";
if (var_pchar != "hola") {
throw new Exception("bad pointer case");
}
/* FIXME: pc is not string
var_namet = pc;
*/
var_namet = "hola";
if (var_namet != "hola") {
throw new Exception("bad pointer case");
}
delete_pchar(pc);
}

View File

@ -0,0 +1,51 @@
module director_binary_string_runme;
import director_binary_string.Callback;
import director_binary_string.Caller;
import std.string;
import std.conv : text;
import std.exception : enforce;
void main() {
auto caller = new Caller();
Callback callback = new DirectorBinaryStringCallback();
caller.setCallback(callback);
int sum = caller.call();
int sumData = caller.callWriteData();
caller.delCallback();
enforce(sum == 9*2*8 + 13*3*5, text("Unexpected sum: ", sum));
enforce(sumData == 9*2*8, text("Unexpected sumData: ", sumData));
new Callback().run(null, null);
callback = new DirectorBinaryStringCallback();
caller.setCallback(callback);
caller.call_null();
caller.delCallback();
}
class DirectorBinaryStringCallback : Callback {
public:
this() {
super();
}
override int run(string dataBufferAA, string dataBufferBB)
{
int ret = 0;
char[] aa = dataBufferAA.dup;
for (int i = 0; i < aa.length; i++)
ret += aa[i] * 2;
char[] bb = dataBufferBB.dup;
for (int i = 0; i < bb.length; i++)
ret += bb[i] * 3;
return ret;
}
override int writeData(string dataBufferAA)
{
int ret = 0;
char[] aa = dataBufferAA.dup;
for (int i = 0; i < aa.length; i++)
ret += aa[i] * 2;
return ret;
}
}

View File

@ -0,0 +1,13 @@
module li_cdata_cpp_runme;
import li_cdata_cpp.li_cdata_cpp;
import std.conv;
void main() {
ubyte[] s = cast(ubyte[]) "ABC\0abc".dup;
auto m = malloc(256);
memmove(m, s);
string ss = std.conv.text(cast(char[]) cdata(m, 7));
if (ss != "ABC\0abc")
throw new Exception("failed got: " ~ ss);
}

View File

@ -0,0 +1,13 @@
module li_cdata_runme;
import li_cdata.li_cdata;
import std.conv;
void main() {
ubyte[] s = cast(ubyte[]) "ABC\0abc".dup;
auto m = malloc(256);
memmove(m, s);
string ss = std.conv.text(cast(char[]) cdata(m, 7));
if (ss != "ABC\0abc")
throw new Exception("failed got: " ~ ss);
}

View File

@ -4,11 +4,7 @@
%apply (char *STRING, size_t LENGTH) { (char *dataBufferAA, int sizeAA) };
%apply (char *STRING, size_t LENGTH) { (char *dataBufferBB, int sizeBB) };
#ifdef SWIGD
%apply (const char* STRING, size_t LENGTH) { (const char* data, size_t datalen) };
#else
%apply (char* STRING, size_t LENGTH) { (const void* data, size_t datalen) };
#endif
%apply (const char *STRING, size_t LENGTH) { (const char *data, size_t datalen) };
%inline %{
#include <stdlib.h>
@ -20,57 +16,41 @@
class Callback {
public:
virtual ~Callback() {}
virtual void run(char* dataBufferAA, int sizeAA, char* dataBufferBB, int sizeBB) {
if (dataBufferAA)
memset(dataBufferAA, -1, sizeAA);
if (dataBufferBB)
memset(dataBufferBB, -1, sizeBB);
virtual int run(char* dataBufferAA, int sizeAA, char* dataBufferBB, int sizeBB) {
return 0;
}
#ifdef SWIGD
virtual void writeData(const char* data, size_t datalen) = 0;
#else
virtual void writeData(const void* data, size_t datalen) = 0;
#endif
virtual int writeData(const char* data, size_t datalen) = 0;
};
class Caller {
private:
Callback *_callback;
public:
Caller(): _callback(0) {}
~Caller() { delCallback(); }
void delCallback() { delete _callback; _callback = 0; }
void setCallback(Callback *cb) { delCallback(); _callback = cb; }
Caller(): _callback(NULL) {}
void delCallback() { _callback = NULL; }
void setCallback(Callback *cb) { _callback = cb; }
int call() {
int sum = 0;
if (_callback) {
char* aa = (char*)malloc(BUFFER_SIZE_AA);
char aa[BUFFER_SIZE_AA];
memset(aa, 9, BUFFER_SIZE_AA);
char* bb = (char*)malloc(BUFFER_SIZE_BB);
char bb[BUFFER_SIZE_BB];
memset(bb, 13, BUFFER_SIZE_BB);
_callback->run(aa, BUFFER_SIZE_AA, bb, BUFFER_SIZE_BB);
for (int i = 0; i < BUFFER_SIZE_AA; i++)
sum += aa[i];
for (int i = 0; i < BUFFER_SIZE_BB; i++)
sum += bb[i];
free(aa);
free(bb);
return _callback->run(aa, BUFFER_SIZE_AA, bb, BUFFER_SIZE_BB);
}
return sum;
return 0;
}
void call_null() {
_callback->run(NULL, 0, NULL, 0);
if (_callback) {
_callback->run(NULL, 0, NULL, 0);
}
}
int callWriteData() {
int sum = 0;
if (_callback) {
char* aa = (char*)malloc(BUFFER_SIZE_AA);
char aa[BUFFER_SIZE_AA];
memset(aa, 9, BUFFER_SIZE_AA);
_callback->writeData(aa, BUFFER_SIZE_AA);
for (int i = 0; i < BUFFER_SIZE_AA; i++)
sum += aa[i];
return _callback->writeData(aa, BUFFER_SIZE_AA);
}
return sum;
return 0;
}
};

View File

@ -5,13 +5,18 @@ import . "swigtests/char_binary"
func main() {
t := NewTest()
if t.Strlen("hile") != 4 {
print(t.Strlen("hile"))
panic("bad multi-arg typemap")
}
if t.Ustrlen("hile") != 4 {
panic("bad multi-arg typemap")
}
if t.Strlen("hil\000") != 4 {
panic("bad multi-arg typemap")
}
if t.Ustrlen("hil\000") != 4 {
panic("bad multi-arg typemap")
}
// creating a raw char*
pc := New_pchar(5)
@ -21,5 +26,30 @@ func main() {
Pchar_setitem(pc, 3, 'a')
Pchar_setitem(pc, 4, 0)
/* FIXME: pc is not a string
if t.Strlen(pc) != 4 {
panic("bad multi-arg typemap")
}
if t.Ustrlen(pc) != 4 {
panic("bad multi-arg typemap")
}
*/
/* FIXME: pc is not a string
SetVar_pchar(pc)
*/
SetVar_pchar("hola")
if GetVar_pchar() != "hola" {
panic("bad pointer case")
}
/* FIXME: pc is not a string
SetVar_namet(pc)
*/
SetVar_namet("hola")
if GetVar_namet() != "hola" {
panic("bad pointer case")
}
Delete_pchar(pc)
}

View File

@ -0,0 +1,49 @@
package main
import wrap "swigtests/director_binary_string"
type DirectorBinaryStringCallback struct{}
func (p *DirectorBinaryStringCallback) Run(dataBufferAA string, dataBufferBB string) int {
ret := 0
for i := 0; i < len(dataBufferAA); i++ {
ret += int(dataBufferAA[i]) * 2
}
for i := 0; i < len(dataBufferBB); i++ {
ret += int(dataBufferBB[i]) * 3
}
return ret
}
func (p *DirectorBinaryStringCallback) WriteData(data string) int {
ret := 0
for i := 0; i < len(data); i++ {
ret += int(data[i]) * 2
}
return ret
}
func main() {
caller := wrap.NewCaller()
callback := wrap.NewDirectorCallback(&DirectorBinaryStringCallback{})
caller.SetCallback(callback)
sum := caller.Call()
sumData := caller.CallWriteData()
caller.DelCallback()
if sum != 9*2*8 + 13*3*5 {
panic(sum)
}
if sumData != 9*2*8 {
panic(sumData)
}
// FIXME panic: accessing abstract class or protected constructor
// It does make sense as writeData() is abstract
// wrap.NewCallback().Run("", "")
callback = wrap.NewDirectorCallback(&DirectorBinaryStringCallback{})
caller.SetCallback(callback)
caller.Call_null()
}

View File

@ -3,11 +3,11 @@ package main
import . "swigtests/li_cdata_cpp"
func main() {
s := "ABC abc"
s := []byte("ABC\x00abc")
m := Malloc(256)
Memmove(m, s, len(s))
Memmove(m, s)
ss := Cdata(m, 7)
if string(ss) != "ABC abc" {
if string(ss) != "ABC\x00abc" {
panic("failed")
}
}

View File

@ -3,11 +3,11 @@ package main
import . "swigtests/li_cdata"
func main() {
s := "ABC abc"
s := []byte("ABC\x00abc")
m := Malloc(256)
Memmove(m, s, len(s))
Memmove(m, s)
ss := Cdata(m, 7)
if string(ss) != "ABC abc" {
if string(ss) != "ABC\x00abc" {
panic("failed")
}
}

View File

@ -0,0 +1,6 @@
;; The SWIG modules have "passive" Linkage, i.e., they don't generate
;; Guile modules (namespaces) but simply put all the bindings into the
;; current module. That's enough for such a simple test.
(dynamic-call "scm_init_char_binary_module" (dynamic-link "./libchar_binary"))
(load "testsuite.scm")
(load "../schemerunme/char_binary.scm")

View File

@ -0,0 +1,6 @@
;; The SWIG modules have "passive" Linkage, i.e., they don't generate
;; Guile modules (namespaces) but simply put all the bindings into the
;; current module. That's enough for such a simple test.
(dynamic-call "scm_init_li_cdata_cpp_module" (dynamic-link "./libli_cdata_cpp"))
(load "testsuite.scm")
(load "../schemerunme/li_cdata_cpp.scm")

View File

@ -0,0 +1,6 @@
;; The SWIG modules have "passive" Linkage, i.e., they don't generate
;; Guile modules (namespaces) but simply put all the bindings into the
;; current module. That's enough for such a simple test.
(dynamic-call "scm_init_li_cdata_module" (dynamic-link "./libli_cdata"))
(load "testsuite.scm")
(load "../schemerunme/li_cdata.scm")

View File

@ -13,18 +13,50 @@ public class char_binary_runme {
public static void main(String argv[]) {
Test t = new Test();
byte[] hile = "hile".getBytes();
byte[] hil0 = "hil\0".getBytes();
if (t.strlen(hile) != 4)
throw new RuntimeException("bad multi-arg typemap");
if (t.strlen(hil0) != 4)
String hile = "hile";
if (t.strlen(hile) != 4)
throw new RuntimeException("bad multi-arg typemap");
if (t.ustrlen(hile) != 4)
throw new RuntimeException("bad multi-arg typemap");
String hil0 = "hil\0";
if (t.strlen(hil0) != 4)
throw new RuntimeException("bad multi-arg typemap");
if (t.ustrlen(hil0) != 4)
throw new RuntimeException("bad multi-arg typemap");
// creating a raw char*
SWIGTYPE_p_char pc = char_binary.new_pchar(5);
char_binary.pchar_setitem(pc, 0, 'h');
char_binary.pchar_setitem(pc, 1, 'o');
char_binary.pchar_setitem(pc, 2, 'l');
char_binary.pchar_setitem(pc, 3, 'a');
char_binary.pchar_setitem(pc, 4, '\0');
/* FIXME: incompatible types: SWIGTYPE_p_char cannot be converted to String
if (t.strlen(pc) != 4)
throw new RuntimeException("bad multi-arg typemap");
if (t.ustrlen(pc) != 4)
throw new RuntimeException("bad multi-arg typemap");
*/
/* FIXME: pc cannot be converted to String
char_binary.setVar_pchar(pc);
*/
char_binary.setVar_pchar("hola");
if (!char_binary.getVar_pchar().equals("hola"))
throw new RuntimeException("bad pointer case");
/* FIXME: pc cannot be converted to String
char_binary.setVar_namet(pc);
*/
char_binary.setVar_namet("hola");
if (!char_binary.getVar_namet().equals("hola"))
throw new RuntimeException("bad pointer case");
char_binary.delete_pchar(pc);
}
}

View File

@ -24,7 +24,7 @@ public class director_binary_string_runme {
throw new RuntimeException("Unexpected sum: " + sum);
if (sumData != 9*2*8)
throw new RuntimeException("Unexpected sum: " + sum);
throw new RuntimeException("Unexpected sumData: " + sumData);
new Callback().run(null, null);
callback = new DirectorBinaryStringCallback();
@ -39,23 +39,31 @@ class DirectorBinaryStringCallback extends Callback {
}
@Override
public void run(byte[] dataBufferAA, byte[] dataBufferBB)
public int run(String dataBufferAA, String dataBufferBB)
{
if (dataBufferAA != null)
for (int i = 0; i < dataBufferAA.length; i++)
dataBufferAA[i] = (byte)(dataBufferAA[i] * 2);
int ret = 0;
if (dataBufferAA != null) {
for (int i = 0; i < dataBufferAA.length(); i++)
ret += (int)dataBufferAA.charAt(i) * 2;
}
if (dataBufferBB != null)
for (int i = 0; i < dataBufferBB.length; i++)
dataBufferBB[i] = (byte)(dataBufferBB[i] * 3);
if (dataBufferBB != null) {
for (int i = 0; i < dataBufferBB.length(); i++) {
ret += (int)dataBufferBB.charAt(i) * 3;
}
}
return ret;
}
@Override
public void writeData(byte[] dataBufferAA)
public int writeData(String dataBufferAA)
{
if (dataBufferAA != null)
for (int i = 0; i < dataBufferAA.length; i++)
dataBufferAA[i] = (byte)(dataBufferAA[i] * 2);
int ret = 0;
if (dataBufferAA != null) {
for (int i = 0; i < dataBufferAA.length(); i++)
ret += (int)dataBufferAA.charAt(i) * 2;
}
return ret;
}
}

View File

@ -13,12 +13,12 @@ public class li_cdata_cpp_runme {
public static void main(String argv[]) throws Throwable
{
byte[] s = "ABC abc".getBytes();
byte[] s = "ABC\0abc".getBytes("UTF-8");
SWIGTYPE_p_void m = li_cdata_cpp.malloc(256);
li_cdata_cpp.memmove(m, s);
byte[] ss = li_cdata_cpp.cdata(m, 7);
String ss_string = new String(ss);
if (!ss_string.equals("ABC abc"))
String ss_string = new String(ss, "UTF-8");
if (!ss_string.equals("ABC\0abc"))
throw new RuntimeException("failed got: " + ss_string);
}
}

View File

@ -13,12 +13,12 @@ public class li_cdata_runme {
public static void main(String argv[]) throws Throwable
{
byte[] s = "ABC abc".getBytes();
byte[] s = "ABC\0abc".getBytes("UTF-8");
SWIGTYPE_p_void m = li_cdata.malloc(256);
li_cdata.memmove(m, s);
byte[] ss = li_cdata.cdata(m, 7);
String ss_string = new String(ss);
if (!ss_string.equals("ABC abc"))
String ss_string = new String(ss, "UTF-8");
if (!ss_string.equals("ABC\0abc"))
throw new RuntimeException("failed got: " + ss_string);
}
}

View File

@ -2,11 +2,9 @@ var char_binary = require("char_binary");
var t = new char_binary.Test();
if (t.strlen('hile') != 4) {
print(t.strlen('hile'));
throw("bad multi-arg typemap 1");
}
if (t.ustrlen('hile') != 4) {
print(t.ustrlen('hile'));
throw("bad multi-arg typemap 1");
}
@ -37,7 +35,6 @@ if (t.ustrlen(pc) != 4) {
char_binary.var_pchar = pc;
if (char_binary.var_pchar != "hola") {
print(char_binary.var_pchar);
throw("bad pointer case (1)");
}

View File

@ -1,6 +1,10 @@
var li_cdata_cpp = require("li_cdata_cpp");
/* FIXME:
* Use null in middle of string
* Apple JSC JSStringCreateWithUTF8CString()
* usses null terminated string without length */
s = "ABC abc";
m = li_cdata_cpp.malloc(256);
li_cdata_cpp.memmove(m, s);

View File

@ -1,6 +1,10 @@
var li_cdata = require("li_cdata");
/* FIXME:
* Use null in middle of string
* Apple JSC JSStringCreateWithUTF8CString()
* usses null terminated string without length */
s = "ABC abc";
m = li_cdata.malloc(256);
li_cdata.memmove(m, s);

View File

@ -0,0 +1,46 @@
require("import") -- the import fn
import("char_binary") -- import lib
v = char_binary
-- catch "undefined" global variables
local env = _ENV -- Lua 5.2
if not env then env = getfenv () end -- Lua 5.1
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
t = v.Test()
assert(t:strlen("hile") == 4, "bad multi-arg typemap");
assert(t:ustrlen("hile") == 4, "bad multi-arg typemap");
assert(t:strlen("hil\0") == 4, "bad multi-arg typemap");
assert(t:ustrlen("hil\0") == 4, "bad multi-arg typemap");
-- creating a raw char*
pc = v.new_pchar(5);
v.pchar_setitem(pc, 0, 'h');
v.pchar_setitem(pc, 1, 'o');
v.pchar_setitem(pc, 2, 'l');
v.pchar_setitem(pc, 3, 'a');
v.pchar_setitem(pc, 4, 0);
-- FIXME: strlen except 'char const *' but pc is 'char *'
if false then
assert(t:strlen(pc) == 4, "bad multi-arg typemap");
assert(t:ustrlen(pc) == 4, "bad multi-arg typemap");
end
-- FIXME: expected 'pchar' got 'char *'
if false then
v.var_pchar = pc;
else
v.var_pchar = "hola";
end
assert(v.var_pchar == "hola", "bad pointer case");
-- FIXME: expected 'pchar' got 'char *'
if false then
v.var_namet = pc;
else
v.var_namet = "hola";
end
assert(v.var_namet == "hola", "bad pointer case");
v.delete_pchar(pc);

View File

@ -0,0 +1,16 @@
require("import") -- the import fn
import("li_cdata_cpp") -- import lib
v = li_cdata_cpp
-- catch "undefined" global variables
local env = _ENV -- Lua 5.2
if not env then env = getfenv () end -- Lua 5.1
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
s = "ABC\0abc";
m = v.malloc(256);
v.memmove(m, s);
ss = v.cdata(m, 7);
if ss ~= "ABC\0abc" then
error("failed")
end

View File

@ -0,0 +1,16 @@
require("import") -- the import fn
import("li_cdata") -- import lib
v = li_cdata
-- catch "undefined" global variables
local env = _ENV -- Lua 5.2
if not env then env = getfenv () end -- Lua 5.1
setmetatable(env, {__index=function (t,i) error("undefined global variable `"..i.."'",2) end})
s = "ABC\0abc";
m = v.malloc(256);
v.memmove(m, s);
ss = v.cdata(m, 7);
if ss ~= "ABC\0abc" then
error("failed")
end

View File

@ -0,0 +1,45 @@
director_binary_string
function self=DirectorBinaryStringCallback()
global director_binary_string;
self=subclass(director_binary_string.Callback());
self.run=@DirectorBinaryStringCallback_run;
self.writeData=@DirectorBinaryStringCallback_writeData;
end
function ret=DirectorBinaryStringCallback_run(self, dataBufferAA, dataBufferBB)
ret = 0;
for i = 1:length(dataBufferAA)
ret = ret + double(dataBufferAA(i)) * 2;
end
for i = 1:length(dataBufferBB)
ret = ret + double(dataBufferBB(i)) * 3;
end
end
function ret=DirectorBinaryStringCallback_writeData(self, dataBufferAA)
ret = 0;
for i = 1:length(dataBufferAA)
ret = ret + double(dataBufferAA(i)) * 2;
end
end
caller = director_binary_string.Caller();
callback = DirectorBinaryStringCallback();
caller.setCallback(callback);
sum = caller.call();
sumData = caller.callWriteData();
caller.delCallback();
if (sum != 9*2*8 + 13*3*5)
error(sum);
end
if (sumData != 9*2*8)
error(sumData);
end
% FIXME how do we create null string?
% director_binary_string.Callback().run(null, null);
director_binary_string.Callback().run('', '');
callback = DirectorBinaryStringCallback();
caller.setCallback(callback);
caller.call_null();
caller.delCallback();

View File

@ -0,0 +1,10 @@
li_cdata_cpp
s = "ABC\000abc";
m = li_cdata_cpp.malloc(256);
li_cdata_cpp.memmove(m, s);
ss = li_cdata_cpp.cdata(m, 7);
if (ss != "ABC\000abc")
disp(ss);
error("failed");
endif

View File

@ -0,0 +1,10 @@
li_cdata
s = "ABC\000abc";
m = li_cdata.malloc(256);
li_cdata.memmove(m, s);
ss = li_cdata.cdata(m, 7);
if (ss != "ABC\000abc")
disp(ss);
error("failed");
endif

View File

@ -0,0 +1,50 @@
use strict;
use warnings;
use Test::More tests => 4;
BEGIN { use_ok 'director_binary_string' }
require_ok 'director_binary_string';
{
package DirectorBinaryStringCallback;
use base 'director_binary_string::Callback';
sub run {
my $ret = 0;
my ($self, $dataBufferAA, $dataBufferBB) = @_;
if(defined $dataBufferAA) {
$ret += ord($_) * 2 for(split('', $dataBufferAA));
}
if(defined $dataBufferBB) {
$ret += ord($_) * 3 for(split('', $dataBufferBB));
}
return $ret;
}
sub writeData {
my $ret = 0;
my ($self, $dataBufferAA) = @_;
if(defined $dataBufferAA) {
$ret += ord($_) * 2 for(split('', $dataBufferAA));
}
return $ret;
}
}
{
print "Start\n";
my $caller = director_binary_string::Caller->new();
my $callback = DirectorBinaryStringCallback->new();
$caller->setCallback($callback);
my $sum = $caller->call();
my $sumData = $caller->callWriteData();
$caller->delCallback();
is($sum, 9*2*8 + 13*3*5, 'Unexpected sum: ' . $sum);
is($sumData, 9*2*8, 'Unexpected sumData: ' . $sumData);
# FIXME accessing abstract class or protected constructor
# It does make sense as writeData() is abstract
# director_binary_string::Callback->new()->run(undef, undef);
$callback = DirectorBinaryStringCallback->new();
$caller->setCallback($callback);
$caller->call_null();
$caller->delCallback();
}

View File

@ -0,0 +1,11 @@
use strict;
use warnings;
use Test::More tests => 3;
BEGIN { use_ok('li_cdata_cpp') }
require_ok('li_cdata_cpp');
my $s = "ABC\x00abc";
my $m = li_cdata_cpp::malloc(256);
li_cdata_cpp::memmove($m, $s);
my $ss = li_cdata_cpp::cdata($m, 7);
is($ss, "ABC\x00abc", "failed");

View File

@ -0,0 +1,11 @@
use strict;
use warnings;
use Test::More tests => 3;
BEGIN { use_ok('li_cdata') }
require_ok('li_cdata');
my $s = "ABC\x00abc";
my $m = li_cdata::malloc(256);
li_cdata::memmove($m, $s);
my $ss = li_cdata::cdata($m, 7);
is($ss, "ABC\x00abc", "failed");

View File

@ -26,14 +26,24 @@ pchar_setitem($pc, 4, 0);
if (0) {
check::equal($t->strlen($pc), 4, "bad multi-arg typemap");
check::equal($t->ustrlen($pc), 4, "bad multi-arg typemap");
var_pchar_set($pc);
check::equal(var_pchar_get(), "hola", "bad pointer case");
var_namet_set($pc);
check::equal(var_namet_get(), "hola", "bad pointer case");
}
// FIXME: Cannot convert pc to string
if (0) {
var_pchar_set($pc);
} else {
var_pchar_set("hola");
}
check::equal(var_pchar_get(), "hola", "bad pointer case");
// FIXME: Cannot convert pc to string
if (0) {
var_namet_set($pc);
} else {
var_namet_set("hola");
}
check::equal(var_namet_get(), "hola", "bad pointer case");
delete_pchar($pc);
check::done();

View File

@ -0,0 +1,53 @@
<?php
require "tests.php";
// No new functions
check::functions(array());
check::classes(array('director_binary_string','Callback','Caller'));
// No new vars
check::globals(array());
class DirectorBinaryStringCallback extends Callback {
function run(string $dataBufferAA, string $dataBufferBB)
{
$ret = 0;
if ($dataBufferAA != null) {
for ($i = 0; $i < strlen($dataBufferAA); $i++)
$ret = $ret + ord($dataBufferAA[$i]) * 2;
}
if ($dataBufferBB != null) {
for ($i = 0; $i < strlen($dataBufferBB); $i++)
$ret = $ret + ord($dataBufferBB[$i]) * 3;
}
return $ret;
}
function writeData(string $dataBufferAA)
{
$ret = 0;
if ($dataBufferAA != null) {
for ($i = 0; $i < strlen($dataBufferAA); $i++)
$ret = $ret + ord($dataBufferAA[$i]) * 2;
}
return $ret;
}
}
$caller = new Caller();
$callback = new DirectorBinaryStringCallback();
$caller->setCallback($callback);
$sum = $caller->call();
$sumData = $caller->callWriteData();
$caller->delCallback();
check::equal($sum, 9*2*8 + 13*3*5, "Unexpected sum: $sum");
check::equal($sumData, 9*2*8, "Unexpected sumData: $sumData");
// FIXME Error: Cannot instantiate abstract class Callback
// It does make sense as writeData() is abstract
// (new Callback())->run("", "");
$callback = new DirectorBinaryStringCallback();
$caller->setCallback($callback);
$caller->call_null();
$caller->delCallback();

View File

@ -0,0 +1,17 @@
<?php
require "tests.php";
check::functions(array('malloc', 'memmove', 'cdata', 'cdata_int', 'cdata_double'));
check::classes(array('li_cdata_cpp'));
// No new vars
check::globals(array());
$s = "ABC\0abc";
$m = malloc(256);
memmove($m, $s);
$ss = cdata($m, 7);
check::equal($ss, "ABC\0abc", "failed");
check::done();

View File

@ -0,0 +1,17 @@
<?php
require "tests.php";
check::functions(array('malloc', 'memmove', 'cdata', 'cdata_int', 'cdata_double'));
check::classes(array('li_cdata'));
// No new vars
check::globals(array());
$s = "ABC\0abc";
$m = malloc(256);
memmove($m, $s);
$ss = cdata($m, 7);
check::equal($ss, "ABC\0abc", "failed");
check::done();

View File

@ -0,0 +1,35 @@
import director_binary_string
class DirectorBinaryStringCallback(director_binary_string.Callback):
def run(self, dataBufferAA, dataBufferBB):
ret = 0
for c in dataBufferAA:
ret += ord(c) * 2
for c in dataBufferBB:
ret += ord(c) * 3
return ret
def writeData(self, dataBufferAA):
ret = 0
for c in dataBufferAA:
ret += ord(c) * 2
return ret
caller = director_binary_string.Caller()
callback = DirectorBinaryStringCallback()
caller.setCallback(callback)
sum = caller.call()
sumData = caller.callWriteData()
caller.delCallback()
if sum != 9*2*8 + 13*3*5:
raise RuntimeError("Unexpected sum: {}".format(sum))
if sumData != 9*2*8:
raise RuntimeError("Unexpected sumData: {}".format(sumData))
# FIXME accessing abstract class or protected constructor
# It does make sense as writeData() is abstract
# director_binary_string.Callback().run(null, null)
callback = DirectorBinaryStringCallback()
caller.setCallback(callback)
caller.call_null()
caller.delCallback()

View File

@ -1,9 +1,9 @@
from li_cdata_cpp import *
s = "ABC abc"
s = "ABC\x00abc"
m = malloc(256)
memmove(m, s)
ss = cdata(m, 7)
if ss != "ABC abc":
if ss != "ABC\x00abc":
raise "failed"

View File

@ -1,9 +1,9 @@
from li_cdata import *
s = "ABC abc"
s = "ABC\x00abc"
m = malloc(256)
memmove(m, s)
ss = cdata(m, 7)
if ss != "ABC abc":
if ss != "ABC\x00abc":
raise "failed"

View File

@ -0,0 +1,52 @@
#!/usr/bin/env ruby
#
# Put description here
#
require 'swig_assert'
require 'director_binary_string'
class DirectorBinaryStringCallback < Director_binary_string::Callback
def run(dataBufferAA, dataBufferBB)
ret = 0
unless dataBufferAA.nil?
dataBufferAA.each_char do |c|
ret += c.ord * 2
end
end
unless dataBufferBB.nil?
dataBufferBB.each_char do |c|
ret += c.ord * 3
end
end
return ret
end
def writeData(dataBufferAA)
ret = 0
unless dataBufferAA.nil?
dataBufferAA.each_char do |c|
ret += c.ord * 2
end
end
return ret
end
end
caller = Director_binary_string::Caller.new
callback = DirectorBinaryStringCallback.new
caller.setCallback(callback)
sum = caller.call()
sumData = caller.callWriteData()
caller.delCallback()
raise RuntimeError if sum != 9*2*8 + 13*3*5
raise RuntimeError if sumData != 9*2*8
# FIXME accessing abstract class or protected constructor
# It does make sense as writeData() is abstract
# Director_binary_string::Callback.new.run(nil, nil)
callback = DirectorBinaryStringCallback.new
caller.setCallback(callback)
caller.call_null()
caller.delCallback()

View File

@ -0,0 +1,16 @@
#!/usr/bin/env ruby
#
# Put description here
#
require 'swig_assert'
require 'li_cdata_cpp'
include Li_cdata_cpp
s = "ABC\0abc"
m = malloc(256)
memmove(m, s)
ss = cdata(m, 7)
swig_assert(ss == "ABC\0abc", binding, "failed");

View File

@ -0,0 +1,16 @@
#!/usr/bin/env ruby
#
# Put description here
#
require 'swig_assert'
require 'li_cdata'
include Li_cdata
s = "ABC\0abc"
m = malloc(256)
memmove(m, s)
ss = cdata(m, 7)
swig_assert(ss == "ABC\0abc", binding, "failed");

View File

@ -0,0 +1,41 @@
(define t (new-Test))
(if (not (= (Test-strlen t "hile") 4))
(error "bad multi-arg typemap"))
(if (not (= (Test-ustrlen t "hile") 4))
(error "bad multi-arg typemap"))
(if (not (= (Test-strlen t "hil\x00") 4))
(error "bad multi-arg typemap"))
(if (not (= (Test-ustrlen t "hil\x00") 4))
(error "bad multi-arg typemap"))
;; creating a raw char*
(define pc (new-pchar 5))
(pchar-setitem pc 0 #\h)
(pchar-setitem pc 1 #\o)
(pchar-setitem pc 2 #\l)
(pchar-setitem pc 3 #\a)
(pchar-setitem pc 4 0)
;; FIXME: pc in not string
;; (if (not (= (Test-strlen pc) 4))
;; (error "bad multi-arg typemap"))
;; (if (not (= (Test-ustrlen pc) 4))
;; (error "bad multi-arg typemap"))
;; FIXME: pc in not string
;; (var-pchar pc)
(var-pchar "hola")
(if (not (string=? (var-pchar) "hola"))
(error "bad pointer case"))
;; FIXME: pc in not string and var-namet do not except string
;; (var-namet pc)
;; (if (not (string=? (var-namet) "hola"))
;; (error "bad pointer case"))
(delete-pchar pc)
(exit 0)

View File

@ -0,0 +1,10 @@
(define s "ABC\x00abc")
(define m (malloc 256))
(memmove m s)
(define ss (cdata m 7))
(if (not (string=? ss "ABC\x00abc"))
(error "failed"))
(exit 0)

View File

@ -0,0 +1,10 @@
(define s "ABC\x00abc")
(define m (malloc 256))
(memmove m s)
(define ss (cdata m 7))
(if (not (string=? ss "ABC\x00abc"))
(error "failed"))
(exit 0)

View File

@ -0,0 +1,53 @@
exec("swigtest.start", -1);
t = new_Test();
str = "hile";
if Test_strlen(t, str) <> 4 then
swigtesterror("bad multi-arg typemap");
end
if Test_ustrlen(t, str) <> 4 then
swigtesterror("bad multi-arg typemap");
end
// String can not contain null character same as C
// creating a raw char*
pc = new_pchar(5);
pchar_setitem(pc, 0, 'h');
pchar_setitem(pc, 1, 'o');
pchar_setitem(pc, 2, 'l');
pchar_setitem(pc, 3, 'a');
pchar_setitem(pc, 4, ascii(0));
// FIXME: pc is not 'char const *'
if 0 then
if Test_strlen(t, pc) <> 4 then
swigtesterror("bad multi-arg typemap");
end
if Test_ustrlen(t, pc) <> 4 then
swigtesterror("bad multi-arg typemap");
end
end
// FIXME: pc is not string
if 0 then
var_pchar_set(pc);a
else
var_pchar_set("hola");
end
if var_pchar_get() <> "hola" then
swigtesterror("bad pointer case");
end
// FIXME: pc is not string
if 0 then
var_namet_set(pc);
else
var_namet_set("hola");
end
if var_namet_get() <> "hola" then
swigtesterror("bad pointer case");
end
delete_pchar(pc);
exec("swigtest.quit", -1);

View File

@ -0,0 +1,12 @@
exec("swigtest.start", -1);
// Strings in Scilab can not contain null character same as C
s = "ABC abc";
m = malloc(256);
memmove(m, s);
ss = cdata(m, 7);
if ss <> "ABC abc" then
swigtesterror("failed");
end
exec("swigtest.quit", -1);

View File

@ -0,0 +1,12 @@
exec("swigtest.start", -1);
// Strings in Scilab can not contain null character same as C
s = "ABC abc";
m = malloc(256);
memmove(m, s);
ss = cdata(m, 7);
if ss <> "ABC abc" then
swigtesterror("failed");
end
exec("swigtest.quit", -1);

View File

@ -0,0 +1,70 @@
if [ catch { load ./char_binary[info sharedlibextension] Char_binary} err_msg ] {
puts stderr "Could not load shared object:\n$err_msg"
}
Test t
set str "hile"
if { [ t strlen $str ] != 4 } {
puts stderr "bad multi-arg typemap"
exit 1
}
if { [ t ustrlen $str ] != 4 } {
puts stderr "bad multi-arg typemap"
exit 1
}
# The universal character Escape Sequence is 2 bytes
set str2 "hi\u0000"
if { [ t strlen $str2 ] != 4 } {
puts stderr "bad multi-arg typemap"
exit 1
}
if { [ t ustrlen $str2 ] != 4 } {
puts stderr "bad multi-arg typemap"
exit 1
}
# creating a raw char*
set pc [ new_pchar 5 ]
pchar_setitem $pc 0 h
pchar_setitem $pc 1 o
pchar_setitem $pc 2 l
pchar_setitem $pc 3 a
pchar_setitem $pc 4 0
# FIXME: pc is not a string in tcl
if { 0 } {
if { [ t strlen $pc ] != 4 } {
puts stderr "bad multi-arg typemap"
exit 1
}
if { [ t ustrlen $pc ] != 4 } {
puts stderr "bad multi-arg typemap"
exit 1
}
}
# FIXME: pc is not a string in tcl
if { 0 } {
set var_pchar $pc
} else {
set var_pchar "hola"
}
if { $var_pchar != "hola" } {
puts stderr "bad pointer case"
exit 1
}
# FIXME: pc is not a string in tcl
if { 0 } {
set var_namet $pc
} else {
set var_namet "hola"
}
if { $var_namet != "hola" } {
puts stderr "bad pointer case"
exit 1
}
delete_pchar $pc

View File

@ -0,0 +1,14 @@
if [ catch { load ./li_cdata_cpp[info sharedlibextension] Li_cdata_cpp} err_msg ] {
puts stderr "Could not load shared object:\n$err_msg"
}
# The universal character Escape Sequence is 2 bytes
set s "ABC\u0000bc"
set m [ malloc 256 ]
memmove $m $s
set ss [ cdata $m 7 ]
if {$ss != "ABC\u0000bc"} {
puts stderr "failed"
exit 1
}

View File

@ -0,0 +1,14 @@
if [ catch { load ./li_cdata[info sharedlibextension] Li_cdata} err_msg ] {
puts stderr "Could not load shared object:\n$err_msg"
}
# The universal character Escape Sequence is 2 bytes
set s "ABC\u0000bc"
set m [ malloc 256 ]
memmove $m $s
set ss [ cdata $m 7 ]
if {$ss != "ABC\u0000bc"} {
puts stderr "failed"
exit 1
}

View File

@ -1,108 +1,8 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data as strings.
* SWIG library file containing macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
%include <swigfragments.swg>
%include <typemaps/cdata.swg>
%{
typedef struct SWIGCDATA {
char *data;
int len;
} SWIGCDATA;
%}
/* -----------------------------------------------------------------------------
* Typemaps for returning binary data
* ----------------------------------------------------------------------------- */
#if SWIGGUILE
%typemap(out) SWIGCDATA {
$result = scm_from_locale_stringn($1.data,$1.len);
}
%typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH);
#elif SWIGPHP
%typemap(out) SWIGCDATA {
ZVAL_STRINGL($result, $1.data, $1.len);
}
%typemap(in) (const void *indata, int inlen) = (char *STRING, int LENGTH);
#elif SWIGJAVA
%apply (char *STRING, int LENGTH) { (const void *indata, int inlen) }
%typemap(jni) SWIGCDATA "jbyteArray"
%typemap(jtype) SWIGCDATA "byte[]"
%typemap(jstype) SWIGCDATA "byte[]"
%fragment("SWIG_JavaArrayOutCDATA", "header") {
static jbyteArray SWIG_JavaArrayOutCDATA(JNIEnv *jenv, char *result, jsize sz) {
jbyte *arr;
int i;
jbyteArray jresult = JCALL1(NewByteArray, jenv, sz);
if (!jresult)
return NULL;
arr = JCALL2(GetByteArrayElements, jenv, jresult, 0);
if (!arr)
return NULL;
for (i=0; i<sz; i++)
arr[i] = (jbyte)result[i];
JCALL3(ReleaseByteArrayElements, jenv, jresult, arr, 0);
return jresult;
}
}
%typemap(out, fragment="SWIG_JavaArrayOutCDATA") SWIGCDATA
%{$result = SWIG_JavaArrayOutCDATA(jenv, (char *)$1.data, $1.len); %}
%typemap(javaout) SWIGCDATA {
return $jnicall;
}
#endif
/* -----------------------------------------------------------------------------
* %cdata(TYPE [, NAME])
*
* Convert raw C data to a binary string.
* ----------------------------------------------------------------------------- */
%define %cdata(TYPE,NAME...)
%insert("header") {
#if #NAME == ""
static SWIGCDATA cdata_##TYPE(TYPE *ptr, int nelements) {
#else
static SWIGCDATA cdata_##NAME(TYPE *ptr, int nelements) {
#endif
SWIGCDATA d;
d.data = (char *) ptr;
#if #TYPE != "void"
d.len = nelements*sizeof(TYPE);
#else
d.len = nelements;
#endif
return d;
}
}
%typemap(default) int nelements "$1 = 1;"
#if #NAME == ""
SWIGCDATA cdata_##TYPE(TYPE *ptr, int nelements);
#else
SWIGCDATA cdata_##NAME(TYPE *ptr, int nelements);
#endif
%enddef
%typemap(default) int nelements;
%rename(cdata) ::cdata_void(void *ptr, int nelements);
%cdata(void);
%fragment("<string.h>");
/* Memory move function. Due to multi-argument typemaps this appears to be wrapped as
void memmove(void *data, const char *s); */
void memmove(void *data, const void *indata, int inlen);

84
Lib/csharp/cdata.i Normal file
View File

@ -0,0 +1,84 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
/* ------------------------------------------------------------
* Typemap for passing bytes with length
* ------------------------------------------------------------ */
%pragma(csharp) imclasscode=%{
[global::System.Runtime.InteropServices.DllImport("$module", EntryPoint="SWIG_csharp_bytes_to_c")]
public static extern global::System.IntPtr SWIG_csharp_bytes_to_c0(int len, byte[] ptr);
public static global::System.IntPtr SWIG_csharp_bytes_to_c(byte[] ptr) {
return SWIG_csharp_bytes_to_c0(ptr.Length, ptr);
}
%}
%fragment("SWIG_csharp_bytes", "header") %{
#ifdef __cplusplus
extern "C" {
#endif
typedef struct { int len; void* ptr; } SWIG_csharp_bytes;
SWIGEXPORT void* SWIGSTDCALL SWIG_csharp_bytes_to_c(int len, void *ptr) {
SWIG_csharp_bytes *ret;
ret = (SWIG_csharp_bytes*)malloc(sizeof(SWIG_csharp_bytes));
if (ret == SWIG_NULLPTR) {
SWIG_CSharpSetPendingException(SWIG_CSharpOutOfMemoryException, "fail to duplicate bytes container.");
return SWIG_NULLPTR;
}
if(len > 0) {
ret->ptr = malloc(len);
if (ret->ptr == SWIG_NULLPTR) {
SWIG_CSharpSetPendingException(SWIG_CSharpOutOfMemoryException, "fail to duplicate bytes.");
free(ret);
return SWIG_NULLPTR;
}
memcpy(ret->ptr, ptr, len);
ret->len = len;
} else {
ret->ptr = SWIG_NULLPTR;
ret->len = 0;
}
return ret;
}
#ifdef __cplusplus
}
#endif
%}
%typemap(cstype) (const void *BYTES, size_t LENGTH) "byte[]"
%typemap(ctype) (const void *BYTES, size_t LENGTH) "void *"
%typemap(imtype) (const void *BYTES, size_t LENGTH) "global::System.IntPtr"
%typemap(csin) (const void *BYTES, size_t LENGTH) "$modulePINVOKE.SWIG_csharp_bytes_to_c($csinput)"
%typemap(in, canthrow=1, fragment="SWIG_csharp_bytes") (const void *BYTES, size_t LENGTH) %{
{
SWIG_csharp_bytes *p;
p = (SWIG_csharp_bytes *)$input;
if(p != SWIG_NULLPTR) {
$1 = ($1_ltype)p->ptr;
$2 = ($2_ltype)p->len;
} else {
$1 = 0;
$2 = 0;
}
}
%}
%typemap(freearg, fragment="SWIG_csharp_bytes") (const void *BYTES, size_t LENGTH) %{
{
SWIG_csharp_bytes *p;
p = (SWIG_csharp_bytes *)$input;
if(p != SWIG_NULLPTR) {
free(p->ptr);
free(p);
}
}
%}
%apply (const void *BYTES, size_t LENGTH) { (void *BYTES, size_t LENGTH) }
%include <typemaps/cdata.swg>

View File

@ -1083,6 +1083,125 @@ SWIG_CSBODY_TYPEWRAPPER(internal, protected, internal, SWIGTYPE)
%apply SWIGTYPE (CLASS::*) { SWIGTYPE (CLASS::*const) }
%apply SWIGTYPE & { SWIGTYPE (CLASS::*const&) }
/* String & length */
%pragma(csharp) imclasscode=%{
[global::System.Runtime.InteropServices.DllImport("$module", EntryPoint="SWIG_csharp_string_to_c")]
public static extern global::System.IntPtr SWIG_csharp_string_to_c0(int size, int len, [global::System.Runtime.InteropServices.In,global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPUTF8Str, SizeParamIndex=0)] string str);
public static global::System.IntPtr SWIG_csharp_string_to_c(string str) {
if (str == null)
return global::System.IntPtr.Zero;
global::System.Text.Encoding utf8 = global::System.Text.Encoding.UTF8;
return SWIG_csharp_string_to_c0(utf8.GetByteCount(str), str.Length, str);
}
[global::System.Runtime.InteropServices.DllImport("$module", EntryPoint="SWIG_csharp_string_size")]
public static extern int SWIG_csharp_string_size(global::System.IntPtr str);
[global::System.Runtime.InteropServices.DllImport("$module", EntryPoint="SWIG_csharp_string_str")]
public static extern global::System.IntPtr SWIG_csharp_string_str(global::System.IntPtr str);
public static string SWIG_c_to_csharp_string(global::System.IntPtr _str) {
int size = SWIG_csharp_string_size(_str);
if (size > 0) {
global::System.IntPtr s = SWIG_csharp_string_str(_str);
byte[] b = new byte[size];
global::System.Runtime.InteropServices.Marshal.Copy(s, b, 0, size);
global::System.Text.Encoding utf8 = global::System.Text.Encoding.UTF8;
return utf8.GetString(b);
}
return null;
}
%}
%fragment("SWIG_csharp_string", "header") %{
#ifdef __cplusplus
extern "C" {
#endif
typedef struct { int len; int size; char* str; } SWIG_csharp_string;
SWIGEXPORT void* SWIGSTDCALL SWIG_csharp_string_to_c(int size, int len, char *str) {
SWIG_csharp_string *ret;
ret = (SWIG_csharp_string*)malloc(sizeof(SWIG_csharp_string));
if (ret == SWIG_NULLPTR) {
SWIG_CSharpSetPendingException(SWIG_CSharpOutOfMemoryException, "fail to duplicate string container.");
return SWIG_NULLPTR;
}
if(size > 0) {
/* The string is in UTF8 encoding */
ret->str = (char*)malloc(size + 1);
if (ret->str == SWIG_NULLPTR) {
SWIG_CSharpSetPendingException(SWIG_CSharpOutOfMemoryException, "fail to duplicate string.");
free(ret);
return SWIG_NULLPTR;
}
memcpy(ret->str, str, size + 1);
ret->size = size; /* number of bytes in the UTF8 encoding */
ret->len = len; /* number of characters in string */
} else {
ret->str = SWIG_NULLPTR;
ret->size = 0;
ret->len = 0;
}
return ret;
}
SWIGEXPORT int SWIGSTDCALL SWIG_csharp_string_size(SWIG_csharp_string *p) {
if(p) {
return p->size;
}
return 0;
}
SWIGEXPORT char *SWIGSTDCALL SWIG_csharp_string_str(SWIG_csharp_string *p) {
if(p) {
char *ret = p->str;
free(p);
return ret;
}
return NULL;
}
#ifdef __cplusplus
}
#endif
%}
%typemap(cstype) (const char *STRING, size_t LENGTH) "string"
%typemap(ctype) (const char *STRING, size_t LENGTH) "void *"
%typemap(imtype) (const char *STRING, size_t LENGTH) "global::System.IntPtr"
%typemap(csin) (const char *STRING, size_t LENGTH) "$modulePINVOKE.SWIG_csharp_string_to_c($csinput)"
%typemap(in, canthrow=1, fragment="SWIG_csharp_string") (const char *STRING, size_t LENGTH) {
SWIG_csharp_string *p;
p = (SWIG_csharp_string *)$input;
if(p != SWIG_NULLPTR) {
$1 = ($1_ltype)p->str;
$2 = ($2_ltype)p->size; /* We use number of bytes */
} else {
$1 = 0;
$2 = 0;
}
}
%typemap(freearg, fragment="SWIG_csharp_string") (const char *STRING, size_t LENGTH) %{
{
SWIG_csharp_string *p;
p = (SWIG_csharp_string *)$input;
if(p != SWIG_NULLPTR) {
free(p->str);
free(p);
}
}
%}
%typemap(directorin) (const char *STRING, size_t LENGTH) %{
if ($1 && $2 > 0) {
$input = malloc(sizeof(SWIG_csharp_string));
if ($input) {
SWIG_csharp_string *p = (SWIG_csharp_string *)$input;
p->str = (char *)$1;
p->size = (int)$2;
}
}
%}
%typemap(csdirectorin) (const char *STRING, size_t LENGTH) "$modulePINVOKE.SWIG_c_to_csharp_string($iminput)"
%apply (const char *STRING, size_t LENGTH) { (char *STRING, size_t LENGTH) }
%apply (const char *STRING, size_t LENGTH) { (const char *STRING, int LENGTH) }
%apply (char *STRING, size_t LENGTH) { (char *STRING, int LENGTH) }
/* csharp keywords */
%include <csharpkw.swg>

20
Lib/d/cdata.i Normal file
View File

@ -0,0 +1,20 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
/* ------------------------------------------------------------
* Typemap for passing bytes with length
* ------------------------------------------------------------ */
%typemap(imtype) (const void *BYTES, size_t LENGTH) "ubyte[]"
%typemap(dtype) (const void *BYTES, size_t LENGTH) "ubyte[]"
%typemap(ctype) (const void *BYTES, size_t LENGTH) "SWIG_c_array"
%typemap(din) (const void *BYTES, size_t LENGTH) "$dinput"
%typemap(in, canthrow=1) (const void *BYTES, size_t LENGTH) %{
$1 = ($1_ltype)$input.array;
$2 = ($2_ltype)$input.len;
%}
%apply (const void *BYTES, size_t LENGTH) { (void *BYTES, size_t LENGTH) }
%include <typemaps/cdata.swg>

View File

@ -247,6 +247,7 @@
#include <stddef.h>
typedef struct { size_t len; char *str;} SWIG_c_dstring;
typedef struct { size_t len; SWIG_c_dstring *array; } SWIG_c_dstring_array;
typedef struct { size_t len; void *array; } SWIG_c_array;
%}
/*

View File

@ -1,16 +1,9 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data as strings.
* SWIG library file containing macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
%{
typedef struct SWIGCDATA {
char *data;
intgo len;
} SWIGCDATA;
%}
%fragment("cdata", "header") %{
struct swigcdata {
intgo size;
@ -18,80 +11,17 @@ struct swigcdata {
};
%}
%typemap(gotype) SWIGCDATA "[]byte"
/* ------------------------------------------------------------
* Typemap for passing bytes with length
* ------------------------------------------------------------ */
%typemap(imtype) SWIGCDATA "uint64"
%typemap(out, fragment="cdata") SWIGCDATA(struct swigcdata *swig_out) %{
swig_out = (struct swigcdata *)malloc(sizeof(*swig_out));
if (swig_out) {
swig_out->size = $1.len;
swig_out->data = malloc(swig_out->size);
if (swig_out->data) {
memcpy(swig_out->data, $1.data, swig_out->size);
}
}
$result = *(long long *)(void **)&swig_out;
%typemap(gotype) (const void *BYTES, size_t LENGTH) "[]byte"
%typemap(in) (const void *BYTES, size_t LENGTH)
%{
$1 = ($1_ltype)$input.array;
$2 = ($2_ltype)$input.len;
%}
%apply (const void *BYTES, size_t LENGTH) { (void *BYTES, size_t LENGTH) }
%typemap(goout) SWIGCDATA %{
{
type swigcdata struct { size int; data uintptr }
p := (*swigcdata)(unsafe.Pointer(uintptr($1)))
if p == nil || p.data == 0 {
$result = nil
} else {
b := make([]byte, p.size)
a := (*[0x7fffffff]byte)(unsafe.Pointer(p.data))[:p.size]
copy(b, a)
Swig_free(p.data)
Swig_free(uintptr(unsafe.Pointer(p)))
$result = b
}
}
%}
%include <typemaps/cdata.swg>
/* -----------------------------------------------------------------------------
* %cdata(TYPE [, NAME])
*
* Convert raw C data to a binary string.
* ----------------------------------------------------------------------------- */
%define %cdata(TYPE,NAME...)
%insert("header") {
#if #NAME == ""
static SWIGCDATA cdata_##TYPE(TYPE *ptr, int nelements) {
#else
static SWIGCDATA cdata_##NAME(TYPE *ptr, int nelements) {
#endif
SWIGCDATA d;
d.data = (char *) ptr;
#if #TYPE != "void"
d.len = nelements*sizeof(TYPE);
#else
d.len = nelements;
#endif
return d;
}
}
%typemap(default) int nelements "$1 = 1;"
#if #NAME == ""
SWIGCDATA cdata_##TYPE(TYPE *ptr, int nelements);
#else
SWIGCDATA cdata_##NAME(TYPE *ptr, int nelements);
#endif
%enddef
%typemap(default) int nelements;
%rename(cdata) ::cdata_void(void *ptr, int nelements);
%cdata(void);
/* Memory move function. Due to multi-argument typemaps this appears
to be wrapped as
void memmove(void *data, const char *s); */
void memmove(void *data, char *indata, int inlen);

View File

@ -498,31 +498,34 @@ rvrdeleter.reset($1); %}
/* String & length */
%typemap(gotype) (char *STRING, size_t LENGTH) "string"
%typemap(gotype) (const char *STRING, size_t LENGTH) "string"
%typemap(in) (char *STRING, size_t LENGTH)
%typemap(in) (const char *STRING, size_t LENGTH)
%{
$1 = ($1_ltype)$input.p;
$2 = ($2_ltype)$input.n;
%}
%typemap(out,fragment="AllocateString") (char *STRING, size_t LENGTH)
%typemap(out,fragment="AllocateString") (const char *STRING, size_t LENGTH)
%{ $result = Swig_AllocateString((char*)$1, (size_t)$2); %}
%typemap(goout,fragment="CopyString") (char *STRING, size_t LENGTH)
%typemap(goout,fragment="CopyString") (const char *STRING, size_t LENGTH)
%{ $result = swigCopyString($1) %}
%typemap(directorin,fragment="AllocateString") (char *STRING, size_t LENGTH)
%typemap(directorin,fragment="AllocateString") (const char *STRING, size_t LENGTH)
%{ $input = Swig_AllocateString((char*)$1, $2); %}
%typemap(godirectorin,fragment="CopyString") (char *STRING, size_t LENGTH)
%typemap(godirectorin,fragment="CopyString") (const char *STRING, size_t LENGTH)
%{ $result = swigCopyString($input) %}
%typemap(directorout) (char *STRING, size_t LENGTH)
%typemap(directorout) (const char *STRING, size_t LENGTH)
%{
$1 = ($1_ltype)$input.p;
$2 = ($2_ltype)$input.n;
%}
%apply (const char *STRING, size_t LENGTH) { (const char *STRING, int LENGTH) }
%apply (const char *STRING, size_t LENGTH) { (char *STRING, size_t LENGTH) }
%apply (char *STRING, size_t LENGTH) { (char *STRING, int LENGTH) }
/* The int & type needs to convert to intgo. */

19
Lib/guile/cdata.i Normal file
View File

@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
/* ------------------------------------------------------------
* Typemap for passing bytes with length
* ------------------------------------------------------------ */
%typemap(in) (const void *BYTES, size_t LENGTH) {
size_t temp;
$1 = ($1_ltype) SWIG_Guile_scm2newstr($input, &temp);
$2 = ($2_ltype) temp;
}
%apply (const void *BYTES, size_t LENGTH) { (void *BYTES, size_t LENGTH) }
%include <typemaps/cdata.swg>

View File

@ -91,15 +91,9 @@ static swig_module_info *SWIG_Guile_GetModule(void *SWIGUNUSEDPARM(clientdata));
SWIGINTERN char *
SWIG_Guile_scm2newstr(SCM str, size_t *len) {
#define FUNC_NAME "SWIG_Guile_scm2newstr"
char *ret;
SCM_ASSERT (scm_is_string(str), str, 1, FUNC_NAME);
ret = scm_to_utf8_string(str);
if (!ret) return NULL;
if (len) *len = strlen(ret) - 1;
return ret;
return scm_to_utf8_stringn(str, len);
#undef FUNC_NAME
}

47
Lib/java/cdata.i Normal file
View File

@ -0,0 +1,47 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
/* ------------------------------------------------------------
* Typemap for passing bytes with length
* ------------------------------------------------------------ */
/* String & length */
%typemap(jni) (const void *BYTES, size_t LENGTH) "jbyteArray"
%typemap(jtype) (const void *BYTES, size_t LENGTH) "byte[]"
%typemap(jstype) (const void *BYTES, size_t LENGTH) "byte[]"
%typemap(javain) (const void *BYTES, size_t LENGTH) "$javainput"
%typemap(freearg) (const void *BYTES, size_t LENGTH) ""
%typemap(in) (const void *BYTES, size_t LENGTH) {
if ($input) {
$1 = ($1_ltype) JCALL2(GetByteArrayElements, jenv, $input, 0);
$2 = ($2_type) JCALL1(GetArrayLength, jenv, $input);
} else {
$1 = 0;
$2 = 0;
}
}
%typemap(argout) (const void *BYTES, size_t LENGTH) {
if ($input) JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *)$1, JNI_ABORT);
}
%typemap(directorin, descriptor="[B", noblock=1) (const void *BYTES, size_t LENGTH) {
$input = 0;
if ($1) {
$input = JCALL1(NewByteArray, jenv, (jsize)$2);
if (!$input) return $null;
JCALL4(SetByteArrayRegion, jenv, $input, 0, (jsize)$2, (jbyte *)$1);
}
Swig::LocalRefGuard $1_refguard(jenv, $input);
}
%typemap(javadirectorin, descriptor="[B") (const void *BYTES, size_t LENGTH) "$jniinput"
%apply (const void *BYTES, size_t LENGTH) { (void *BYTES, size_t LENGTH) }
/* Enable write-back for non-const version */
%typemap(argout) (void *BYTES, size_t LENGTH) {
if ($input) JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *)$1, 0);
}
%typemap(directorargout, noblock=1) (void *BYTES, size_t LENGTH)
{ if ($input && $1) JCALL4(GetByteArrayRegion, jenv, $input, 0, (jsize)$2, (jbyte *)$1); }
%include <typemaps/cdata.swg>

View File

@ -1421,10 +1421,24 @@ SWIG_PROXY_CONSTRUCTOR(true, true, SWIGTYPE)
%apply SWIGTYPE & { SWIGTYPE (CLASS::*const&) }
/* String & length */
%pragma(java) jniclasscode=%{
public final static byte[] SWIG_string_to_utf8_bytes(String str) {
try {
return str.getBytes("UTF-8");
} catch(java.lang.Exception e) {}
return new byte[0];
}
public final static String SWIG_utf8_bytes_to_string(byte[] b_utf8) {
try {
return new String(b_utf8, "UTF-8");
} catch(java.lang.Exception e) {}
return "";
}
%}
%typemap(jni) (const char *STRING, size_t LENGTH) "jbyteArray"
%typemap(jtype) (const char *STRING, size_t LENGTH) "byte[]"
%typemap(jstype) (const char *STRING, size_t LENGTH) "byte[]"
%typemap(javain) (const char *STRING, size_t LENGTH) "$javainput"
%typemap(jstype) (const char *STRING, size_t LENGTH) "String"
%typemap(javain) (const char *STRING, size_t LENGTH) "$moduleJNI.SWIG_string_to_utf8_bytes($javainput)"
%typemap(freearg) (const char *STRING, size_t LENGTH) ""
%typemap(in) (const char *STRING, size_t LENGTH) {
if ($input) {
@ -1438,7 +1452,7 @@ SWIG_PROXY_CONSTRUCTOR(true, true, SWIGTYPE)
%typemap(argout) (const char *STRING, size_t LENGTH) {
if ($input) JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *)$1, JNI_ABORT);
}
%typemap(directorin, descriptor="[B", noblock=1) (const char *STRING, size_t LENGTH) {
%typemap(directorin, descriptor="Ljava/lang/String;", noblock=1) (const char *STRING, size_t LENGTH) {
$input = 0;
if ($1) {
$input = JCALL1(NewByteArray, jenv, (jsize)$2);
@ -1447,7 +1461,7 @@ SWIG_PROXY_CONSTRUCTOR(true, true, SWIGTYPE)
}
Swig::LocalRefGuard $1_refguard(jenv, $input);
}
%typemap(javadirectorin, descriptor="[B") (const char *STRING, size_t LENGTH) "$jniinput"
%typemap(javadirectorin, descriptor="[B") (const char *STRING, size_t LENGTH) "SWIG_utf8_bytes_to_string($jniinput)"
%apply (const char *STRING, size_t LENGTH) { (char *STRING, size_t LENGTH) }
/* Enable write-back for non-const version */
%typemap(argout) (char *STRING, size_t LENGTH) {

21
Lib/lua/cdata.i Normal file
View File

@ -0,0 +1,21 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
/* ------------------------------------------------------------
* Typemap for passing bytes with length
* ------------------------------------------------------------ */
%typemap(in,checkfn="lua_isstring") (const void *BYTES, size_t LENGTH) {
size_t len;
$1 = ($1_ltype)lua_tolstring(L, $input, &len);
SWIG_contract_assert($1, "non null string");
$2 = ($2_ltype)len;
}
%apply (const void *BYTES, size_t LENGTH) { (void *BYTES, size_t LENGTH) }
%include <typemaps/cdata.swg>

View File

@ -52,6 +52,15 @@
%typemap(consttab) SWIGTYPE (CLASS::*)
{ SWIG_LUA_CONSTTAB_BINARY("$symname", sizeof($type),&$value, $1_descriptor) }
%typemap(in,checkfn="lua_isstring") (const char *STRING, size_t LENGTH) {
size_t len;
$1 = ($1_ltype)lua_tolstring(L, $input, &len);
SWIG_contract_assert($1, "non null string");
$2 = ($2_ltype)len;
}
%apply (const char *STRING, size_t LENGTH) { (char *STRING, size_t LENGTH) }
%apply (const char *STRING, size_t LENGTH) { (const char *STRING, int LENGTH) }
%apply (char *STRING, size_t LENGTH) { (char *STRING, int LENGTH) }
/* -----------------------------------------------------------------------------
* Overloaded operator support

View File

@ -1 +0,0 @@
%include <typemaps/cdata.swg>

View File

@ -1 +0,0 @@
%include <typemaps/cdata.swg>

19
Lib/php/cdata.i Normal file
View File

@ -0,0 +1,19 @@
/* -----------------------------------------------------------------------------
* cdata.i
*
* SWIG library file containing macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
/* ------------------------------------------------------------
* Typemap for passing bytes with length
* ------------------------------------------------------------ */
%typemap(in, phptype="string") (const void *BYTES, size_t LENGTH) %{
convert_to_string(&$input);
$1 = ($1_ltype) Z_STRVAL($input);
$2 = ($2_ltype) Z_STRLEN($input);
%}
%apply (const void *BYTES, size_t LENGTH) { (void *BYTES, size_t LENGTH) }
%include <typemaps/cdata.swg>

View File

@ -83,11 +83,21 @@
$1 = ($1_ltype) Z_STRVAL($input);
%}
%typemap(in, phptype="string") (char *STRING, int LENGTH), (char *STRING, size_t LENGTH) %{
%typemap(in, phptype="string", noblock=1) (const char *STRING, size_t LENGTH) %{
convert_to_string(&$input);
$1 = ($1_ltype) Z_STRVAL($input);
$2 = ($2_ltype) Z_STRLEN($input);
%}
%typemap(directorin, noblock=1) (const char *STRING, size_t LENGTH) {
if (!$1) {
ZVAL_EMPTY_STRING($input);
} else {
ZVAL_STRINGL($input, (const char*)$1, (size_t)$2);
}
}
%apply (const char *STRING, size_t LENGTH) { (const char *STRING, int LENGTH) }
%apply (const char *STRING, size_t LENGTH) { (char *STRING, size_t LENGTH) }
%apply (char *STRING, size_t LENGTH) { (char *STRING, int LENGTH) }
/* Object passed by value. Convert to a pointer */
%typemap(in, phptype="SWIGTYPE") SWIGTYPE ($&1_ltype tmp)

View File

@ -1 +0,0 @@
%include <typemaps/cdata.swg>

View File

@ -1 +0,0 @@
%include <typemaps/cdata.swg>

View File

@ -1 +0,0 @@
%include <typemaps/cdata.swg>

View File

@ -148,7 +148,8 @@ SWIG_SciString_AsCharPtrAndSize(void *pvApiCtx, int iVar, char **pcValue, size_t
}
if (piLength != NULL) {
*piLength = strlen(pstString);
/* Add the null termination */
*piLength = iLen + 1;
}
return SWIG_OK;

View File

@ -11,7 +11,7 @@ SWIG_AsPtr_dec(std::string)(int iVar, std::string **pstValue) {
if (SWIG_IsOK((SWIG_SciString_AsCharPtrAndSize(pvApiCtx, iVar, &buf, &size, &alloc, SWIG_Scilab_GetFuncName())))) {
if (buf) {
if (pstValue) {
*pstValue = new std::string(buf, size);
*pstValue = new std::string(buf, size - 1);
}
if (alloc == SWIG_NEWOBJ) {
delete[] buf;

View File

@ -1 +0,0 @@
%include <typemaps/cdata.swg>

View File

@ -1,30 +1,246 @@
/* -----------------------------------------------------------------------------
* cdata.swg
*
* This library file contains macros for manipulating raw C data as strings.
* This library file contains macros for manipulating raw C data.
* ----------------------------------------------------------------------------- */
#ifdef SWIGGO
#define swig_cdata_nelems_type int
#else
#define swig_cdata_nelems_type size_t
#endif
%{
typedef struct SWIGCDATA {
char *data;
#ifdef SWIGGO
intgo len;
#else
size_t len;
#endif
} SWIGCDATA;
%}
/* ------------------------------------------------------------
* Typemap for passing bytes with length
* ------------------------------------------------------------ */
#if !defined SWIGJAVA && !defined SWIGGO && !defined SWIGD && !defined SWIGPHP &&\
!defined SWIGGUILE && !defined SWIGCSHARP && !defined SWIGLUA &&\
!defined SWIGMZSCHEME
%typemap(in,noblock=0,fragment="SWIG_AsCharPtrAndSize")
(const void *BYTES, size_t LENGTH) (int res, void *buf = 0, size_t len = 0, int alloc = 0)
{
res = SWIG_AsCharPtrAndSize($input, (char **)&buf, &len, &alloc);
if (!SWIG_IsOK(res)) {
%argument_fail(res,"$type",$symname, $argnum);
}
$1 = %reinterpret_cast(buf, $1_ltype);
$2 = %numeric_cast(len, $2_ltype);
}
%typemap(freearg,noblock=1,match="in")
(const void *BYTES, size_t LENGTH)
{
if (alloc$argnum == SWIG_NEWOBJ) {
%delete_array((char*)(buf$argnum));
}
}
%typemap(directorin,noblock=1,fragment="SWIG_FromCharPtrAndSize")
(const void *BYTES, size_t LENGTH)
{
if ($1 && $2 > 0) {
$input = SWIG_FromCharPtrAndSize((const char*)$1, (size_t)$2);
} else {
$input = SWIG_FromCharPtrAndSize("", 0);
}
}
%typemap(in) (void *BYTES, size_t LENGTH) = (const void *BYTES, size_t LENGTH);
%typemap(freearg) (void *BYTES, size_t LENGTH) = (const void *BYTES, size_t LENGTH);
%typemap(directorin) (void *BYTES, size_t LENGTH) = (const void *BYTES, size_t LENGTH);
%typemap(in) (const void *BYTES, int LENGTH) = (const void *BYTES, size_t LENGTH);
%typemap(freearg) (const void *BYTES, int LENGTH) = (const void *BYTES, size_t LENGTH);
%typemap(directorin) (const void *BYTES, int LENGTH) = (const void *BYTES, size_t LENGTH);
%typemap(in) (void *BYTES, int LENGTH) = (const void *BYTES, int LENGTH);
%typemap(freearg) (void *BYTES, int LENGTH) = (const void *BYTES, int LENGTH);
%typemap(directorin) (void *BYTES, int LENGTH) = (const void *BYTES, int LENGTH);
%typemap(in) (const void *indata, size_t inlen) = (const void *BYTES, size_t LENGTH);
%typemap(freearg) (const void *indata, size_t inlen) = (const void *BYTES, size_t LENGTH);
%typemap(directorin) (const void *indata, size_t inlen) = (const void *BYTES, size_t LENGTH);
#else
%apply (const void *BYTES, size_t LENGTH) { (const void *BYTES, int LENGTH) }
%apply (void *BYTES, size_t LENGTH) { (void *BYTES, int LENGTH) }
%apply (const void *BYTES, size_t LENGTH) { (const void *indata, size_t inlen) }
#endif
/* -----------------------------------------------------------------------------
* Typemaps for returning binary data
* ----------------------------------------------------------------------------- */
#ifdef SWIGJAVA
%typemap(jni) SWIGCDATA "jbyteArray"
%typemap(jtype) SWIGCDATA "byte[]"
%typemap(jstype) SWIGCDATA "byte[]"
%fragment("SWIG_JavaArrayOutCDATA", "header") {
static jbyteArray SWIG_JavaArrayOutCDATA(JNIEnv *jenv, char *result, jsize sz) {
jbyteArray jresult;
jbyte *arr;
int i;
jresult = JCALL1(NewByteArray, jenv, sz);
if (!jresult) {
return SWIG_NULLPTR;
}
arr = JCALL2(GetByteArrayElements, jenv, jresult, 0);
if (!arr) {
return SWIG_NULLPTR;
}
for (i=0; i<sz; i++) {
arr[i] = (jbyte)result[i];
}
JCALL3(ReleaseByteArrayElements, jenv, jresult, arr, 0);
return jresult;
}
}
%typemap(out, fragment="SWIG_JavaArrayOutCDATA") SWIGCDATA
%{$result = SWIG_JavaArrayOutCDATA(jenv, (char *)$1.data, $1.len); %}
%typemap(javaout) SWIGCDATA {
return $jnicall;
}
#elif SWIGGO
%typemap(gotype) SWIGCDATA "[]byte"
%typemap(imtype) SWIGCDATA "uint64"
%typemap(out, fragment="cdata") SWIGCDATA(struct swigcdata *swig_out) %{
swig_out = (struct swigcdata *)malloc(sizeof(*swig_out));
if (swig_out) {
swig_out->size = $1.len;
swig_out->data = malloc(swig_out->size);
if (swig_out->data) {
memcpy(swig_out->data, $1.data, swig_out->size);
}
}
$result = *(long long *)(void **)&swig_out;
%}
%typemap(goout) SWIGCDATA %{
{
type swigcdata struct { size int; data uintptr }
p := (*swigcdata)(unsafe.Pointer(uintptr($1)))
if p == nil || p.data == 0 {
$result = nil
} else {
b := make([]byte, p.size)
a := (*[0x7fffffff]byte)(unsafe.Pointer(p.data))[:p.size]
copy(b, a)
Swig_free(p.data)
Swig_free(uintptr(unsafe.Pointer(p)))
$result = b
}
}
%}
#elif SWIGD
%typemap(dtype) SWIGCDATA "ubyte[]"
%typemap(imtype) SWIGCDATA "ubyte[]"
%typemap(ctype) SWIGCDATA "SWIG_c_array"
%typemap(out) SWIGCDATA %{
$result.array = $1.data;
$result.len = $1.len;
%}
%typemap(dout) SWIGCDATA { return $imcall; }
#elif SWIGPHP
%typemap(out) SWIGCDATA {
ZVAL_STRINGL($result, $1.data, $1.len);
}
#elif SWIGGUILE
%typemap(out) SWIGCDATA {
$result = scm_from_locale_stringn($1.data,$1.len);
}
#elif SWIGCSHARP
%pragma(csharp) imclasscode=%{
[global::System.Runtime.InteropServices.DllImport("$module", EntryPoint="SWIG_csharp_data")]
public static extern int SWIG_csharp_data(global::System.IntPtr data, ref global::System.IntPtr m);
%}
%fragment("SWIG_csharp_data", "header") %{
#ifdef __cplusplus
extern "C" {
#endif
SWIGEXPORT int SWIGSTDCALL SWIG_csharp_data(SWIGCDATA *d, void **ptr) {
int ret;
ret = 0;
if (d != SWIG_NULLPTR) {
if (d->len > 0 && d->data != SWIG_NULLPTR) {
*ptr = (void *)d->data;
ret = (int)d->len;
}
free(d); /* allocated in 'out' typemap */
}
return ret;
}
#ifdef __cplusplus
}
#endif
%}
%typemap(ctype) SWIGCDATA "SWIGCDATA *"
%typemap(imtype) SWIGCDATA "global::System.IntPtr"
%typemap(cstype) SWIGCDATA "byte[]"
%typemap(out) SWIGCDATA %{
$result = (SWIGCDATA*)malloc(sizeof($1));
if($result != SWIG_NULLPTR) {
memcpy($result, &$1, sizeof($1));
}
%}
%typemap(csout, fragment="SWIG_csharp_data") SWIGCDATA {
global::System.IntPtr mm;
byte[] ret;
int size;
mm = global::System.IntPtr.Zero;
size = $modulePINVOKE.SWIG_csharp_data($imcall, ref mm);
ret = new byte[size];
if (size > 0) {
System.Runtime.InteropServices.Marshal.Copy(mm, ret, 0, size);
}
return ret;
}
#elif SWIGLUA
%typemap(out) SWIGCDATA %{lua_pushlstring(L, $1.data,$1.len); SWIG_arg++;%}
#elif SWIGMZSCHEME
#else
%typemap(out,noblock=1,fragment="SWIG_FromCharPtrAndSize") SWIGCDATA {
%set_output(SWIG_FromCharPtrAndSize($1.data,$1.len));
}
%typemap(in) (const void *indata, size_t inlen) = (char *STRING, size_t SIZE);
#endif
/* -----------------------------------------------------------------------------
* %cdata(TYPE [, NAME])
*
* Convert raw C data to a binary string.
* Convert raw C data to binary
* ----------------------------------------------------------------------------- */
%define %cdata(TYPE,NAME...)
@ -34,9 +250,9 @@ typedef struct SWIGCDATA {
extern "C" {
#endif
#if #NAME == ""
static SWIGCDATA cdata_##TYPE(TYPE *ptr, size_t nelements)
static SWIGCDATA cdata_##TYPE(TYPE *ptr, swig_cdata_nelems_type nelements)
#else
static SWIGCDATA cdata_##NAME(TYPE *ptr, size_t nelements)
static SWIGCDATA cdata_##NAME(TYPE *ptr, swig_cdata_nelems_type nelements)
#endif
{
SWIGCDATA d;
@ -57,13 +273,13 @@ static SWIGCDATA cdata_##NAME(TYPE *ptr, size_t nelements)
extern "C"
#endif
#if #NAME == ""
SWIGCDATA cdata_##TYPE(TYPE *ptr, size_t nelements = 1);
SWIGCDATA cdata_##TYPE(TYPE *ptr, swig_cdata_nelems_type nelements = 1);
#else
SWIGCDATA cdata_##NAME(TYPE *ptr, size_t nelements = 1);
SWIGCDATA cdata_##NAME(TYPE *ptr, swig_cdata_nelems_type nelements = 1);
#endif
%enddef
%rename(cdata) ::cdata_void(void *ptr, size_t nelements = 1);
%rename(cdata) ::cdata_void(void *ptr, swig_cdata_nelems_type nelements = 1);
%cdata(void);

View File

@ -414,8 +414,7 @@
/* Here len doesn't include the '0' terminator */
%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize)
(Char *STRING, size_t LENGTH) (int res, Char *buf = 0, size_t size = 0, int alloc = 0),
(const Char *STRING, size_t LENGTH) (int res, Char *buf = 0, size_t size = 0, int alloc = 0)
(Char *STRING, size_t LENGTH) (int res, Char *buf = 0, size_t size = 0, int alloc = 0)
{
res = SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc);
if (!SWIG_IsOK(res)) {
@ -427,70 +426,27 @@
%typemap(freearg,noblock=1,match="in") (Char *STRING, size_t LENGTH) {
if (alloc$argnum == SWIG_NEWOBJ) SWIG_DeleteCharArray(buf$argnum);
}
%typemap(directorin,noblock=1,fragment=#SWIG_FromCharPtrAndSize)
(Char *STRING, size_t LENGTH)
{
if ($1 && $2 > 0) {
$input = SWIG_FromCharPtrAndSize((const char*)$1, (size_t)$2);
} else {
$input = SWIG_FromCharPtrAndSize("", 0);
}
}
%typemap(in) (const Char *STRING, size_t LENGTH) = (Char *STRING, size_t LENGTH);
%typemap(freearg) (const Char *STRING, size_t LENGTH) = (Char *STRING, size_t LENGTH);
%typemap(directorin) (const Char *STRING, size_t LENGTH) = (Char *STRING, size_t LENGTH);
/* old 'int' form */
%typemap(in) (Char *STRING, int LENGTH) = (Char *STRING, size_t LENGTH);
%typemap(freearg) (Char *STRING, int LENGTH) = (Char *STRING, size_t LENGTH);
/* Here size includes the '0' terminator */
%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize)
(Char *STRING, size_t SIZE) (int res, Char *buf = 0, size_t size = 0, int alloc = 0),
(const Char *STRING, size_t SIZE) (int res, Char *buf = 0, size_t size = 0, int alloc = 0)
{
res = SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc);
if (!SWIG_IsOK(res)) {
%argument_fail(res,"$type",$symname, $argnum);
}
$1 = %reinterpret_cast(buf, $1_ltype);
$2 = %numeric_cast(size, $2_ltype);
}
%typemap(freearg,noblock=1,match="in") (Char *STRING, size_t SIZE) {
if (alloc$argnum == SWIG_NEWOBJ) SWIG_DeleteCharArray(buf$argnum);
}
/* old 'int' form */
%typemap(in) (Char *STRING, int SIZE) = (Char *STRING, size_t SIZE);
%typemap(freearg) (Char *STRING, int SIZE) = (Char *STRING, size_t SIZE);
/* reverse order versions */
/* Here len doesn't include the '0' terminator */
%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize)
(size_t LENGTH, Char *STRING) (int res, Char *buf = 0, size_t size = 0, int alloc = 0),
(size_t LENGTH, const Char *STRING) (int res, Char *buf = 0, size_t size = 0, int alloc = 0)
{
res = SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc);
if (!SWIG_IsOK(res)) {
%argument_fail(res,"$type",$symname, $argnum);
}
$2 = %reinterpret_cast(buf, $2_ltype) ;
$1 = %numeric_cast(size - 1, $1_ltype) ;
}
%typemap(freearg, noblock=1, match="in") (size_t LENGTH, Char *STRING) {
if (alloc$argnum == SWIG_NEWOBJ) SWIG_DeleteCharArray(buf$argnum);
}
/* old 'int' form */
%typemap(in) (int LENGTH, Char *STRING) = (size_t LENGTH, Char *STRING);
%typemap(freearg) (int LENGTH, Char *STRING) = (size_t LENGTH, Char *STRING);
/* Here size includes the '0' terminator */
%typemap(in,noblock=1,fragment=#SWIG_AsCharPtrAndSize)
(size_t SIZE, Char *STRING) (int res, Char *buf = 0, size_t size = 0, int alloc = 0),
(size_t SIZE, const Char *STRING) (int res, Char *buf = 0, size_t size = 0, int alloc = 0)
{
res = SWIG_AsCharPtrAndSize($input, &buf, &size, &alloc);
if (!SWIG_IsOK(res)) {
%argument_fail(res, "$type",$symname, $argnum);
}
$2 = %reinterpret_cast(buf, $2_ltype) ;
$1 = %numeric_cast(size, $1_ltype) ;
}
%typemap(freearg, noblock=1, match="in") (size_t SIZE, Char *STRING) {
if (alloc$argnum == SWIG_NEWOBJ) SWIG_DeleteCharArray(buf$argnum);
}
/* old 'int' form */
%typemap(in) (int SIZE, Char *STRING) = (size_t SIZE, Char *STRING);
%typemap(freearg) (int SIZE, Char *STRING) = (size_t SIZE, Char *STRING);
%typemap(directorin) (Char *STRING, int LENGTH) = (Char *STRING, size_t LENGTH);
%typemap(in) (const Char *STRING, int LENGTH) = (Char *STRING, size_t LENGTH);
%typemap(freearg) (const Char *STRING, int LENGTH) = (Char *STRING, size_t LENGTH);
%typemap(directorin) (const Char *STRING, int LENGTH) = (Char *STRING, size_t LENGTH);
%enddef