-
Notifications
You must be signed in to change notification settings - Fork 1
/
hdsCopy.c
213 lines (182 loc) · 7.81 KB
/
hdsCopy.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
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "ems.h" /* EMS error reporting routines */
#include "hds1.h" /* Global definitions for HDS */
#include "rec.h" /* Public rec_ definitions */
#include "rec1.h" /* Private 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"
int
hdsCopy(const HDSLoc *locator,
const char *file_str,
const char name_str[DAT__SZNAM],
int *status )
{
/*
*+
* Name:
* HDS_COPY
* Purpose:
* Copy an object to a new container file.
* Language:
* Starlink C
* Invocation:
* CALL hdsCopy( locator, file, name, status )
* Description:
* The routine makes a copy of an HDS object, placing the copy in a
* new container file (which is created), as the top-level object.
* The copying operation is recursive; i.e. all sub-components of a
* structure will also be copied.
* Arguments:
* locator = char[DAT__SZLOC] (Given)
* Locator for the object to be copied.
* file = char * (Given)
* Name of the new container file to be created.
* name= char[DAT__SZNAM] (Given)
* Name which the new top-level object is to have.
* status = int * (Given and Returned)
* The global status.
* Notes:
* - This routine attempts to eliminate unused space during the
* copying operation and is therefore a useful method of compressing
* a container file from which components have been deleted.
* - The routine may be used to copy both primitive and structured
* objects, but cannot be used to make a copy of a cell or a slice.
* Copyright:
* Copyright (C) 1992 Science & Engineering Research Council
* Authors:
* WFL: William Lupton (AAO)
* RFWS: R.F. Warren-Smith (STARLINK, RAL)
* BKM: Brian McIlwrath (STARLINK, RAL)
* TIMJ: Tim Jenness (JAC, Hawaii)
* {enter_new_authors_here}
* History:
* 18-JUL-1991 (RFWS):
* Complete re-write to avoid file corruption problems associated
* with previous attempts to copy more quickly. The routine now
* always performs a recursive copy and therefore recovers unused
* space. Tidied and added prologue.
* 21-AUG-1991 (RFWS):
* Turn off file mapping for efficiency.
* 8-SEP-1992 (RFWS):
* Allow copy operation to use file mapping or I/O according to
* which performs better on the host machine.
* 8-FEB-1993 (RFWS):
* Fixed bug which caused a crash if the output file was not
* opened successfully (extra ststus check needed).
* 16-JAN-2002 (BKM):
* Convert from C function with a FORTRAN interface to a C-callable
* function with FORTRAN wrapper.
* 15-NOV-2005 (TIMJ):
* Use dat1_import_loc
* Use HDSLoc*
* {enter_further_changes_here}
* Bugs:
* {note_any_bugs_here}
*-
*/
/* Local Variables: */
char *name2; /* Pointer to name in output CRV */
char nambuf[ DAT__SZNAM ]; /* Name buffer */
int save_map; /* Saved mapping flag value */
struct DSC file; /* Descriptor for file name */
struct DSC name; /* Descriptor for component name */
struct HAN han1; /* Handle to input record */
struct HAN han2; /* Handle to output record */
struct LCP *lcp; /* Pointer to Locator Control Packet */
struct LCP_DATA *data=NULL;/* Pointer to LCP data fields */
struct LCP_STATE *state; /* Pointer to LCP state fields */
struct RCL rcl; /* Record Control Label */
struct RID rid1; /* Input Record ID */
unsigned char *crv2; /* Pointer to output CRV */
unsigned char crv1[DAT__SZCRV]; /* Input Component Record Vector */
/*. */
/* Check the inherited global status. */
if ( !_ok( *status ) ) return *status;
hds_gl_status = *status;
/* Import the file and name strings. */
_strcsimp( &file, file_str );
_strcsimp( &name, name_str );
/* Import the source locator. */
dat1_import_loc(locator, &lcp );
if ( _ok( hds_gl_status ) )
{
/* Find the Locator Control Packet data and state fields. */
data = &lcp->data;
state = &data->state;
/* Report an error if the locator is associated with an array slice or */
/* a cell. */
if ( state->slice )
{
hds_gl_status = DAT__OBJIN;
emsRep( "HDS_COPY_1",
"Input object is an array slice and cannot be copied \
(possible programming error).",
&hds_gl_status );
}
else if ( state->cell )
{
hds_gl_status = DAT__OBJIN;
emsRep( "HDS_COPY_2",
"Input object is an array cell and cannot be copied \
(possible programming error).",
&hds_gl_status );
}
}
/* Validate the output object's name. */
dau_check_name( &name, nambuf );
/* Save the current value of the global mapping flag and set a new value to */
/* give the best performance for sequential access. */
save_map = hds_gl_map;
hds_gl_map = ( HDS__MAPSEQ && HDS__CANMAP );
/* Obtain a handle for the input record and manufacture a Component Record */
/* Vector for it (only the RID component is needed). */
if ( _ok( hds_gl_status ) )
{
han1 = data->han;
rec_get_rid( &han1, &rid1 );
dat1_pack_crv( &rid1, 0, crv1 );
/* Attach a handle to the new (output) container file, thus creating the */
/* file. */
hds_gl_64bit = hds_gl_c64bit;
rcl.zero = 1;
rcl.class = DAT__CONTAINER;
rcl.slen = 0;
rcl.dlen = SZCRV;
rec_attach_file( 1, (const char *) file.body, file.length, 'N', 'W',
&rcl, &han2 );
if ( _ok( hds_gl_status ) )
{
/* Locate the output Container Record Vector and copy the new top-level */
/* component name into it. */
rec_locate_data( &han2, SZCRV, 0, 'W', &crv2 );
dat1_locate_name( crv2, 0, &name2 );
if ( _ok( hds_gl_status ) )
{
(void) memcpy( (void *) name2, (void *) nambuf,
(size_t) DAT__SZNAM );
}
/* Perform the copy and release the CRV. */
dau_copy_object( 1, &han1, crv1, &han2, crv2 );
rec_release_data( &han2, SZCRV, 0, 'W', &crv2 );
/* Close the new file. */
rec_close_file( &han2 );
}
}
/* Restore the value of the global mapping flag. */
hds_gl_map = save_map;
/* If an error occurred, then report contextual information. */
if ( !_ok( hds_gl_status ) )
{
emsRep( "HDS_COPY_ERR",
"HDS_COPY: Error copying an HDS object to a new container \
file.",
&hds_gl_status );
}
/* Return the current global status value. */
*status = hds_gl_status;
return *status;
}