diff options
Diffstat (limited to 'misc/swig/stdio')
-rw-r--r-- | misc/swig/stdio/file.swig | 15 | ||||
-rw-r--r-- | misc/swig/stdio/file_test.go | 22 | ||||
-rw-r--r-- | misc/swig/stdio/hello.go | 11 |
3 files changed, 36 insertions, 12 deletions
diff --git a/misc/swig/stdio/file.swig b/misc/swig/stdio/file.swig index 57c623f8f..8ba341d08 100644 --- a/misc/swig/stdio/file.swig +++ b/misc/swig/stdio/file.swig @@ -6,6 +6,19 @@ %{ #include <stdio.h> +#include <stdlib.h> %} -int puts(const char *); +%typemap(gotype) const char * "string" +%typemap(in) const char * %{ + $1 = malloc($input.n + 1); + memcpy($1, $input.p, $input.n); + $1[$input.n] = '\0'; +%} +%typemap(freearg) const char * %{ + free($1); +%} + +FILE *fopen(const char *name, const char *mode); +int fclose(FILE *); +int fgetc(FILE *); diff --git a/misc/swig/stdio/file_test.go b/misc/swig/stdio/file_test.go new file mode 100644 index 000000000..6478a7cf3 --- /dev/null +++ b/misc/swig/stdio/file_test.go @@ -0,0 +1,22 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package file + +import "testing" + +// Open this file itself and verify that the first few characters are +// as expected. +func TestRead(t *testing.T) { + f := Fopen("file_test.go", "r") + if f == nil { + t.Fatal("fopen failed") + } + if Fgetc(f) != '/' || Fgetc(f) != '/' || Fgetc(f) != ' ' || Fgetc(f) != 'C' { + t.Error("read unexpected characters") + } + if Fclose(f) != 0 { + t.Error("fclose failed") + } +} diff --git a/misc/swig/stdio/hello.go b/misc/swig/stdio/hello.go deleted file mode 100644 index eec294278..000000000 --- a/misc/swig/stdio/hello.go +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package main - -import "swig/file" - -func main() { - file.Puts("Hello, world") -} |