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
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* Copyright by the Board of Trustees of the University of Illinois. *
* All rights reserved. *
* *
* This file is part of HDF. The full HDF copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://support.hdfgroup.org/ftp/HDF/releases/. *
* If you do not have access to either file, you may request a copy from *
* help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <assert.h>
#include <stdlib.h>
#include "hdf.h"
#include "mfhdf.h"
#include "hrepack_vs.h"
#include "hrepack_utils.h"
#include "hrepack_an.h"
/*-------------------------------------------------------------------------
* Function: copy_vs
*
* Purpose: copy a VS from input file to output file
*
* Return: 0, -1 for error
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: July 11, 2003
*
*-------------------------------------------------------------------------
*/
int
copy_vs(int32 infile_id, int32 outfile_id, int32 tag, /* tag of input VS */
int32 ref, /* ref of input VS */
int32 vgroup_id_out_par, /* output parent group ID */
char *path_name, /* absolute path for input group name */
options_t *options, list_table_t *list_tbl, int is_lone)
{
int32 vdata_id = FAIL; /* vdata identifier */
int32 vdata_out = FAIL; /* vdata identifier */
int32 vdata_ref, /* reference number of the vdata */
n_records, /* number of records */
vdata_size, interlace_mode, field_type, field_order;
int n_fields, n_attrs;
char *vdata_name = NULL;
char *vdata_class = NULL;
char *fieldname_list = NULL;
char *path = NULL;
char *field_name;
uint8 *buf = NULL;
int ret = 0;
if (NULL == (vdata_name = calloc(1, VSNAMELENMAX))) {
ret = -1;
goto out;
}
if (NULL == (vdata_class = calloc(1, VSNAMELENMAX))) {
ret = -1;
goto out;
}
if (NULL == (fieldname_list = calloc(1, VSFIELDMAX * FIELDNAMELENMAX))) {
ret = -1;
goto out;
}
/*-------------------------------------------------------------------------
* attach the vdata, gets its name and class
*-------------------------------------------------------------------------
*/
if ((vdata_id = VSattach(infile_id, ref, "r")) == FAIL) {
printf("Failed to attach vdata ref %d\n", ref);
ret = -1;
goto out;
}
if (VSgetname(vdata_id, vdata_name) == FAIL) {
printf("Failed to name for vdata ref %d\n", ref);
ret = -1;
goto out;
}
if (VSgetclass(vdata_id, vdata_class) == FAIL) {
printf("Failed to name for vdata ref %d\n", ref);
ret = -1;
goto out;
}
/* ignore reserved HDF groups/vdatas; they are lone ones */
if (is_lone == 1 && vdata_class[0] == '\0') {
if (is_reserved(vdata_class)) {
if (VSdetach(vdata_id) == FAIL)
printf("Failed to detach vdata <%s>\n", path_name);
ret = 0;
goto out;
}
}
/* initialize path */
path = get_path(path_name, vdata_name);
/* add object to table */
list_table_add(list_tbl, tag, ref, path);
if (options->verbose) {
printf(PFORMAT, "", "", "", path);
}
/* check inspection mode */
if (options->trip == 0) {
if (VSdetach(vdata_id) == FAIL) {
printf("Failed to detach vdata <%s>\n", path_name);
ret = -1;
goto out;
}
vdata_id = FAIL;
ret = 0;
goto out;
}
/*-------------------------------------------------------------------------
* get vdata info
*-------------------------------------------------------------------------
*/
if (VSinquire(vdata_id, &n_records, &interlace_mode, fieldname_list, &vdata_size, vdata_name) == FAIL) {
printf("Failed to get info for vdata ref %d\n", ref);
ret = -1;
goto out;
}
/*-------------------------------------------------------------------------
* create the VS in the output file. the vdata reference number is set
* to -1 for creating and the access mode is "w" for writing
*-------------------------------------------------------------------------
*/
if ((vdata_out = VSattach(outfile_id, -1, "w")) == FAIL) {
printf("Failed to create new VS <%s>\n", path);
VSdetach(vdata_id);
ret = -1;
goto out;
}
if (VSsetname(vdata_out, vdata_name) == FAIL) {
printf("Failed to set name for new VS <%s>\n", path);
ret = -1;
goto out;
}
if (VSsetclass(vdata_out, vdata_class) == FAIL) {
printf("Failed to set class for new VS <%s>\n", path);
ret = -1;
goto out;
}
if (VSsetinterlace(vdata_out, interlace_mode) == FAIL) {
printf("Failed to set interlace mode for output vdata\n");
ret = -1;
goto out;
}
/*-------------------------------------------------------------------------
* define the fields for vdata_out
*-------------------------------------------------------------------------
*/
if ((n_fields = VFnfields(vdata_id)) == FAIL) {
printf("Failed getting fields for VS <%s>\n", path);
ret = -1;
goto out;
}
for (int i = 0; i < n_fields; i++) {
field_name = VFfieldname(vdata_id, i);
field_type = VFfieldtype(vdata_id, i);
field_order = VFfieldorder(vdata_id, i);
if (VSfdefine(vdata_out, field_name, field_type, field_order) == FAIL) {
printf("Error: cannot define fields for VS <%s>\n", path);
ret = -1;
goto out;
}
}
/* Set fields */
if (VSsetfields(vdata_out, fieldname_list) == FAIL) {
printf("Error: cannot define fields for VS <%s>\n", path);
ret = -1;
goto out;
}
/*-------------------------------------------------------------------------
* read, write vdata
*-------------------------------------------------------------------------
*/
/* Set fields for reading */
if (VSsetfields(vdata_id, fieldname_list) == FAIL) {
printf("Error: cannot define fields for VS <%s>\n", path);
ret = -1;
goto out;
}
if (n_records > 0) {
if ((buf = (uint8 *)malloc((size_t)(n_records * vdata_size))) == NULL) {
printf("Failed to get memory for new VS <%s>\n", path);
ret = -1;
goto out;
}
if (VSread(vdata_id, buf, n_records, interlace_mode) == FAIL) {
printf("Error reading vdata <%s>\n", path);
ret = -1;
goto out;
}
if (VSwrite(vdata_out, buf, n_records, interlace_mode) == FAIL) {
printf("Error writing vdata <%s>\n", path);
ret = -1;
goto out;
}
}
/*-------------------------------------------------------------------------
* read, write attributes
*-------------------------------------------------------------------------
*/
if ((n_attrs = VSfnattrs(vdata_id, -1)) == FAIL) {
printf("Failed getting attributes for VS <%s>\n", path);
ret = -1;
goto out;
}
for (int i = 0; i < n_attrs; i++) {
copy_vdata_attribute(vdata_id, vdata_out, -1, i);
}
/*-------------------------------------------------------------------------
* read, write field attributes
*-------------------------------------------------------------------------
*/
for (int i = 0; i < n_fields; i++) {
if ((n_attrs = VSfnattrs(vdata_id, i)) == FAIL) {
printf("Failed getting fields for VS <%s>\n", path);
ret = -1;
goto out;
}
for (int j = 0; j < n_attrs; j++) {
copy_vdata_attribute(vdata_id, vdata_out, i, j);
}
}
/*-------------------------------------------------------------------------
* add VS to group, if needed
*-------------------------------------------------------------------------
*/
if (vgroup_id_out_par) {
/* obtain the reference number of the new VS */
if ((vdata_ref = VSQueryref(vdata_out)) == 0) {
printf("Failed to get new VS reference in <%s>\n", path);
}
/* add the VS to the vgroup. the INPUT TAG is used */
if (Vaddtagref(vgroup_id_out_par, tag, vdata_ref) == FAIL) {
printf("Failed to add new VS to group <%s>\n", path);
}
}
/*-------------------------------------------------------------------------
* copy ANs
*-------------------------------------------------------------------------
*/
if (copy_vs_an(infile_id, outfile_id, vdata_id, vdata_out, path, options) < 0) {
ret = -1;
goto out;
}
out:
/* terminate access to the VSs */
if (FAIL != vdata_id && VSdetach(vdata_id)) {
printf("Could not detach VG in <%s>\n", path);
ret = -1;
}
if (FAIL != vdata_out && VSdetach(vdata_out) == FAIL) {
printf("Could not detach VG in <%s>\n", path);
ret = -1;
}
free(vdata_name);
free(vdata_class);
free(fieldname_list);
free(path);
free(buf);
return ret;
}
/*-------------------------------------------------------------------------
* Function: copy_vdata_attribute
*
* Purpose: copy VS attributes from input file to output file
*
* Return: 1, for success, -1 for error
*
* Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
*
* Date: July 28, 2003
*
*-------------------------------------------------------------------------
*/
int
copy_vdata_attribute(int32 in, int32 out, int32 findex, intn attrindex)
{
char attr_name[H4_MAX_NC_NAME];
int32 n_values, attr_size, attr_type;
void **values = NULL;
/* Get attribute information */
VSattrinfo(in, findex, attrindex, attr_name, &attr_type, &n_values, &attr_size);
/* Allocate space for attribute values */
if ((values = (void *)malloc((size_t)(attr_size * n_values))) == NULL) {
printf("Cannot allocate %d values of size %d for attribute %s", n_values, attr_size, attr_name);
return -1;
}
/* Read attribute from input object */
if (VSgetattr(in, findex, attrindex, values) == FAIL) {
printf("Cannot read attribute %s\n", attr_name);
free(values);
return -1;
}
/* Write attribute to output object */
if (VSsetattr(out, findex, attr_name, attr_type, n_values, values) == FAIL) {
printf("Cannot write attribute %s\n", attr_name);
free(values);
return -1;
}
free(values);
return 1;
}
|