FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
mscript/mscript.cpp at master · jpollack/mscript · GitHub
jpollack
/
mscript
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
1
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
mscript
/
mscript.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
168 lines (141 loc) · 3.49 KB
Breadcrumbs
mscript
/
mscript.cpp
Copy path
File metadata and controls
168 lines (141 loc) · 3.49 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#
include
<
stdio.h
>
#
include
<
stdlib.h
>
#
include
<
string.h
>
#
include
"
engine.h
"
#
include
"
matrix.h
"
Engine *ep;
int
runFile
(
char
*fname,
int
argc,
char
**argv);
char
*
stripRChars
(
char
*str,
const
char
*chars);
char
*
stripLChars
(
char
*str,
const
char
*chars);
inline
char
*
chomp
(
char
* str) {
return
stripRChars
(str,
"
\n\r
"
); }
inline
char
*
stripChars
(
char
*str,
const
char
*chars)
{
return
(
stripLChars
(
stripRChars
(str, chars), chars));
}
inline
char
*
stripWhitespace
(
char
*str) {
return
stripChars
(str,
"
\t\r\n
"
); }
int
main
(
int
argc,
char
*argv[])
{
fclose
(stderr);
ep =
engOpen
(
"
matlab -nosplash
"
);
if
(!ep)
{
fprintf
(stderr,
"
engOpen failed.
\n
"
);
exit
(
1
);
}
runFile
(argv[
1
], argc -
1
, argv +
1
);
engClose
(ep);
return
0
;
}
int
runFile
(
char
*fname,
int
argc,
char
**argv)
{
FILE
* fp =
NULL
;
char
lineBuf[
4096
];
char
evalBuf[
4096
];
fp =
fopen
(fname,
"
r
"
);
if
(!fp)
{
return
1
;
}
mxArray *mxArgc =
mxCreateNumericMatrix
(
1
,
1
, mxINT32_CLASS, mxREAL);
(
static_cast
<
int
*> (
mxGetData
(mxArgc)))[
0
] = argc;
engPutVariable
(ep,
"
argc
"
, mxArgc);
mxArray *mxArgv =
mxCreateCellMatrix
(
1
, argc);
for
(
int
idx =
0
; idx < argc; idx++)
{
mxSetCell
(mxArgv, idx,
mxCreateString
(argv[idx]));
}
engPutVariable
(ep,
"
argv
"
, mxArgv);
evalBuf[
0
] =
0
;
char
engBuf[
65536
];
engOutputBuffer
(ep, engBuf,
65536
);
for
(
int
lineNo =
1
;
fgets
(lineBuf,
4096
, fp); lineNo++)
{
stripWhitespace
(lineBuf);
if
((lineNo ==
1
)
&& (lineBuf[
0
] ==
'
#
'
)
&& (lineBuf[
1
] ==
'
!
'
))
{
continue
;
}
if
(!lineBuf[
0
] || (lineBuf[
0
] ==
'
%
'
))
{
continue
;
}
int
lineLen =
strlen
(lineBuf);
if
((lineLen >
3
)
&& (lineBuf[lineLen -
1
] ==
'
.
'
)
&& (lineBuf[lineLen -
2
] ==
'
.
'
)
&& (lineBuf[lineLen -
3
] ==
'
.
'
))
{
lineBuf[lineLen -
3
] =
0
;
strncat
(evalBuf, lineBuf,
4096
);
}
else
{
strncat
(evalBuf, lineBuf,
4096
);
if
(
engEvalString
(ep, evalBuf))
{
return
3
;
}
evalBuf[
0
] =
0
;
if
(engBuf[
0
])
{
printf
(
"
%s
"
, engBuf);
}
}
}
mxDestroyArray
(mxArgc);
mxDestroyArray
(mxArgv);
if
(
fclose
(fp))
{
return
2
;
}
return
0
;
}
//
Remove all characters from the RHS.
char
*
stripRChars
(
char
*str,
const
char
*chars)
{
int
pos =
strlen
(str);
int
nChars =
strlen
(chars);
while
(pos--)
{
int
idx = nChars;
while
(idx && (chars[--idx] != str[pos]));
if
(chars[idx] == str[pos])
{
str[pos] =
0
;
}
else
{
pos =
0
;
}
}
return
str;
}
//
Remove all characters from the LHS.
char
*
stripLChars
(
char
*str,
const
char
*chars)
{
int
sLen
=
strlen
(str);
int
nChars =
strlen
(chars);
int
pos;
for
(pos =
0
; (pos <
sLen
); pos++)
{
int
idx = nChars;
while
(idx && (chars[--idx] != str[pos]));
if
(chars[idx] == str[pos])
{
continue
;
}
else
{
break
;
}
}
//
pos is the first non-matching character
int
ssLen =
strlen
(str + pos);
if
(ssLen && (ssLen !=
sLen
))
{
memmove
(str, str + pos, (ssLen +
1
));
}
return
str;
}
Back
|
FazBrowse Home
|
New Git URL