-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from jonas/bindings
Add initial bindings subproject
- Loading branch information
Showing
22 changed files
with
716 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
bindings/iconv/src/main/scala/org/scalanative/bindgen/bindings/iconv/iconv.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.scalanative.bindgen.bindings.iconv | ||
|
||
import scala.scalanative._ | ||
import scala.scalanative.native._ | ||
|
||
@native.extern | ||
object iconv { | ||
type iconv_t = native.Ptr[Byte] | ||
def iconv_open(__tocode: native.CString, __fromcode: native.CString): native.Ptr[Byte] = native.extern | ||
def iconv(__cd: native.Ptr[Byte], __inbuf: native.Ptr[native.CString], __inbytesleft: native.Ptr[native.CSize], __outbuf: native.Ptr[native.CString], __outbytesleft: native.Ptr[native.CSize]): native.CSize = native.extern | ||
def iconv_close(__cd: native.Ptr[Byte]): native.CInt = native.extern | ||
|
||
object defines { | ||
val _ICONV_H: native.CInt = 1 | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
bindings/iconv/src/test/scala/org/scalanative/bindgen/bindings/tests/IconvSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.scalanative.bindgen.bindings.iconv.tests | ||
|
||
import org.scalatest.FunSpec | ||
|
||
class IconvSpec extends FunSpec { | ||
describe("iconv") { | ||
it("should convert back and forth between UTF-8 and ISO-8859-1") { | ||
//#usage-example | ||
import org.scalanative.bindgen.bindings.iconv.iconv._ | ||
import scala.scalanative.native._ | ||
import java.nio.charset.Charset | ||
|
||
val UTF8 = Charset.forName("UTF-8") | ||
val encode = iconv_open(c"UTF-8", c"ISO-8859-1") | ||
val decode = iconv_open(c"ISO-8859-1", c"UTF-8") | ||
|
||
Zone { implicit zone => | ||
val originalBuf = toCString("øre", UTF8) // Ear in Danish | ||
val originalBufPtr = alloc[CString] | ||
!originalBufPtr = originalBuf | ||
val originalBytesLeft = alloc[CSize] | ||
!originalBytesLeft = string.strlen(originalBuf) | ||
//#usage-example | ||
assert(!originalBytesLeft == 4) | ||
//#usage-example | ||
|
||
val translatedBuf = alloc[Byte](32) | ||
val translatedBufPtr = alloc[CString] | ||
!translatedBufPtr = translatedBuf | ||
val translatedBytesLeft = alloc[CSize] | ||
!translatedBytesLeft = 32 | ||
|
||
val translatedCode = iconv( | ||
encode, | ||
originalBufPtr, | ||
originalBytesLeft, | ||
translatedBufPtr, | ||
translatedBytesLeft | ||
) | ||
//#usage-example | ||
|
||
assert(translatedCode == 0) | ||
|
||
!translatedBufPtr = translatedBuf | ||
!translatedBytesLeft = string.strlen(translatedBuf) | ||
|
||
val roundtripBuf = alloc[Byte](32) | ||
val roundtripBufPtr = alloc[CString] | ||
!roundtripBufPtr = roundtripBuf | ||
val roundtripBytesLeft = alloc[CSize] | ||
!roundtripBytesLeft = 32 | ||
|
||
val roundtripCode = iconv( | ||
decode, | ||
translatedBufPtr, | ||
translatedBytesLeft, | ||
roundtripBufPtr, | ||
roundtripBytesLeft | ||
) | ||
|
||
assert(string.strcmp(originalBuf, roundtripBuf) == 0) | ||
//#usage-example | ||
} | ||
//#usage-example | ||
|
||
//#usage-example | ||
iconv_close(encode) | ||
//#usage-example | ||
iconv_close(decode) | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
bindings/posix/src/main/scala/org/scalanative/bindgen/bindings/posix/fnmatch.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.scalanative.bindgen.bindings.posix | ||
|
||
import scala.scalanative._ | ||
import scala.scalanative.native._ | ||
|
||
@native.extern | ||
object fnmatch { | ||
def fnmatch(__pattern: native.CString, __name: native.CString, __flags: native.CInt): native.CInt = native.extern | ||
|
||
object defines { | ||
val _FNMATCH_H: native.CInt = 1 | ||
val FNM_NOMATCH: native.CInt = 1 | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
bindings/posix/src/main/scala/org/scalanative/bindgen/bindings/posix/regex.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.scalanative.bindgen.bindings.posix | ||
|
||
import scala.scalanative._ | ||
import scala.scalanative.native._ | ||
|
||
@native.extern | ||
object regex { | ||
type enum_reg_errcode_t = native.CUnsignedInt | ||
object enum_reg_errcode_t { | ||
final val REG_NOERROR: enum_reg_errcode_t = 0.toUInt | ||
final val REG_NOMATCH: enum_reg_errcode_t = 1.toUInt | ||
final val REG_BADPAT: enum_reg_errcode_t = 2.toUInt | ||
final val REG_ECOLLATE: enum_reg_errcode_t = 3.toUInt | ||
final val REG_ECTYPE: enum_reg_errcode_t = 4.toUInt | ||
final val REG_EESCAPE: enum_reg_errcode_t = 5.toUInt | ||
final val REG_ESUBREG: enum_reg_errcode_t = 6.toUInt | ||
final val REG_EBRACK: enum_reg_errcode_t = 7.toUInt | ||
final val REG_EPAREN: enum_reg_errcode_t = 8.toUInt | ||
final val REG_EBRACE: enum_reg_errcode_t = 9.toUInt | ||
final val REG_BADBR: enum_reg_errcode_t = 10.toUInt | ||
final val REG_ERANGE: enum_reg_errcode_t = 11.toUInt | ||
final val REG_ESPACE: enum_reg_errcode_t = 12.toUInt | ||
final val REG_BADRPT: enum_reg_errcode_t = 13.toUInt | ||
final val REG_EEND: enum_reg_errcode_t = 14.toUInt | ||
final val REG_ESIZE: enum_reg_errcode_t = 15.toUInt | ||
final val REG_ERPAREN: enum_reg_errcode_t = 16.toUInt | ||
} | ||
|
||
type s_reg_t = native.CLong | ||
type active_reg_t = native.CUnsignedLong | ||
type reg_syntax_t = native.CUnsignedLong | ||
type reg_errcode_t = enum_reg_errcode_t | ||
type struct_re_pattern_buffer = native.CArray[Byte, native.Nat.Digit[native.Nat._6, native.Nat._4]] | ||
type regex_t = struct_re_pattern_buffer | ||
type regoff_t = native.CInt | ||
type struct_regmatch_t = native.CStruct2[regoff_t, regoff_t] | ||
type regmatch_t = struct_regmatch_t | ||
val re_syntax_options: reg_syntax_t = native.extern | ||
def regcomp(__preg: native.Ptr[regex_t], __pattern: native.CString, __cflags: native.CInt): native.CInt = native.extern | ||
def regexec(__preg: native.Ptr[regex_t], __string: native.CString, __nmatch: native.CSize, __pmatch: native.Ptr[regmatch_t], __eflags: native.CInt): native.CInt = native.extern | ||
def regerror(__errcode: native.CInt, __preg: native.Ptr[regex_t], __errbuf: native.CString, __errbuf_size: native.CSize): native.CSize = native.extern | ||
def regfree(__preg: native.Ptr[regex_t]): Unit = native.extern | ||
|
||
object defines { | ||
val _REGEX_H: native.CInt = 1 | ||
val REG_EXTENDED: native.CInt = 1 | ||
val REG_NOTBOL: native.CInt = 1 | ||
} | ||
|
||
object implicits { | ||
implicit class struct_regmatch_t_ops(val p: native.Ptr[struct_regmatch_t]) extends AnyVal { | ||
def rm_so: regoff_t = !p._1 | ||
def rm_so_=(value: regoff_t): Unit = !p._1 = value | ||
def rm_eo: regoff_t = !p._2 | ||
def rm_eo_=(value: regoff_t): Unit = !p._2 = value | ||
} | ||
def struct_regmatch_t()(implicit z: native.Zone): native.Ptr[struct_regmatch_t] = native.alloc[struct_regmatch_t] | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
bindings/posix/src/test/scala/org/scalanative/bindgen/bindings/tests/FnmatchSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.scalanative.bindgen.bindings.tests | ||
|
||
import org.scalatest.FunSpec | ||
|
||
class FnmatchSpec extends FunSpec { | ||
describe("fnmatch") { | ||
it("should match patterns") { | ||
//#usage-example | ||
import scala.scalanative.native._ | ||
import org.scalanative.bindgen.bindings.posix.fnmatch._ | ||
|
||
assert(fnmatch(c"*.md", c"README.md", 0) == 0) | ||
assert(fnmatch(c"*.[ch]", c"main.h", 0) == 0) | ||
assert(fnmatch(c"*.[ch]", c"main.c", 0) == 0) | ||
assert(fnmatch(c"*.md", c"README_md", 0) == defines.FNM_NOMATCH) | ||
//#usage-example | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
bindings/posix/src/test/scala/org/scalanative/bindgen/bindings/tests/RegexSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.scalanative.bindgen.bindings.tests | ||
|
||
import org.scalatest.FunSpec | ||
|
||
class RegexSpec extends FunSpec { | ||
describe("regex") { | ||
it("should match regular expressions") { | ||
//#usage-example | ||
import scala.scalanative.native._ | ||
import org.scalanative.bindgen.bindings.posix.regex._ | ||
|
||
val reg = stackalloc[regex_t] | ||
|
||
val compResult = | ||
regcomp(reg, c"Scala \\(\\(J\\(VM\\|S\\)\\)\\|Native\\)", 0) | ||
assert(compResult == 0) | ||
|
||
assert(regexec(reg, c"Scala JVM", 0, null, 0) == 0) | ||
assert(regexec(reg, c"Scala JS", 0, null, 0) == 0) | ||
assert(regexec(reg, c"Scala Native", 0, null, 0) == 0) | ||
assert(regexec(reg, c"Scala .NET", 0, null, 0) != 0) | ||
|
||
regfree(reg) | ||
//#usage-example | ||
} | ||
} | ||
} |
Oops, something went wrong.