[ismrmrd] 15/177: Added C error handling functionality.

Ghislain Vaillant ghisvail-guest at moszumanska.debian.org
Wed Jan 14 20:01:57 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 0216eda624a690b631f89699749e529187c70df6
Author: Souheil Inati <souheil.inati at nih.gov>
Date:   Wed Jul 30 23:49:03 2014 -0400

    Added C error handling functionality.
---
 ismrmrd.c            |  33 +++++++++++++++
 ismrmrd.h            |  16 ++++++-
 ismrmrd_dataset.c    |   2 +-
 tests/c/basic_test.c | 117 ++++++++++++++++++++++++++++++---------------------
 4 files changed, 117 insertions(+), 51 deletions(-)

diff --git a/ismrmrd.c b/ismrmrd.c
index 950c31f..38e3558 100644
--- a/ismrmrd.c
+++ b/ismrmrd.c
@@ -3,11 +3,17 @@
 #include <cstring>
 #include <cstdlib>
 #include <cmath>
+#include <cassert>
+/* TODO this should be wrapped in ifdef DEBUG */
+#include <cstdio>
 #else
 /* C99 compiler */
 #include <string.h>
 #include <stdlib.h>
 #include <math.h>
+#include <assert.h>
+/* TODO this should be wrapped in ifdef DEBUG */
+#include <stdio.h>
 #endif /* __cplusplus */
 
 #include "ismrmrd.h"
@@ -265,6 +271,33 @@ void ismrmrd_clear_all_flags(uint64_t *flags) {
     *flags = 0;
 }
 
