From cb9c1e1c0437ca27edb7f4a6202a0ead3611de70 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 1 Dec 2014 10:18:54 -0500 Subject: [PATCH] lib/c: intro `CByteArray` MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- lib/c.nit | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/c.nit b/lib/c.nit index 80701ab..6c50cf8 100644 --- a/lib/c.nit +++ b/lib/c.nit @@ -99,6 +99,43 @@ extern class NativeCIntArray `{ int* `} redef fun +(offset) `{ return recv + offset; `} end +# Wrapper around an array of `unsigned char` in C (`unsigned char*`) with length and destroy state +class CByteArray + super CArray[Int] + redef type NATIVE: NativeCByteArray + + # Allocate a new array of `size` + init(size: Int) + do + native_array = new NativeCByteArray(size) + super size + end + + # Build from an `Array[Int]` + new from(array: Array[Int]) + do + var carray = new CByteArray(array.length) + for i in array.length.times do + carray[i] = array[i] + end + return carray + end +end + +# An array of `unsigned char` in C (`unsigned char*`) +extern class NativeCByteArray `{ unsigned char* `} + super NativeCArray + redef type E: Int + + # Allocate a new array of `size` + new(size: Int) `{ return calloc(size, sizeof(unsigned char)); `} + + redef fun [](index) `{ return recv[index]; `} + redef fun []=(index, val) `{ recv[index] = val; `} + + redef fun +(offset) `{ return recv + offset; `} +end + redef class NativeString super NativeCArray redef type E: Char -- 1.7.9.5