-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathros_toolbox.c
More file actions
130 lines (106 loc) · 3.03 KB
/
ros_toolbox.c
File metadata and controls
130 lines (106 loc) · 3.03 KB
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
//@author : Saumya Doshi, DA-IICT.
//This code receives a command as an input from Scilab (GUI/CLI), runs it at the terminal,
//and the output from the terminal is stored in a string, which in turn, is transferred
//to Scilab as a string and stored there.
#include "api_scilab.h"
#include "BOOL.h"
#include <localization.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* ==================================================================== */
int ros_toolbox(char *fname, unsigned long fname_len)
{
/*
*/
//=========================INPUT==================================//
SciErr sciErr;
int i,j;
int iLen = 0;
//variable info
int iRows = 0;
int iCols = 0;
int* piAddr = NULL;
int* piLen = NULL;
int iRowsOut = 1;
int iColsOut = 1;
char* pstOut = NULL;
char** pstData = NULL;
//check input and output arguments
CheckInputArgument(pvApiCtx, 1, 1);
CheckOutputArgument(pvApiCtx, 1, 1);
//get variable address
sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//first call to retrieve dimensions
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
piLen = (int*)malloc(sizeof(int) * iRows * iCols);
//second call to retrieve length of each string
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, NULL);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
pstData = (char**)malloc(sizeof(char*) * iRows * iCols);
for(i = 0 ; i < iRows * iCols ; i++)
{
pstData[i] = (char*)malloc(sizeof(char) * (piLen[i] + 1));//+ 1 for null termination
}
//third call to retrieve data
sciErr = getMatrixOfString(pvApiCtx, piAddr, &iRows, &iCols, piLen, pstData);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//====================================================================//
char *command = pstData[0];
//=======================TERMINAL OPERATION============================//
char a[100][10000];
int size = 0;
FILE *fpipe = (FILE*)popen(command,"r");
char line[10000];
while(fgets(line, sizeof(line), fpipe)){
strcpy(a[size],line);
size++;
if(strncmp(line,"---",3)==0){
break;
}
//sciprint("%s", line);
}
pclose(fpipe);
//========================================================================//
//================OUTPUT STRING WRITING=====================================
int iRows2 = size;
int iCols2 = 1;
char** pstData2 = NULL;
pstData2 = (char**)malloc(sizeof(char*) * iRows2 * iCols2);
//printf("%d", size);
int k;
for(k=0;k<size;k++){
pstData2[k] = a[k];
}
sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iRows2, iCols2, pstData2);
if(sciErr.iErr)
{
printError(&sciErr, 0);
return 0;
}
//free container
free(pstData2);
free(pstData);
//assign allocated variables to Lhs position
AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
return 0;
}
/* ==================================================================== */