static int
test_plists(const char *filename1, const char *filename2) 
{
    int fd_le, fd_be;
    size_t size_le = 0, size_be = 0;
    void *buf_le = NULL, *buf_be = NULL;
    hid_t plist_le, plist_be;	       	/* dataset create prop. list */
    const char *testfile;

    testfile = H5_get_srcdir_filename(filename1);
    if((fd_le = HDopen(testfile, O_RDONLY, 0666)) < 0)
        TEST_ERROR
    size_le = HDlseek(fd_le, (HDoff_t)0, SEEK_END);
    HDlseek(fd_le, (HDoff_t)0, SEEK_SET);
    buf_le = (void *)HDmalloc(size_le);
    if(HDread(fd_le, buf_le, size_le) < 0)
        TEST_ERROR
    HDclose(fd_le);

    testfile = H5_get_srcdir_filename(filename2);
    if((fd_be = HDopen(testfile, O_RDONLY, 0666)) < 0)
        TEST_ERROR
    size_be = HDlseek(fd_be, (HDoff_t)0, SEEK_END);
    HDlseek(fd_be, (HDoff_t)0, SEEK_SET);
    buf_be = (void *)HDmalloc(size_be);
    if(HDread(fd_be, buf_be, size_be) < 0)
        TEST_ERROR
    HDclose(fd_be);

    if((plist_le = H5Pdecode(buf_le)) < 0)
        FAIL_STACK_ERROR
    if((plist_be = H5Pdecode(buf_be)) < 0)
        FAIL_STACK_ERROR

    if(!H5Pequal(plist_le, plist_be))
        FAIL_PUTS_ERROR("PLIST encoding/decoding comparison failed\n")

    if((H5Pclose(plist_le)) < 0)
        FAIL_STACK_ERROR
    if((H5Pclose(plist_be)) < 0)
        FAIL_STACK_ERROR

    HDfree(buf_le);
    HDfree(buf_be);

    return 1;

error:
    printf("***** Plist Encode/Decode tests FAILED! *****\n");
    return -1;
}
Esempio n. 2
0
/*-------------------------------------------------------------------------
 * Function:	test_filter_error
 *
 * Purpose:	Make sure the error message prints out the filter name
 *              when the existent file is opened but the filter isn't 
 *              registered. The existent file was created with 
 *              gen_filters.c. 
 *
 * Return:	Success:	0
 *
 *		Failure:	-1
 *
 * Programmer:	Raymond Lu
 *		2 June 2011
 *
 *-------------------------------------------------------------------------
 */
