-
Notifications
You must be signed in to change notification settings - Fork 0
/
ndrxconvert.c
389 lines (302 loc) · 8.81 KB
/
ndrxconvert.c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
This file implements some functions to convert ENDUROX data types (STRING,
UBF) to Python data types (string, dictionary) and vice versa
(c) 1999 Ralf Henschkowski ([email protected])
*/
#include <stdio.h>
#include <atmi.h> /* ENDUROX Header File */
#include <ubf.h> /* ENDUROX Header File */
#include <userlog.h> /* ENDUROX Header File */
#include <Python.h>
#include "ndrxconvert.h"
/* New for Python 3: strict Unicode / CString sparation, so first
convert Unicode Object to Python String and then to C-String */
char* utf8_to_cstring(PyObject * pystring) {
char* result = NULL;
if (PyUnicode_Check(pystring)) {
PyObject * pybytes = PyUnicode_AsUTF8String(pystring);
result = PyBytes_AsString(pybytes);
}
return result;
}
PyObject* ubf_to_dict(UBFH* ubf) {
PyObject* result = NULL;
int res ;
char * name;
BFLDLEN len;
BFLDOCC oc;
BFLDID id;
PyObject* dict, *list;
char* type;
dict = PyDict_New();
#ifdef DEBUG
Bprint(ubf);
#endif
id = BFIRSTFLDID;
while (1) {
len = NDRXBUFSIZE;
#ifdef DEBUG
printf("[run %d] vor bnext() (id = %d, oc = %d, len = %d) \n",
run++, id, oc, len);
#endif
/* get next field id and occurence */
res = Bnext(ubf, &id, &oc, NULL, NULL);
if (res <= 0) break;
if ((name = Bbfname(id)) == NULL) {
bprintf(stderr, "Bfname(%lu): %s", (long)id, Bstrerror(Berror));
result = NULL;
goto leave_func;
}
if ((list = PyDict_GetItemString (dict, name)) == NULL) {
/* key doesn't exist -> insert new list into dict */
list = PyList_New(0);
PyDict_SetItemString(dict, name, list);
Py_DECREF(list); /* reference now owned by dictionary */
}
type = Btype(id);
if (strcmp(type, "long") == 0) {
long longval = 0;
BFLDLEN longlen = sizeof (BFLDLEN);
PyObject* pyval;
Bget(ubf, id, oc, (char*)&longval, &longlen);
pyval = Py_BuildValue("i", longval);
PyList_Append(list, pyval);
Py_DECREF(pyval); /* reference now owned by list */
} else if (strcmp(type, "double") == 0) {
double doubleval = 0.0;
BFLDLEN doublelen = sizeof (BFLDLEN);
PyObject* pyval;
Bget(ubf, id, oc, (char*)&doubleval, &doublelen);
pyval = Py_BuildValue("d", doubleval);
PyList_Append(list, pyval);
Py_DECREF(pyval);
} else if (strcmp(type, "string") == 0) {
PyObject* pyval;
BFLDLEN stringlen = NDRXBUFSIZE;
char stringval[NDRXBUFSIZE] = "";
Bget(ubf, id, oc, (char*)&stringval, &stringlen);
pyval = Py_BuildValue("s", stringval);
PyList_Append(list, pyval);
Py_DECREF(pyval);
} else {
char msg[100];
sprintf(msg, "unsupported UBF type <%s>", type);
PyErr_SetString(PyExc_RuntimeError, msg);
bprintf(stderr, "Btype(): %s", Bstrerror(Berror));
result = NULL;
goto leave_func;
}
}
if (res < 0) {
PyErr_SetString(PyExc_RuntimeError, "Problems with Bnext()");
bprintf(stderr, "Bnext(): %s", Bstrerror(Berror));
result = NULL;
goto leave_func;
}
result = dict;
leave_func:
#ifdef DEBUG
PyObject_Print(result, stdout, 0);
printf("\n");
#endif
return result;
}
UBFH* dict_to_ubf(PyObject* dict) {
UBFH* result = NULL;
int idx, oc, bfldtype;
BFLDID id;
UBFH* ubf;
PyObject* keylist;
keylist = PyDict_Keys(dict);
#ifdef DEBUG
PyObject_Print(dict, stdout, 0);
printf("\n");
#endif
if ((ubf = (UBFH*)tpalloc("UBF", NULL, NDRXBUFSIZE)) == NULL) {
bprintf(stderr, "tpalloc(): %s\n", tpstrerror(tperrno));
goto leave_func;
}
if (Binit(ubf, NDRXBUFSIZE) < 0) {
bprintf(stderr, "Binit(): %s\n", Bstrerror(Berror));
goto leave_func;
}
for (idx = 0; idx < PyList_Size(keylist); idx++) {
PyObject* vallist = NULL;
PyObject* key = NULL;
char* key_cstring = NULL;
key = PyList_GetItem(keylist, idx); /* borrowed reference */
if (!key) {
bprintf(stderr, "PyList_GetItem(keys, %d) returned NULL\n", idx);
goto leave_func;
}
key_cstring = utf8_to_cstring(key);
id = Bfldid(key_cstring);
if (id == BBADFLDID) {
char tmp[1024] = "";
sprintf(tmp, "Bfldid(): %d - %s:", Berror, Bstrerror(Berror));
PyErr_SetString(PyExc_RuntimeError, tmp);
goto leave_func;
}
vallist = PyDict_GetItemString(dict, key_cstring); /* borrowed reference */
if (PyBytes_Check(vallist)) {
char* cval = NULL;
cval = PyBytes_AsString(vallist);
if (cval == NULL) {
bprintf(stderr, "error in PyBytes_AsString()\n");
goto leave_func;
}
if (Bchgs(ubf, id, 0, cval) < 0) {
if (Berror == BNOSPACE) {
/* realloc buffer */
}
bprintf(stderr, "error in Bchgs() : %s\n", Bstrerror(Berror));
goto leave_func;
}
} else {
/* process all occurences (elements of the list) for this ID
(Field Name) */
for (oc = 0; oc < PyList_Size(vallist); oc++) {
PyObject* pyvalue = NULL;
if (PyList_Check(vallist)) {
pyvalue = PyList_GetItem(vallist, oc); /* borrowed reference */
}
if (pyvalue == NULL) continue;
/* !!! type given by field id, not by Python types !!! */
bfldtype = Bbfldtype(id);
switch (bfldtype) {
case BFLD_LONG:
/* convert input to BFLD_LONG type */
if (PyLong_Check(pyvalue)) {
long cval = 0;
BFLDLEN len = sizeof(cval);
cval = PyLong_AsLong(pyvalue);
if (Bchg(ubf, id, oc,(char*) &cval, len) < 0) {
bprintf(stderr, "error in Bchg(): %s\n", Bstrerror(Berror));
goto leave_func;
}
break;
}
if (PyUnicode_Check(pyvalue)) {
char * cval;
long lval = 0;
cval = utf8_to_cstring(pyvalue);
if (cval == NULL) {
goto leave_func;
}
lval = atol(cval);
if (Bchg(ubf, id, oc,(char*) &lval, (BFLDLEN)sizeof(long)) < 0) {
bprintf(stderr, "error in Bchg(): %s\n", Bstrerror(Berror));
goto leave_func;
}
break;
}
bprintf(stderr,
"could not convert value for key %s to UBF type BFLD_LONG\n",
key_cstring);
goto leave_func;
break;
case BFLD_STRING:
/* convert input to BFLD_STRING type */
if (PyFloat_Check(pyvalue)) {
double cval = 0.0;
char string[255] = "";
cval = PyFloat_AsDouble(pyvalue);
sprintf(string, "%f", cval);
if (Bchgs(ubf, id, oc, string) < 0) {
bprintf(stderr, "error in Bchgs(): %s\n", Bstrerror(Berror));
goto leave_func;
}
break;
}
if (PyLong_Check(pyvalue)) {
long cval = 0;
char string[255] = "";
cval = PyLong_AsLong(pyvalue);
sprintf(string, "%ld", cval);
if (Bchgs(ubf, id, oc, string) < 0) {
bprintf(stderr, "error in Bchgs(): %s\n", Bstrerror(Berror));
goto leave_func;
}
break;
}
if (PyLong_Check(pyvalue)) {
long cval = 0;
char string[255] = "";
cval = PyLong_AsLong(pyvalue);
sprintf(string, "%ld", cval);
if (Bchgs(ubf, id, oc, string) < 0) {
bprintf(stderr, "error in Bchgs(): %s\n", Bstrerror(Berror));
goto leave_func;
}
break;
}
if (PyUnicode_Check(pyvalue)) {
char * cval;
cval = utf8_to_cstring(pyvalue);
if (cval == NULL) {
goto leave_func;
}
if (Bchgs(ubf, id, oc, cval) < 0) {
bprintf(stderr, "error in Bchgs(): %s\n", Bstrerror(Berror));
goto leave_func;
}
break;
}
bprintf(stderr,
"could not convert value for key %s to UBF type BFLD_STRING\n",
key_cstring);
goto leave_func;
break;
default:
bprintf(stderr, "unsupported UBF type %d\n", bfldtype);
goto leave_func;
}
/* Not recognized types do not cause an error and are simply discarded */
}
}
}
result = ubf;
leave_func:
if (keylist) {
Py_DECREF(keylist);
}
if (!result) {
tpfree((char*)ubf);
}
#ifdef DEBUG
Bprint(result);
#endif
return result;
}
char* pystring_to_string(PyObject* pyunicodestring) {
char* result = NULL;
char* string = NULL;
PyObject * pystring = NULL;
long len = 0;
/* Creates new Object */
pystring = PyUnicode_AsUTF8String(pyunicodestring);
len = strlen(PyBytes_AsString(pystring));
if ((string = (char*)tpalloc("STRING", NULL, len+1)) == NULL) {
bprintf(stderr, "tpalloc(): %s\n", tpstrerror(tperrno));
goto leave_func;
}
strcpy(string, PyBytes_AsString(pystring));
result = string;
leave_func:
Py_XDECREF(pystring);
if (!result) {
tpfree((char*)string);
}
return result;
}
PyObject* string_to_pystring(char* string) {
PyObject* result = NULL;
PyObject* pystring = NULL;
if ((pystring = Py_BuildValue("s", string)) == NULL) {
bprintf(stderr, "Py_BuildValue(): %s", string);
goto leave_func;
}
result = pystring;
leave_func:
return result;
}