| 1 | /*--------------------------------------------------------------------------- |
|---|
| 2 | NeXus - Neutron & X-ray Common Data Format |
|---|
| 3 | |
|---|
| 4 | Test program for C API |
|---|
| 5 | |
|---|
| 6 | Copyright (C) 1997-2011 Freddie Akeroyd |
|---|
| 7 | |
|---|
| 8 | This library is free software; you can redistribute it and/or |
|---|
| 9 | modify it under the terms of the GNU Lesser General Public |
|---|
| 10 | License as published by the Free Software Foundation; either |
|---|
| 11 | version 2 of the License, or (at your option) any later version. |
|---|
| 12 | |
|---|
| 13 | This library is distributed in the hope that it will be useful, |
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 16 | Lesser General Public License for more details. |
|---|
| 17 | |
|---|
| 18 | You should have received a copy of the GNU Lesser General Public |
|---|
| 19 | License along with this library; if not, write to the Free Software |
|---|
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 21 | |
|---|
| 22 | For further information, see <http://www.nexusformat.org> |
|---|
| 23 | |
|---|
| 24 | $Id$ |
|---|
| 25 | |
|---|
| 26 | ----------------------------------------------------------------------------*/ |
|---|
| 27 | #include <stdio.h> |
|---|
| 28 | #include <stdlib.h> |
|---|
| 29 | #include <string.h> |
|---|
| 30 | #ifndef _WIN32 |
|---|
| 31 | #include <unistd.h> |
|---|
| 32 | #endif |
|---|
| 33 | #include "napi.h" |
|---|
| 34 | #include "napiconfig.h" |
|---|
| 35 | |
|---|
| 36 | static void print_data (const char *prefix, void *data, int type, int num); |
|---|
| 37 | static int testLoadPath(); |
|---|
| 38 | static int testExternal(char *progName); |
|---|
| 39 | |
|---|
| 40 | static const char *relativePathOf(const char* filename) { |
|---|
| 41 | char cwd[1024]; |
|---|
| 42 | |
|---|
| 43 | getcwd(cwd, sizeof(cwd)); |
|---|
| 44 | |
|---|
| 45 | if (strncmp(filename, cwd, strlen(cwd)) == 0) |
|---|
| 46 | { |
|---|
| 47 | return filename+strlen(cwd)+1; |
|---|
| 48 | } |
|---|
| 49 | else |
|---|
| 50 | { |
|---|
| 51 | return filename; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | int main (int argc, char *argv[]) |
|---|
| 56 | { |
|---|
| 57 | int i, j, k, n, NXrank, NXdims[32], NXtype, NXlen, entry_status, attr_status; |
|---|
| 58 | float r; |
|---|
| 59 | void *data_buffer; |
|---|
| 60 | unsigned char i1_array[4] = {1, 2, 3, 4}; |
|---|
| 61 | short int i2_array[4] = {1000, 2000, 3000, 4000}; |
|---|
| 62 | int i4_array[4] = {1000000, 2000000, 3000000, 4000000}; |
|---|
| 63 | float r4_array[5][4] = |
|---|
| 64 | {{1., 2., 3., 4.}, {5., 6., 7., 8.}, {9., 10., 11., 12.}, {13., 14., 15., 16.}, {17., 18., 19., 20.}}; |
|---|
| 65 | double r8_array[5][4] = |
|---|
| 66 | {{1., 2., 3., 4.}, {5., 6., 7., 8.}, {9., 10., 11., 12.}, {13., 14., 15., 16.}, {17., 18., 19., 20.}}; |
|---|
| 67 | int array_dims[2] = {5, 4}; |
|---|
| 68 | int unlimited_dims[1] = {NX_UNLIMITED}; |
|---|
| 69 | int chunk_size[2]={5,4}; |
|---|
| 70 | int slab_start[2], slab_size[2]; |
|---|
| 71 | char name[64], char_class[64], char_buffer[128]; |
|---|
| 72 | char group_name[64], class_name[64]; |
|---|
| 73 | char c1_array[5][4] = {{'a', 'b', 'c' ,'d'}, {'e', 'f', 'g' ,'h'}, |
|---|
| 74 | {'i', 'j', 'k', 'l'}, {'m', 'n', 'o', 'p'}, {'q', 'r', 's' , 't'}}; |
|---|
| 75 | int unlimited_cdims[2] = {NX_UNLIMITED, 4}; |
|---|
| 76 | NXhandle fileid, clone_fileid; |
|---|
| 77 | NXlink glink, dlink, blink; |
|---|
| 78 | int comp_array[100][20]; |
|---|
| 79 | int dims[2]; |
|---|
| 80 | int cdims[2]; |
|---|
| 81 | int nx_creation_code; |
|---|
| 82 | char nxFile[80]; |
|---|
| 83 | char filename[256]; |
|---|
| 84 | int64_t grossezahl[4]; |
|---|
| 85 | const char* ch_test_data = "NeXus ><}&{'\\&\" Data"; |
|---|
| 86 | char path[512]; |
|---|
| 87 | |
|---|
| 88 | grossezahl[0] = 12; |
|---|
| 89 | grossezahl[2] = 23; |
|---|
| 90 | #if HAVE_LONG_LONG_INT |
|---|
| 91 | grossezahl[1] = (int64_t)555555555555LL; |
|---|
| 92 | grossezahl[3] = (int64_t)777777777777LL; |
|---|
| 93 | #else |
|---|
| 94 | grossezahl[1] = (int64_t)555555555555; |
|---|
| 95 | grossezahl[3] = (int64_t)777777777777; |
|---|
| 96 | #endif /* HAVE_LONG_LONG_INT */ |
|---|
| 97 | |
|---|
| 98 | if(strstr(argv[0],"napi_test-hdf5") != NULL){ |
|---|
| 99 | nx_creation_code = NXACC_CREATE5; |
|---|
| 100 | strcpy(nxFile,"NXtest.h5"); |
|---|
| 101 | }else if(strstr(argv[0],"napi_test-xml-table") != NULL){ |
|---|
| 102 | nx_creation_code = NXACC_CREATEXML | NXACC_TABLE; |
|---|
| 103 | strcpy(nxFile,"NXtest-table.xml"); |
|---|
| 104 | }else if(strstr(argv[0],"napi_test-xml") != NULL){ |
|---|
| 105 | nx_creation_code = NXACC_CREATEXML; |
|---|
| 106 | strcpy(nxFile,"NXtest.xml"); |
|---|
| 107 | } else { |
|---|
| 108 | nx_creation_code = NXACC_CREATE; |
|---|
| 109 | strcpy(nxFile,"NXtest.hdf"); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | /* create file */ |
|---|
| 113 | if (NXopen (nxFile, nx_creation_code, &fileid) != NX_OK) return 1; |
|---|
| 114 | if (nx_creation_code == NXACC_CREATE5) |
|---|
| 115 | { |
|---|
| 116 | if (NXreopen (fileid, &clone_fileid) != NX_OK) return 1; |
|---|
| 117 | } |
|---|
| 118 | NXsetnumberformat(fileid,NX_FLOAT32,"%9.3f"); |
|---|
| 119 | if (NXmakegroup (fileid, "entry", "NXentry") != NX_OK) return 1; |
|---|
| 120 | if (NXopengroup (fileid, "entry", "NXentry") != NX_OK) return 1; |
|---|
| 121 | if(NXputattr(fileid,"hugo","namenlos",strlen("namenlos"), NX_CHAR) != NX_OK) return 1; |
|---|
| 122 | if(NXputattr(fileid,"cucumber","passion",strlen("passion"), NX_CHAR) != NX_OK) return 1; |
|---|
| 123 | NXlen = strlen(ch_test_data); |
|---|
| 124 | if (NXmakedata (fileid, "ch_data", NX_CHAR, 1, &NXlen) != NX_OK) return 1; |
|---|
| 125 | if (NXopendata (fileid, "ch_data") != NX_OK) return 1; |
|---|
| 126 | if (NXputdata (fileid, ch_test_data) != NX_OK) return 1; |
|---|
| 127 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 128 | if (NXmakedata (fileid, "c1_data", NX_CHAR, 2, array_dims) != NX_OK) return 1; |
|---|
| 129 | if (NXopendata (fileid, "c1_data") != NX_OK) return 1; |
|---|
| 130 | if (NXputdata (fileid, c1_array) != NX_OK) return 1; |
|---|
| 131 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 132 | if (NXmakedata (fileid, "i1_data", NX_INT8, 1, &array_dims[1]) != NX_OK) return 1; |
|---|
| 133 | if (NXopendata (fileid, "i1_data") != NX_OK) return 1; |
|---|
| 134 | if (NXputdata (fileid, i1_array) != NX_OK) return 1; |
|---|
| 135 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 136 | if (NXmakedata (fileid, "i2_data", NX_INT16, 1, &array_dims[1]) != NX_OK) return 1; |
|---|
| 137 | if (NXopendata (fileid, "i2_data") != NX_OK) return 1; |
|---|
| 138 | if (NXputdata (fileid, i2_array) != NX_OK) return 1; |
|---|
| 139 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 140 | if (NXmakedata (fileid, "i4_data", NX_INT32, 1, &array_dims[1]) != NX_OK) return 1; |
|---|
| 141 | if (NXopendata (fileid, "i4_data") != NX_OK) return 1; |
|---|
| 142 | if (NXputdata (fileid, i4_array) != NX_OK) return 1; |
|---|
| 143 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 144 | if (NXcompmakedata (fileid, "r4_data", NX_FLOAT32, 2, array_dims,NX_COMP_LZW,chunk_size) != NX_OK) return 1; |
|---|
| 145 | if (NXopendata (fileid, "r4_data") != NX_OK) return 1; |
|---|
| 146 | if (NXputdata (fileid, r4_array) != NX_OK) return 1; |
|---|
| 147 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 148 | if (NXmakedata (fileid, "r8_data", NX_FLOAT64, 2, array_dims) != NX_OK) return 1; |
|---|
| 149 | if (NXopendata (fileid, "r8_data") != NX_OK) return 1; |
|---|
| 150 | slab_start[0] = 4; slab_start[1] = 0; slab_size[0] = 1; slab_size[1] = 4; |
|---|
| 151 | if (NXputslab (fileid, (double*)r8_array + 16, slab_start, slab_size) != NX_OK) return 1; |
|---|
| 152 | slab_start[0] = 0; slab_start[1] = 0; slab_size[0] = 4; slab_size[1] = 4; |
|---|
| 153 | if (NXputslab (fileid, r8_array, slab_start, slab_size) != NX_OK) return 1; |
|---|
| 154 | if (NXputattr (fileid, "ch_attribute", ch_test_data, strlen (ch_test_data), NX_CHAR) != NX_OK) return 1; |
|---|
| 155 | i = 42; |
|---|
| 156 | if (NXputattr (fileid, "i4_attribute", &i, 1, NX_INT32) != NX_OK) return 1; |
|---|
| 157 | r = 3.14159265; |
|---|
| 158 | if (NXputattr (fileid, "r4_attribute", &r, 1, NX_FLOAT32) != NX_OK) return 1; |
|---|
| 159 | if (NXgetdataID (fileid, &dlink) != NX_OK) return 1; |
|---|
| 160 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 161 | dims[0] = 4; |
|---|
| 162 | if (nx_creation_code != NXACC_CREATE) |
|---|
| 163 | { |
|---|
| 164 | if (NXmakedata (fileid, "grosse_zahl", NX_INT64, 1,dims) == NX_OK) { |
|---|
| 165 | if (NXopendata (fileid, "grosse_zahl") != NX_OK) return 1; |
|---|
| 166 | if (NXputdata (fileid, grossezahl) != NX_OK) return 1; |
|---|
| 167 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | if (NXmakegroup (fileid, "data", "NXdata") != NX_OK) return 1; |
|---|
| 171 | if (NXopengroup (fileid, "data", "NXdata") != NX_OK) return 1; |
|---|
| 172 | if (NXmakelink (fileid, &dlink) != NX_OK) return 1; |
|---|
| 173 | dims[0] = 100; |
|---|
| 174 | dims[1] = 20; |
|---|
| 175 | for(i = 0; i < 100; i++) |
|---|
| 176 | { |
|---|
| 177 | for(j = 0; j < 20; j++) |
|---|
| 178 | { |
|---|
| 179 | comp_array[i][j] = i; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | cdims[0] = 20; |
|---|
| 183 | cdims[1] = 20; |
|---|
| 184 | if (NXcompmakedata (fileid, "comp_data", NX_INT32, 2, dims, NX_COMP_LZW, cdims) != NX_OK) return 1; |
|---|
| 185 | if (NXopendata (fileid, "comp_data") != NX_OK) return 1; |
|---|
| 186 | if (NXputdata (fileid, comp_array) != NX_OK) return 1; |
|---|
| 187 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 188 | if (NXflush (&fileid) != NX_OK) return 1; |
|---|
| 189 | if (NXmakedata (fileid, "flush_data", NX_INT32, 1, unlimited_dims) != NX_OK) return 1; |
|---|
| 190 | slab_size[0] = 1; |
|---|
| 191 | for (i = 0; i < 7; i++) |
|---|
| 192 | { |
|---|
| 193 | slab_start[0] = i; |
|---|
| 194 | if (NXopendata (fileid, "flush_data") != NX_OK) return 1; |
|---|
| 195 | if (NXputslab (fileid, &i, slab_start, slab_size) != NX_OK) return 1; |
|---|
| 196 | if (NXflush (&fileid) != NX_OK) return 1; |
|---|
| 197 | } |
|---|
| 198 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 199 | if (NXmakegroup (fileid, "sample", "NXsample") != NX_OK) return 1; |
|---|
| 200 | if (NXopengroup (fileid, "sample", "NXsample") != NX_OK) return 1; |
|---|
| 201 | NXlen = 12; |
|---|
| 202 | if (NXmakedata (fileid, "ch_data", NX_CHAR, 1, &NXlen) != NX_OK) return 1; |
|---|
| 203 | if (NXopendata (fileid, "ch_data") != NX_OK) return 1; |
|---|
| 204 | if (NXputdata (fileid, "NeXus sample") != NX_OK) return 1; |
|---|
| 205 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 206 | if (NXgetgroupID (fileid, &glink) != NX_OK) return 1; |
|---|
| 207 | if (( nx_creation_code & NXACC_CREATEXML) == 0 ) { |
|---|
| 208 | if (NXmakedata (fileid, "cdata_unlimited", NX_CHAR, 2, unlimited_cdims) != NX_OK) return 1; |
|---|
| 209 | if (NXopendata (fileid, "cdata_unlimited") != NX_OK) return 1; |
|---|
| 210 | slab_size[0] = 1; |
|---|
| 211 | slab_size[1] = 4; |
|---|
| 212 | slab_start[1] = 0; |
|---|
| 213 | for (i = 0; i < 5; i++) |
|---|
| 214 | { |
|---|
| 215 | slab_start[0] = i; |
|---|
| 216 | if (NXputslab (fileid, &(c1_array[i][0]), slab_start, slab_size) != NX_OK) return 1; |
|---|
| 217 | } |
|---|
| 218 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 219 | } |
|---|
| 220 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 221 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 222 | if (NXmakegroup (fileid, "link", "NXentry") != NX_OK) return 1; |
|---|
| 223 | if (NXopengroup (fileid, "link", "NXentry") != NX_OK) return 1; |
|---|
| 224 | if (NXmakelink (fileid, &glink) != NX_OK) return 1; |
|---|
| 225 | if (NXmakenamedlink (fileid,"renLinkGroup", &glink) != NX_OK) return 1; |
|---|
| 226 | if (NXmakenamedlink (fileid, "renLinkData", &dlink) != NX_OK) return 1; |
|---|
| 227 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 228 | if (NXclose (&fileid) != NX_OK) return 1; |
|---|
| 229 | |
|---|
| 230 | if ( (argc >= 2) && !strcmp(argv[1], "-q") ) |
|---|
| 231 | { |
|---|
| 232 | return 0; /* create only */ |
|---|
| 233 | } |
|---|
| 234 | /* |
|---|
| 235 | read test |
|---|
| 236 | */ |
|---|
| 237 | if (NXopen (nxFile, NXACC_RDWR,&fileid) != NX_OK) return 1; |
|---|
| 238 | if(NXinquirefile(fileid,filename,256) != NX_OK){ |
|---|
| 239 | return 1; |
|---|
| 240 | } |
|---|
| 241 | printf("NXinquirefile found: %s\n", relativePathOf(filename)); |
|---|
| 242 | NXgetattrinfo (fileid, &i); |
|---|
| 243 | if (i > 0) { |
|---|
| 244 | printf ("Number of global attributes: %d\n", i); |
|---|
| 245 | } |
|---|
| 246 | do { |
|---|
| 247 | attr_status = NXgetnextattr (fileid, name, NXdims, &NXtype); |
|---|
| 248 | if (attr_status == NX_ERROR) return 1; |
|---|
| 249 | if (attr_status == NX_OK) { |
|---|
| 250 | switch (NXtype) { |
|---|
| 251 | case NX_CHAR: |
|---|
| 252 | NXlen = sizeof (char_buffer); |
|---|
| 253 | if (NXgetattr (fileid, name, char_buffer, &NXlen, &NXtype) |
|---|
| 254 | != NX_OK) return 1; |
|---|
| 255 | if ( strcmp(name, "file_time") && |
|---|
| 256 | strcmp(name, "HDF_version") && |
|---|
| 257 | strcmp(name, "HDF5_Version") && |
|---|
| 258 | strcmp(name, "XML_version") ) |
|---|
| 259 | { |
|---|
| 260 | printf (" %s = %s\n", name, char_buffer); |
|---|
| 261 | } |
|---|
| 262 | break; |
|---|
| 263 | } |
|---|
| 264 | } |
|---|
| 265 | } while (attr_status == NX_OK); |
|---|
| 266 | if (NXopengroup (fileid, "entry", "NXentry") != NX_OK) return 1; |
|---|
| 267 | NXgetattrinfo(fileid,&i); |
|---|
| 268 | printf("Number of group attributes: %d\n", i); |
|---|
| 269 | if(NXgetpath(fileid,path,512) != NX_OK)return 1; |
|---|
| 270 | printf("NXentry path %s\n", path); |
|---|
| 271 | do { |
|---|
| 272 | attr_status = NXgetnextattr (fileid, name, NXdims, &NXtype); |
|---|
| 273 | if (attr_status == NX_ERROR) return 1; |
|---|
| 274 | if (attr_status == NX_OK) { |
|---|
| 275 | switch (NXtype) { |
|---|
| 276 | case NX_CHAR: |
|---|
| 277 | NXlen = sizeof (char_buffer); |
|---|
| 278 | if (NXgetattr (fileid, name, char_buffer, &NXlen, &NXtype) |
|---|
| 279 | != NX_OK) return 1; |
|---|
| 280 | printf (" %s = %s\n", name, char_buffer); |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | } while (attr_status == NX_OK); |
|---|
| 284 | if (NXgetgroupinfo (fileid, &i, group_name, class_name) != NX_OK) return 1; |
|---|
| 285 | printf ("Group: %s(%s) contains %d items\n", group_name, class_name, i); |
|---|
| 286 | do { |
|---|
| 287 | entry_status = NXgetnextentry (fileid, name, char_class, &NXtype); |
|---|
| 288 | if (entry_status == NX_ERROR) return 1; |
|---|
| 289 | if (strcmp(char_class,"SDS") != 0) { |
|---|
| 290 | if (entry_status != NX_EOD) { |
|---|
| 291 | printf (" Subgroup: %s(%s)\n", name, char_class); |
|---|
| 292 | entry_status = NX_OK; |
|---|
| 293 | } |
|---|
| 294 | } else { |
|---|
| 295 | if (entry_status == NX_OK) { |
|---|
| 296 | if (NXopendata (fileid, name) != NX_OK) return 1; |
|---|
| 297 | if(NXgetpath(fileid,path,512) != NX_OK)return 1; |
|---|
| 298 | printf("Data path %s\n", path); |
|---|
| 299 | if (NXgetinfo (fileid, &NXrank, NXdims, &NXtype) != NX_OK) return 1; |
|---|
| 300 | printf (" %s(%d)", name, NXtype); |
|---|
| 301 | if (NXmalloc ((void **) &data_buffer, NXrank, NXdims, NXtype) != NX_OK) return 1; |
|---|
| 302 | n = 1; |
|---|
| 303 | for(k=0; k<NXrank; k++) |
|---|
| 304 | { |
|---|
| 305 | n *= NXdims[k]; |
|---|
| 306 | } |
|---|
| 307 | if (NXtype == NX_CHAR) { |
|---|
| 308 | if (NXgetdata (fileid, data_buffer) != NX_OK) return 1; |
|---|
| 309 | print_data (" = ", data_buffer, NXtype, n); |
|---|
| 310 | } else if (NXtype != NX_FLOAT32 && NXtype != NX_FLOAT64) { |
|---|
| 311 | if (NXgetdata (fileid, data_buffer) != NX_OK) return 1; |
|---|
| 312 | print_data (" = ", data_buffer, NXtype, n); |
|---|
| 313 | } else { |
|---|
| 314 | slab_start[0] = 0; |
|---|
| 315 | slab_start[1] = 0; |
|---|
| 316 | slab_size[0] = 1; |
|---|
| 317 | slab_size[1] = 4; |
|---|
| 318 | if (NXgetslab (fileid, data_buffer, slab_start, slab_size) != NX_OK) return 1; |
|---|
| 319 | print_data ("\n ", data_buffer, NXtype, 4); |
|---|
| 320 | slab_start[0] = 1; |
|---|
| 321 | if (NXgetslab (fileid, data_buffer, slab_start, slab_size) != NX_OK) return 1; |
|---|
| 322 | print_data (" ", data_buffer, NXtype, 4); |
|---|
| 323 | slab_start[0] = 2; |
|---|
| 324 | if (NXgetslab (fileid, data_buffer, slab_start, slab_size) != NX_OK) return 1; |
|---|
| 325 | print_data (" ", data_buffer, NXtype, 4); |
|---|
| 326 | slab_start[0] = 3; |
|---|
| 327 | if (NXgetslab (fileid, data_buffer, slab_start, slab_size) != NX_OK) return 1; |
|---|
| 328 | print_data (" ", data_buffer, NXtype, 4); |
|---|
| 329 | slab_start[0] = 4; |
|---|
| 330 | if (NXgetslab (fileid, data_buffer, slab_start, slab_size) != NX_OK) return 1; |
|---|
| 331 | print_data (" ", data_buffer, NXtype, 4); |
|---|
| 332 | if (NXgetattrinfo (fileid, &i) != NX_OK) return 1; |
|---|
| 333 | if (i > 0) { |
|---|
| 334 | printf (" Number of attributes : %d\n", i); |
|---|
| 335 | } |
|---|
| 336 | do { |
|---|
| 337 | attr_status = NXgetnextattr (fileid, name, NXdims, &NXtype); |
|---|
| 338 | if (attr_status == NX_ERROR) return 1; |
|---|
| 339 | if (attr_status == NX_OK) { |
|---|
| 340 | switch (NXtype) { |
|---|
| 341 | case NX_INT32: |
|---|
| 342 | NXlen = 1; |
|---|
| 343 | if (NXgetattr (fileid, name, &i, &NXlen, &NXtype) != NX_OK) return 1; |
|---|
| 344 | printf (" %s : %d\n", name, i); |
|---|
| 345 | break; |
|---|
| 346 | case NX_FLOAT32: |
|---|
| 347 | NXlen = 1; |
|---|
| 348 | if (NXgetattr (fileid, name, &r, &NXlen, &NXtype) != NX_OK) return 1; |
|---|
| 349 | printf (" %s : %f\n", name, r); |
|---|
| 350 | break; |
|---|
| 351 | case NX_CHAR: |
|---|
| 352 | NXlen = sizeof (char_buffer); |
|---|
| 353 | if (NXgetattr (fileid, name, char_buffer, &NXlen, &NXtype) != NX_OK) return 1; |
|---|
| 354 | printf (" %s : %s\n", name, char_buffer); |
|---|
| 355 | break; |
|---|
| 356 | } |
|---|
| 357 | } |
|---|
| 358 | } while (attr_status == NX_OK); |
|---|
| 359 | } |
|---|
| 360 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 361 | if (NXfree ((void **) &data_buffer) != NX_OK) return 1; |
|---|
| 362 | } |
|---|
| 363 | } |
|---|
| 364 | } while (entry_status == NX_OK); |
|---|
| 365 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 366 | /* |
|---|
| 367 | * check links |
|---|
| 368 | */ |
|---|
| 369 | if (NXopengroup (fileid, "entry", "NXentry") != NX_OK) return 1; |
|---|
| 370 | if (NXopengroup (fileid, "sample", "NXsample") != NX_OK) return 1; |
|---|
| 371 | if (NXgetgroupID (fileid, &glink) != NX_OK) return 1; |
|---|
| 372 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 373 | if (NXopengroup (fileid, "data", "NXdata") != NX_OK) return 1; |
|---|
| 374 | if (NXopendata (fileid, "r8_data") != NX_OK) return 1; |
|---|
| 375 | if (NXgetdataID (fileid, &dlink) != NX_OK) return 1; |
|---|
| 376 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 377 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 378 | if (NXopendata (fileid, "r8_data") != NX_OK) return 1; |
|---|
| 379 | if (NXgetdataID (fileid, &blink) != NX_OK) return 1; |
|---|
| 380 | if (NXclosedata (fileid) != NX_OK) return 1; |
|---|
| 381 | if (NXsameID(fileid, &dlink, &blink) != NX_OK) |
|---|
| 382 | { |
|---|
| 383 | printf ("Link check FAILED (r8_data)\n"); |
|---|
| 384 | printf ("original data\n"); |
|---|
| 385 | NXIprintlink(fileid, &dlink); |
|---|
| 386 | printf ("linked data\n"); |
|---|
| 387 | NXIprintlink(fileid, &blink); |
|---|
| 388 | return 1; |
|---|
| 389 | } |
|---|
| 390 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 391 | |
|---|
| 392 | if (NXopengroup (fileid, "link", "NXentry") != NX_OK) return 1; |
|---|
| 393 | if (NXopengroup (fileid, "sample", "NXsample") != NX_OK) return 1; |
|---|
| 394 | if(NXgetpath(fileid,path,512) != NX_OK)return 1; |
|---|
| 395 | printf("Group path %s\n", path); |
|---|
| 396 | if (NXgetgroupID (fileid, &blink) != NX_OK) return 1; |
|---|
| 397 | if (NXsameID(fileid, &glink, &blink) != NX_OK) |
|---|
| 398 | { |
|---|
| 399 | printf ("Link check FAILED (sample)\n"); |
|---|
| 400 | printf ("original group\n"); |
|---|
| 401 | NXIprintlink(fileid, &glink); |
|---|
| 402 | printf ("linked group\n"); |
|---|
| 403 | NXIprintlink(fileid, &blink); |
|---|
| 404 | return 1; |
|---|
| 405 | } |
|---|
| 406 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 407 | |
|---|
| 408 | if (NXopengroup (fileid, "renLinkGroup", "NXsample") != NX_OK) return 1; |
|---|
| 409 | if (NXgetgroupID (fileid, &blink) != NX_OK) return 1; |
|---|
| 410 | if (NXsameID(fileid, &glink, &blink) != NX_OK) |
|---|
| 411 | { |
|---|
| 412 | printf ("Link check FAILED (renLinkGroup)\n"); |
|---|
| 413 | printf ("original group\n"); |
|---|
| 414 | NXIprintlink(fileid, &glink); |
|---|
| 415 | printf ("linked group\n"); |
|---|
| 416 | NXIprintlink(fileid, &blink); |
|---|
| 417 | return 1; |
|---|
| 418 | } |
|---|
| 419 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 420 | |
|---|
| 421 | if(NXopendata(fileid,"renLinkData") != NX_OK) return 1; |
|---|
| 422 | if(NXgetdataID(fileid,&blink) != NX_OK) return 1; |
|---|
| 423 | if (NXsameID(fileid, &dlink, &blink) != NX_OK) |
|---|
| 424 | { |
|---|
| 425 | printf ("Link check FAILED (renLinkData)\n"); |
|---|
| 426 | printf ("original group\n"); |
|---|
| 427 | NXIprintlink(fileid, &glink); |
|---|
| 428 | printf ("linked group\n"); |
|---|
| 429 | NXIprintlink(fileid, &blink); |
|---|
| 430 | return 1; |
|---|
| 431 | } |
|---|
| 432 | if(NXclosedata(fileid) != NX_OK) return 1; |
|---|
| 433 | if (NXclosegroup (fileid) != NX_OK) return 1; |
|---|
| 434 | printf ("Link check OK\n"); |
|---|
| 435 | |
|---|
| 436 | /* |
|---|
| 437 | tests for NXopenpath |
|---|
| 438 | */ |
|---|
| 439 | if(NXopenpath(fileid,"/entry/data/comp_data") != NX_OK){ |
|---|
| 440 | printf("Failure on NXopenpath\n"); |
|---|
| 441 | return 0; |
|---|
| 442 | } |
|---|
| 443 | if(NXopenpath(fileid,"/entry/data/comp_data") != NX_OK){ |
|---|
| 444 | printf("Failure on NXopenpath\n"); |
|---|
| 445 | return 0; |
|---|
| 446 | } |
|---|
| 447 | if(NXopenpath(fileid,"../r8_data") != NX_OK){ |
|---|
| 448 | printf("Failure on NXopenpath\n"); |
|---|
| 449 | return 0; |
|---|
| 450 | } |
|---|
| 451 | if(NXopengrouppath(fileid,"/entry/data/comp_data") != NX_OK){ |
|---|
| 452 | printf("Failure on NXopengrouppath\n"); |
|---|
| 453 | return 0; |
|---|
| 454 | } |
|---|
| 455 | if(NXopenpath(fileid,"/entry/data/r8_data") != NX_OK){ |
|---|
| 456 | printf("Failure on NXopenpath\n"); |
|---|
| 457 | return 0; |
|---|
| 458 | } |
|---|
| 459 | printf("NXopenpath checks OK\n"); |
|---|
| 460 | |
|---|
| 461 | if (NXclose (&fileid) != NX_OK) return 1; |
|---|
| 462 | |
|---|
| 463 | if(testLoadPath() != 0) return 1; |
|---|
| 464 | |
|---|
| 465 | if(testExternal(argv[0]) != 0) { |
|---|
| 466 | return 1; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | return 0; |
|---|
| 470 | } |
|---|
| 471 | /*---------------------------------------------------------------------*/ |
|---|
| 472 | static int testLoadPath() { |
|---|
| 473 | NXhandle h; |
|---|
| 474 | |
|---|
| 475 | if(getenv("NX_LOAD_PATH") != NULL){ |
|---|
| 476 | if (NXopen ("dmc01.hdf", NXACC_RDWR,&h) != NX_OK) { |
|---|
| 477 | printf("Loading NeXus file dmc01.hdf from path %s FAILED\n", getenv("NX_LOAD_PATH")); |
|---|
| 478 | return 1; |
|---|
| 479 | } else { |
|---|
| 480 | printf("Success loading NeXus file from path\n"); |
|---|
| 481 | NXclose(&h); |
|---|
| 482 | return 0; |
|---|
| 483 | } |
|---|
| 484 | } |
|---|
| 485 | return 0; |
|---|
| 486 | } |
|---|
| 487 | /*---------------------------------------------------------------------*/ |
|---|
| 488 | static int testExternal(char *progName){ |
|---|
| 489 | char nxfile[255], ext[5], testFile[80], time[132], filename[256]; |
|---|
| 490 | int create; |
|---|
| 491 | NXhandle hfil; |
|---|
| 492 | int dummylen = 1; |
|---|
| 493 | float dummyfloat = 1; |
|---|
| 494 | float temperature; |
|---|
| 495 | |
|---|
| 496 | if(strstr(progName,"hdf4") != NULL){ |
|---|
| 497 | strcpy(ext,"hdf"); |
|---|
| 498 | create = NXACC_CREATE; |
|---|
| 499 | } else if(strstr(progName,"hdf5") != NULL){ |
|---|
| 500 | strcpy(ext,"h5"); |
|---|
| 501 | create = NXACC_CREATE5; |
|---|
| 502 | } else if(strstr(progName,"xml") != NULL){ |
|---|
| 503 | strcpy(ext,"xml"); |
|---|
| 504 | create = NXACC_CREATEXML; |
|---|
| 505 | } else { |
|---|
| 506 | printf("Failed to recognise napi_test program in testExternal\n"); |
|---|
| 507 | return 1; |
|---|
| 508 | } |
|---|
| 509 | |
|---|
| 510 | sprintf(testFile,"nxext.%s", ext); |
|---|
| 511 | |
|---|
| 512 | /* |
|---|
| 513 | create the test file |
|---|
| 514 | */ |
|---|
| 515 | if(NXopen(testFile,create,&hfil) != NX_OK){ |
|---|
| 516 | return 1; |
|---|
| 517 | } |
|---|
| 518 | /*if(NXmakegroup(hfil,"entry1","NXentry") != NX_OK){ |
|---|
| 519 | return 1; |
|---|
| 520 | }*/ |
|---|
| 521 | sprintf(nxfile,"nxfile://data/dmc01.%s#/entry1",ext); |
|---|
| 522 | if(NXlinkexternal(hfil,"entry1","NXentry",nxfile) != NX_OK){ |
|---|
| 523 | return 1; |
|---|
| 524 | } |
|---|
| 525 | /*if(NXmakegroup(hfil,"entry2","NXentry") != NX_OK){ |
|---|
| 526 | return 1; |
|---|
| 527 | }*/ |
|---|
| 528 | sprintf(nxfile,"nxfile://data/dmc02.%s#/entry1",ext); |
|---|
| 529 | if(NXlinkexternal(hfil,"entry2","NXentry",nxfile) != NX_OK){ |
|---|
| 530 | return 1; |
|---|
| 531 | } |
|---|
| 532 | if(NXmakegroup(hfil,"entry3","NXentry") != NX_OK){ |
|---|
| 533 | return 1; |
|---|
| 534 | } |
|---|
| 535 | if(NXopengroup(hfil,"entry3","NXentry") != NX_OK){ |
|---|
| 536 | return 1; |
|---|
| 537 | } |
|---|
| 538 | if (NXmakedata (hfil, "extlinkdata", NX_FLOAT32, 1, &dummylen) != NX_OK) return 1; |
|---|
| 539 | if (NXopendata (hfil, "extlinkdata") != NX_OK) return 1; |
|---|
| 540 | if (NXputdata (hfil, &dummyfloat) != NX_OK) return 1; |
|---|
| 541 | sprintf(nxfile,"nxfile://data/dmc01.%s#/entry1/sample/temperature_mean",ext); |
|---|
| 542 | if(NXputattr(hfil,"napimount",nxfile,strlen(nxfile), NX_CHAR) != NX_OK) return 1; |
|---|
| 543 | if(NXclose(&hfil) != NX_OK){ |
|---|
| 544 | return 1; |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | /* |
|---|
| 548 | actually test linking |
|---|
| 549 | */ |
|---|
| 550 | if(NXopen(testFile,NXACC_RDWR,&hfil) != NX_OK){ |
|---|
| 551 | return 1; |
|---|
| 552 | } |
|---|
| 553 | if(NXopenpath(hfil,"/entry1/start_time") != NX_OK){ |
|---|
| 554 | return 1; |
|---|
| 555 | } |
|---|
| 556 | memset(time,0,132); |
|---|
| 557 | if(NXgetdata(hfil,time) != NX_OK){ |
|---|
| 558 | return 1; |
|---|
| 559 | } |
|---|
| 560 | printf("First file time: %s\n", time); |
|---|
| 561 | |
|---|
| 562 | if(NXinquirefile(hfil,filename,256) != NX_OK){ |
|---|
| 563 | return 1; |
|---|
| 564 | } |
|---|
| 565 | printf("NXinquirefile found: %s\n", relativePathOf(filename)); |
|---|
| 566 | |
|---|
| 567 | if(NXopenpath(hfil,"/entry2/sample/sample_name") != NX_OK){ |
|---|
| 568 | return 1; |
|---|
| 569 | } |
|---|
| 570 | memset(time,0,132); |
|---|
| 571 | if(NXgetdata(hfil,time) != NX_OK){ |
|---|
| 572 | return 1; |
|---|
| 573 | } |
|---|
| 574 | printf("Second file sample: %s\n", time); |
|---|
| 575 | if(NXinquirefile(hfil,filename,256) != NX_OK){ |
|---|
| 576 | return 1; |
|---|
| 577 | } |
|---|
| 578 | printf("NXinquirefile found: %s\n", relativePathOf(filename)); |
|---|
| 579 | |
|---|
| 580 | if(NXopenpath(hfil,"/entry2/start_time") != NX_OK){ |
|---|
| 581 | return 1; |
|---|
| 582 | } |
|---|
| 583 | memset(time,0,132); |
|---|
| 584 | if(NXgetdata(hfil,time) != NX_OK){ |
|---|
| 585 | return 1; |
|---|
| 586 | } |
|---|
| 587 | printf("Second file time: %s\n", time); |
|---|
| 588 | NXopenpath(hfil,"/"); |
|---|
| 589 | if(NXisexternalgroup(hfil,"entry1","NXentry",filename,255) != NX_OK){ |
|---|
| 590 | return 1; |
|---|
| 591 | } else { |
|---|
| 592 | printf("entry1 external URL = %s\n", filename); |
|---|
| 593 | } |
|---|
| 594 | |
|---|
| 595 | printf("testing link to external data set\n"); |
|---|
| 596 | if(NXopenpath(hfil,"/entry3") != NX_OK){ |
|---|
| 597 | return 1; |
|---|
| 598 | } |
|---|
| 599 | if (NXopendata (hfil, "extlinkdata") != NX_OK) return 1; |
|---|
| 600 | memset(&temperature,0,4); |
|---|
| 601 | if(NXgetdata(hfil,&temperature) != NX_OK){ |
|---|
| 602 | return 1; |
|---|
| 603 | } |
|---|
| 604 | printf("value retrieved: %4.2f\n", temperature); |
|---|
| 605 | |
|---|
| 606 | NXclose(&hfil); |
|---|
| 607 | printf("External File Linking tested OK\n"); |
|---|
| 608 | return 0; |
|---|
| 609 | } |
|---|
| 610 | /*----------------------------------------------------------------------*/ |
|---|
| 611 | static void |
|---|
| 612 | print_data (const char *prefix, void *data, int type, int num) |
|---|
| 613 | { |
|---|
| 614 | int i; |
|---|
| 615 | printf ("%s", prefix); |
|---|
| 616 | for (i = 0; i < num; i++) { |
|---|
| 617 | switch (type) { |
|---|
| 618 | case NX_CHAR: |
|---|
| 619 | printf ("%c", ((char *) data)[i]); |
|---|
| 620 | break; |
|---|
| 621 | |
|---|
| 622 | case NX_INT8: |
|---|
| 623 | printf (" %d", ((unsigned char *) data)[i]); |
|---|
| 624 | break; |
|---|
| 625 | |
|---|
| 626 | case NX_INT16: |
|---|
| 627 | printf (" %d", ((short *) data)[i]); |
|---|
| 628 | break; |
|---|
| 629 | |
|---|
| 630 | case NX_INT32: |
|---|
| 631 | printf (" %d", ((int *) data)[i]); |
|---|
| 632 | break; |
|---|
| 633 | |
|---|
| 634 | case NX_INT64: |
|---|
| 635 | printf (" %lld", (long long)((int64_t *) data)[i]); |
|---|
| 636 | break; |
|---|
| 637 | |
|---|
| 638 | case NX_UINT64: |
|---|
| 639 | printf (" %llu", (unsigned long long)((uint64_t *) data)[i]); |
|---|
| 640 | break; |
|---|
| 641 | |
|---|
| 642 | case NX_FLOAT32: |
|---|
| 643 | printf (" %f", ((float *) data)[i]); |
|---|
| 644 | break; |
|---|
| 645 | |
|---|
| 646 | case NX_FLOAT64: |
|---|
| 647 | printf (" %f", ((double *) data)[i]); |
|---|
| 648 | break; |
|---|
| 649 | |
|---|
| 650 | default: |
|---|
| 651 | printf ("print_data: invalid type"); |
|---|
| 652 | break; |
|---|
| 653 | } |
|---|
| 654 | } |
|---|
| 655 | printf ("\n"); |
|---|
| 656 | } |
|---|