-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmexldr.cpp
More file actions
110 lines (98 loc) · 3.01 KB
/
mexldr.cpp
File metadata and controls
110 lines (98 loc) · 3.01 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
// -*- Mode: C++; c-basic-offset: 2 -*-
//
// JCAMP-DX c++ mex wrapper
//
// Macos: mex mexldr.cpp jcampdx.cpp jcamp_parse.cpp jcamp_scan.cpp FileLoc.cpp
// Linux: mex mexldr.cpp jcampdx.cpp jcamp_parse.cpp jcamp_scan.cpp FileLoc.cpp CXXFLAGS="-std=c++11 -fPIC"
//
// (c)2016 Michael Tesch, tesch1@gmail.com
//
//#include "premexh.h"
#include "mex.h"
#include "jcampdx.hpp"
#include "debug.hpp"
DebugLevel g_debug_level = LEVEL_INFO2;
void DebugFunc(DebugLevel level, string str, string location)
{
mexErrMsgTxt((location + ":" + str).c_str());
}
/* paramstruct = mexLoadLDRS(filename) */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char ldrfile[512];
const char * errmsg = "usage: paramstruct = mexldr('../path/to/ldrfile')\n";
Ldrset ldrset;
int count;
int ii;
const char ** keys;
mwSize dims[2] = {1, 1};
if (nrhs != 1 || nlhs != 1 || !mxIsChar(prhs[0])) {
mexWarnMsgTxt(" bad params");
mexErrMsgTxt(errmsg);
}
if (mxGetString(prhs[0], ldrfile, sizeof(ldrfile))) {
mexWarnMsgTxt(" can't get param file name from argument");
mexErrMsgTxt(errmsg);
}
/* read the procpar */
try {
ldrset.loadFile(ldrfile);
}
catch (std::exception & exc) {
mexWarnMsgIdAndTxt("mexldr:loadFile", "%s / %s", ldrfile, exc.what());
}
count = ldrset.size();
/* gather param names into key array */
keys = (const char **)malloc(sizeof(char *) * count);
if (!keys)
mexErrMsgTxt("malloc");
auto labels = ldrset.getLabels();
auto label = labels.begin();
for (ii = 0; ii < count; ii++, label++) {
// nasty...
keys[ii] = label->c_str();
if (keys[ii][0] == '$')
keys[ii]++;
}
/* Construct outdata */
plhs[0] = mxCreateStructArray(2, dims, count, keys);
/* set struct values */
for (ii = 0; ii < count; ii++) {
mxArray *field_value = NULL; // compiler warning
int j, fieldcount;
Ldr & ldr = ldrset.getLdr(keys[ii]);
std::vector<int> shape = ldr.shape();
fieldcount = dims[0] = shape[0];
if (shape.size() > 1) {
dims[1] = shape[1];
fieldcount *= shape[1];
}
switch (ldr.type()) {
case RECORD_TEXT:
case RECORD_STRING:
case RECORD_QSTRING:
field_value = mxCreateCellMatrix(fieldcount, 1);
for (j = 0; j < fieldcount; j++) {
mxSetCell(field_value, j, mxCreateString(ldr.str(j).c_str()));
}
break;
case RECORD_NUMERIC:
field_value = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
for (j = 0; j < fieldcount; j++) {
mxGetPr(field_value)[j] = ldr.num(j);
}
break;
case RECORD_GROUP:
field_value = mxCreateCellMatrix(0, 1);
//field_value = mxCreateStructArray(2, dims, count, keys);
break;
default:
//mexErrMsgTxt("mexLoadLDRS internal error");
mexWarnMsgIdAndTxt("mexldr:unkn",
"mexLoadLDRS internal error, unhandled ldr type %d from '%s'",
ldr.type(), keys[ii]);
}
mxSetFieldByNumber(plhs[0], 0, ii, field_value);
}
free(keys);
}