[ismrmrd] 81/177: Changed C code error checking (no slient fails) and put return codes for most functions.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:02:05 UTC 2015


This is an automated email from the git hooks/post-receive script.

ghisvail-guest pushed a commit to annotated tag v1.1.0.beta.1
in repository ismrmrd.

commit ae67060a61838ff7a386e86309e2c8f30b48eba0
Author: Souheil Inati <souheil.inati at nih.gov>
Date:   Tue Sep 23 11:21:21 2014 -0400

    Changed C code error checking (no slient fails) and put return codes for most functions.
---
 include/ismrmrd/ismrmrd.h |  34 +--
 libsrc/dataset.c          | 634 ++++++++++++++++++++++++----------------------
 libsrc/ismrmrd.c          | 549 +++++++++++++++++++++++----------------
 3 files changed, 691 insertions(+), 526 deletions(-)

diff --git a/include/ismrmrd/ismrmrd.h b/include/ismrmrd/ismrmrd.h
index 23bf656..b68cf3d 100644
--- a/include/ismrmrd/ismrmrd.h
+++ b/include/ismrmrd/ismrmrd.h
@@ -233,7 +233,7 @@ typedef struct ISMRMRD_AcquisitionHeader {
 } ISMRMRD_AcquisitionHeader;
 
 /* Initialize an Acquisition Header */
-EXPORTISMRMRD void ismrmrd_init_acquisition_header(ISMRMRD_AcquisitionHeader *hdr);
+EXPORTISMRMRD int ismrmrd_init_acquisition_header(ISMRMRD_AcquisitionHeader *hdr);
 
 /**
    Individual MR acquisition.
@@ -245,10 +245,10 @@ typedef struct ISMRMRD_Acquisition {
 } ISMRMRD_Acquisition;
 
 EXPORTISMRMRD ISMRMRD_Acquisition * ismrmrd_create_acquisition();
-EXPORTISMRMRD void ismrmrd_free_acquisition(ISMRMRD_Acquisition *acq);
-EXPORTISMRMRD void ismrmrd_init_acquisition(ISMRMRD_Acquisition *acq);
-EXPORTISMRMRD void ismrmrd_cleanup_acquisition(ISMRMRD_Acquisition *acq);
-EXPORTISMRMRD void ismrmrd_copy_acquisition(ISMRMRD_Acquisition *acqdest, const ISMRMRD_Acquisition *acqsource);
+EXPORTISMRMRD int ismrmrd_free_acquisition(ISMRMRD_Acquisition *acq);
+EXPORTISMRMRD int ismrmrd_init_acquisition(ISMRMRD_Acquisition *acq);
+EXPORTISMRMRD int ismrmrd_cleanup_acquisition(ISMRMRD_Acquisition *acq);
+EXPORTISMRMRD int ismrmrd_copy_acquisition(ISMRMRD_Acquisition *acqdest, const ISMRMRD_Acquisition *acqsource);
 EXPORTISMRMRD int ismrmrd_make_consistent_acquisition(ISMRMRD_Acquisition *acq);
 EXPORTISMRMRD size_t ismrmrd_size_of_acquisition_traj(const ISMRMRD_Acquisition *acq);
 EXPORTISMRMRD size_t ismrmrd_size_of_acquisition_data(const ISMRMRD_Acquisition *acq);
@@ -289,7 +289,7 @@ typedef struct ISMRMRD_ImageHeader {
     uint32_t attribute_string_len;                       /**< Length of attributes string */
 } ISMRMRD_ImageHeader;
 
-EXPORTISMRMRD void ismrmrd_init_image_header(ISMRMRD_ImageHeader *hdr);
+EXPORTISMRMRD int ismrmrd_init_image_header(ISMRMRD_ImageHeader *hdr);
 
 /**
  *  An individual Image
@@ -302,10 +302,10 @@ typedef struct ISMRMRD_Image {
 
 
 EXPORTISMRMRD ISMRMRD_Image * ismrmrd_create_image();
-EXPORTISMRMRD void ismrmrd_free_image(ISMRMRD_Image *im);
-EXPORTISMRMRD void ismrmrd_init_image(ISMRMRD_Image *im);
-EXPORTISMRMRD void ismrmrd_cleanup_image(ISMRMRD_Image *im);
-EXPORTISMRMRD void ismrmrd_copy_image(ISMRMRD_Image *imdest, const ISMRMRD_Image *imsource);
+EXPORTISMRMRD int ismrmrd_free_image(ISMRMRD_Image *im);
+EXPORTISMRMRD int ismrmrd_init_image(ISMRMRD_Image *im);
+EXPORTISMRMRD int ismrmrd_cleanup_image(ISMRMRD_Image *im);
+EXPORTISMRMRD int ismrmrd_copy_image(ISMRMRD_Image *imdest, const ISMRMRD_Image *imsource);
 EXPORTISMRMRD int ismrmrd_make_consistent_image(ISMRMRD_Image *im);
 EXPORTISMRMRD size_t ismrmrd_size_of_image_attribute_string(const ISMRMRD_Image *im);
 EXPORTISMRMRD size_t ismrmrd_size_of_image_data(const ISMRMRD_Image *im);
@@ -326,10 +326,10 @@ typedef struct ISMRMRD_NDArray {
 } ISMRMRD_NDArray;
 
 EXPORTISMRMRD ISMRMRD_NDArray * ismrmrd_create_ndarray();
-EXPORTISMRMRD void ismrmrd_free_ndarray(ISMRMRD_NDArray *arr);
-EXPORTISMRMRD void ismrmrd_init_ndarray(ISMRMRD_NDArray *arr);
-EXPORTISMRMRD void ismrmrd_cleanup_ndarray(ISMRMRD_NDArray *arr);
-EXPORTISMRMRD void ismrmrd_copy_ndarray(ISMRMRD_NDArray *arrdest, const ISMRMRD_NDArray *arrsource);
+EXPORTISMRMRD int ismrmrd_free_ndarray(ISMRMRD_NDArray *arr);
+EXPORTISMRMRD int ismrmrd_init_ndarray(ISMRMRD_NDArray *arr);
+EXPORTISMRMRD int ismrmrd_cleanup_ndarray(ISMRMRD_NDArray *arr);
+EXPORTISMRMRD int ismrmrd_copy_ndarray(ISMRMRD_NDArray *arrdest, const ISMRMRD_NDArray *arrsource);
 EXPORTISMRMRD int ismrmrd_make_consistent_ndarray(ISMRMRD_NDArray *arr);
 EXPORTISMRMRD size_t ismrmrd_size_of_ndarray_data(const ISMRMRD_NDArray *arr);
 
@@ -337,9 +337,9 @@ EXPORTISMRMRD size_t ismrmrd_size_of_ndarray_data(const ISMRMRD_NDArray *arr);
 /* Flags */
 /*********/
 EXPORTISMRMRD bool ismrmrd_is_flag_set(const uint64_t flags, const uint64_t val);