static herr_t
test_filter_error(const char *fname)
{
    const char *pathname = H5_get_srcdir_filename(fname); /* Corrected test file name */
    hid_t       file, dataset;         /* handles */
    int         buf[20];

    HDfprintf(stderr, "\nTesting error message during data reading when filter isn't registered\n");

    /* Open the file */
    if((file = H5Fopen(pathname, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Open the regular dataset */
    if((dataset = H5Dopen2(file, DSET_FILTER_NAME, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    if(H5Dread(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) >= 0)
	TEST_ERROR;

    /* Close/release resources */
    if(H5Dclose(dataset) < 0)
        TEST_ERROR

    if(H5Fclose(file) < 0)
        TEST_ERROR

    return 0;

error:
    return -1;
}
Esempio n. 3
0
/*-------------------------------------------------------------------------
 * Function:    h5_make_local_copy
 *
 * Purpose:     Make copy of file.  Some tests write to data files under that
 *              are under version control.  Those tests should make a copy of
 *              the versioned file and write to the copy.  This function
 *              prepends srcdir to the name of the file to be copied and uses
 *              the name of the copy as is.
 *
 * Return:      Success:        0
 *
 *              Failure:        -1
 *
 * Programmer:  Larry Knox
 *              Monday, October 13, 2009
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
hid_t
h5_make_local_copy(const char *origfilename, const char *local_copy_name)
{
    int fd_old = (-1), fd_new = (-1);   /* File descriptors for copying data */
    ssize_t nread;                      /* Number of bytes read in */
    char  buf[READ_BUF_SIZE];           /* Buffer for copying data */
    const char *filename = H5_get_srcdir_filename(origfilename);;       /* Get the test file name to copy */

    /* Copy old file into temporary file */
    if((fd_old = HDopen(filename, O_RDONLY, 0666)) < 0) return -1;
    if((fd_new = HDopen(local_copy_name, O_RDWR|O_CREAT|O_TRUNC, 0666))
        < 0) return -1;

    /* Copy data */
    while((nread = HDread(fd_old, buf, (size_t)READ_BUF_SIZE)) > 0)
        HDwrite(fd_new, buf, (size_t)nread);

    /* Close files */
    if(HDclose(fd_old) < 0) return -1;
    if(HDclose(fd_new) < 0) return -1;

    return 0;
}
Esempio n. 4
0
/*-------------------------------------------------------------------------
 * Function:	main
 *
 * Purpose:     Exercise private object header behavior and routines
 *
 * Return:	Success:        0
 *		Failure:        1
 *
 * Programmer:	Robb Matzke
 *              Tuesday, November 24, 1998
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    hid_t	fapl = -1, file = -1;
    hid_t	dset = -1;
    H5F_t	*f = NULL;
    char	filename[1024];
    H5O_hdr_info_t hdr_info;            /* Object info */
    H5O_loc_t	oh_loc, oh_loc2;        /* Object header locations */
    time_t	time_new, ro;
    int         chunkno;                /* Chunk index for message */
    int		i;                      /* Local index variable */
    hbool_t     b;                      /* Index for "new format" loop */
    herr_t      ret;                    /* Generic return value */

    /* Reset library */
    h5_reset();
    fapl = h5_fileaccess();
    h5_fixname(FILENAME[0], fapl, filename, sizeof filename);

    /* Loop over old & new formats */
    for(b = FALSE; b <= TRUE; b++) {
        /* Display info about testing */
        if(b)
            HDputs("Using new file format:");
        else
            HDputs("Using default file format:");

        /* Set the format to use for the file */
        if(H5Pset_libver_bounds(fapl, (b ? H5F_LIBVER_LATEST : H5F_LIBVER_EARLIEST), H5F_LIBVER_LATEST) < 0)
            FAIL_STACK_ERROR

	/* test on object continuation block */
	if(test_cont(filename, fapl) < 0)
            FAIL_STACK_ERROR

        /* Create the file to operate on */
        if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
            TEST_ERROR
        if(NULL == (f = (H5F_t *)H5I_object(file)))
            FAIL_STACK_ERROR
        if (H5AC_ignore_tags(f) < 0) {
	    H5_FAILED();
	    H5Eprint2(H5E_DEFAULT, stdout);
	    goto error;
        }


        /*
         * Test object header creation
         * (using default group creation property list only because it's convenient)
         */
        TESTING("object header creation");
        HDmemset(&oh_loc, 0, sizeof(oh_loc));
        if(H5O_create(f, H5P_DATASET_XFER_DEFAULT, (size_t)64, (size_t)0, H5P_GROUP_CREATE_DEFAULT, &oh_loc/*out*/) < 0)
            FAIL_STACK_ERROR
        PASSED();


        /* create a new message */
        TESTING("message creation");
        time_new = 11111111;
        if(H5O_msg_create(&oh_loc, H5O_MTIME_NEW_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(1 != H5O_link(&oh_loc, 1, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR
        if(H5AC_flush(f, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(H5AC_expunge_entry(f, H5P_DATASET_XFER_DEFAULT, H5AC_OHDR, oh_loc.addr, H5AC__NO_FLAGS_SET) < 0)
            FAIL_STACK_ERROR
        if(NULL == H5O_msg_read(&oh_loc, H5O_MTIME_NEW_ID, &ro, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR
        if(ro != time_new)
            TEST_ERROR
        PASSED();


        /*
         * Test modification of an existing message.
         */
        TESTING("message modification");
        time_new = 33333333;
        if(H5O_msg_write(&oh_loc, H5O_MTIME_NEW_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(H5AC_flush(f, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(H5AC_expunge_entry(f, H5P_DATASET_XFER_DEFAULT, H5AC_OHDR, oh_loc.addr, H5AC__NO_FLAGS_SET) < 0)
            FAIL_STACK_ERROR
        if(NULL == H5O_msg_read(&oh_loc, H5O_MTIME_NEW_ID, &ro, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR
        if(ro != time_new)
            TEST_ERROR

        /* Make certain that chunk #0 in the object header can be encoded with a 1-byte size */
        if(H5O_get_hdr_info(&oh_loc, H5P_DATASET_XFER_DEFAULT, &hdr_info) < 0)
            FAIL_STACK_ERROR
        if(hdr_info.space.total >=256)
            TEST_ERROR

        PASSED();

        /*
         * Test creation of a bunch of messages one after another to see
         * what happens when the object header overflows in core.
         * (Use 'old' MTIME message here, because it is large enough to be
         *  replaced with a continuation message (the new one is too small)
         *  and the library doesn't understand how to migrate more than one
         *  message from an object header currently - QAK - 10/8/03)
         */
        TESTING("object header overflow in memory");
        for(i = 0; i < 40; i++) {
            time_new = (i + 1) * 1000 + 1000000;
            if(H5O_msg_create(&oh_loc, H5O_MTIME_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
                FAIL_STACK_ERROR
        } /* end for */
        if(H5AC_flush(f, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(H5AC_expunge_entry(f, H5P_DATASET_XFER_DEFAULT, H5AC_OHDR, oh_loc.addr, H5AC__NO_FLAGS_SET) < 0)
            FAIL_STACK_ERROR

        /* Make certain that chunk #0 in the object header will be encoded with a 2-byte size */
        if(H5O_get_hdr_info(&oh_loc, H5P_DATASET_XFER_DEFAULT, &hdr_info) < 0)
            FAIL_STACK_ERROR
        if(hdr_info.space.total < 256)
            TEST_ERROR

        PASSED();

        /* Close & re-open file & object header */
        /* (makes certain that an object header in the new format that transitions
         *  between 1-byte chunk #0 size encoding and 2-byte chunk #0 size encoding
         *  works correctly - QAK)
         */
        TESTING("close & re-open object header");
        if(H5O_close(&oh_loc) < 0)
            FAIL_STACK_ERROR
        if(H5Fclose(file) < 0)
            FAIL_STACK_ERROR
        if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0)
            FAIL_STACK_ERROR
        if(NULL == (f = (H5F_t *)H5I_object(file)))
            FAIL_STACK_ERROR
        if (H5AC_ignore_tags(f) < 0)
            FAIL_STACK_ERROR
        oh_loc.file = f;
        if(H5O_open(&oh_loc) < 0)
            FAIL_STACK_ERROR
        PASSED();

        /*
         * Test creation of a bunch of messages one after another to see
         * what happens when the object header overflows on disk.
         */
        TESTING("object header overflow on disk");
        for(i = 0; i < 10; i++) {
            time_new = (i + 1) * 1000 + 10;
            if(H5O_msg_create(&oh_loc, H5O_MTIME_NEW_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
                FAIL_STACK_ERROR
            if(H5AC_flush(f, H5P_DATASET_XFER_DEFAULT) < 0)
                FAIL_STACK_ERROR
            if(H5AC_expunge_entry(f, H5P_DATASET_XFER_DEFAULT, H5AC_OHDR, oh_loc.addr, H5AC__NO_FLAGS_SET) < 0)
                FAIL_STACK_ERROR
        } /* end for */
        PASSED();

        /*
         * Delete all time messages.
         */
        TESTING("message deletion");
        if(H5O_msg_remove(&oh_loc, H5O_MTIME_NEW_ID, H5O_ALL, TRUE, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(H5O_msg_remove(&oh_loc, H5O_MTIME_ID, H5O_ALL, TRUE, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(H5O_msg_read(&oh_loc, H5O_MTIME_NEW_ID, &ro, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR
        if(H5O_msg_read(&oh_loc, H5O_MTIME_ID, &ro, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR
        PASSED();


        /*
         * Constant message handling.
         * (can't write to them, but should be able to remove them)
         */
        TESTING("constant message handling");
        time_new = 22222222;
        if(H5O_msg_create(&oh_loc, H5O_MTIME_NEW_ID, H5O_MSG_FLAG_CONSTANT, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(H5AC_flush(f, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        if(H5AC_expunge_entry(f, H5P_DATASET_XFER_DEFAULT, H5AC_OHDR, oh_loc.addr, H5AC__NO_FLAGS_SET) < 0)
            FAIL_STACK_ERROR
        if(NULL == H5O_msg_read(&oh_loc, H5O_MTIME_NEW_ID, &ro, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR
        if(ro != time_new)
            TEST_ERROR
        time_new = 33333333;
        H5E_BEGIN_TRY {
            ret = H5O_msg_write(&oh_loc, H5O_MTIME_NEW_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT);
        } H5E_END_TRY;
        if(ret >= 0)
            TEST_ERROR
        if(H5O_msg_remove(&oh_loc, H5O_MTIME_NEW_ID, H5O_ALL, TRUE, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR
        PASSED();


        /* release resources */
        TESTING("object header closing");
        if(H5O_close(&oh_loc) < 0)
            FAIL_STACK_ERROR
        PASSED();

        /*
         * Test moving message to first chunk
         */
        TESTING("locking messages");
        HDmemset(&oh_loc, 0, sizeof(oh_loc));
        if(H5O_create(f, H5P_DATASET_XFER_DEFAULT, (size_t)64, (size_t)0, H5P_GROUP_CREATE_DEFAULT, &oh_loc/*out*/) < 0)
            FAIL_STACK_ERROR
        if(1 != H5O_link(&oh_loc, 1, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR

        /* Create second object header, to guarantee that first object header uses multiple chunks */
        HDmemset(&oh_loc2, 0, sizeof(oh_loc2));
        if(H5O_create(f, H5P_DATASET_XFER_DEFAULT, (size_t)64, (size_t)0, H5P_GROUP_CREATE_DEFAULT, &oh_loc2/*out*/) < 0)
            FAIL_STACK_ERROR
        if(1 != H5O_link(&oh_loc2, 1, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR

        /* Fill object header with messages, creating multiple chunks */
        for(i = 0; i < 10; i++) {
            time_new = (i + 1) * 1000 + 10;
            if(H5O_msg_create(&oh_loc, H5O_MTIME_NEW_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
                FAIL_STACK_ERROR
        } /* end for */

        /* Get # of object header chunks */
        if(H5O_get_hdr_info(&oh_loc, H5P_DATASET_XFER_DEFAULT, &hdr_info) < 0)
            FAIL_STACK_ERROR
        if(hdr_info.nchunks != 2)
            TEST_ERROR

        /* Add message to lock to object header */
        time_new = 11111111;
        if(H5O_msg_create(&oh_loc, H5O_MTIME_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR

        /* Verify chunk index for message */
        if((chunkno = H5O_msg_get_chunkno(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT)) < 0)
            FAIL_STACK_ERROR
        if(chunkno != 1)
            TEST_ERROR

        /* Lock the message into the chunk */
        if(H5O_msg_lock(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR

        /* Attempt to lock the message twice */
        H5E_BEGIN_TRY {
            ret = H5O_msg_lock(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT);
        } H5E_END_TRY;
        if(ret >= 0)
            TEST_ERROR

        /* Delete all the other messages, which would move the message into
         * chunk #0, if it wasn't locked
         */
        if(H5O_msg_remove(&oh_loc, H5O_MTIME_NEW_ID, H5O_ALL, TRUE, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR

        /* Verify chunk index for message */
        if((chunkno = H5O_msg_get_chunkno(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT)) < 0)
            FAIL_STACK_ERROR
        if(chunkno != 1)
            TEST_ERROR

        /* Unlock the message */
        if(H5O_msg_unlock(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR

        /* Attempt to unlock the message twice */
        H5E_BEGIN_TRY {
            ret = H5O_msg_unlock(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT);
        } H5E_END_TRY;
        if(ret >= 0)
            TEST_ERROR

        /* Close object headers */
        if(H5O_close(&oh_loc2) < 0)
            FAIL_STACK_ERROR
        if(H5O_close(&oh_loc) < 0)
            FAIL_STACK_ERROR

        /* Open first object header */
        HDmemset(&oh_loc, 0, sizeof(oh_loc));
        if(H5O_create(f, H5P_DATASET_XFER_DEFAULT, (size_t)64, (size_t)0, H5P_GROUP_CREATE_DEFAULT, &oh_loc/*out*/) < 0)
            FAIL_STACK_ERROR
        if(1 != H5O_link(&oh_loc, 1, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR

        /* Create second object header, to guarantee that first object header uses multiple chunks */
        HDmemset(&oh_loc2, 0, sizeof(oh_loc2));
        if(H5O_create(f, H5P_DATASET_XFER_DEFAULT, (size_t)64, (size_t)0, H5P_GROUP_CREATE_DEFAULT, &oh_loc2/*out*/) < 0)
            FAIL_STACK_ERROR
        if(1 != H5O_link(&oh_loc2, 1, H5P_DATASET_XFER_DEFAULT))
            FAIL_STACK_ERROR

        /* Add message to move to object header */
        time_new = 11111111;
        if(H5O_msg_create(&oh_loc, H5O_MTIME_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR

        /* Verify chunk index for message */
        if((chunkno = H5O_msg_get_chunkno(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT)) < 0)
            FAIL_STACK_ERROR
        if(chunkno != 0)
            TEST_ERROR

        /* Lock the message into the chunk */
        if(H5O_msg_lock(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR

        /* Fill object header with messages, creating multiple chunks */
        /* (would normally move locked message to new chunk) */
        for(i = 0; i < 10; i++) {
            time_new = (i + 1) * 1000 + 10;
            if(H5O_msg_create(&oh_loc, H5O_MTIME_NEW_ID, 0, 0, &time_new, H5P_DATASET_XFER_DEFAULT) < 0)
                FAIL_STACK_ERROR
        } /* end for */

        /* Get # of object header chunks */
        if(H5O_get_hdr_info(&oh_loc, H5P_DATASET_XFER_DEFAULT, &hdr_info) < 0)
            FAIL_STACK_ERROR
        if(hdr_info.nchunks != 2)
            TEST_ERROR

        /* Verify chunk index for message */
        if((chunkno = H5O_msg_get_chunkno(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT)) < 0)
            FAIL_STACK_ERROR
        if(chunkno != 0)
            TEST_ERROR

        /* Unlock the message */
        if(H5O_msg_unlock(&oh_loc, H5O_MTIME_ID, H5P_DATASET_XFER_DEFAULT) < 0)
            FAIL_STACK_ERROR

        /* Close object headers */
        if(H5O_close(&oh_loc2) < 0)
            FAIL_STACK_ERROR
        if(H5O_close(&oh_loc) < 0)
            FAIL_STACK_ERROR

        PASSED();


        /* Test reading datasets with undefined object header messages
         * and the various "fail/mark if unknown" object header message flags
         */
        HDputs("Accessing objects with unknown header messages:");
        {
            hid_t file2;                    /* File ID for 'bogus' object file */
            hid_t sid;                      /* Dataspace ID */
            hid_t aid;                      /* Attribute ID */
            const char *testfile = H5_get_srcdir_filename(FILE_BOGUS);

            TESTING("object with unknown header message and no flags set");

            /* Open the file with objects that have unknown header messages (generated with gen_bogus.c) */
            if((file2 = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0)
                TEST_ERROR

            /* Open the dataset with the unknown header message, but no extra flags */
            if((dset = H5Dopen2(file2, "/Dataset1", H5P_DEFAULT)) < 0)
                TEST_ERROR
            if(H5Dclose(dset) < 0)
                TEST_ERROR

            PASSED();

            TESTING("object in r/o file with unknown header message & 'fail if unknown and open for write' flag set");

            /* Open the dataset with the unknown header message, and "fail if unknown and open for write" flag */
            if((dset = H5Dopen2(file2, "/Dataset2", H5P_DEFAULT)) < 0)
                TEST_ERROR
            if(H5Dclose(dset) < 0)
                TEST_ERROR

            PASSED();

            TESTING("object in r/o file with unknown header message & 'fail if unknown always' flag set");

            /* Attempt to open the dataset with the unknown header message, and "fail if unknown always" flag */
            H5E_BEGIN_TRY {
                dset = H5Dopen2(file2, "/Dataset3", H5P_DEFAULT);
            } H5E_END_TRY;
            if(dset >= 0) {
                H5Dclose(dset);
                TEST_ERROR
            } /* end if */

            PASSED();

            TESTING("object with unknown header message & 'mark if unknown' flag set");

            /* Copy object with "mark if unknown" flag on message into file that can be modified */
            if(H5Ocopy(file2, "/Dataset4", file, "/Dataset4", H5P_DEFAULT, H5P_DEFAULT) < 0)
                TEST_ERROR

            /* Close the file we created (to flush changes to file) */
            if(H5Fclose(file) < 0)
                TEST_ERROR

            /* Re-open the file created, with read-only permissions */
            if((file = H5Fopen(filename, H5F_ACC_RDONLY, fapl)) < 0)
                TEST_ERROR

            /* Open the dataset with the "mark if unknown" message */
            if((dset = H5Dopen2(file, "/Dataset4", H5P_DEFAULT)) < 0)
                TEST_ERROR

            /* Check that the "unknown" message was _NOT_ marked */
            if(H5O_check_msg_marked_test(dset, FALSE) < 0)
                FAIL_STACK_ERROR

            /* Close the dataset */
            if(H5Dclose(dset) < 0)
                TEST_ERROR

            /* Close the file we created (to flush change to object header) */
            if(H5Fclose(file) < 0)
                TEST_ERROR

            /* Re-open the file created */
            if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0)
                TEST_ERROR

            /* Open the dataset with the "mark if unknown" message */
            if((dset = H5Dopen2(file, "/Dataset4", H5P_DEFAULT)) < 0)
                TEST_ERROR

            /* Create data space */
            if((sid = H5Screate(H5S_SCALAR)) < 0)
                FAIL_STACK_ERROR

            /* Create an attribute, to get the object header into write access */
            if((aid = H5Acreate2(dset, "Attr", H5T_NATIVE_INT, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
                FAIL_STACK_ERROR

            /* Close dataspace */
            if(H5Sclose(sid) < 0)
                FAIL_STACK_ERROR

            /* Close attribute */
            if(H5Aclose(aid) < 0)
                FAIL_STACK_ERROR

            /* Close the dataset */
            if(H5Dclose(dset) < 0)
                TEST_ERROR

            /* Close the file we created (to flush change to object header) */
            if(H5Fclose(file) < 0)
                TEST_ERROR

            /* Re-open the file created */
            if((file = H5Fopen(filename, H5F_ACC_RDONLY, fapl)) < 0)
                TEST_ERROR

            /* Re-open the dataset with the "mark if unknown" message */
            if((dset = H5Dopen2(file, "/Dataset4", H5P_DEFAULT)) < 0)
                TEST_ERROR

            /* Check that the "unknown" message was marked */
            if(H5O_check_msg_marked_test(dset, TRUE) < 0)
                FAIL_STACK_ERROR

            /* Close the dataset */
            if(H5Dclose(dset) < 0)
                TEST_ERROR

            /* Close the file with the bogus objects */
            if(H5Fclose(file2) < 0)
                TEST_ERROR

            PASSED();

            /* Open the file with objects that have unknown header messages (generated with gen_bogus.c) with RW intent this time */
            if((file2 = H5Fopen(testfile, H5F_ACC_RDWR, H5P_DEFAULT)) < 0)
                TEST_ERROR

            TESTING("object in r/w file with unknown header message & 'fail if unknown and open for write' flag set");

            /* Attempt to open the dataset with the unknown header message, and "fail if unknown and open for write" flag */
            H5E_BEGIN_TRY {
                dset = H5Dopen2(file2, "/Dataset2", H5P_DEFAULT);
            } H5E_END_TRY;
            if(dset >= 0) {
                H5Dclose(dset);
                TEST_ERROR
            } /* end if */

            PASSED();

            TESTING("object in r/w file with unknown header message & 'fail if unknown always' flag set");

            /* Attempt to open the dataset with the unknown header message, and "fail if unknown always" flag */
            H5E_BEGIN_TRY {
                dset = H5Dopen2(file2, "/Dataset3", H5P_DEFAULT);
            } H5E_END_TRY;
            if(dset >= 0) {
                H5Dclose(dset);
                TEST_ERROR
            } /* end if */
Esempio n. 5
0
File: ohdr.c Progetto: csgcmai/hdf5
/*
 *  To test objects with unknown messages in a file with:
 *  	a) H5O_BOGUS_VALID_ID:
 *	   --the bogus_id is within the range of H5O_msg_class_g[]
 *  	b) H5O_BOGUS_INVALID_ID:
 *  	   --the bogus_id is outside the range of H5O_msg_class_g[]
 *
 *   The test file is FILE_BOGUS: "tbogus.h5" generated with gen_bogus.c
 *   --objects that have unknown header messages with H5O_BOGUS_VALID_ID in "/"
 *   --objects that have unknown header messages with H5O_BOGUS_INVALID_ID in "/group"
 *
 *   The test also uses the test file FILENAME[0] (ohdr.h5): the parameter "filename"
 */
static herr_t
test_unknown(unsigned bogus_id, char *filename, hid_t fapl)
{
    hid_t fid = -1;	/* file ID */
    hid_t gid = -1;	/* group ID */
    hid_t did = -1;	/* Dataset ID */
    hid_t sid = -1;     /* Dataspace ID */
    hid_t aid = -1;     /* Attribute ID */
    hid_t loc = -1;	/* location: file or group ID */
    hid_t fid_bogus = -1;	/* bogus file ID */
    hid_t gid_bogus = -1;	/* bogus group ID */
    hid_t loc_bogus = -1;	/* location: bogus file or group ID */
    const char *testfile = H5_get_srcdir_filename(FILE_BOGUS);

    TESTING("object with unknown header message and no flags set");

    /* Open filename */
    if((fid = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0)
        FAIL_STACK_ERROR

    /* Open FILE_BOGUS */
    if((fid_bogus = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0)
        FAIL_STACK_ERROR

    /* Set up location ID depending on bogus_id */
    if(bogus_id == H5O_BOGUS_INVALID_ID) {
        /* Open "group" in FILE_BOGUS */
        if((gid_bogus = H5Gopen2(fid_bogus, "group", H5P_DEFAULT)) < 0) 
            FAIL_STACK_ERROR
        loc_bogus = gid_bogus;

        /* Create "group" in filename */
        if((gid = H5Gcreate2(fid, "group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            FAIL_STACK_ERROR
        loc = gid;

    } else { /* H5O_BOGUS_VALID_ID */
        loc_bogus = fid_bogus;
        loc = fid;
    } /* end else */

   /* Open the dataset with the unknown header message, but no extra flags */
    if((did = H5Dopen2(loc_bogus, "Dataset1", H5P_DEFAULT)) < 0)
        FAIL_STACK_ERROR
    if(H5Dclose(did) < 0)
        FAIL_STACK_ERROR

    PASSED();

    TESTING("object in r/o file with unknown header message & 'fail if unknown and open for write' flag set");

    /* Open the dataset with the unknown header message, and "fail if unknown and open for write" flag */
    if((did = H5Dopen2(loc_bogus, "Dataset2", H5P_DEFAULT)) < 0)
        FAIL_STACK_ERROR
    if(H5Dclose(did) < 0)
        FAIL_STACK_ERROR

    PASSED();

    TESTING("object in r/o file with unknown header message & 'fail if unknown always' flag set");

    /* Attempt to open the dataset with the unknown header message, and "fail if unknown always" flag */
    H5E_BEGIN_TRY {
        did = H5Dopen2(loc_bogus, "Dataset3", H5P_DEFAULT);
    } H5E_END_TRY;
    if(did >= 0) {
        H5Dclose(did);
        TEST_ERROR
    } /* end if */
Esempio n. 6
0
/*-------------------------------------------------------------------------
 * Function:	main
 *
 * Purpose:	H5O_mtime_decode() test.
 *
 * Return:	Success:
 *
 *		Failure:
 *
 * Programmer:	Robb Matzke
 *              Thursday, July 30, 1998
 *
 * Modifications:
 *              Added checks for old and new modification time messages
 *              in pre-created datafiles (generated with gen_old_mtime.c and
 *              gen_new_mtime.c).
 *              Quincey Koziol
 *              Friday, January  3, 2003
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    hid_t	fapl, file, space, dset;
    hsize_t	size[1] = {2};
    time_t	now;
    struct tm	*tm;
    H5O_info_t	oi1, oi2;
    signed char	buf1[32], buf2[32];
    char	filename[1024];

    h5_reset();
    fapl = h5_fileaccess();

    TESTING("modification time messages");

    /* Create the file, create a dataset, then close the file */
    h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
    if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
        TEST_ERROR;
    if((space = H5Screate_simple(1, size, NULL)) < 0) TEST_ERROR;
    if((dset = H5Dcreate2(file, "dset", H5T_NATIVE_SCHAR, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0)
        TEST_ERROR;
    now = HDtime(NULL);
    if(H5Dclose(dset) < 0) TEST_ERROR;
    if(H5Sclose(space) < 0) TEST_ERROR;
    if(H5Fclose(file) < 0) TEST_ERROR;

    /*
     * Open the file and get the modification time. We'll test the
     * H5Oget_info() arguments too: being able to stat something without
     * knowing its name.
     */
    h5_fixname(FILENAME[0], fapl, filename, sizeof filename);
    if((file = H5Fopen(filename, H5F_ACC_RDONLY, fapl)) < 0) TEST_ERROR;
    if(H5Oget_info_by_name(file, "dset", &oi1, H5P_DEFAULT) < 0) TEST_ERROR;
    if((dset = H5Dopen2(file, "dset", H5P_DEFAULT)) < 0) TEST_ERROR;
    if(H5Oget_info(dset, &oi2) < 0) TEST_ERROR;
    if(H5Dclose(dset) < 0) TEST_ERROR;
    if(H5Fclose(file) < 0) TEST_ERROR;

    /* Compare addresses & times from the two ways of calling H5Oget_info() */
    if(oi1.addr != oi2.addr || oi1.ctime != oi2.ctime) {
        H5_FAILED();
        puts("    Calling H5Oget_info() with the dataset ID returned");
        puts("    different values than calling it with a file and dataset");
        puts("    name.");
        goto error;
    }

    /* Compare times -- they must be within 60 seconds of one another */
    if(0 == oi1.ctime) {
        SKIPPED();
        puts("    The modification time could not be decoded on this OS.");
        puts("    Modification times will be mantained in the file but");
        puts("    cannot be queried on this system.  See H5O_mtime_decode().");
        return 0;
    } else if(HDfabs(HDdifftime(now, oi1.ctime)) > (double)60.0F) {
        H5_FAILED();
        tm = HDlocaltime(&(oi1.ctime));
        HDstrftime((char*)buf1, sizeof buf1, "%Y-%m-%d %H:%M:%S", tm);
        tm = HDlocaltime(&now);
        HDstrftime((char*)buf2, sizeof buf2, "%Y-%m-%d %H:%M:%S", tm);
        printf("    got: %s\n    ans: %s\n", buf1, buf2);
        goto error;
    }
    PASSED();


    /* Check opening existing file with old-style modification time information
     * and make certain that the time is correct
     */
    TESTING("accessing old modification time messages");

    {
        const char *testfile = H5_get_srcdir_filename(TESTFILE1); /* Corrected test file name */

        file = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT);
        if(file >= 0){
            if(H5Oget_info_by_name(file, "/Dataset1", &oi1, H5P_DEFAULT) < 0)
                TEST_ERROR;
            if(oi1.ctime != MTIME1) {
                H5_FAILED();
                   /* If this fails, examine H5Omtime.c.  Modification time is very
                    * system dependant (e.g., on Windows DST must be hardcoded). */
                puts("    Old modification time incorrect");
                goto error;
            }
            if(H5Fclose(file) < 0) TEST_ERROR;
        }
        else {
            H5_FAILED();
            printf("***cannot open the pre-created old modification test file (%s)\n",
                testfile);
            goto error;
        } /* end else */
    }
    PASSED();

    /* Check opening existing file with new-style modification time information
     * and make certain that the time is correct
     */
    TESTING("accessing new modification time messages");

    {
        const char *testfile = H5_get_srcdir_filename(TESTFILE2); /* Corrected test file name */

        file = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT);
        if(file >= 0){
            if(H5Oget_info_by_name(file, "/Dataset1", &oi2, H5P_DEFAULT) < 0)
                TEST_ERROR;
            if(oi2.ctime != MTIME2) {
               H5_FAILED();
               puts("    Modification time incorrect.");
               goto error;
            }
            if(H5Fclose(file) < 0) TEST_ERROR;
        }
        else {
            H5_FAILED();
            printf("***cannot open the pre-created old modification test file (%s)\n",
                testfile);
            goto error;
        } /* end else */
    }
    PASSED();

    /* Verify symbol table messages are cached */
    if(h5_verify_cached_stabs(FILENAME, fapl) < 0) TEST_ERROR

    /* All looks good */
    puts("All modification time tests passed.");
    h5_cleanup(FILENAME, fapl);
    return 0;

    /* Something broke */
error:
    return 1;
}
Esempio n. 7
0
static int read_data( const char* fname, /*IN*/
                     hsize_t *width, /*OUT*/
                     hsize_t *height /*OUT*/ )
{
    int    i, n;
    int    color_planes;
    char   str[20];
    FILE   *f = NULL;
    int    w, h;
    int    n_elements;
    const char *data_file = H5_get_srcdir_filename(fname);
    int    ret_val = -1;

    /*-------------------------------------------------------------------------
    * read
    *-------------------------------------------------------------------------
    */

    if(NULL == (f = HDfopen(data_file, "r"))) {
        printf( "Could not open file %s. Try set $srcdir \n", data_file );
        goto out;
    }

    fscanf(f, "%s", str);
    fscanf(f, "%d", &color_planes);
    fscanf(f, "%s", str);
    fscanf(f, "%d", &h);
    fscanf(f, "%s", str);
    fscanf(f, "%d", &w);

    /* Check product for overflow */
    if(w < 1 || h < 1 || color_planes < 1)
        goto out;
    if(w > INT_MAX / h)
        goto out;
    if(w * h > INT_MAX / color_planes)
        goto out;

    /* Compute buffer size */
    n_elements = w * h * color_planes;

    /* Check buffer size for overflow */
    if(n_elements > INT_MAX / (int)sizeof(unsigned char))
        goto out;

    /* Release the buffer, if it was previously allocated */
    if(image_data)
        HDfree(image_data);

    /* Allocate the image data buffer */
    image_data = (unsigned char *)HDmalloc((size_t)n_elements * sizeof(unsigned char));
    if(NULL == image_data)
        goto out;

    *width = (hsize_t)w;
    *height = (hsize_t)h;

    /* Read data elements */
    for(i = 0; i < n_elements; i++) {
        fscanf(f, "%d",&n);
        image_data[i] = (unsigned char)n;
    } /* end for */

    /* Indicate success */
    ret_val = 1;

out:    
    if(f)
        HDfclose(f);

    return ret_val;
} /* end read_data() */
Esempio n. 8
0
/*-------------------------------------------------------------------------
 * Function:    open_dataset
 *
 * Purpose:     Handle each dataset from the data file.
 *
 * Return:      Success:        0
 *              Failure:        Number of failures 
 *
 * Programmer:  Raymond Lu
 *              21 January 2011
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static int open_dataset(char *fname)
{
    const char *pathname = H5_get_srcdir_filename(fname); /* Corrected test file name */
    hid_t       file;         /* handles */
    unsigned 	nerrors = 0;
    const char  *not_supported= "    filter is not enabled.";

    /*
     * Open the file.
     */
    if((file = H5Fopen(pathname, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    TESTING("regular dataset of LE DOUBLE");
    nerrors += check_data(DATASETNAME, file, TRUE);

    TESTING("regular dataset of BE DOUBLE");
    nerrors += check_data(DATASETNAME1, file, TRUE);

    TESTING("dataset of LE FLOAT with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME2, file, TRUE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/
 
    TESTING("dataset of BE FLOAT with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME3, file, TRUE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/

    TESTING("dataset of LE DOUBLE with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME4, file, TRUE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/
 
    TESTING("dataset of BE DOUBLE with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME5, file, TRUE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/
 
    TESTING("dataset of LE CHAR with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME6, file, FALSE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/
 
    TESTING("dataset of BE CHAR with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME7, file, FALSE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/
 
    TESTING("dataset of LE SHORT with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME8, file, FALSE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/
 
    TESTING("dataset of BE SHORT with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME9, file, FALSE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/

    TESTING("dataset of LE INT with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME10, file, FALSE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/
 
    TESTING("dataset of BE INT with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME11, file, FALSE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/

    TESTING("dataset of LE LONG LONG with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME12, file, FALSE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/
 
    TESTING("dataset of BE LONG LONG with scale-offset filter");
#ifdef H5_HAVE_FILTER_SCALEOFFSET
    nerrors += check_data(DATASETNAME13, file, FALSE);
#else /*H5_HAVE_FILTER_SCALEOFFSET*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SCALEOFFSET*/

    TESTING("dataset of LE FLOAT with Fletcher32 filter");
#ifdef H5_HAVE_FILTER_FLETCHER32
    nerrors += check_data(DATASETNAME14, file, TRUE);
#else /*H5_HAVE_FILTER_FLETCHER32*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_FLETCHER32*/
 
    TESTING("dataset of BE FLOAT with Fletcher32 filter");
#ifdef H5_HAVE_FILTER_FLETCHER32
    nerrors += check_data(DATASETNAME15, file, TRUE);
#else /*H5_HAVE_FILTER_FLETCHER32*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_FLETCHER32*/
 
    TESTING("dataset of LE FLOAT with Deflate filter");
#ifdef H5_HAVE_FILTER_DEFLATE
    nerrors += check_data(DATASETNAME16, file, TRUE);
#else /*H5_HAVE_FILTER_DEFLATE*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_DEFLATE*/

    TESTING("dataset of BE FLOAT with Deflate filter");
#ifdef H5_HAVE_FILTER_DEFLATE
    nerrors += check_data(DATASETNAME17, file, TRUE);
#else /*H5_HAVE_FILTER_DEFLATE*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_DEFLATE*/

    TESTING("dataset of LE FLOAT with Szip filter");
#ifdef H5_HAVE_FILTER_SZIP
    nerrors += check_data(DATASETNAME18, file, TRUE);
#else /*H5_HAVE_FILTER_SZIP*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SZIP*/

    TESTING("dataset of BE FLOAT with Szip filter");
#ifdef H5_HAVE_FILTER_SZIP
    nerrors += check_data(DATASETNAME19, file, TRUE);
#else /*H5_HAVE_FILTER_SZIP*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SZIP*/

    TESTING("dataset of LE FLOAT with Shuffle filter");
#ifdef H5_HAVE_FILTER_SHUFFLE
    nerrors += check_data(DATASETNAME20, file, TRUE);
#else /*H5_HAVE_FILTER_SHUFFLE*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SHUFFLE*/

    TESTING("dataset of BE FLOAT with Shuffle filter");
#ifdef H5_HAVE_FILTER_SHUFFLE
    nerrors += check_data(DATASETNAME21, file, TRUE);
#else /*H5_HAVE_FILTER_SHUFFLE*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_SHUFFLE*/

    TESTING("dataset of LE FLOAT with Nbit filter");
#ifdef H5_HAVE_FILTER_NBIT
    nerrors += check_data(DATASETNAME22, file, TRUE);
#else /*H5_HAVE_FILTER_NBIT*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_NBIT*/

    TESTING("dataset of BE FLOAT with Nbit filter");
#ifdef H5_HAVE_FILTER_NBIT
    nerrors += check_data(DATASETNAME23, file, TRUE);
#else /*H5_HAVE_FILTER_NBIT*/
    SKIPPED();
    puts(not_supported);
#endif /*H5_HAVE_FILTER_NBIT*/

    if(H5Fclose(file))
        TEST_ERROR
    return 0;

error:
    H5E_BEGIN_TRY {
        H5Fclose(file);
    } H5E_END_TRY;
    return nerrors;
}
Esempio n. 9
0
static int read_palette(const char* fname,
                        rgb_t *palette,
                        size_t palette_size)
{
    FILE          *file;
    char          buffer[80];
    unsigned      u;
    unsigned int  red;
    unsigned int  green;
    unsigned int  blue;
    unsigned      nentries;
    const char *data_file = H5_get_srcdir_filename(fname);

    /* ensure the given palette is valid */
    if (!palette)
        return -1;

    /* open the input file */
    if (!(file = HDfopen(data_file, "r")))
    {
        printf( "Could not open file %s. Try set $srcdir \n", data_file );
        return -1;
    }

    /* read the file ident string */
    if (HDfgets(buffer, sizeof(buffer), file) == NULL)
    {
        HDfclose(file);
        return -1;
    }

    /* ensure it matches the palette file ident string */
    if ( HDstrncmp(buffer, STRING_JASC, sizeof(STRING_JASC) - 1) != 0 &&
            HDstrncmp(buffer, STRING_CWPAL, sizeof(STRING_CWPAL) - 1) != 0 )
    {
        HDfclose(file);
        return -1;
    }

    /* read the version string */
    if (HDfgets(buffer, sizeof(buffer), file) == NULL)
    {
        HDfclose(file);
        return -1;
    }

    /* ensure it matches the palette file version string */
    if ( HDstrncmp(buffer, VERSION_JASC, sizeof(VERSION_JASC) - 1) != 0 &&
            HDstrncmp(buffer, VERSION_CWPAL, sizeof(VERSION_CWPAL) - 1) != 0 )
    {
        HDfclose(file);
        return -1;
    }

    /* read the number of colors */
    if (HDfgets(buffer, sizeof(buffer), file) == NULL)
    {
        HDfclose(file);
        return -1;
    }


    /* extract the number of colors.
    check for missing version or number of colors
    in this case it reads the first entry
    */
    if ( HDstrlen( buffer ) > 4 )
    {
        HDfclose(file);
        return -1;
    }

    if (sscanf(buffer, "%u", &nentries) != 1)
    {
        HDfclose(file);
        return -1;
    }

    /* ensure there are a sensible number of colors in the palette */
    if ((nentries > 256) || (nentries > palette_size))
    {
        HDfclose(file);
        return(-1);
    }

    /* read the palette entries */
    for (u = 0; u < nentries; u++)
    {
        /* extract the red, green and blue color components.  */
        if (fscanf(file, "%u %u %u", &red, &green, &blue) != 3)
        {
            HDfclose(file);
            return -1;
        }
        /* store this palette entry */
        palette[u].r = (unsigned char)red;
        palette[u].g = (unsigned char)green;
        palette[u].b = (unsigned char)blue;
    }

    /* close file */
    HDfclose(file);

    return (int)nentries;
}
Esempio n. 10
0
static int test_generate(void)
{
    hid_t    fid;
    hsize_t  pal_dims[2] = { 256, 3 };
    float    *data = NULL;
    int      imax, jmax, kmax;
    int      n_elements;
    float    valex, xmin, xmax, value;
    FILE     *f = NULL;
    const char *data_file = H5_get_srcdir_filename(DATA_FILE4);
    int      i;
    int      retval = FAIL;

    /* create a file using default properties */
    if ((fid=H5Fcreate(FILE3,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT))<0)
        goto out;

    printf("Testing read and process data and make indexed images\n");

    /*-------------------------------------------------------------------------
    * read data; the file data format is described below
    *-------------------------------------------------------------------------
    */

    f  = HDfopen( data_file, "r" ) ;
    if ( f == NULL )
    {
        printf( "Could not find file %s. Try set $srcdir \n", data_file );
        goto out;
    }

    /*
    !The first line of the ASCII file contains the dimension of the array :
    ! IMAX, JMAX, KMAX. The integers are stored with the FORTRAN format I5.
    !The second line contains the exclusion value, the minimum and the maximum value of this
    ! file. These numbers are stored with the FORTRAN format E12.5.
    ! The remaining lines contains the data of the array, with 5 numbers per line
    ! (except the last line for each I-line).
    ! The array is stored in horizontal slices from sea surface to sea bottom and from
    ! north to south. So the indexes go from :
    !
    !   DO K = KMAX to 1
    !       DO J = JMAX to 1
    !           DO I = 1 to IMAX
    !              read
    !           OD
    !       OD
    !   OD
    !
    !              ____________________________
    !             /                           /| (imax,jmax,kmax)
    !            /        sea surface        / |
    !           /                           /  |
    !          /__________________________ /   |
    !          |                          |    |
    !          |                          |    | (imax,jmax,1)        n
    !          |                          |   /                      /
    !          |                          |  /                      /
    !     ^  j |                          | /             w  <-----o-----> e
    !  k  |  / |__________________________|/                      /
    !     | /                           (imax,1,1)               /
    !     |---------->                                          s
    !     i
    !
    */


    fscanf( f, "%d %d %d", &imax, &jmax, &kmax );
    fscanf( f, "%f %f %f", &valex, &xmin, &xmax );

    /* Sanity check on scanned-in values */
    if(imax < 1 || jmax < 1 || kmax < 1)
        goto out;

    /* Test product for integer overflow */
    if(imax > INT_MAX / jmax)
        goto out;
    if(imax * jmax > INT_MAX / kmax)
        goto out;

    n_elements = imax * jmax * kmax;

    /* Test buffer sizes for overflow */
    if(n_elements > INT_MAX / (int)sizeof(unsigned char))
        goto out;
    if(n_elements > INT_MAX / (int)sizeof(float))
        goto out;
    
    data = (float *)HDmalloc((size_t)n_elements * sizeof(float));
    if(NULL == data)
        goto out;
    image_data = (unsigned char *)HDmalloc((size_t)n_elements * sizeof(unsigned char));
    if(NULL == image_data)
        goto out;

    for ( i = 0; i < n_elements; i++ )
    {
        fscanf( f, "%f ", &value );
        data[i] = value;
    }
    HDfclose(f);
    f = NULL;

    /*-------------------------------------------------------------------------
    * transform the data from floating point to unsigned char
    * we are processing all the data here
    *-------------------------------------------------------------------------
    */

    TESTING2("make indexed image from all the data");

    for ( i = 0; i < n_elements; i++ )
        image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / (xmax - xmin ));

    /* Make the image */
    if ((H5IMmake_image_8bit(fid,"All data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
        goto out;

    PASSED();

    /*-------------------------------------------------------------------------
    * transform the data from floating point to unsigned char
    * here we just process the land data
    *-------------------------------------------------------------------------
    */

    TESTING2("make indexed image from land data");

    for ( i = 0; i < n_elements; i++ )
    {
        if ( data[i] < 0 )
            image_data[i] = 0;
        else
            image_data[i] = (unsigned char)(( 255 * (data[i] ) ) / xmax );
    }

    /* make the image */
    if ((H5IMmake_image_8bit(fid,"Land data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
        goto out;

    PASSED();

    /*-------------------------------------------------------------------------
    * transform the data from floating point to unsigned char
    * here we just process the sea data
    *-------------------------------------------------------------------------
    */

    TESTING2("make indexed image from sea data");

    for ( i = 0; i < n_elements; i++ )
    {
        if ( data[i] > 0 )
            image_data[i] = 0;
        else
            image_data[i] = (unsigned char)(( 255 * (data[i] - xmin ) ) / xmin );
    }

    /* make the image */
    if ((H5IMmake_image_8bit(fid,"Sea data",(hsize_t)imax,(hsize_t)jmax,image_data))<0)
        goto out;

    PASSED();

    /*-------------------------------------------------------------------------
    * make a palette and attach it to the datasets
    *-------------------------------------------------------------------------
    */

    TESTING2("attaching palettes");

    /* make a palette */
    if ((H5IMmake_palette(fid,PAL1_NAME,pal_dims,pal_rgb))<0)
        goto out;

    /* attach the palette to the image datasets */
    if ((H5IMlink_palette(fid,"All data",PAL1_NAME))<0)
        goto out;
    if ((H5IMlink_palette(fid,"Land data",PAL1_NAME))<0)
        goto out;
    if ((H5IMlink_palette(fid,"Sea data",PAL1_NAME))<0)
        goto out;

    PASSED();


    /*-------------------------------------------------------------------------
    * close
    *-------------------------------------------------------------------------
    */
    if (H5Fclose(fid)<0)
        goto out;

    /* Release memory buffers */
    HDfree(data);
    HDfree(image_data);

    /* Indicate success */
    return 0;

    /* error zone, gracefully close */
out:
    /* Release memory buffers */
    if(data)
        HDfree(data);
    if(image_data)
        HDfree(image_data);

    H5E_BEGIN_TRY {
        H5Fclose(fid);
    } H5E_END_TRY;
    if(f)
        HDfclose(f);
    H5_FAILED();
    return retval;
}
Esempio n. 11
0
/*-------------------------------------------------------------------------
* subroutine for test_text_dtype(): test_complicated_compound().
*-------------------------------------------------------------------------
*/
static int test_complicated_compound(void)
{
    hid_t   dtype;
    int     nmembs;
    H5T_class_t type_class;
    char   *line = NULL;
    FILE   *fp = NULL;
    size_t  size = 1024;
    const char *filename = H5_get_srcdir_filename(INPUT_FILE);

    TESTING3("        text for complicated compound types");

    /* Open input file */
    fp = fopen(filename, "r");
    if(fp == NULL) {
        printf( "Could not find file %s. Try set $srcdir \n", filename);
        goto out;
    }

    /* This part reads in the input as a string in a slow manner.  GNU C
    * Library has convenient function getline() but isn't available on
    * all machines.
    */
    if((line = (char*)calloc(size, sizeof(char)))==NULL)
        goto out;
    if(fgets(line, (int)size, fp)==NULL)
        goto out;
    while(strlen(line)==size-1) {
        size *= 2;
        if(line)
            free(line);
        if((line = (char*)calloc(size, sizeof(char)))==NULL)
            goto out;
        if(fseek(fp, 0L, SEEK_SET)!=0)
            goto out;
        if(fgets(line, (int)size, fp)==NULL)
            goto out;
    }

    fclose(fp);
    fp = NULL;

    if((dtype = H5LTtext_to_dtype(line, H5LT_DDL))<0)
        goto out;

    if((type_class = H5Tget_class(dtype))<0)
        goto out;
    if(type_class != H5T_COMPOUND)
        goto out;

    /* There should be 101 compound members */
    if((nmembs = H5Tget_nmembers(dtype))<0)
        goto out;
    if(nmembs != 101)
        goto out;

    if(line)
        free(line);

    PASSED();
    return 0;

out:

    if(line)
        free(line);
    if(fp)
        fclose(fp);

    H5_FAILED();
    return -1;
}