10f2736e0ba5ae0d92d993cc31f0b8aeecb4ad61
[scilab.git] / scilab / modules / core / examples / call_scilab / c / visual_studio / CallsciC.c
1 /* Allan CORNET */
2 /* Scilab, INRIA 2004 */
3 /* Only For Windows */
4 /*--------------------------------------------------------------------------*/
5 #pragma comment(lib, "../../../../../../bin/libScilab.lib")
6 #pragma comment(lib, "../../../../../../bin/MALLOC.lib")
7 /*--------------------------------------------------------------------------*/
8 #include <windows.h>
9 #include <math.h>
10 #include <stdio.h> 
11 #include <string.h> 
12 #include <conio.h> 
13
14 #include "../../../../../../modules/core/includes/machine.h"
15 #include "../../../../../../modules/core/includes/stack-c.h"
16 #include "../../../../../../modules/core/includes/CallScilab.h"
17 #define TRUE 1
18 #define FALSE 0
19 /*--------------------------------------------------------------------------*/
20 /* See SCI/modules/core/includes/CallScilab.h */
21 /*--------------------------------------------------------------------------*/
22 static int example1(void)
23 {
24         static double A[]={1,2,3,4};  int mA=2,nA=2;
25         static double b[]={4,5};  int mb=2,nb=1;
26
27
28         /* Create Scilab matrices A and b */
29         WriteMatrix("A", &mA, &nA, A);
30         WriteMatrix("b", &mb, &nb, b);
31
32         SendScilabJob("disp('A=');");
33         SendScilabJob("disp(A);");
34         SendScilabJob("disp('b=');");
35         SendScilabJob("disp(b);");
36         SendScilabJob("disp('x=A\\b');");
37
38         if ( SendScilabJob("A,b,x=A\\b;") != 0) 
39         {
40                 fprintf(stdout,"Error occured during scilab execution (SendScilabJob)\n");
41         }
42         else 
43         {
44                 double *cxtmp=NULL;
45                 int m,n,lp,i;
46
47                 /* Get m and n */
48                 GetMatrixptr("x", &m, &n, &lp);
49
50                 cxtmp=(double*)malloc((m*n)*sizeof(double));
51
52                 ReadMatrix("x", &m, &n, cxtmp);
53
54                 for(i=0;i<m*n;i++)
55                 {
56                         fprintf(stdout,"x[%d] = %5.2f\n",i,cxtmp[i]);
57                 }
58
59                 if (cxtmp) 
60                 {
61                         free(cxtmp);
62                         cxtmp=NULL;
63                 }
64         }
65         return 0;
66 }
67 /*--------------------------------------------------------------------------*/
68 static int example2(void)
69 {
70         SendScilabJob("plot3d();");
71         printf("\nClose Graphical Windows to close this example.\n");
72         while( ScilabHaveAGraph() )
73         {
74                 ScilabDoOneEvent();
75                 Sleep(1);
76         }
77         return 1; 
78 }
79 /*--------------------------------------------------------------------------*/
80 static int example3(void)
81 {
82         int code=0;
83
84         char **JOBS=NULL;
85         const int SizeJOBS=6;
86         int i=0;
87
88         JOBS=(char**)malloc(sizeof(char**)*SizeJOBS);
89
90         for (i=0;i<SizeJOBS;i++)
91         {
92                 JOBS[i]=(char*)malloc(sizeof(char*)*1024);
93         }
94
95         strcpy(JOBS[0],"A=1 ..");
96         strcpy(JOBS[1],"+3;");
97         strcpy(JOBS[2],"B = 8;");
98         /* strcpy(JOBS[2],"b = V_NOT_EXIST;"); */
99         strcpy(JOBS[3],"+3;");
100         strcpy(JOBS[4],"disp('C=');");
101         strcpy(JOBS[5],"C=A+B;disp(C);"); /* C = 12 */
102
103         code=SendScilabJobs(JOBS,SizeJOBS);
104
105         if (code)
106         {
107                 char lastjob[4096]; // bsiz in scilab 4096 max
108                 if (GetLastJob(lastjob,4096))
109                 {
110                         printf("Error %s\n",lastjob);
111                 }
112         }
113
114         for (i=0;i<SizeJOBS;i++)
115         {
116                 if (JOBS[i]) {free(JOBS[i]);JOBS[i]=NULL;}
117         }
118         return 1;
119 }
120 /*--------------------------------------------------------------------------*/
121 int main(void)
122 /* int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR szCmdLine, int iCmdShow) */
123 {
124         if ( StartScilab(NULL,NULL,NULL) == FALSE ) printf("Error : StartScilab\n");
125
126         printf("\nexample 1\n");
127         example1();
128         system("pause");
129         printf("\nexample 2\n");
130         example2();
131         system("pause");
132         printf("\nexample 3\n");
133         example3();
134         system("pause");
135         
136         if ( TerminateScilab(NULL) == FALSE ) printf("Error : TerminateScilab\n");
137         return 0;
138 }
139 /*--------------------------------------------------------------------------*/