-EXPORTISMRMRD void ismrmrd_set_flag(uint64_t *flags, const uint64_t val);
-EXPORTISMRMRD void ismrmrd_clear_flag(uint64_t *flags, const uint64_t val);
-EXPORTISMRMRD void ismrmrd_clear_all_flags(uint64_t *flags);
+EXPORTISMRMRD int ismrmrd_set_flag(uint64_t *flags, const uint64_t val);
+EXPORTISMRMRD int ismrmrd_clear_flag(uint64_t *flags, const uint64_t val);
+EXPORTISMRMRD int ismrmrd_clear_all_flags(uint64_t *flags);
 
 /* TODO add helper functions for channel mask */
 
diff --git a/libsrc/dataset.c b/libsrc/dataset.c
index f670a6a..28a70c6 100644
--- a/libsrc/dataset.c
+++ b/libsrc/dataset.c
@@ -698,362 +698,388 @@ int ismrmrd_close_dataset(ISMRMRD_Dataset *dset) {
 }
 
 int ismrmrd_write_header(const ISMRMRD_Dataset *dset, const char *xmlstring) {
-    if (dset) {
-        hid_t dataset, dataspace, datatype, props;
-        hsize_t dims[] = {1};
-        herr_t h5status;
-        void *buff[1];
-        char * path;
-        
-        /* The path to the xml header */
-        path = make_path(dset, "xml");
-        
-        /* Delete the old header if it exists */
-        h5status = delete_var(dset, "xml");
-        
-        /* Create a new dataset for the xmlstring */
-        /* i.e. create the memory type, data space, and data set */
-        dataspace = H5Screate_simple(1, dims, NULL);
-        datatype = get_hdf5type_xmlheader();
-        props = H5Pcreate (H5P_DATASET_CREATE);
-        dataset = H5Dcreate2(dset->fileid, path, datatype, dataspace, H5P_DEFAULT, props,  H5P_DEFAULT);
-        
-        /* Write it out */
-        /* We have to wrap the xmlstring in an array */
-        buff[0] = (void *) xmlstring;  /* safe to get rid of const the type */
-        h5status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buff);
-        
-        /* Clean up */
-        h5status = H5Pclose(props);
-        h5status = H5Tclose(datatype);
-        h5status = H5Sclose(dataspace);
-        h5status = H5Dclose(dataset);
+    hid_t dataset, dataspace, datatype, props;
+    hsize_t dims[] = {1};
+    herr_t h5status;
+    void *buff[1];
+    char * path;
 
-        if (h5status < 0) {
-            ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to write header.");
-            return ISMRMRD_FILEERROR;
-        }
-        
-        return ISMRMRD_NOERROR;
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
-    else {
+
+    if (xmlstring==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "xmlstring should not be NULL.");
         return ISMRMRD_RUNTIMEERROR;
     }
+
+    /* The path to the xml header */
+    path = make_path(dset, "xml");
+    
+    /* Delete the old header if it exists */
+    h5status = delete_var(dset, "xml");
+    
+    /* Create a new dataset for the xmlstring */
+    /* i.e. create the memory type, data space, and data set */
+    dataspace = H5Screate_simple(1, dims, NULL);
+    datatype = get_hdf5type_xmlheader();
+    props = H5Pcreate (H5P_DATASET_CREATE);
+    dataset = H5Dcreate2(dset->fileid, path, datatype, dataspace, H5P_DEFAULT, props,  H5P_DEFAULT);
+        
+    /* Write it out */
+    /* We have to wrap the xmlstring in an array */
+    buff[0] = (void *) xmlstring;  /* safe to get rid of const the type */
+    h5status = H5Dwrite(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buff);
+    
+    /* Clean up */
+    h5status = H5Pclose(props);
+    h5status = H5Tclose(datatype);
+    h5status = H5Sclose(dataspace);
+    h5status = H5Dclose(dataset);
+    
+    if (h5status < 0) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to write header.");
+        return ISMRMRD_FILEERROR;
+    }
+        
+    return ISMRMRD_NOERROR;
 }
 
 char * ismrmrd_read_header(const ISMRMRD_Dataset *dset) {
-    if (dset) {
-        hid_t dataset, datatype;
-        herr_t h5status;
-        char * xmlstring;
+    hid_t dataset, datatype;
+    herr_t h5status;
+    char * xmlstring;
         
-        /* The path to the xml header */
-        char *path = make_path(dset, "xml");
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return NULL;
+    }
+
+    /* The path to the xml header */
+    char *path = make_path(dset, "xml");
         
-        if (link_exists(dset, path)) {
-            void *buff[1];
-            dataset = H5Dopen2(dset->fileid, path, H5P_DEFAULT);
-            datatype = get_hdf5type_xmlheader();
-            /* Read it into a 1D buffer*/
-            h5status = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buff);
-            
-            /* Unpack */
-            xmlstring = (char *) malloc(strlen(buff[0])+1);
-            if (xmlstring == NULL) {
-                ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to malloc xmlstring");
-            } else {
-                memcpy(xmlstring, buff[0], strlen(buff[0])+1);
-            }
-            
-            /* Clean up */
-            h5status = H5Tclose(datatype);
-            h5status = H5Dclose(dataset);
-            free(path);
-            
-            if (h5status < 0) {
-                ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to read header.");
-                return NULL;
-            }
-            
-            return xmlstring;
+    if (link_exists(dset, path)) {
+        void *buff[1];
+        dataset = H5Dopen2(dset->fileid, path, H5P_DEFAULT);
+        datatype = get_hdf5type_xmlheader();
+        /* Read it into a 1D buffer*/
+        h5status = H5Dread(dataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, buff);
+        
+        /* Unpack */
+        xmlstring = (char *) malloc(strlen(buff[0])+1);
+        if (xmlstring == NULL) {
+            ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to malloc xmlstring");
+        } else {
+            memcpy(xmlstring, buff[0], strlen(buff[0])+1);
         }
-        else {
-            // No XML String found
-            // TODO handle errors
-            free(path);
+        
+        /* Clean up */
+        h5status = H5Tclose(datatype);
+        h5status = H5Dclose(dataset);
+        free(path);
+        
+        if (h5status < 0) {
+            ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to read header.");
             return NULL;
         }
+            
+        return xmlstring;
     }
     else {
+        // No XML String found
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "No XML Header found.");
+        free(path);
         return NULL;
     }
 }
 
 uint32_t ismrmrd_get_number_of_acquisitions(const ISMRMRD_Dataset *dset) {
-    if (dset) {
-        /* The path to the acqusition data */    
-        char *path = make_path(dset, "data");
-        uint32_t numacq = get_number_of_elements(dset, path);
-        free(path);
-        return numacq;
-    }
-    else {
+    char *path;
+    uint32_t numacq;
+    
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
         return 0;
     }
+    /* The path to the acqusition data */    
+    path = make_path(dset, "data");
+    numacq = get_number_of_elements(dset, path);
+    free(path);
+    return numacq;
 }
 
 int ismrmrd_append_acquisition(const ISMRMRD_Dataset *dset, const ISMRMRD_Acquisition *acq) {
-    if(dset) {
-        if (acq) {
-            int status;
-            char *path;
-            hid_t datatype;
-            HDF5_Acquisition hdf5acq[1];
-            
-            /* The path to the acqusition data */    
-            path = make_path(dset, "data");
-            
-            /* The acquisition datatype */
-            datatype = get_hdf5type_acquisition();
-            
-            /* Create the HDF5 version of the acquisition */
-            hdf5acq[0].head = acq->head;
-            hdf5acq[0].traj.len = acq->head.number_of_samples * acq->head.trajectory_dimensions;
-            hdf5acq[0].traj.p = acq->traj;
-            hdf5acq[0].data.len = acq->head.number_of_samples * acq->head.active_channels;
-            hdf5acq[0].data.p = acq->data;
-            
-            /* Write it */
-            status = append_element(dset, path, hdf5acq, datatype, 0, NULL);    
-            if (status != ISMRMRD_NOERROR) {
-                ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append acquisition.");
-                return status;
-            }
-            
-            /* Clean up */
-            H5Tclose(datatype);
-            free(path);
+    int status;
+    char *path;
+    hid_t datatype;
+    HDF5_Acquisition hdf5acq[1];
             
-            return ISMRMRD_NOERROR;
-        }
-        else {
-            return ISMRMRD_RUNTIMEERROR;
-        }
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
-    else {
+    if (acq==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Acquisition pointer should not be NULL.");
         return ISMRMRD_RUNTIMEERROR;
     }
+    
+    /* The path to the acqusition data */    
+    path = make_path(dset, "data");
+            
+    /* The acquisition datatype */
+    datatype = get_hdf5type_acquisition();
+    
+    /* Create the HDF5 version of the acquisition */
+    hdf5acq[0].head = acq->head;
+    hdf5acq[0].traj.len = acq->head.number_of_samples * acq->head.trajectory_dimensions;
+    hdf5acq[0].traj.p = acq->traj;
+    hdf5acq[0].data.len = acq->head.number_of_samples * acq->head.active_channels;
+    hdf5acq[0].data.p = acq->data;
+    
+    /* Write it */
+    status = append_element(dset, path, hdf5acq, datatype, 0, NULL);
+    if (status != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append acquisition.");
+        return ISMRMRD_FILEERROR;
+    }
+
+    /* Clean up */
+    H5Tclose(datatype);
+    free(path);
+    
+    return ISMRMRD_NOERROR;
 }
 
 int ismrmrd_read_acquisition(const ISMRMRD_Dataset *dset, uint32_t index, ISMRMRD_Acquisition *acq)
 {
-    if (dset) {
-        if (acq) {
-            hid_t dataset, datatype, filespace, memspace;
-            hsize_t dims[1];
-            hsize_t offset[1];
-            hsize_t dimsr[1] = {1};
-            herr_t h5status;
-            HDF5_Acquisition hdf5acq;
-            
-            /* The path to the acqusition data */    
-            char *path = make_path(dset, "data");
+    hid_t dataset, datatype, filespace, memspace;
+    hsize_t dims[1];
+    hsize_t offset[1];
+    hsize_t dimsr[1] = {1};
+    herr_t h5status;
+    HDF5_Acquisition hdf5acq;
+    char *path;        
+
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+    if (acq==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Acquisition pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+
+    /* The path to the acqusition data */    
+    path = make_path(dset, "data");
             
-            /* Check the path, extend or create if needed */
-            if (link_exists(dset, path)) {
-                /* open */
-                dataset = H5Dopen2(dset->fileid, path, H5P_DEFAULT);
-                
-                /* The acquisition datatype */
-                datatype = get_hdf5type_acquisition();
-                /* TODO check that the dataset's datatype is correct */
+    /* Check the path, extend or create if needed */
+    if (link_exists(dset, path)) {
+        /* open */
+        dataset = H5Dopen2(dset->fileid, path, H5P_DEFAULT);
+        
+        /* The acquisition datatype */
+        datatype = get_hdf5type_acquisition();
+        /* TODO check that the dataset's datatype is correct */
                 
-                filespace = H5Dget_space(dataset);
-                h5status = H5Sget_simple_extent_dims(filespace, dims, NULL);
+        filespace = H5Dget_space(dataset);
+        h5status = H5Sget_simple_extent_dims(filespace, dims, NULL);
                 
-                if (index < dims[0]) {
-                    offset[0] = index;
-                    h5status = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, dimsr, NULL);
-                    memspace = H5Screate_simple(1, dimsr, NULL);
-                    h5status = H5Dread(dataset, datatype, memspace, filespace, H5P_DEFAULT, (void *) &hdf5acq);
-                    memcpy(&acq->head, &hdf5acq.head, sizeof(ISMRMRD_AcquisitionHeader));
-                    ismrmrd_make_consistent_acquisition(acq);
-                    memcpy(acq->traj, hdf5acq.traj.p, ismrmrd_size_of_acquisition_traj(acq));
-                    memcpy(acq->data, hdf5acq.data.p, ismrmrd_size_of_acquisition_data(acq));
+        if (index < dims[0]) {
+            offset[0] = index;
+            h5status = H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, dimsr, NULL);
+            memspace = H5Screate_simple(1, dimsr, NULL);
+            h5status = H5Dread(dataset, datatype, memspace, filespace, H5P_DEFAULT, (void *) &hdf5acq);
+            memcpy(&acq->head, &hdf5acq.head, sizeof(ISMRMRD_AcquisitionHeader));
+            ismrmrd_make_consistent_acquisition(acq);
+            memcpy(acq->traj, hdf5acq.traj.p, ismrmrd_size_of_acquisition_traj(acq));
+            memcpy(acq->data, hdf5acq.data.p, ismrmrd_size_of_acquisition_data(acq));
                     
-                    /* clean up */
-                    free(hdf5acq.traj.p);
-                    free(hdf5acq.data.p);
-                    h5status = H5Tclose(datatype);
-                    h5status = H5Sclose(filespace);
-                    h5status = H5Sclose(memspace);
-                    h5status = H5Dclose(dataset);
-                    if (h5status < 0) {
-                        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to read acquisition.");
-                        return ISMRMRD_FILEERROR;
-                    }
-                }
-                else {
-                    /* index out of range */
-                    ISMRMRD_THROW(ISMRMRD_FILEERROR, "Acquisition index out of range.");
-                    return ISMRMRD_FILEERROR;
-                }
-            }
-            else {
-                /* No data */
-                /* TODO Throw error */
+            /* clean up */
+            free(hdf5acq.traj.p);
+            free(hdf5acq.data.p);
+            h5status = H5Tclose(datatype);
+            h5status = H5Sclose(filespace);
+            h5status = H5Sclose(memspace);
+            h5status = H5Dclose(dataset);
+            if (h5status < 0) {
+                ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to read acquisition.");
                 return ISMRMRD_FILEERROR;
             }
-            
-            return ISMRMRD_NOERROR;
         }
         else {
-            return ISMRMRD_RUNTIMEERROR;
+            /* index out of range */
+            ISMRMRD_THROW(ISMRMRD_FILEERROR, "Acquisition index out of range.");
+            return ISMRMRD_FILEERROR;
         }
     }
     else {
-        return ISMRMRD_RUNTIMEERROR;
+        /* No data */
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Data not found.");
+        return ISMRMRD_FILEERROR;
     }
+    
+    return ISMRMRD_NOERROR;
 }
 
 int ismrmrd_append_image(const ISMRMRD_Dataset *dset, const char *varname,
                          const int block_mode, const ISMRMRD_Image *im) {
-    if (dset) {
-        if (im) {
-            int status;
-            hid_t datatype;
-            char *path, *headerpath, *attrpath, *datapath;
-            uint16_t dims[4];
-            
-            /* The group for this set of images */
-            /* /groupname/varname */
-            path = make_path(dset, varname);
-            /* Make sure the path exists */
-            create_link(dset, path);        
-            
-            /* Handle the header */
-            headerpath = append_to_path(dset, path, "header");
-            datatype = get_hdf5type_imageheader();
-            status = append_element(dset, headerpath, (void *) &im->head, datatype, 0, NULL);
-            if (status != ISMRMRD_NOERROR) {
-                ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append image header.");
-                return ISMRMRD_FILEERROR;
-            }
-            free(headerpath);
-            
-            /* Handle the attribute string */
-            attrpath = append_to_path(dset, path, "attributes");
-            datatype = get_hdf5type_image_attribute_string();
-            status = append_element(dset, attrpath, (void *) &im->attribute_string, datatype, 0, NULL);
-            if (status != ISMRMRD_NOERROR) {
-                ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append image attribute string.");
-                return ISMRMRD_FILEERROR;
-            }
-            free(attrpath);
-            
-            /* Handle the data */
-            datapath = append_to_path(dset, path, "data");
-            datatype = get_hdf5type_ndarray(im->head.data_type);
-            /* permute the dimensions in the hdf5 file */
-            dims[3] = im->head.matrix_size[0];
-            dims[2] = im->head.matrix_size[1];
-            dims[1] = im->head.matrix_size[2];
-            dims[0] = im->head.channels;
-            status = append_element(dset, datapath, im->data, datatype, 4, dims);
-            if (status != ISMRMRD_NOERROR) {
-                ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append image data.");
-                return ISMRMRD_FILEERROR;
-            }
-            free(datapath);
-            
-            /* Final cleanup */
-            H5Tclose(datatype);
-            free(path);
-            
-            return ISMRMRD_NOERROR;
-        }
-        else {
-            return ISMRMRD_RUNTIMEERROR;
-        }
+    int status;
+    hid_t datatype;
+    char *path, *headerpath, *attrpath, *datapath;
+    uint16_t dims[4];
+
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
-    else {
+    if (varname==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Varname should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Image pointer should not be NULL.");
         return ISMRMRD_RUNTIMEERROR;
     }
+
+    /* The group for this set of images */
+    /* /groupname/varname */
+    path = make_path(dset, varname);
+    /* Make sure the path exists */
+    create_link(dset, path);        
+    
+    /* Handle the header */
+    headerpath = append_to_path(dset, path, "header");
+    datatype = get_hdf5type_imageheader();
+    status = append_element(dset, headerpath, (void *) &im->head, datatype, 0, NULL);
+    if (status != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append image header.");
+        return ISMRMRD_FILEERROR;
+    }
+    free(headerpath);
+            
+    /* Handle the attribute string */
+    attrpath = append_to_path(dset, path, "attributes");
+    datatype = get_hdf5type_image_attribute_string();
+    status = append_element(dset, attrpath, (void *) &im->attribute_string, datatype, 0, NULL);
+    if (status != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append image attribute string.");
+        return ISMRMRD_FILEERROR;
+    }
+    free(attrpath);
+            
+    /* Handle the data */
+    datapath = append_to_path(dset, path, "data");
+    datatype = get_hdf5type_ndarray(im->head.data_type);
+    /* permute the dimensions in the hdf5 file */
+    dims[3] = im->head.matrix_size[0];
+    dims[2] = im->head.matrix_size[1];
+    dims[1] = im->head.matrix_size[2];
+    dims[0] = im->head.channels;
+    status = append_element(dset, datapath, im->data, datatype, 4, dims);
+    if (status != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append image data.");
+        return ISMRMRD_FILEERROR;
+    }
+    free(datapath);
+            
+    /* Final cleanup */
+    H5Tclose(datatype);
+    free(path);
+    
+    return ISMRMRD_NOERROR;
 };
 
 uint32_t ismrmrd_get_number_of_images(const ISMRMRD_Dataset *dset, const char *varname)
 {
-    if (dset) {
-        /* The group for this set of images */
-        /* /groupname/varname */
-        char *path = make_path(dset, varname);
-        /* The path to the acqusition image headers */
-        char *headerpath = append_to_path(dset, path, "header");
-        uint32_t numimages = get_number_of_elements(dset, headerpath);
-        free(headerpath);
-        free(path);
-        return numimages;
+    char *path, *headerpath;
+    uint32_t numimages;
+    
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return 0;
     }
-    else {
+    if (varname==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Varname should not be NULL.");
         return 0;
     }
+    /* The group for this set of images */
+    /* /groupname/varname */
+    path = make_path(dset, varname);
+    /* The path to the acqusition image headers */
+    headerpath = append_to_path(dset, path, "header");
+    numimages = get_number_of_elements(dset, headerpath);
+    free(headerpath);
+    free(path);
+    return numimages;
 }
 
 int ismrmrd_append_array(const ISMRMRD_Dataset *dset, const char *varname,
                          const int block_mode, const ISMRMRD_NDArray *arr) {
-    if (dset) {
-        if (arr) {
-            int status;
-            hid_t datatype;
-            uint16_t ndim, *dims;
-            int n;
-            
-            /* The group for this set */
-            /* /groupname/varname */
-            char *path = make_path(dset, varname);
-            
-            /* Handle the data */
-            datatype = get_hdf5type_ndarray(arr->data_type);
-            ndim = arr->ndim;
-            dims = (uint16_t *) malloc(ndim*sizeof(uint16_t));
-            /* permute the dimensions in the hdf5 file */
-            for (n=0; n<ndim; n++) {
-                dims[ndim-n-1] = arr->dims[n];
-            }
-            status = append_element(dset, path, arr->data, datatype, ndim, dims);
-            if (status != ISMRMRD_NOERROR) {
-                ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append array.");
-                return ISMRMRD_FILEERROR;
-            }
-            
-            /* Final cleanup */
-            free(dims);
-            H5Tclose(datatype);
-            free(path);
+    int status;
+    hid_t datatype;
+    uint16_t ndim, *dims;
+    int n;
+    char *path;
+    
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return 0;
+    }
+    if (varname==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Varname should not be NULL.");
+        return 0;
+    }
+    if (arr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Array pointer should not be NULL.");
+        return 0;
+    }
+    
+    /* The group for this set */
+    /* /groupname/varname */
+    path = make_path(dset, varname);
             
-            return ISMRMRD_NOERROR;
-        }
-        else {
-            return ISMRMRD_RUNTIMEERROR;
-        }
+    /* Handle the data */
+    datatype = get_hdf5type_ndarray(arr->data_type);
+    ndim = arr->ndim;
+    dims = (uint16_t *) malloc(ndim*sizeof(uint16_t));
+    /* permute the dimensions in the hdf5 file */
+    for (n=0; n<ndim; n++) {
+        dims[ndim-n-1] = arr->dims[n];
     }
-    else {
-        return ISMRMRD_RUNTIMEERROR;
+    status = append_element(dset, path, arr->data, datatype, ndim, dims);
+    if (status != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to append array.");
+        return ISMRMRD_FILEERROR;
     }
-};
+    
+    /* Final cleanup */
+    free(dims);
+    H5Tclose(datatype);
+    free(path);
+    
+    return ISMRMRD_NOERROR;
+}
 
 uint32_t ismrmrd_get_number_of_arrays(const ISMRMRD_Dataset *dset, const char *varname) {
-    if (dset) {
-        /* The group for this set */
-        /* /groupname/varname */
-        char *path = make_path(dset, varname);
-        uint32_t numarrays = get_number_of_elements(dset, path);
-        free(path);
-        return numarrays;
+    char *path;
+    uint32_t numarrays;
+    
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return 0;
     }
-    else {
+    if (varname==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Varname should not be NULL.");
         return 0;
     }
+
+    /* The group for this set */
+    /* /groupname/varname */
+    path = make_path(dset, varname);
+    numarrays = get_number_of_elements(dset, path);
+    free(path);
+    return numarrays;
 }
 
 /*****************************/
@@ -1061,23 +1087,39 @@ uint32_t ismrmrd_get_number_of_arrays(const ISMRMRD_Dataset *dset, const char *v
 /*****************************/
 int ismrmrd_read_image(const ISMRMRD_Dataset *dset, const char *varname,
                        const uint32_t index, ISMRMRD_Image *im) {
-    if (dset) {
-        return ISMRMRD_NOERROR;
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return 0;
     }
-    else {
-        return ISMRMRD_RUNTIMEERROR;
+    if (varname==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Varname should not be NULL.");
+        return 0;
+    }
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Image should not be NULL.");
+        return 0;
     }
-};
+
+    return ISMRMRD_NOERROR;
+}
 
 int ismrmrd_read_array(const ISMRMRD_Dataset *dset, const char *varname,
                        const uint32_t index, ISMRMRD_NDArray *arr) {
-    if (dset) {
-        return ISMRMRD_NOERROR;
+    if (dset==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Dataset pointer should not be NULL.");
+        return 0;
     }
-    else {
-        return ISMRMRD_RUNTIMEERROR;
+    if (varname==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Varname should not be NULL.");
+        return 0;
     }
-};
+    if (arr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Array pointer should not be NULL.");
+        return 0;
+    }
+
+    return ISMRMRD_NOERROR;
+}
 
 
 #ifdef __cplusplus
diff --git a/libsrc/ismrmrd.c b/libsrc/ismrmrd.c
index 4663f4d..9c36382 100644
--- a/libsrc/ismrmrd.c
+++ b/libsrc/ismrmrd.c
@@ -5,19 +5,13 @@
 #ifdef __cplusplus
 #include <cmath>
 #include <cassert>
-
-#ifdef DEBUG
 #include <cstdio>
-#endif /* DEBUG */
 
 #else
 /* C99 compiler */
 #include <math.h>
 #include <assert.h>
-
-#ifdef DEBUG
 #include <stdio.h>
-#endif /* DEBUG */
 
 #endif /* __cplusplus */
 
@@ -29,197 +23,286 @@ extern "C" {
 #endif
 
 /* Acquisition functions */
-void ismrmrd_init_acquisition_header(ISMRMRD_AcquisitionHeader *hdr) {
-    if (hdr) {
-        memset(hdr, 0, sizeof(ISMRMRD_AcquisitionHeader));
-        hdr->version = ISMRMRD_VERSION;
-        hdr->number_of_samples = 0;
-        hdr->available_channels = 1;
-        hdr->active_channels = 1;
+int ismrmrd_init_acquisition_header(ISMRMRD_AcquisitionHeader *hdr) {
+    if (hdr == NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+
+    memset(hdr, 0, sizeof(ISMRMRD_AcquisitionHeader));
+    hdr->version = ISMRMRD_VERSION;
+    hdr->number_of_samples = 0;
+    hdr->available_channels = 1;
+    hdr->active_channels = 1;
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_init_acquisition(ISMRMRD_Acquisition *acq) {
-    if (acq) {
-        ismrmrd_init_acquisition_header(&acq->head);
-        acq->traj = NULL;
-        acq->data = NULL;
+int ismrmrd_init_acquisition(ISMRMRD_Acquisition *acq) {
+    if (acq == NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    ismrmrd_init_acquisition_header(&acq->head);
+    acq->traj = NULL;
+    acq->data = NULL;
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_cleanup_acquisition(ISMRMRD_Acquisition *acq) {
-    if (acq) {
-        free(acq->data); acq->data = NULL;
-        free(acq->traj); acq->traj = NULL;
+int ismrmrd_cleanup_acquisition(ISMRMRD_Acquisition *acq) {
+    if (acq == NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    
+    free(acq->data);
+    acq->data = NULL;
+    free(acq->traj);
+    acq->traj = NULL;
+    return ISMRMRD_NOERROR;
 }
     
 ISMRMRD_Acquisition * ismrmrd_create_acquisition() {
     ISMRMRD_Acquisition *acq = (ISMRMRD_Acquisition *) malloc(sizeof(ISMRMRD_Acquisition));
-    ismrmrd_init_acquisition(acq);
+    if (acq == NULL) {
+        ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to malloc new ISMRMRD_Acquistion.");
+        return NULL;
+    }
+    if (ismrmrd_init_acquisition(acq) != ISMRMRD_NOERROR)
+    {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to initialize acquistion.");
+        return NULL;
+    }
     return acq;
 }
 
-void ismrmrd_free_acquisition(ISMRMRD_Acquisition *acq) {
-    if (acq) {
-        ismrmrd_cleanup_acquisition(acq);
-        free(acq);
+int ismrmrd_free_acquisition(ISMRMRD_Acquisition *acq) {
+
+    if (acq == NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    
+    if (ismrmrd_cleanup_acquisition(acq)!=ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to cleanup acquisition.");
+        return ISMRMRD_RUNTIMEERROR;        
+    }
+    free(acq);
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_copy_acquisition(ISMRMRD_Acquisition *acqdest, const ISMRMRD_Acquisition *acqsource) {
-    if (acqsource) {
-        if (acqdest) {
-            /* Copy the header */
-            memcpy(&acqdest->head, &acqsource->head, sizeof(ISMRMRD_AcquisitionHeader));
-            /* Reallocate memory for the trajectory and the data*/
-            ismrmrd_make_consistent_acquisition(acqdest);
-            /* Copy the trajectory and the data */
-            memcpy(acqdest->traj, acqsource->traj, ismrmrd_size_of_acquisition_traj(acqsource));
-            memcpy(acqdest->data, acqsource->data, ismrmrd_size_of_acquisition_data(acqsource));
-        }
+int ismrmrd_copy_acquisition(ISMRMRD_Acquisition *acqdest, const ISMRMRD_Acquisition *acqsource) {
+
+    if (acqsource==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Source pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+    if (acqdest==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Destination pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+
+    /* Copy the header */
+    memcpy(&acqdest->head, &acqsource->head, sizeof(ISMRMRD_AcquisitionHeader));
+    /* Reallocate memory for the trajectory and the data*/
+    ismrmrd_make_consistent_acquisition(acqdest);
+    /* Copy the trajectory and the data */
+    memcpy(acqdest->traj, acqsource->traj, ismrmrd_size_of_acquisition_traj(acqsource));
+    memcpy(acqdest->data, acqsource->data, ismrmrd_size_of_acquisition_data(acqsource));
+    return ISMRMRD_NOERROR;
 }
 
 int ismrmrd_make_consistent_acquisition(ISMRMRD_Acquisition *acq) {
-    if (acq) {
-        size_t traj_size, data_size;
-        
-        if (acq->head.available_channels < acq->head.active_channels) {
-            acq->head.available_channels = acq->head.active_channels;
-        }
+
+    size_t traj_size, data_size;
         
-        traj_size = ismrmrd_size_of_acquisition_traj(acq);
-        if (traj_size > 0) {
-            acq->traj = (float *)realloc(acq->traj, traj_size);
-            if (acq->traj == NULL) {
-                ISMRMRD_THROW(ISMRMRD_MEMORYERROR,
-                        "Failed to realloc acquisition trajectory array");
-                return ISMRMRD_MEMORYERROR;
-            }
+    if (acq==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+
+    if (acq->head.available_channels < acq->head.active_channels) {
+        acq->head.available_channels = acq->head.active_channels;
+    }
+    
+    traj_size = ismrmrd_size_of_acquisition_traj(acq);
+    if (traj_size > 0) {
+        acq->traj = (float *)realloc(acq->traj, traj_size);
+        if (acq->traj == NULL) {
+            ISMRMRD_THROW(ISMRMRD_MEMORYERROR,
+                          "Failed to realloc acquisition trajectory array");
+            return ISMRMRD_MEMORYERROR;
         }
+    }
         
-        data_size = ismrmrd_size_of_acquisition_data(acq);
-        if (data_size > 0) {
-            acq->data = (complex_float_t *)realloc(acq->data, data_size);
-            if (acq->data == NULL) {
-                ISMRMRD_THROW(ISMRMRD_MEMORYERROR,
-                        "Failed to realloc acquisition data array");
-                return ISMRMRD_MEMORYERROR;
-            }
+    data_size = ismrmrd_size_of_acquisition_data(acq);
+    if (data_size > 0) {
+        acq->data = (complex_float_t *)realloc(acq->data, data_size);
+        if (acq->data == NULL) {
+            ISMRMRD_THROW(ISMRMRD_MEMORYERROR,
+                          "Failed to realloc acquisition data array");
+            return ISMRMRD_MEMORYERROR;
         }
-        return ISMRMRD_NOERROR;
-    }
-    else {
-        return ISMRMRD_RUNTIMEERROR;
     }
+
+    return ISMRMRD_NOERROR;
 }
 
 size_t ismrmrd_size_of_acquisition_traj(const ISMRMRD_Acquisition *acq) {
-    if (acq) {
-        int num_traj = acq->head.number_of_samples * acq->head.trajectory_dimensions;
-        return num_traj * sizeof(*acq->traj);
+
+    int num_traj;
+    
+    if (acq==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return 0;
     }
-    return 0;
+
+    num_traj = acq->head.number_of_samples * acq->head.trajectory_dimensions;
+    return num_traj * sizeof(*acq->traj);
+
 }
 
 size_t ismrmrd_size_of_acquisition_data(const ISMRMRD_Acquisition *acq) {
-    if (acq) {
-        int num_data = acq->head.number_of_samples * acq->head.active_channels;
-        return num_data * sizeof(*acq->data);
+    int num_data;
+    
+    if (acq==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return 0;
     }
-    return 0;
+
+    num_data = acq->head.number_of_samples * acq->head.active_channels;
+    return num_data * sizeof(*acq->data);
+
 }
 
 /* Image functions */
-void ismrmrd_init_image_header(ISMRMRD_ImageHeader *hdr) {
-    if (hdr) {
-        memset(hdr, 0, sizeof(ISMRMRD_ImageHeader));
-        hdr->version = ISMRMRD_VERSION;
-        hdr->matrix_size[0] = 0;
-        hdr->matrix_size[1] = 1;
-        hdr->matrix_size[2] = 1;
-        hdr->channels = 1;
+int ismrmrd_init_image_header(ISMRMRD_ImageHeader *hdr) {
+    if (hdr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+    memset(hdr, 0, sizeof(ISMRMRD_ImageHeader));
+    hdr->version = ISMRMRD_VERSION;
+    hdr->matrix_size[0] = 0;
+    hdr->matrix_size[1] = 1;
+    hdr->matrix_size[2] = 1;
+    hdr->channels = 1;
+    return ISMRMRD_NOERROR;
+}
+
+/* ImageHeader functions */
+int ismrmrd_init_image(ISMRMRD_Image *im) {
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
-}
 
-void ismrmrd_init_image(ISMRMRD_Image *im) {
-    if (im) {
-        ismrmrd_init_image_header(&im->head);
-        im->attribute_string = NULL;
-        im->data = NULL;
+    if (ismrmrd_init_image_header(&im->head) != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to initialize image header.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    im->attribute_string = NULL;
+    im->data = NULL;
+    return ISMRMRD_NOERROR;
 }
 
 ISMRMRD_Image * ismrmrd_create_image() {
     ISMRMRD_Image *im = (ISMRMRD_Image *) malloc(sizeof(ISMRMRD_Image));
-    ismrmrd_init_image(im);
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to allocate new Image.");
+        return NULL;
+    }
+    
+    if (ismrmrd_init_image(im) != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to initialize image.");
+        return NULL;
+    }
     return im;
 }
 
-void ismrmrd_cleanup_image(ISMRMRD_Image *im) {
-    if (im) {
-        free(im->attribute_string); im->attribute_string = NULL;
-        free(im->data); im->data = NULL;
+int ismrmrd_cleanup_image(ISMRMRD_Image *im) {
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    free(im->attribute_string);
+    im->attribute_string = NULL;
+    free(im->data);
+    im->data = NULL;
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_free_image(ISMRMRD_Image *im) {
-    if (im) {
-        ismrmrd_cleanup_image(im);
-        free(im);
+int ismrmrd_free_image(ISMRMRD_Image *im) {
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }        
+    if (ismrmrd_cleanup_image(im) != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to clean up image.");
+        return ISMRMRD_RUNTIMEERROR;        
     }
+    free(im);
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_copy_image(ISMRMRD_Image *imdest, const ISMRMRD_Image *imsource) {
-    if (imsource) {
-        if (imdest) {
-            memcpy(&imdest->head, &imsource->head, sizeof(ISMRMRD_ImageHeader));
-            ismrmrd_make_consistent_image(imdest);
-            memcpy(&imdest->attribute_string, &imsource->attribute_string,
-                    ismrmrd_size_of_image_attribute_string(imdest));
-            memcpy(&imdest->data, &imsource->data, ismrmrd_size_of_image_data(imdest));
-        }
+int ismrmrd_copy_image(ISMRMRD_Image *imdest, const ISMRMRD_Image *imsource) {
+    if (imsource==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Source pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+    if (imdest==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Destination pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+    memcpy(&imdest->head, &imsource->head, sizeof(ISMRMRD_ImageHeader));
+    if (ismrmrd_make_consistent_image(imdest) != ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to make image consistent.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    memcpy(&imdest->attribute_string, &imsource->attribute_string,
+           ismrmrd_size_of_image_attribute_string(imdest));
+    memcpy(&imdest->data, &imsource->data, ismrmrd_size_of_image_data(imdest));
+    return ISMRMRD_NOERROR;
 }
 
 int ismrmrd_make_consistent_image(ISMRMRD_Image *im) {
-    if (im) {
-        size_t attr_size, data_size;
-        
-        attr_size = ismrmrd_size_of_image_attribute_string(im);
-        if (attr_size > 0) {
-            im->attribute_string = (char *)realloc(im->attribute_string, attr_size);
-            if (im->attribute_string == NULL) {
-                ISMRMRD_THROW(ISMRMRD_MEMORYERROR,
-                        "Failed to realloc image attribute string");
-                return ISMRMRD_MEMORYERROR;
-            }
+    size_t attr_size, data_size;
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+   
+    attr_size = ismrmrd_size_of_image_attribute_string(im);
+    if (attr_size > 0) {
+        im->attribute_string = (char *)realloc(im->attribute_string, attr_size);
+        if (im->attribute_string == NULL) {
+            ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to realloc image attribute string");
+            return ISMRMRD_MEMORYERROR;
         }
+    }
         
-        data_size = ismrmrd_size_of_image_data(im);
-        if (data_size > 0) {
-            im->data = realloc(im->data, data_size);
-            if (im->data == NULL) {
-                ISMRMRD_THROW(ISMRMRD_MEMORYERROR,
-                        "Failed to realloc image data array");
-                return ISMRMRD_MEMORYERROR;
-            }
+    data_size = ismrmrd_size_of_image_data(im);
+    if (data_size > 0) {
+        im->data = realloc(im->data, data_size);
+        if (im->data == NULL) {
+            ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to realloc image data array");
+            return ISMRMRD_MEMORYERROR;
         }
-        return ISMRMRD_NOERROR;
-    }
-    else {
-        return ISMRMRD_RUNTIMEERROR;
     }
+    return ISMRMRD_NOERROR;
 }
 
 size_t ismrmrd_size_of_image_data(const ISMRMRD_Image *im) {
-    if (im) {
-        size_t data_size = 0;
-        int num_data = im->head.matrix_size[0] * im->head.matrix_size[1] *
-                im->head.matrix_size[2] * im->head.channels;
+    size_t data_size = 0;
+    int num_data;
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not NULL.");
+        return 0;
+    }
+
+    num_data = im->head.matrix_size[0] * im->head.matrix_size[1] *
+            im->head.matrix_size[2] * im->head.channels;
         
-        switch (im->head.data_type) {
+    switch (im->head.data_type) {
         case ISMRMRD_USHORT:
             data_size = num_data * sizeof(uint16_t);
             break;
@@ -247,105 +330,138 @@ size_t ismrmrd_size_of_image_data(const ISMRMRD_Image *im) {
         default:
             ISMRMRD_THROW(ISMRMRD_TYPEERROR, "Invalid image data type");
             data_size = 0;
-        }
-        return data_size;
     }
-    return 0;
+    return data_size;
 }
 
 size_t ismrmrd_size_of_image_attribute_string(const ISMRMRD_Image *im) {
-    if (im) { 
-        return im->head.attribute_string_len * sizeof(*im->attribute_string);
+    if (im==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return 0;
     }
-    return 0;
+    return im->head.attribute_string_len * sizeof(*im->attribute_string);
 }
 
 /* NDArray functions */
-void ismrmrd_init_ndarray(ISMRMRD_NDArray *arr) {
-    if (arr) {
-        int n;
-        
-        arr->version = ISMRMRD_VERSION;
-        arr->data_type = 0; // no default data type
-        arr->ndim = 0;
-        
-        for (n = 0; n < ISMRMRD_NDARRAY_MAXDIM; n++) {
-            arr->dims[n] = 0;
-        }
-        arr->data = NULL;
+int ismrmrd_init_ndarray(ISMRMRD_NDArray *arr) {
+    int n;
+
+    if (arr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+
+    arr->version = ISMRMRD_VERSION;
+    arr->data_type = 0; // no default data type
+    arr->ndim = 0;
+    
+    for (n = 0; n < ISMRMRD_NDARRAY_MAXDIM; n++) {
+        arr->dims[n] = 0;
+    }
+    arr->data = NULL;
+    return ISMRMRD_NOERROR;
 }
 
 ISMRMRD_NDArray * ismrmrd_create_ndarray() {
     ISMRMRD_NDArray *arr = (ISMRMRD_NDArray *) malloc(sizeof(ISMRMRD_NDArray));
-    ismrmrd_init_ndarray(arr);
+    if (arr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to malloc new ISMRMRD_NDArray.");
+        return NULL;
+    }
+        
+    if (ismrmrd_init_ndarray(arr)!=ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to initialize ndarray.");
+        return NULL;
+    }
     return arr;
 }
 
-void ismrmrd_cleanup_ndarray(ISMRMRD_NDArray *arr) {
-    if (arr) {
-        free(arr->data); arr->data = NULL;
+int ismrmrd_cleanup_ndarray(ISMRMRD_NDArray *arr) {
+    if (arr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+
+    free(arr->data);
+    arr->data = NULL;
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_free_ndarray(ISMRMRD_NDArray *arr) {
-    if (arr) {
-        ismrmrd_cleanup_ndarray(arr);
-        free(arr);
+int ismrmrd_free_ndarray(ISMRMRD_NDArray *arr) {
+    if (arr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+
+    if (ismrmrd_cleanup_ndarray(arr)!=ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to cleanup ndarray.");
+        return ISMRMRD_RUNTIMEERROR;        
     }
+    free(arr);
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_copy_ndarray(ISMRMRD_NDArray *arrdest, const ISMRMRD_NDArray *arrsource) {
-    if (arrsource) {
-        if (arrdest) {
-            int n;
+int ismrmrd_copy_ndarray(ISMRMRD_NDArray *arrdest, const ISMRMRD_NDArray *arrsource) {
+    int n;
+
+    if (arrsource==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Source pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+    if (arrdest==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Destination pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
             
-            arrdest->version = arrsource->version;
-            arrdest->data_type = arrsource->data_type;
-            arrdest->ndim = arrsource->ndim;
+    arrdest->version = arrsource->version;
+    arrdest->data_type = arrsource->data_type;
+    arrdest->ndim = arrsource->ndim;
             
-            for (n = 0; n < ISMRMRD_NDARRAY_MAXDIM; n++) {
-                arrdest->dims[n] = arrsource->dims[n];
-            }
-            ismrmrd_make_consistent_ndarray(arrdest);
-            memcpy(&arrdest->data, &arrsource->data, ismrmrd_size_of_ndarray_data(arrdest));
-        }
+    for (n = 0; n < ISMRMRD_NDARRAY_MAXDIM; n++) {
+        arrdest->dims[n] = arrsource->dims[n];
+    }
+    if (ismrmrd_make_consistent_ndarray(arrdest)!=ISMRMRD_NOERROR) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Failed to make ndarray consistent.");
+        return ISMRMRD_RUNTIMEERROR;        
     }
+    memcpy(&arrdest->data, &arrsource->data, ismrmrd_size_of_ndarray_data(arrdest));
+    return ISMRMRD_NOERROR;
 }
 
 int ismrmrd_make_consistent_ndarray(ISMRMRD_NDArray *arr) {
-    if (arr) {
-        size_t data_size = ismrmrd_size_of_ndarray_data(arr);
-        if (data_size > 0) {
-            arr->data = realloc(arr->data, data_size);
-            if (arr->data == NULL) {
-                ISMRMRD_THROW(ISMRMRD_MEMORYERROR,
-                        "Failed to realloc NDArray data array");
-                return ISMRMRD_MEMORYERROR;
-            }
-        }
-        else {
-            /* data_size == 0 */
-            /* the data type is invalid for some other reason */
+    size_t data_size;
+    
+    if (arr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
+    }
+
+    data_size = ismrmrd_size_of_ndarray_data(arr);
+    if (data_size > 0) {
+        arr->data = realloc(arr->data, data_size);
+        if (arr->data == NULL) {
+            ISMRMRD_THROW(ISMRMRD_MEMORYERROR, "Failed to realloc NDArray data array");
             return ISMRMRD_MEMORYERROR;
         }
-        return ISMRMRD_NOERROR;
-    }
-    else {
-        return ISMRMRD_RUNTIMEERROR;
     }
+    return ISMRMRD_NOERROR;
 }
 
 size_t ismrmrd_size_of_ndarray_data(const ISMRMRD_NDArray *arr) {
-    if (arr) {
-        size_t data_size = 0;
-        int num_data = 1;
-        int n;
-        for (n = 0; n < arr->ndim; n++) {
-            num_data *= arr->dims[n];
-        }
+    size_t data_size = 0;
+    int num_data = 1;
+    int n;
+    
+    if (arr==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return 0;
+    }
+
+    for (n = 0; n < arr->ndim; n++) {
+        num_data *= arr->dims[n];
+    }
         
-        switch (arr->data_type) {
+    switch (arr->data_type) {
         case ISMRMRD_USHORT:
             data_size = num_data * sizeof(uint16_t);
             break;
@@ -373,11 +489,9 @@ size_t ismrmrd_size_of_ndarray_data(const ISMRMRD_NDArray *arr) {
         default:
             ISMRMRD_THROW(ISMRMRD_TYPEERROR, "Invalid NDArray data type");
             data_size = 0;
-        }
-        
-        return data_size;
     }
-    return 0;
+        
+    return data_size;
 }
 
 /* Misc. functions */
@@ -386,32 +500,41 @@ bool ismrmrd_is_flag_set(const uint64_t flags, const uint64_t val) {
     return (flags & bitmask) > 0;
 }
 
-void ismrmrd_set_flag(uint64_t *flags, const uint64_t val) {
-    if (flags) {
-        uint64_t bitmask = 1 << (val - 1);
-        *flags |= bitmask;
+int ismrmrd_set_flag(uint64_t *flags, const uint64_t val) {
+    uint64_t bitmask;
+    if (flags==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    bitmask = 1 << (val - 1);
+    *flags |= bitmask;
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_clear_flag(uint64_t *flags, const uint64_t val) {
-    if (flags) {
-        uint64_t bitmask = 1 << (val - 1);
-        *flags &= ~bitmask;
+int ismrmrd_clear_flag(uint64_t *flags, const uint64_t val) {
+    uint64_t bitmask;
+    if (flags==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    bitmask = 1 << (val - 1);
+    *flags &= ~bitmask;
+    return ISMRMRD_NOERROR;
 }
 
-void ismrmrd_clear_all_flags(uint64_t *flags) {
-    if (flags) {
-        *flags = 0;
+int ismrmrd_clear_all_flags(uint64_t *flags) {
+    if (flags==NULL) {
+        ISMRMRD_THROW(ISMRMRD_RUNTIMEERROR, "Pointer should not be NULL.");
+        return ISMRMRD_RUNTIMEERROR;
     }
+    *flags = 0;
+    return ISMRMRD_NOERROR;
 }
 
 static void ismrmrd_error_default(const char *file, int line, const char *func, int err, char *msg)
 {
-#ifdef DEBUG
     char *msgtype = ismrmrd_strerror(err);
     fprintf(stderr, "ERROR: %s in %s, line %d: %s\n", msgtype, file, line, msg);
-#endif
 }
 
 ismrmrd_error_handler_t ismrmrd_error_handler = ismrmrd_error_default;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/ismrmrd.git



More information about the debian-science-commits mailing list