From 5c81cf1dee561b02325947a9911adcf74b30e71a Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Fri, 19 Mar 2021 18:17:01 +0100 Subject: [PATCH] Added qmckl_string_of_error --- src/qmckl_error.org | 137 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 117 insertions(+), 20 deletions(-) diff --git a/src/qmckl_error.org b/src/qmckl_error.org index 7373f9f..08284d5 100644 --- a/src/qmckl_error.org +++ b/src/qmckl_error.org @@ -47,34 +47,36 @@ typedef int32_t qmckl_exit_code; Here is the complete list of exit codes. #+NAME: table-exit-codes - | ~QMCKL_SUCCESS~ | 0 | - | ~QMCKL_INVALID_ARG_1~ | 1 | - | ~QMCKL_INVALID_ARG_2~ | 2 | - | ~QMCKL_INVALID_ARG_3~ | 3 | - | ~QMCKL_INVALID_ARG_4~ | 4 | - | ~QMCKL_INVALID_ARG_5~ | 5 | - | ~QMCKL_INVALID_ARG_6~ | 6 | - | ~QMCKL_INVALID_ARG_7~ | 7 | - | ~QMCKL_INVALID_ARG_8~ | 8 | - | ~QMCKL_INVALID_ARG_9~ | 9 | - | ~QMCKL_INVALID_ARG_10~ | 10 | - | ~QMCKL_FAILURE~ | 101 | - | ~QMCKL_ERRNO~ | 102 | - | ~QMCKL_INVALID_CONTEXT~ | 103 | - | ~QMCKL_ALLOCATION_FAILED~ | 104 | - | ~QMCKL_DEALLOCATION_FAILED~ | 105 | - | ~QMCKL_INVALID_EXIT_CODE~ | 106 | + | Macro | Code | Description | + |-----------------------------+------+------------------------| + | ~QMCKL_SUCCESS~ | 0 | 'Success' | + | ~QMCKL_INVALID_ARG_1~ | 1 | 'Invalid argument 1' | + | ~QMCKL_INVALID_ARG_2~ | 2 | 'Invalid argument 2' | + | ~QMCKL_INVALID_ARG_3~ | 3 | 'Invalid argument 3' | + | ~QMCKL_INVALID_ARG_4~ | 4 | 'Invalid argument 4' | + | ~QMCKL_INVALID_ARG_5~ | 5 | 'Invalid argument 5' | + | ~QMCKL_INVALID_ARG_6~ | 6 | 'Invalid argument 6' | + | ~QMCKL_INVALID_ARG_7~ | 7 | 'Invalid argument 7' | + | ~QMCKL_INVALID_ARG_8~ | 8 | 'Invalid argument 8' | + | ~QMCKL_INVALID_ARG_9~ | 9 | 'Invalid argument 9' | + | ~QMCKL_INVALID_ARG_10~ | 10 | 'Invalid argument 10' | + | ~QMCKL_FAILURE~ | 101 | 'Failure' | + | ~QMCKL_ERRNO~ | 102 | strerror(errno) | + | ~QMCKL_INVALID_CONTEXT~ | 103 | 'Invalid context' | + | ~QMCKL_ALLOCATION_FAILED~ | 104 | 'Allocation failed' | + | ~QMCKL_DEALLOCATION_FAILED~ | 105 | 'De-allocation failed' | + | ~QMCKL_INVALID_EXIT_CODE~ | 106 | 'Invalid exit code' | # We need to force Emacs not to indent the Python code: # -*- org-src-preserve-indentation: t - + #+begin_src python :var table=table-exit-codes :results drawer :exports none """ This script generates the C and Fortran constants for the error codes from the org-mode table. """ result = [ "#+begin_src c :comments org :tangle (eval h) :exports none" ] -for (text, code) in table: +for (text, code,_) in table: text=text.replace("~","") result += [ f"#define {text:30s} {code:d}" ] result += [ "#+end_src" ] @@ -82,7 +84,7 @@ result += [ "#+end_src" ] result += [ "" ] result += [ "#+begin_src f90 :comments org :tangle (eval fh) :exports none" ] -for (text, code) in table: +for (text, code,_) in table: text=text.replace("~","") result += [ f" integer, parameter :: {text:30s} = {code:d}" ] result += [ "#+end_src" ] @@ -134,6 +136,101 @@ return '\n'.join(result) #+end_src :end: + The ~qmckl_strerror~ converts an exit code into a string. The + string is assumed to be large enough to contain the error message + (typically 128 characters). + + #+begin_src c :comments org :tangle (eval h) :exports none +#define QMCKL_ERROR_MAX_STRING_LENGTH 128 + +void qmckl_string_of_error(qmckl_exit_code error, char string[QMCKL_ERROR_MAX_STRING_LENGTH]); + #+end_src + + The text strings are extracted from the previous table. + + #+NAME:cases + #+begin_src python :var table=table-exit-codes :exports none +""" This script extracts the text associated with the error codes + from the table. +""" + +result = [] +for (text, code, message) in table: + text = text.replace("~","") + message = message.replace("'",'"') + result += [ f"""case {text}: + message = {message}; + break;""" ] +return '\n'.join(result) + + #+end_src + + #+RESULTS: cases + #+begin_example + case QMCKL_SUCCESS: + message = "Success"; + break; + case QMCKL_INVALID_ARG_1: + message = "Invalid argument 1"; + break; + case QMCKL_INVALID_ARG_2: + message = "Invalid argument 2"; + break; + case QMCKL_INVALID_ARG_3: + message = "Invalid argument 3"; + break; + case QMCKL_INVALID_ARG_4: + message = "Invalid argument 4"; + break; + case QMCKL_INVALID_ARG_5: + message = "Invalid argument 5"; + break; + case QMCKL_INVALID_ARG_6: + message = "Invalid argument 6"; + break; + case QMCKL_INVALID_ARG_7: + message = "Invalid argument 7"; + break; + case QMCKL_INVALID_ARG_8: + message = "Invalid argument 8"; + break; + case QMCKL_INVALID_ARG_9: + message = "Invalid argument 9"; + break; + case QMCKL_INVALID_ARG_10: + message = "Invalid argument 10"; + break; + case QMCKL_FAILURE: + message = "Failure"; + break; + case QMCKL_ERRNO: + message = strerror(errno); + break; + case QMCKL_INVALID_CONTEXT: + message = "Invalid context"; + break; + case QMCKL_ALLOCATION_FAILED: + message = "Allocation failed"; + break; + case QMCKL_DEALLOCATION_FAILED: + message = "De-allocation failed"; + break; + case QMCKL_INVALID_EXIT_CODE: + message = "Invalid exit code"; + break; + #+end_example + + + #+begin_src c :comments org :tangle (eval c) :noweb yes +void qmckl_string_of_error(qmckl_exit_code error, char string[QMCKL_ERROR_MAX_STRING_LENGTH]) { + char* message; + switch (error) { + <> + } + strncpy(string,message,QMCKL_ERROR_MAX_STRING_LENGTH); +} + #+end_src + * End of files :noexport: ** Test