-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatnew.c
384 lines (306 loc) · 9.81 KB
/
datnew.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
#if HAVE_CONFIG_H
# include <config.h>
#endif
/*+DATNEW.C-*/
/* Include files */
#include <stdio.h> /* stdio for sprintf() prototype */
#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"
/*================================*/
/* DAT_NEW - Create new component */
/*================================*/
int
datNew( const HDSLoc *locator,
const char *name_str,
const char *type_str,
int ndim,
const HDS_PTYPE dims[],
int *status)
{
#undef context_name
#undef context_message
#define context_name "DAT_NEW_ERR"
#define context_message\
"DAT_NEW: Error creating a new HDS component."
struct DSC name;
struct DSC type;
struct LCP *lcp;
struct LCP_DATA *data;
struct PDD obj;
unsigned char *srv;
unsigned char *crv;
char nambuf[DAT__SZNAM];
char typbuf[DAT__SZTYP];
struct RCL rcl;
struct ODL odl;
struct HAN han[2];
INT_BIG size;
int ncomp;
struct RID rid;
INT_BIG off;
int i;
char *name1;
struct RID rid1;
/* Enter routine. */
if (!_ok(*status))
return *status;
hds_gl_status = DAT__OK;
/* Convert name and type strings to descriptors */
_strcsimp( &name, name_str );
_strcsimp( &type, type_str );
/* Import locator. */
_call(dat1_import_loc(locator, &lcp ));
data = &lcp->data;
/* Return if the locator points to anything other than a single structure
object or if the container file was opened for read-only access. */
if (!data->struc || data->naxes != 0)
_call(DAT__OBJIN)
if (data->read)
_call(DAT__ACCON)
/* Validate the object name and type specifications. */
_call( dau_check_name( &name, nambuf ))
_call( dat1_check_type( &type, typbuf ))
/* Determine the object attributes and verify the shape. */
_call( dat1_unpack_type( typbuf, &obj ))
_call( dau_check_shape( ndim, dims, &odl ))
/* Calculate the total size of the object. */
size = 1;
for (i=0; i<ndim; i++)
size *= dims[i];
/* Locate the Structure Record Vector entry which contains the ID of the
component record. */
off = data->offset * SZSRV;
_call( rec_locate_data( &data->han, SZSRV, off, 'U', &srv ) )
dat1_unpack_srv( srv, &rid );
/* If the component Record-ID is null, then create a new record. */
if ( (rid.bloc == 0) && (rid.chip == 0) )
{
rcl.class = DAT__COMPONENT;
rcl.zero = 0;
rcl.slen = DAT__SZNCOMP;
rcl.dlen = SZCRV * hds_gl_ncomp;
rec_create_record( &data->han, &rcl, &han[0] );
rec_get_rid( &han[0], &rid );
/* Pack the new record ID into the Structure Record Vector. */
dat1_pack_srv( &rid, srv );
hds_gl_ncomp = hds_gl_ncomp0;
ncomp = 0;
}
/* Otherwise, stick a handle on the component record, get the Record Control
Label and read the component count. */
else
{
rec_get_handle( &rid, &data->han, &han[0] );
rec_get_rcl( &han[0], &rcl );
dat1_get_ncomp( &han[0], &ncomp );
}
/* Unmap the Structure Record Vector and expand the Component Record Vector
if necessary. */
rec_release_data( &data->han, SZSRV, off, 'U', &srv );
_call(hds_gl_status)
if (ncomp * SZCRV == rcl.dlen)
_call( rec_extend_record( &han[0], SZCRV * hds_gl_ncomp0 ) )
/* If the structure currently has components, then locate the Component Record
Vector and ensure that an object of the same name does not already exist. */
if (ncomp > 0)
{
rid = rec_gl_ridzero;
_call( rec_locate_data( &han[0], rcl.dlen, 0, 'R', &crv ) )
for ( i = 0; i < ncomp; i++ )
{
dat1_locate_name( crv, i, &name1 );
if ( _cheql( DAT__SZNAM, nambuf, name1 ) )
{
dat1_unpack_crv( crv, i, &rid );
break;
}
}
rec_release_data( &han[0], rcl.dlen, 0, 'R', &crv );
if ( (rid.bloc != 0) || (rid.chip != 0) )
_call(DAT__COMEX)
}
/* Fill the appropriate fields in the Record Control Label for the new object
and create the record. */
rcl.class = obj.class;
rcl.zero = (obj.class == DAT__STRUCTURE);
rcl.slen = DAT__SZTYP + DAT__SZNDIM + ( ndim * DAT__SZDIM );
rcl.dlen = size * obj.length;
_call( rec_create_record( &han[0], &rcl, &han[1]) )
/* Save the type specification and write the Object Descriptor Label to
the record's static domain. */
_chmove( DAT__SZTYP, typbuf, odl.type );
_call( dat1_put_odl( &han[1], &odl ) )
/* Remap the Component Record Vector and save the name specification and
Record-ID of the new object. */
_call( rec_locate_data( &han[0], SZCRV, ncomp*SZCRV, 'W', &crv ) )
dat1_locate_name( crv, 0, &name1 );
_chmove( DAT__SZNAM, nambuf, name1 );
rec_get_rid( &han[1], &rid1 );
dat1_pack_crv( &rid1, 0, crv );
rec_release_data( &han[0] ,SZCRV, ncomp*SZCRV, 'W', &crv );
/* Bump the component count. */
++ncomp;
_call( dat1_put_ncomp( &han[0], ncomp ) )
return hds_gl_status;
}
/*====================================*/
/* DAT_NEWC - Create string component */
/*====================================*/
int
datNewC(const HDSLoc *locator,
const char *name_str,
size_t len,
int ndim,
const HDS_PTYPE dims[],
int *status)
{
/* Local variables */
char type_str[DAT__SZTYP+1];
/* Enter routine */
if (!_ok(*status))
return *status;
hds_gl_status = DAT__OK;
/* Construct the type string */
datCctyp( len, type_str );
datNew( locator, name_str, type_str, ndim, dims, status );
return hds_gl_status;
}
/*====================================*/
/* DAT_CCTYP - Create type string */
/*====================================*/
void
datCctyp( size_t size,
char *type )
{
/* have to assume the buffer is big enough - should be DAT__SZTYP+1
- noting that snprintf is C99 so not used (yet) */
sprintf( type, "_CHAR*%lu", (unsigned long)size);
}
/*================================================*/
/* DAT_NEW1 - Create a vector structure component */
/*================================================*/
int
datNew1( const HDSLoc * locator,
const char * name,
const char * type,
size_t len,
int * status )
{
hdsdim dims[1];
if (*status != DAT__OK) return *status;
dims[0] = (hdsdim)len;
datNew( locator, name, type, 1, dims, status );
return *status;
}
/*================================================*/
/* DAT_NEW1D - Create a vector double component */
/*================================================*/
int
datNew1D( const HDSLoc * locator,
const char * name,
size_t len,
int * status )
{
if (*status != DAT__OK) return *status;
datNew1( locator, name, "_DOUBLE", len, status );
return *status;
}
/*================================================*/
/* DAT_NEW1I - Create a vector integer component */
/*================================================*/
int
datNew1I( const HDSLoc * locator,
const char * name,
size_t len,
int * status )
{
if (*status != DAT__OK) return *status;
datNew1( locator, name, "_INTEGER", len, status );
return *status;
}
/*========================================================*/
/* DAT_NEW1K - Create a vector 64-bit integer component */
/*========================================================*/
int
datNew1K( const HDSLoc * locator,
const char * name,
size_t len,
int * status )
{
if (*status != DAT__OK) return *status;
datNew1( locator, name, "_INT64", len, status );
return *status;
}
/*======================================================*/
/* DAT_NEW1W - Create a vector short integer component */
/*======================================================*/
int
datNew1W( const HDSLoc * locator,
const char * name,
size_t len,
int * status )
{
if (*status != DAT__OK) return *status;
datNew1( locator, name, "_WORD", len, status );
return *status;
}
/*================================================================*/
/* DAT_NEW1UW - Create a vector unsigned short integer component */
/*================================================================*/
int
datNew1UW( const HDSLoc * locator,
const char * name,
size_t len,
int * status )
{
if (*status != DAT__OK) return *status;
datNew1( locator, name, "_UWORD", len, status );
return *status;
}
/*================================================*/
/* DAT_NEW1L - Create a vector logical component */
/*================================================*/
int
datNew1L( const HDSLoc * locator,
const char * name,
size_t len,
int * status )
{
if (*status != DAT__OK) return *status;
datNew1( locator, name, "_LOGICAL", len, status );
return *status;
}
/*================================================*/
/* DAT_NEW1R - Create a vector real component */
/*================================================*/
int
datNew1R( const HDSLoc * locator,
const char * name,
size_t len,
int * status )
{
if (*status != DAT__OK) return *status;
datNew1( locator, name, "_REAL", len, status );
return *status;
}
/*================================================*/
/* DAT_NEW1C - Create a vector CHAR component */
/*================================================*/
int
datNew1C( const HDSLoc * locator,
const char * name,
size_t len,
size_t nelem,
int * status )
{
char type[DAT__SZTYP+1];
if (*status != DAT__OK) return *status;
datCctyp( len, type );
datNew1( locator, name, type, nelem, status );
return *status;
}