summaryrefslogtreecommitdiff
path: root/sci_gateway/cpp/sci_matrix.cpp
blob: 715390369e174acd8217ebb98d1dff6d40f0deeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <string>
#include "wchar.h"
#include <cstdlib>

extern "C"
{
#include<Scierror.h>
#include<sciprint.h>
#include<api_scilab.h>
#include "localization.h"
#include "fun.h"
#include <cstdio>
#include <math.h>
#include <stdio.h>
#include "os_string.h"
#include <stdlib.h>



static const char fname[] = "octave_fun";
///////#####call octave_fun("hamming",[5],"periodic") ########////////////
int sci_octave_fun(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt* opt, int nout, scilabVar* out)

{
//printf("nin: %d\n", nin);
	if (nin < 1)
    {
        Scierror(999, _("%s: Wrong number of input arguments. Atleast %d expected.\n"), fname, 1);
        return STATUS_ERROR;
    }

	FUNCCALL funcall;
	FUNCCALL *funptr = &funcall;
	funcall.n_in_arguments = nin;
	funcall.n_out_user = nout;

	FUNCARGS ins[funcall.n_in_arguments*nout];
	FUNCARGS *argptr = ins;

	int i,j;
	double* d;
	int size;
	char str[20];
	char* c;
	double* n = NULL;
	int row = 0;
  int col = 0;

	for(i=0;i<nin;i++)
	{
		if(scilab_getType(env, in[i])==1)
		{	
			ins[i].type = TYPE_DOUBLE;
			size = scilab_getDim2d(env, in[i], &row, &col);
			ins[i].n_in_rows = row;
			ins[i].n_in_cols = col;
			scilab_getDoubleArray(env, in[i], &n);

			ins[i].in_data = malloc(sizeof(double)*size);
			d = (double *)ins[i].in_data;

			for(j=0;j<size;j++)
			{
				d[j] = n[j];
				//printf("%f\n",d[j]);
			}
		}
		else if(scilab_getType(env, in[i])==10)
		{
			wchar_t* in1 = 0;

			scilab_getString(env, in[i], &in1);
			//printf("%S\n", in1);

			wcstombs(str, in1, sizeof(str));
			//printf("%s\n", str);
			if(str)
			{
				//printf("lenght of string input: %d\n", strlen(str));
				ins[i].type = TYPE_STRING;
				ins[i].n_in_rows = 1;
				ins[i].n_in_cols = strlen(str);
				size = (ins[i].n_in_rows)*(ins[i].n_in_cols);
				ins[i].in_data = malloc(sizeof(char)*size+1);
				c = (char *)ins[i].in_data;
				int ci;

				strcpy(c,str);	
				ins[i].n_in_cols = strlen(c);
				//printf("in scilab strin is: %s\n", c);

			}
		}
	}

			int status_fun = fun(argptr, funptr);
		//printf("in scilab status_fun is: %d\n", status_fun);
			//printf("in scilab funcall.n_out_arguments is: %d\n", funcall.n_out_arguments);
			//printf("in scilab funcall.n_out_user is: %d\n", funcall.n_out_user);
//printf("in scilab ins[0].n_out_rows is: %d\n", ins[0].n_out_rows);
//printf("in scilab ins[0].n_out_cols is: %d\n", ins[0].n_out_cols);


//printf("in scilab ouput args are: %d\n", funcall.n_out_arguments);
	if(status_fun==1)
	{
		Scierror(999, "\nOctave unable to process!\nCorrect usage:\n octave_fun(\"octave_function\",input1,input2,...)\n octave_fun(\"octave_function\",input1,input2,...,optional_input1,optional_input2,...)\n octave_fun(\"octave_function\",\"octave_package\",input1,input2,...)\n octave_fun(\"octave_function\",\"octave_package\",input1,input2,...,optional_input1,optional_input2,...)\n");
		return 1;
	}
	else if(funcall.n_out_user <= funcall.n_out_arguments)
	{	
		for(i=0;i<nout;i++)
		{	
			out[i] = scilab_createDoubleMatrix2d(env, ins[i].n_out_rows, ins[i].n_out_cols, 0);

			double* out1 = NULL;
		 	scilab_getDoubleArray(env, out[i], &out1);
			int len = ins[i].n_out_rows*ins[i].n_out_cols;
			double* dd = (double *)ins[i].out_data;
			//printf("output length is: %d\n", len);
			for(j=0; j<len; j++)
				{
					out1[j] = dd[j];//.float_value();
				}
		}
	}
	else
	{
			Scierror(77, _("%s: Wrong number of output argument(s): This function can return a maximum of %d output(s).\n"), fname, funcall.n_out_arguments);
			return 1;
		}




	for(i=0;i<nout;i++)
	{
		//printf("%ld : ",&ins[i].in_data);
	//	printf("%f\n",(*(double *)ins[i].in_data));
	
		free(ins[i].out_data);	
	}
	for(i=0;i<nin;i++)
	{
		//printf("%ld : ",&ins[i].in_data);
	//	printf("%f\n",(*(double *)ins[i].in_data));
	
		free(ins[i].in_data);	
	}
  return 0;
}
}