Initial commit

This commit is contained in:
Bassem Girgis
2018-12-20 17:34:07 -06:00
parent 7a2d899662
commit 81b4b9e273
34743 changed files with 5940233 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS = multimap.c
TARGET = example
INTERFACE = example.i
SWIGOPT =
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' INTERFACE='$(INTERFACE)' go_clean

View File

@@ -0,0 +1,115 @@
/* File : example.i */
%module example
%{
extern int gcd(int x, int y);
extern int gcdmain(int argc, char *argv[]);
extern int count(char *bytes, int len, char c);
extern void capitalize (char *str, int len);
extern void circle (double cx, double cy);
extern int squareCubed (int n, int *OUTPUT);
%}
extern int gcd(int x, int y);
%typemap(gotype) (int argc, char *argv[]) "[]string"
%typemap(in) (int argc, char *argv[])
%{
{
int i;
_gostring_* a;
$1 = $input.len;
a = (_gostring_*) $input.array;
$2 = (char **) malloc (($1 + 1) * sizeof (char *));
for (i = 0; i < $1; i++) {
_gostring_ *ps = &a[i];
$2[i] = (char *) ps->p;
}
$2[i] = NULL;
}
%}
%typemap(argout) (int argc, char *argv[]) "" /* override char *[] default */
%typemap(freearg) (int argc, char *argv[])
%{
free($2);
%}
extern int gcdmain(int argc, char *argv[]);
%typemap(gotype) (char *bytes, int len) "string"
%typemap(in) (char *bytes, int len)
%{
$1 = $input.p;
$2 = $input.n;
%}
extern int count(char *bytes, int len, char c);
/* This example shows how to wrap a function that mutates a c string. A one
* element Go string slice is used so that the string can be returned
* modified.
*/
%typemap(gotype) (char *str, int len) "[]string"
%typemap(in) (char *str, int len)
%{
{
_gostring_ *a;
char *p;
int n;
a = (_gostring_*) $input.array;
p = a[0].p;
n = a[0].n;
$1 = malloc(n + 1);
$2 = n;
memcpy($1, p, n);
}
%}
/* Return the mutated string as a modified element in the array. */
%typemap(argout,fragment="AllocateString") (char *str, int len)
%{
{
_gostring_ *a;
a = (_gostring_*) $input.array;
a[0] = Swig_AllocateString($1, $2);
}
%}
%typemap(goargout,fragment="CopyString") (char *str, int len)
%{
$input[0] = swigCopyString($input[0])
%}
%typemap(freearg) (char *str, int len)
%{
free($1);
%}
extern void capitalize(char *str, int len);
/* A multi-valued constraint. Force two arguments to lie
inside the unit circle */
%typemap(check) (double cx, double cy)
%{
{
double a = $1*$1 + $2*$2;
if (a > 1.0) {
_swig_gopanic("$1_name and $2_name must be in unit circle");
return;
}
}
%}
extern void circle(double cx, double cy);

View File

@@ -0,0 +1,53 @@
/* File : example.c */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
int gcdmain(int argc, char *argv[]) {
int x,y;
if (argc != 3) {
printf("usage: gcd x y\n");
return -1;
}
x = atoi(argv[1]);
y = atoi(argv[2]);
printf("gcd(%d,%d) = %d\n", x,y,gcd(x,y));
return 0;
}
int count(char *bytes, int len, char c) {
int i;
int count = 0;
for (i = 0; i < len; i++) {
if (bytes[i] == c) count++;
}
return count;
}
void capitalize(char *str, int len) {
int i;
for (i = 0; i < len; i++) {
str[i] = (char)toupper(str[i]);
}
}
void circle(double x, double y) {
double a = x*x + y*y;
if (a > 1.0) {
printf("Bad points %g, %g\n", x,y);
} else {
printf("Good points %g, %g\n", x,y);
}
}

View File

@@ -0,0 +1,26 @@
package main
import (
. "./example"
"fmt"
)
func main() {
// Call our gcd() function
x := 42
y := 105
g := Gcd(x, y)
fmt.Println("The gcd of ", x, " and ", y, " is ", g)
// Call the gcdmain() function
args := []string{"gcdmain", "42", "105"}
Gcdmain(args)
// Call the count function
fmt.Println(Count("Hello World", 'l'))
// Call the capitalize function
capitalizeMe := []string{"hello world"}
Capitalize(capitalizeMe)
fmt.Println(capitalizeMe[0])
}