Debian Codegraph @

packages/seacas/libraries/exodus/src/ex_put_blobs.c

raw
1 /*
2 * Copyright(C) 1999-2020, 2022 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "exodusII.h"
10 #include "exodusII_int.h"
11 #include <stdbool.h>
12 /*!
13 * writes the blob parameters for 1 or more blobs
14 * \param exoid exodus file id
15 * \param count size of `blobs` array
16 * \param *blobs array of ex_blob structures
17 */
18
19 int ex_put_blobs(int exoid, size_t count, const struct ex_blob *blobs)
20 {
21 int dimid, status, n1dim, dims[1];
22 char errmsg[MAX_ERR_LENGTH];
23
24 EX_FUNC_ENTER();
25
26 if (exi_check_valid_file_id(exoid, __func__) == EX_FATAL) {
27 EX_FUNC_LEAVE(EX_FATAL);
28 }
29
30 int *entlst_id = (int *)calloc(count, sizeof(int));
31
32 if ((status = exi_redef(exoid, __func__)) != NC_NOERR) {
33 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to put file id %d into define mode", exoid);
34 ex_err_fn(exoid, __func__, errmsg, status);
35 free(entlst_id);
36 EX_FUNC_LEAVE(EX_FATAL);
37 }
38
39 status = nc_inq_dimid(exoid, DIM_N1, &n1dim);
40 if (status != NC_NOERR) {
41 if ((status = nc_def_dim(exoid, DIM_N1, 1L, &n1dim)) != NC_NOERR) {
42 snprintf(errmsg, MAX_ERR_LENGTH,
43 "ERROR: failed to define number \"1\" dimension in file id %d", exoid);
44 ex_err_fn(exoid, __func__, errmsg, status);
45 goto error_ret; /* exit define mode and return */
46 }
47 }
48
49 for (size_t i = 0; i < count; i++) {
50 char *numentryptr = DIM_NUM_VALUES_BLOB(blobs[i].id);
51
52 /* define dimensions and variables */
53 if ((status = nc_def_dim(exoid, numentryptr, blobs[i].num_entry, &dimid)) != NC_NOERR) {
54 if (status == NC_ENAMEINUSE) {
55 snprintf(errmsg, MAX_ERR_LENGTH,
56 "ERROR: blob %" PRId64 " -- size already defined in file id %d", blobs[i].id,
57 exoid);
58 ex_err_fn(exoid, __func__, errmsg, status);
59 }
60 else {
61 snprintf(errmsg, MAX_ERR_LENGTH,
62 "ERROR: failed to define number of entries in blob %" PRId64 " in file id %d",
63 blobs[i].id, exoid);
64 ex_err_fn(exoid, __func__, errmsg, status);
65 }
66 goto error_ret;
67 }
68
69 /* create a variable just as a way to have a blob and its attributes; values not used for
70 * anything */
71 dims[0] = n1dim;
72 if ((status = nc_def_var(exoid, VAR_ENTITY_BLOB(blobs[i].id), NC_INT, 1, dims,
73 &entlst_id[i])) != NC_NOERR) {
74 if (status == NC_ENAMEINUSE) {
75 snprintf(errmsg, MAX_ERR_LENGTH,
76 "ERROR: entity already exists for blob %" PRId64 " in file id %d", blobs[i].id,
77 exoid);
78 ex_err_fn(exoid, __func__, errmsg, status);
79 }
80 else {
81 snprintf(errmsg, MAX_ERR_LENGTH,
82 "ERROR: failed to create entity for blob %" PRId64 " in file id %d", blobs[i].id,
83 exoid);
84 ex_err_fn(exoid, __func__, errmsg, status);
85 }
86 goto error_ret; /* exit define mode and return */
87 }
88 exi_compress_variable(exoid, entlst_id[i], 1);
89
90 if (ex_int64_status(exoid) & EX_IDS_INT64_DB) {
91 long long id = blobs[i].id;
92 status = nc_put_att_longlong(exoid, entlst_id[i], EX_ATTRIBUTE_ID, NC_INT64, 1, &id);
93 }
94 else {
95 int id = blobs[i].id;
96 status = nc_put_att_int(exoid, entlst_id[i], EX_ATTRIBUTE_ID, NC_INT, 1, &id);
97 }
98 if (status != NC_NOERR) {
99 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store blob id %" PRId64 " in file id %d",
100 blobs[i].id, exoid);
101 ex_err_fn(exoid, __func__, errmsg, status);
102 goto error_ret; /* exit define mode and return */
103 }
104
105 if ((status = nc_put_att_text(exoid, entlst_id[i], EX_ATTRIBUTE_NAME, strlen(blobs[i].name) + 1,
106 blobs[i].name)) != NC_NOERR) {
107 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to store blob name %s in file id %d",
108 blobs[i].name, exoid);
109 ex_err_fn(exoid, __func__, errmsg, status);
110 goto error_ret; /* exit define mode and return */
111 }
112
113 /* Increment blob count */
114 struct exi_file_item *file = exi_find_file_item(exoid);
115 if (file) {
116 file->blob_count++;
117 }
118 }
119 /* leave define mode */
120 if ((status = exi_leavedef(exoid, __func__)) != NC_NOERR) {
121 snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: failed to exit define mode in file id %d", exoid);
122 ex_err_fn(exoid, __func__, errmsg, status);
123 free(entlst_id);
124 EX_FUNC_LEAVE(EX_FATAL);
125 }
126
127 /* Output dummy data for the blob var; */
128 long dummy = 0;
129 for (size_t i = 0; i < count; i++) {
130 if ((status = nc_put_var_long(exoid, entlst_id[i], &dummy)) != EX_NOERR) {
131 snprintf(errmsg, MAX_ERR_LENGTH,
132 "ERROR: failed to output dummy value for blob %" PRId64 " in file id %d",
133 blobs[i].id, exoid);
134 ex_err_fn(exoid, __func__, errmsg, status);
135 free(entlst_id);
136 EX_FUNC_LEAVE(EX_FATAL);
137 }
138 }
139 free(entlst_id);
140 EX_FUNC_LEAVE(EX_NOERR);
141
142 /* Fatal error: exit definition mode and return */
143 error_ret:
144 exi_leavedef(exoid, __func__);
145 free(entlst_id);
146 EX_FUNC_LEAVE(EX_FATAL);
147 }