+    static void ismrmrd_error_default(const char *file, int line, const char *func, int err, char *msg)
+{
+    /* TODO this should be a no op or wrapped in a #ifdef DEBUG */
+    char *msgtype = ismrmrd_strerror(err);
+    fprintf(stderr, "ERROR: %s in %s, line %d: %s\n", msgtype, file, line, msg);
+}
+
+ismrmrd_error_handler_t ismrmrd_error_handler = ismrmrd_error_default;
+
+void ismrmrd_set_error_handler(ismrmrd_error_handler_t handler) {
+    ismrmrd_error_handler = handler;
+}
+
+char *ismrmrd_strerror(int err) {
+    assert(err > ISMRMRD_BEGINERROR);
+    assert(err < ISMRMRD_ENDERROR);
+
+    /* Match the ISMRMRD_ErrorCodes */
+    char *error_messages[] = {
+        "No Error",
+        "Memory Error",
+        "File Error"
+    };
+    
+    return error_messages[err];
+}
+
 int ismrmrd_sign_of_directions(float read_dir[3], float phase_dir[3], float slice_dir[3]) {
     float r11 = read_dir[0], r12 = phase_dir[0], r13 = slice_dir[0];
     float r21 = read_dir[1], r22 = phase_dir[1], r23 = slice_dir[1];
diff --git a/ismrmrd.h b/ismrmrd.h
index ea46c09..a6ca04b 100644
--- a/ismrmrd.h
+++ b/ismrmrd.h
@@ -61,8 +61,11 @@ enum ISMRMRD_Constants {
  * Constants
  */
 enum ISMRMRD_ErrorCodes {
+    ISMRMRD_BEGINERROR=-1,
     ISMRMRD_NOERROR,
-    ISMRMRD_FILEERROR
+    ISMRMRD_MEMORYERROR,
+    ISMRMRD_FILEERROR,
+    ISMRMRD_ENDERROR
 };
     
 /**
@@ -296,6 +299,17 @@ void ismrmrd_set_flag(uint64_t *flags, const uint64_t val);
 void ismrmrd_clear_flag(uint64_t *flags, const uint64_t val);
 void ismrmrd_clear_all_flags(uint64_t *flags);
 
+
+/******************/
+/* Error Handling */
+/******************/
+typedef void (*ismrmrd_error_handler_t)(const char *file, int line, const char *function, int err, char *msg);
+extern ismrmrd_error_handler_t ismrmrd_error_handler;
+#define ISMRMRD_THROW(err, msg) ismrmrd_error_handler(__FILE__, __LINE__, __func__, (err), (msg))
+
+void ismrmrd_set_error_handler(ismrmrd_error_handler_t);
+char *ismrmrd_strerror(int err);
+
 /*****************************/
 /* Rotations and Quaternions */
 /*****************************/
diff --git a/ismrmrd_dataset.c b/ismrmrd_dataset.c
index 7731435..32e9d27 100644
--- a/ismrmrd_dataset.c
+++ b/ismrmrd_dataset.c
@@ -117,7 +117,7 @@ int ismrmrd_open_dataset(ISMRMRD_Dataset *dset, const bool create_if_needed) {
     else {
         /* Negative value for does NOT exist or other error */
         if (create_if_needed == false) {
-            // TODO raise error
+            ISMRMRD_THROW(ISMRMRD_FILEERROR, "Failed to open file.");
             return ISMRMRD_FILEERROR;
         }
         else {
diff --git a/tests/c/basic_test.c b/tests/c/basic_test.c
index 5db4ac2..dbcd7d4 100644
--- a/tests/c/basic_test.c
+++ b/tests/c/basic_test.c
@@ -5,56 +5,75 @@
 #include "ismrmrd.h"
 #include "ismrmrd_dataset.h"
 
+void myerror(const char *file, int line, const char *func, int err, char *msg) {
+    char *msgtype = ismrmrd_strerror(err);
+    fprintf(stderr, "Whoa! ERROR: %s in %s, line %d: %s\n", msgtype, file, line, msg);
+    exit(-1);
+}
+
 int main(void)
 {
 
-   ISMRMRD_Acquisition acq;
-   ismrmrd_init_acquisition(&acq);
-   acq.head.number_of_samples = 128;
-   acq.head.active_channels = 4;
-   ismrmrd_make_consistent_acquisition(&acq);
-   printf("Acq Version: %d\n", acq.head.version);
-
-   ismrmrd_set_flag(&(acq.head.flags), ISMRMRD_ACQ_FIRST_IN_SLICE);
-   printf("Flags: %llu\n", acq.head.flags);
-   printf("ACQ_FIRST_IN_SLICE: %d\n", ismrmrd_is_flag_set(acq.head.flags, ISMRMRD_ACQ_FIRST_IN_SLICE));
-   ismrmrd_clear_flag(&(acq.head.flags), ISMRMRD_ACQ_FIRST_IN_SLICE);
-   printf("Flags: %llu\n", acq.head.flags);
-   printf("ACQ_FIRST_IN_SLICE: %d\n", ismrmrd_is_flag_set(acq.head.flags, ISMRMRD_ACQ_FIRST_IN_SLICE));
-
-   //   Image im;
-   //initImage(&im);
-   //printf("Image Version: %d\n", im.head.version);
-
-   //NDArray arr;
-   //initNDArray(&arr);
-   //printf("Array ndim: %d\n", arr.ndim);
-   //printf("Array dim[0]: %d\n", arr.dims[0]);  
-
-   const char *filename = "myfile.h5";
-   const char *groupname = "/G1/V";
-
-   ISMRMRD_Dataset dataset;
-   ismrmrd_init_dataset(&dataset, filename, groupname);
-   printf("Filename: %s\n", dataset.filename);
-   printf("Groupname: %s\n", dataset.groupname);
-   
-   int status;
-   status = ismrmrd_open_dataset(&dataset, true);
-   printf("Status from open: %d\n", status);
-   printf("File_id: %d\n", dataset.fileid);
-
-   const char *xmlhdr="Hello world.";
-   status = ismrmrd_write_header(&dataset, xmlhdr);
-   printf("XMLString: %s\nLength: %lu\n", xmlhdr, strlen(xmlhdr));
-
-   char *xmlstring = ismrmrd_read_header(&dataset);
-   printf("XMLString OUT: %s\n", xmlstring);
-   free(xmlstring);
-
-   status = ismrmrd_close_dataset(&dataset);
-   printf("File_id: %d\n", dataset.fileid);
-   printf("File close status: %d\n", status);
-
-   return 0;
+    int status;
+    ISMRMRD_Dataset dataset, dataset2;
+    const char *filename = "myfile.h5";
+    const char *groupname = "/G1/dataset";
+    const char *xmlhdr = "Hello world.";
+    
+    /* Set the error handler */
+    ismrmrd_set_error_handler(myerror);
+
+    /* Create a data set */
+    ismrmrd_init_dataset(&dataset, filename, groupname);   
+    status = ismrmrd_open_dataset(&dataset, true);
+    printf("Dataset1 open, fileid: %d\n", dataset.fileid);
+
+    status = ismrmrd_write_header(&dataset, xmlhdr);
+    /* Close the dataset */
+    status = ismrmrd_close_dataset(&dataset);
+    printf("Dataset1 closed\n");
+
+
+    
+    /* Reopen the file as a different dataset */
+    ismrmrd_init_dataset(&dataset2, filename, groupname);       
+    status = ismrmrd_open_dataset(&dataset2, true);
+    printf("Dataset2 open, fileid: %d\n", dataset2.fileid);
+
+    /* Read the header */
+    char *xmlstring = ismrmrd_read_header(&dataset2);
+    printf("Header: %s\n", xmlstring);
+
+    /* Clean up */
+    free(xmlstring);
+    
+    /* Close the dataset */
+    status = ismrmrd_close_dataset(&dataset2);
+    printf("Dataset2 closed\n");
+
+            
+    ISMRMRD_Acquisition acq;
+    ismrmrd_init_acquisition(&acq);
+    acq.head.number_of_samples = 128;
+    acq.head.active_channels = 4;
+    ismrmrd_make_consistent_acquisition(&acq);
+    printf("Acq Version: %d\n", acq.head.version);
+
+    ismrmrd_set_flag(&(acq.head.flags), ISMRMRD_ACQ_FIRST_IN_SLICE);
+    printf("Flags: %llu\n", acq.head.flags);
+    printf("ACQ_FIRST_IN_SLICE: %d\n", ismrmrd_is_flag_set(acq.head.flags, ISMRMRD_ACQ_FIRST_IN_SLICE));
+    ismrmrd_clear_flag(&(acq.head.flags), ISMRMRD_ACQ_FIRST_IN_SLICE);
+    printf("Flags: %llu\n", acq.head.flags);
+    printf("ACQ_FIRST_IN_SLICE: %d\n", ismrmrd_is_flag_set(acq.head.flags, ISMRMRD_ACQ_FIRST_IN_SLICE));
+
+    //   Image im;
+    //initImage(&im);
+    //printf("Image Version: %d\n", im.head.version);
+    
+    //NDArray arr;
+    //initNDArray(&arr);
+    //printf("Array ndim: %d\n", arr.ndim);
+    //printf("Array dim[0]: %d\n", arr.dims[0]);  
+    
+    return 0;
 }

-- 
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