Map an error code to a human-readable name
Usage: print *, code_to_string(ERROR_IO) ! “ERROR_IO”
user_name is an optional name for a code PIC does not know about,
so that downstream libraries owning codes above PIC_ERROR_CODE_MAX
still get readable output:
print *, code_to_string(MYLIB_ERROR_SOCKET, “MYLIB_ERROR_SOCKET”)
It is ignored for codes PIC recognises, so it can be passed
unconditionally. Unknown codes without a user_name still render as
“UNKNOWN”.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=default_int), | intent(in) | :: | code | |||
| character(len=*), | intent(in), | optional | :: | user_name |
pure function code_to_string(code, user_name) result(name) !! Map an error code to a human-readable name !! !! Usage: print *, code_to_string(ERROR_IO) ! "ERROR_IO" !! !! `user_name` is an optional name for a code PIC does not know about, !! so that downstream libraries owning codes above PIC_ERROR_CODE_MAX !! still get readable output: !! print *, code_to_string(MYLIB_ERROR_SOCKET, "MYLIB_ERROR_SOCKET") !! It is ignored for codes PIC recognises, so it can be passed !! unconditionally. Unknown codes without a `user_name` still render as !! "UNKNOWN". integer(default_int), intent(in) :: code character(len=*), intent(in), optional :: user_name character(len=:), allocatable :: name select case (code) case (SUCCESS) name = "SUCCESS" case (ERROR_GENERIC) name = "ERROR_GENERIC" case (ERROR_IO) name = "ERROR_IO" case (ERROR_PARSE) name = "ERROR_PARSE" case (ERROR_VALIDATION) name = "ERROR_VALIDATION" case (ERROR_ALLOC) name = "ERROR_ALLOC" case (ERROR_INTERNAL) name = "ERROR_INTERNAL" case (ERROR_BOUNDS) name = "ERROR_BOUNDS" case default if (present(user_name)) then name = trim(user_name) else name = "UNKNOWN" end if end select end function code_to_string