summaryrefslogtreecommitdiffstats
path: root/tools/lib/h5diff_attr.c
blob: e10a0238b31317c578e7aa4d5dbf0e1814a72d39 (plain)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "h5tools.h"
#include "h5diff.h"
#include "H5private.h"

/*-------------------------------------------------------------------------
 * Function: diff_attr
 *
 * Purpose: compare attributes located in LOC1_ID and LOC2_ID, which are
 *  obtained either from
 * loc_id = H5Gopen( fid, name);
 * loc_id = H5Dopen( fid, name);
 * loc_id = H5Topen( fid, name);
 *
 * Return:
 *  0 : no differences found
 *  1 : differences found
 *
 * Programmer: Pedro Vicente, pvn@ncsa.uiuc.edu
 *
 * Date: November, 03, 2003
 *
 *-------------------------------------------------------------------------
 */

int diff_attr(hid_t loc1_id,
              hid_t  loc2_id,
              const char *path1,
              const char *path2,
              diff_opt_t *options)
{
 hid_t      attr1_id=-1;     /* attr ID */
 hid_t      attr2_id=-1;     /* attr ID */
 hid_t      space1_id=-1;    /* space ID */
 hid_t      space2_id=-1;    /* space ID */
 hid_t      ftype1_id=-1;    /* file data type ID */
 hid_t      ftype2_id=-1;    /* file data type ID */
 hid_t      mtype1_id=-1;    /* memory data type ID */
 hid_t      mtype2_id=-1;    /* memory data type ID */
 size_t     msize1;       /* memory size of memory type */
 size_t     msize2;       /* memory size of memory type */
 void       *buf1=NULL;   /* data buffer */
 void       *buf2=NULL;   /* data buffer */
 hsize_t    nelmts1;      /* number of elements in dataset */
 int        rank1;        /* rank of dataset */
 int        rank2;        /* rank of dataset */
 hsize_t    dims1[H5S_MAX_RANK];/* dimensions of dataset */
 hsize_t    dims2[H5S_MAX_RANK];/* dimensions of dataset */
 char       name1[512];
 char       name2[512];
 char       np1[512];
 char       np2[512];
 int        n1, n2, i, j;
 int        ret=0;
 hsize_t    nfound;
 int        cmp=1;

 if ((n1 = H5Aget_num_attrs(loc1_id))<0)
  goto error;
 if ((n2 = H5Aget_num_attrs(loc2_id))<0)
  goto error;

 if (n1!=n2)
  return 1;

 for ( i = 0; i < n1; i++)
 {
  /* reset buffers for every attribute, we might goto out and call free */
  buf1=NULL;
  buf2=NULL;

  /*-------------------------------------------------------------------------
  * open
  *-------------------------------------------------------------------------
  */
  /* open attribute */
  if ((attr1_id = H5Aopen_idx(loc1_id, (unsigned)i))<0)
   goto error;
  if ((attr2_id = H5Aopen_idx(loc2_id, (unsigned)i))<0)
   goto error;

  /* get name */
  if (H5Aget_name( attr1_id, 255, name1 )<0)
   goto error;
  if (H5Aget_name( attr2_id, 255, name2 )<0)
   goto error;

  if (HDstrcmp(name1,name2)!=0)
  {
   if (options->m_verbose)
   {
    parallel_print("Different name for attributes: <%s> and <%s>\n", name1, name2);
   }
   H5Aclose(attr1_id);
   H5Aclose(attr2_id);
   ret=1;
   continue;
  }

  /* get the file datatype  */
  if ((ftype1_id = H5Aget_type( attr1_id )) < 0 )
   goto error;
  if ((ftype2_id = H5Aget_type( attr2_id )) < 0 )
   goto error;

  /* get the dataspace handle  */
  if ((space1_id = H5Aget_space( attr1_id )) < 0 )
   goto error;
  if ((space2_id = H5Aget_space( attr2_id )) < 0 )
   goto error;

  /* get dimensions  */
  if ( (rank1 = H5Sget_simple_extent_dims(space1_id, dims1, NULL)) < 0 )
   goto error;
  if ( (rank2 = H5Sget_simple_extent_dims(space2_id, dims2, NULL)) < 0 )
   goto error;

 /*-------------------------------------------------------------------------
  * check for comparable TYPE and SPACE
  *-------------------------------------------------------------------------
  */

  if (diff_can_type(ftype1_id,
   ftype2_id,
   rank1,
   rank2,
   dims1,
   dims2,
   NULL,
   NULL,
   name1,
   name2,
   options)!=1)
  {
   cmp=0;
   options->not_cmp=1;
  }
 /*-------------------------------------------------------------------------
  * only attempt to compare if possible
  *-------------------------------------------------------------------------
  */
  if (cmp)
  {

  /*-------------------------------------------------------------------------
   * read to memory
   *-------------------------------------------------------------------------
   */
   nelmts1=1;
   for (j=0; j<rank1; j++)
    nelmts1*=dims1[j];

   if ((mtype1_id=h5tools_get_native_type(ftype1_id))<0)
    goto error;

   if ((mtype2_id=h5tools_get_native_type(ftype2_id))<0)
    goto error;

   if ((msize1=H5Tget_size(mtype1_id))==0)
    goto error;
   if ((msize2=H5Tget_size(mtype2_id))==0)
    goto error;

   assert(msize1==msize2);

   buf1=(void *) HDmalloc((unsigned)(nelmts1*msize1));
   buf2=(void *) HDmalloc((unsigned)(nelmts1*msize2));
   if ( buf1==NULL || buf2==NULL){
    parallel_print( "cannot read into memory\n" );
    goto error;
   }
   if (H5Aread(attr1_id,mtype1_id,buf1)<0)
    goto error;
   if (H5Aread(attr2_id,mtype2_id,buf2)<0)
    goto error;

   /* format output string */
   sprintf(np1,"%s of <%s>",name1,path1);
   sprintf(np2,"%s of <%s>",name2,path2);

  /*-------------------------------------------------------------------------
   * array compare
   *-------------------------------------------------------------------------
   */

   /* always print name */
   if (options->m_verbose)
   {
    parallel_print( "Attribute:   <%s> and <%s>\n",np1,np2);
    nfound = diff_array(buf1,
     buf2,
     nelmts1,
     (hsize_t)0,
     rank1,
     dims1,
     options,
     np1,
     np2,
     mtype1_id,
     attr1_id,
     attr2_id);
    print_found(nfound);

   }
   /* check first if we have differences */
   else
   {
    if (options->m_quiet==0)
    {
     /* shut up temporarily */
     options->m_quiet=1;
     nfound = diff_array(buf1,
      buf2,
      nelmts1,
      (hsize_t)0,
      rank1,
      dims1,
      options,
      np1,
      np2,
      mtype1_id,
      attr1_id,
      attr2_id);
     /* print again */
     options->m_quiet=0;
     if (nfound)
     {
      parallel_print( "Attribute:   <%s> and <%s>\n",np1,np2);
      nfound = diff_array(buf1,
       buf2,
       nelmts1,
       (hsize_t)0,
       rank1,
       dims1,
       options,
       np1,
       np2,
       mtype1_id,
       attr1_id,
       attr2_id);
      print_found(nfound);
     } /*if*/
    } /*if*/
    /* in quiet mode, just count differences */
    else
    {
     nfound = diff_array(buf1,
      buf2,
      nelmts1,
      (hsize_t)0,
      rank1,
      dims1,
      options,
      np1,
      np2,
      mtype1_id,
      attr1_id,
      attr2_id);
    } /*else quiet */
   } /*else verbose */
 }/*cmp*/


 /*-------------------------------------------------------------------------
  * close
  *-------------------------------------------------------------------------
  */

  if (H5Tclose(ftype1_id)<0) goto error;
  if (H5Tclose(ftype2_id)<0) goto error;
  if (H5Tclose(mtype1_id)<0) goto error;
  if (H5Tclose(mtype2_id)<0) goto error;
  if (H5Sclose(space1_id)<0) goto error;
  if (H5Sclose(space2_id)<0) goto error;
  if (H5Aclose(attr1_id)<0) goto error;
  if (H5Aclose(attr2_id)<0) goto error;
  if (buf1)
   HDfree(buf1);
  if (buf2)
   HDfree(buf2);
 } /* i */

 return ret;

error:
 H5E_BEGIN_TRY {
  H5Tclose(ftype1_id);
  H5Tclose(ftype2_id);
  H5Tclose(mtype1_id);
  H5Tclose(mtype2_id);
  H5Sclose(space1_id);
  H5Sclose(space2_id);
  H5Aclose(attr1_id);
  H5Aclose(attr2_id);
  if (buf1)
   HDfree(buf1);
  if (buf2)
   HDfree(buf2);
 } H5E_END_TRY;

 options->err_stat=1;
 return 0;
}