-
Notifications
You must be signed in to change notification settings - Fork 1
/
datmap.c
576 lines (480 loc) · 15.6 KB
/
datmap.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#if HAVE_CONFIG_H
# include <config.h>
#endif
/*+DATMAP.C-*/
#include <stdio.h>
#include <string.h>
#include "ems.h" /* EMS error reporting routines */
#include "hds1.h" /* Global definitions for HDS */
#include "rec.h" /* Public rec_ definitions */
#include "str.h" /* Character string import/export macros */
#include "dat1.h" /* Internal dat_ definitions */
#include "dat_err.h" /* DAT__ error code definitions */
#include "hds.h"
#if defined( vms )
#include <descrip.h> /* VMS descriptor definitions */
#endif
/*====================*/
/* DAT_MAP - Map data */
/*====================*/
int
datMap(const HDSLoc *locator,
const char *type_str,
const char *mode_str,
int ndim,
const HDS_PTYPE dims[],
void **pntr,
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_MAP_ERR"
#define context_message\
"DAT_MAP: Error mapping an HDS primitive"
GENPTR_POINTER(pntr)
struct DSC type;
struct DSC mode;
struct LCP *lcp;
struct LCP_DATA *data;
struct LCP_STATE *state;
char typbuf[DAT__SZTYP];
HDS_PTYPE axis[DAT__MXDIM];
struct RCL rcl;
struct PDD *app;
struct PDD *obj;
unsigned char *dom;
int naxes;
int reading;
INT_BIG objlen;
INT_BIG objoff;
INT_BIG applen;
int i;
int nbad;
int temp_status;
void *retpntr;
#if defined( vms )
struct dsc$descriptor *dsc;
#endif
/* Enter routine. */
if (!_ok(*status))
return *status;
hds_gl_status = DAT__OK;
/* Import the type and mode strings. */
_strcsimp ( &type, type_str );
_strcsimp ( &mode, mode_str );
/* Import the locator. */
_call(dat1_import_loc(locator, &lcp ));
data = &lcp->data;
state = &data->state;
/* Ensure that there is no currently mapped data and that the object is
primitive. */
if (state->mapped)
_callnam(DAT__PRMAP)
if (data->struc)
_callnam(DAT__OBJIN)
/* Determine the shape of the object and match the dimensions. */
_callnam( dau_get_shape(data, &naxes, axis))
if (ndim != naxes)
_callnam(DAT__DIMIN)
for (i=0; i<naxes; i++)
if (dims[i] != axis[i])
_callnam(DAT__DIMIN)
/* Validate the application data type, and the access mode. Return if the
container file was opened for read-only access and the access mode is
'WRITE' or 'UPDATE'. */
_callnam(dat1_check_type(&type, typbuf))
dat1_check_mode( (const char *) mode.body, mode.length, &data->mode,
&hds_gl_status );
_callnam( hds_gl_status )
reading = (data->mode != 'W');
if (data->read && data->mode != 'R')
_callnam(DAT__ACCON)
/* Determine the attributes of the application data and reject the operation
if not primitive. */
_callnam(dat1_unpack_type(typbuf, &data->app))
app = &data->app;
if (app->class != DAT__PRIMITIVE)
_callnam(DAT__TYPIN)
/* Match the object and applications data attributes and reject the operation
if the types are incompatible. */
obj = &data->obj;
_callnam(dau_match_types(obj, app))
/* Ensure that the object data is 'active' if reading or updating. */
_callnam(rec_get_rcl(&data->han, &rcl))
if (reading && !rcl.active)
_callnam(DAT__UNSET)
/* If the mapping is character-type and an explicit character string length */
/* was not supplied (i.e. there was no '*' character in the type */
/* specification), then modify the application data attributes descriptor */
/* to describe a character array (or scalar) of an appropriate length to */
/* hold the character-formatted values. In this situation the character */
/* string length of the application's data is simply made identical to that */
/* of the object data if they are both character type. */
if ( app->dtype == DAT__C )
{
if ( memchr( (void *) type.body, '*', type.length ) == NULL )
{
if ( obj->dtype != DAT__C )
{
app->length = dat_gl_ndr[ obj->dtype ].txtsize;
}
else
{
app->length = obj->length;
}
}
}
/* If the object and application data types differ in any respect, then a */
/* virtual memory copy of the data is returned to the program. See if this */
/* is necessary. */
state->unlike = ( obj->dtype != app->dtype ) ||
( obj->length != app->length ) ||
( obj->format != app->format ) ||
( obj->order != app->order );
state->vmcopy = state->unlike;
/* Calculate the length (in bytes) of the virtual memory required for the
application program data and the corresponding length of the object data.
Determine the byte offset into the object record's dynamic domain. */
applen = app->length * data->size;
objlen = obj->length * data->size;
objoff = obj->length * data->offset;
/* Gather discontiguous object data if the program is reading or updating. */
if (state->broken)
{
_callnam( rec_alloc_xmem( applen, (void **) &app->body ) )
if (reading)
dau_gather_data( 1, data, &nbad );
}
/* If a copy of the object data is to be returned, then locate the record's
dynamic domain and translate the data to the allocated virtual memory. */
else if (state->vmcopy)
{
_callnam( rec_alloc_xmem( applen, (void **) &app->body ) )
if (reading)
{
_callnam(rec_locate_data(&data->han, objlen, objoff, 'R', &dom))
obj->body = dom;
dat1_cvt( 1, data->size, obj, app, &nbad );
/* NB workaround for problems with EMS... */
temp_status = hds_gl_status;
if ( (hds_gl_status = DAT__CONER) )
hds_gl_status = DAT__OK;
rec_release_data(&data->han, objlen, objoff, 'R', &dom);
if ( _ok( hds_gl_status ) )
hds_gl_status = temp_status;
}
}
/* Otherwise, the application program gains direct access to the object
data. */
else
{
rec_locate_data(&data->han, objlen, objoff, data->mode, &dom);
app->body = dom;
}
/* Save the current setting of the global file-mapping flag, for use when */
/* the data are un-mapped. */
data->filemap = hds_gl_map;
/* Obtain the returned pointer value and note if the object is mapped. */
retpntr = (void *) app->body;
state->mapped = ( retpntr != NULL );
/* If using VMS and the mapped data are character-type, then construct a */
/* VMS descriptor to describe the mapped values (this descriptor is stored */
/* in the LCP). Modify the returned pointer to point at this descriptor */
/* instead. */
#if defined( vms )
if ( app->dtype == DAT__C )
{
dsc = &data->vmsdsc;
dsc->dsc$w_length = app->length;
dsc->dsc$b_dtype = DSC$K_DTYPE_T;
dsc->dsc$b_class = ( naxes == 0 ) ? DSC$K_CLASS_S : DSC$K_CLASS_A;
dsc->dsc$a_pointer = (char *) app->body;
retpntr = (void *) dsc;
}
#endif
/* copy pointer to user */
*pntr = retpntr;
/* If conversion errors occurred, then report contextual information. */
if ( hds_gl_status == DAT__CONER )
{
emsSeti( "NBAD", nbad );
emsRep( "DAT_MAP_2",
"A total of ^NBAD data conversion error(s) occurred.",
&hds_gl_status );
}
_callnam(hds_gl_status)
return hds_gl_status;
}
/*=============================*/
/* DAT_MAPI - Map INTEGER data */
/*=============================*/
int
datMapI(const HDSLoc *locator,
const char *mode_str,
int ndim,
const HDS_PTYPE dims[],
int **pntr,
int *status )
{
#undef context_name
#undef context_message
#define context_name "DAT_MAPI_ERR"
#define context_message\
"DAT_MAPI: Error mapping HDS primitive as integer values."
datMap( locator, "_INTEGER", mode_str, ndim, dims,
(void**)pntr, status );
return hds_gl_status;
}
/*=============================*/
/* datMapK - Map INT64 data */
/*=============================*/
int
datMapK(const HDSLoc *locator,
const char *mode_str,
int ndim,
const HDS_PTYPE dims[],
int **pntr,
int *status )
{
#undef context_name
#undef context_message
#define context_name "DAT_MAPK_ERR"
#define context_message\
"DAT_MAPK: Error mapping HDS primitive as 64-bit int values."
datMap( locator, "_INT64", mode_str, ndim, dims,
(void**)pntr, status );
return hds_gl_status;
}
/*=============================*/
/* DAT_MAPR - Map REAL data */
/*=============================*/
int
datMapR(const HDSLoc *locator,
const char *mode_str,
int ndim,
const HDS_PTYPE dims[],
float **pntr,
int *status )
{
#undef context_name
#undef context_message
#define context_name "DAT_MAPR_ERR"
#define context_message\
"DAT_MAPR: Error mapping an HDS primitive as real values."
datMap( locator, "_REAL", mode_str, ndim, dims,
(void**)pntr, status );
return hds_gl_status;
}
/*======================================*/
/* DAT_MAPD - Map DOUBLE PRECISION data */
/*======================================*/
int
datMapD(const HDSLoc *locator,
const char *mode_str,
int ndim,
const HDS_PTYPE dims[],
double **pntr,
int *status )
{
#undef context_name
#undef context_message
#define context_name "DAT_MAPD_ERR"
#define context_message\
"DAT_MAPD: Error mapping an HDS primitive as double precision values."
datMap( locator, "_DOUBLE", mode_str, ndim, dims,
(void**)pntr, status );
return hds_gl_status;
}
/*=============================*/
/* DAT_MAPL - Map LOGICAL data */
/*=============================*/
int
datMapL(const HDSLoc *locator,
const char *mode_str,
int ndim,
const HDS_PTYPE dims[],
int **pntr,
int *status )
{
#undef context_name
#undef context_message
#define context_name "DAT_MAPL_ERR"
#define context_message\
"DAT_MAPL: Error mapping an HDS primitive as logical values."
datMap( locator, "_LOGICAL", mode_str, ndim, dims,
(void**)pntr, status );
return hds_gl_status;
}
/*===============================*/
/* DAT_MAPC - Map CHARACTER data */
/*===============================*/
int
datMapC(const HDSLoc *locator,
const char *mode_str,
int ndim,
const HDS_PTYPE dims[],
unsigned char **pntr,
int *status )
{
#undef context_name
#undef context_message
#define context_name "DAT_MAPC_ERR"
#define context_message\
"DAT_MAPC: Error mapping an HDS primitive as character values."
datMap( locator, "_CHAR", mode_str, ndim, dims,
(void**)pntr, status );
return hds_gl_status;
}
/*===============================*/
/* DAT_MAPV - Map values associated with an object as if vectorized */
/*===============================*/
int
datMapV(const HDSLoc *locator,
const char *type_str,
const char *mode_str,
void **pntr,
size_t *actval,
int *status )
{
#undef context_name
#undef context_message
#define context_name "DAT_MAPV_ERR"
#define context_message\
"DAT_MAPV: Error mapping an HDS vectorized primitive."
/* Local variables */
hdsdim dims[DAT__MXDIM];
int ndim;
/* Initialise return values */
*pntr = NULL;
*actval = 0;
datSize( locator, actval, status );
datShape( locator, DAT__MXDIM, dims, &ndim, status );
datMap( locator, type_str, mode_str, ndim, dims,
pntr, status );
return hds_gl_status;
}
/*===============================================*/
/* DAT_BASIC - Map data (in basic machine units) */
/*===============================================*/
int
datBasic(const HDSLoc *locator,
const char *mode_c,
unsigned char **pntr,
size_t *len,
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_BASIC_ERR"
#define context_message\
"DAT_BASIC: Error mapping an HDS primitive as basic machine units."
struct DSC mode;
struct LCP *lcp;
struct LCP_DATA *data;
struct LCP_STATE *state;
struct RCL rcl;
struct PDD *app;
struct PDD *obj;
unsigned char *dom;
int reading;
INT_BIG objlen;
INT_BIG objoff;
int nbad;
unsigned char *retpntr;
/* Enter routine. */
if (!_ok(*status))
return *status;
hds_gl_status = DAT__OK;
/* Import the mode string. */
_strcsimp( &mode, mode_c );
/* Import the locator. */
_call(dat1_import_loc(locator, &lcp ));
data = &lcp->data;
state = &data->state;
/* Ensure that there is no currently mapped data and that the object is
primitive. */
if (state->mapped)
_callnam(DAT__PRMAP)
if (data->struc)
_callnam(DAT__OBJIN)
/* Validate the access mode. Return if the container file was opened for
read-only access and the mode is 'WRITE' or 'UPDATE'. */
dat1_check_mode( (const char *) mode.body, mode.length, &data->mode,
&hds_gl_status );
_callnam( hds_gl_status )
reading = (data->mode != 'W');
if (data->read && data->mode != 'R')
_callnam(DAT__ACCON)
/* Copy the object data attributes to the application data attributes
descriptor. */
data->app = data->obj;
app = &data->app;
obj = &data->obj;
/* Ensure that the object data is 'active' if reading or updating. */
_callnam( rec_get_rcl(&data->han, &rcl) )
if (reading && !rcl.active)
_callnam(DAT__UNSET)
state->vmcopy = 0;
/* Calculate the length (in bytes) of the virtual memory required for the
object data. Determine the byte offset into the object record's dynamic
domain. */
objlen = obj->length * data->size;
objoff = obj->length * data->offset;
/* Gather discontiguous object data if the program is reading or updating. */
if (state->broken)
{
_callnam( rec_alloc_xmem( objlen, (void **) &app->body ) )
if (reading)
dau_gather_data( 1, data, &nbad );
}
/* Otherwise, the application program gains direct access to the object data. */
else
{
rec_locate_data( &data->han, objlen, objoff, data->mode, &dom );
app->body = dom;
}
/* Save the current setting of the global file-mapping flag, for use when */
/* the data are un-mapped. */
data->filemap = hds_gl_map;
/* Obtain the returned pointer value and its length. Note if the object is */
/* mapped. */
retpntr = app->body;
*len = objlen;
state->mapped = ( retpntr != NULL );
/* Copy c pointer to user */
*pntr = retpntr;
_callnam(hds_gl_status)
return hds_gl_status;
}
/*========================*/
/* DAT_UNMAP - Unmap data */
/*========================*/
int
datUnmap(const HDSLoc *locator,
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_UNMAP_ERR"
#define context_message\
"DAT_UNMAP: Error unmapping an HDS primitive."
struct LCP *lcp;
struct LCP_DATA *data;
/* Enter routine and import the locator. */
if (!_ok(*status))
return *status;
hds_gl_status = DAT__OK;
/* Import locator. */
_call(dat1_import_loc(locator, &lcp ));
data = &lcp->data;
/* Return if the locator is associated with a structure. */
if (data->struc)
_callnam(DAT__OBJIN)
/* Otherwise, flush any mapped data. */
else
{
dau_flush_data( data );
_callnam( hds_gl_status );
}
return hds_gl_status;
}