1 /*
2  * Copyright (c) 2005 Sandia Corporation. Under the terms of Contract
3  * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Governement
4  * retains certain rights in this software.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials provided
16  *       with the distribution.
17  *
18  *     * Neither the name of Sandia Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #include <string.h>
37 #include "exodusII.h"
38 #include "exodusII_int.h"
39 
40 /*!
41 
42 The function ex_get_prop_names() returns names of integer properties
43 stored for an element block, node set, or side set. The number of
44 properties (needed to allocate space for the property names) can be
45 obtained via a call to ex_inquire() or ex_inquire_int().
46 
47 \return In case of an error, ex_get_prop_names() returns a negative number; a
48 warning will return a positive number.  Possible causes of errors
49 include:
50   -  data file not properly opened with call to ex_create() or ex_open()
51   -  invalid object type specified.
52 
53 
54 \param[in]   exoid        exodus file ID returned from a previous call to ex_create() or ex_open().
55 \param[in]   obj_type     Type of object; use one of the options in the table below.
56 \param[out]  prop_names   Returned array containing \c num_props (obtained from call to
57                           ex_inquire() or ex_inquire_int()) names (of maximum length
58 			  \p MAX_STR_LENGTH ) of properties to be stored. \b ID, a
59 			  reserved property name, will be the first name in the array.
60 
61 <table>
62 <tr><td> \c EX_NODE_SET   </td><td>  Node Set entity type     </td></tr>
63 <tr><td> \c EX_EDGE_BLOCK </td><td>  Edge Block entity type   </td></tr>
64 <tr><td> \c EX_EDGE_SET   </td><td>  Edge Set entity type     </td></tr>
65 <tr><td> \c EX_FACE_BLOCK </td><td>  Face Block entity type   </td></tr>
66 <tr><td> \c EX_FACE_SET   </td><td>  Face Set entity type     </td></tr>
67 <tr><td> \c EX_ELEM_BLOCK </td><td>  Element Block entity type</td></tr>
68 <tr><td> \c EX_ELEM_SET   </td><td>  Element Set entity type  </td></tr>
69 <tr><td> \c EX_SIDE_SET   </td><td>  Side Set entity type     </td></tr>
70 <tr><td> \c EX_ELEM_MAP   </td><td>  Element Map entity type  </td></tr>
71 <tr><td> \c EX_NODE_MAP   </td><td>  Node Map entity type     </td></tr>
72 <tr><td> \c EX_EDGE_MAP   </td><td>  Edge Map entity type     </td></tr>
73 <tr><td> \c EX_FACE_MAP   </td><td>  Face Map entity type     </td></tr>
74 </table>
75 
76 As an example, the following code segment reads in properties assigned
77 to node sets:
78 
79 \code
80 #include "exodusII.h";
81 int error, exoid, num_props, *prop_values;
82 char *prop_names[MAX_PROPS];
83 
84 \comment{read node set properties}
85 num_props = ex_inquire_int(exoid, EX_INQ_NS_PROP);
86 
87 for (i=0; i < num_props; i++) {
88    prop_names[i] = (char *) malloc ((MAX_STR_LENGTH+1), sizeof(char));
89    prop_values = (int *) malloc (num_node_sets, sizeof(int));
90 }
91 
92 error = ex_get_prop_names(exoid,EX_NODE_SET,prop_names);
93 
94 for (i=0; i < num_props; i++) {
95    error = ex_get_prop_array(exoid, EX_NODE_SET, prop_names[i],
96                              prop_values);
97 }
98 \endcode
99 
100 */
101 
ex_get_prop_names(int exoid,ex_entity_type obj_type,char ** prop_names)102 int ex_get_prop_names (int    exoid,
103                        ex_entity_type obj_type,
104                        char **prop_names)
105 {
106   int status;
107   int i, num_props, propid;
108   char var_name[12];
109   size_t att_len;
110   nc_type att_type;
111   int api_name_size = ex_inquire_int(exoid, EX_INQ_MAX_READ_NAME_LENGTH);
112 
113   char errmsg[MAX_ERR_LENGTH];
114 
115   exerrval = 0;
116 
117   /* determine which type of object property names are desired for */
118 
119   num_props = ex_get_num_props (exoid, obj_type);
120 
121   for (i=0; i<num_props; i++) {
122     switch (obj_type) {
123     case EX_ELEM_BLOCK:
124       strcpy (var_name, VAR_EB_PROP(i+1));
125       break;
126     case EX_FACE_BLOCK:
127       strcpy (var_name, VAR_FA_PROP(i+1));
128       break;
129     case EX_EDGE_BLOCK:
130       strcpy (var_name, VAR_ED_PROP(i+1));
131       break;
132     case EX_NODE_SET:
133       strcpy (var_name, VAR_NS_PROP(i+1));
134       break;
135     case EX_SIDE_SET:
136       strcpy (var_name, VAR_SS_PROP(i+1));
137       break;
138     case EX_EDGE_SET:
139       strcpy (var_name, VAR_ES_PROP(i+1));
140       break;
141     case EX_FACE_SET:
142       strcpy (var_name, VAR_FS_PROP(i+1));
143       break;
144     case EX_ELEM_SET:
145       strcpy (var_name, VAR_ELS_PROP(i+1));
146       break;
147     case EX_ELEM_MAP:
148       strcpy (var_name, VAR_EM_PROP(i+1));
149       break;
150     case EX_FACE_MAP:
151       strcpy (var_name, VAR_FAM_PROP(i+1));
152       break;
153     case EX_EDGE_MAP:
154       strcpy (var_name, VAR_EDM_PROP(i+1));
155       break;
156     case EX_NODE_MAP:
157       strcpy (var_name, VAR_NM_PROP(i+1));
158       break;
159     default:
160       exerrval = EX_BADPARAM;
161       sprintf(errmsg, "Error: object type %d not supported; file id %d",
162 	      obj_type, exoid);
163       ex_err("ex_get_prop_names",errmsg,EX_BADPARAM);
164       return(EX_FATAL);
165     }
166 
167     if ((status = nc_inq_varid(exoid, var_name, &propid)) != NC_NOERR) {
168       exerrval = status;
169       sprintf(errmsg,
170 	      "Error: failed to locate property array %s in file id %d",
171 	      var_name, exoid);
172       ex_err("ex_get_prop_names",errmsg,exerrval);
173       return (EX_FATAL);
174     }
175 
176     /*   for each property, read the "name" attribute of property array variable */
177     if ((status = nc_inq_att(exoid, propid, ATT_PROP_NAME, &att_type, &att_len)) != NC_NOERR) {
178       exerrval = status;
179       sprintf(errmsg,
180 	      "Error: failed to get property attributes (type, len) in file id %d", exoid);
181       ex_err("ex_get_prop_names",errmsg,exerrval);
182       return (EX_FATAL);
183     }
184 
185     if (att_len-1 <= api_name_size) {
186       /* Client has large enough char string to hold text... */
187       if ((status = nc_get_att_text(exoid, propid, ATT_PROP_NAME, prop_names[i])) != NC_NOERR) {
188 	exerrval = status;
189 	sprintf(errmsg,
190 		"Error: failed to get property name in file id %d", exoid);
191 	ex_err("ex_get_prop_names",errmsg,exerrval);
192 	return (EX_FATAL);
193       }
194     }
195     else {
196       /* FIXME */
197       exerrval = NC_ESTS;
198       sprintf(errmsg,
199 	      "Error: property name length exceeds space available to store it in file id %d", exoid);
200       ex_err("ex_get_prop_names",errmsg,exerrval);
201       return (EX_FATAL);
202     }
203   }
204   return (EX_NOERR);
205 }
206