| 1 | /* |
|---|
| 2 | Copyright (C) 2011 J. Alberdi, M. Oliveira, Y. Pouillon, and M. Verstraete |
|---|
| 3 | |
|---|
| 4 | This program is free software; you can redistribute it and/or modify |
|---|
| 5 | it under the terms of the GNU Lesser General Public License as published by |
|---|
| 6 | the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | (at your option) any later version. |
|---|
| 8 | |
|---|
| 9 | This program is distributed in the hope that it will be useful, |
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | GNU Lesser General Public License for more details. |
|---|
| 13 | |
|---|
| 14 | You should have received a copy of the GNU Lesser General Public License |
|---|
| 15 | along with this program; if not, write to the Free Software |
|---|
| 16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | |
|---|
| 18 | $Id: pspio_error.h 58 2011-08-30 10:44:20Z micael $ |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include <stdlib.h> |
|---|
| 22 | #include <stdio.h> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | #include "pspio_error.h" |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | int pspio_error_add(const char *filename, const int line) { |
|---|
| 28 | int s; |
|---|
| 29 | pspio_error_t *last_err; |
|---|
| 30 | |
|---|
| 31 | if ( pspio_error_chain == NULL ) { |
|---|
| 32 | pspio_error_chain == (pspio_error_t *)malloc(sizeof(pspio_error_t)); |
|---|
| 33 | if ( pspio_error_chain == NULL ) { |
|---|
| 34 | return PSPIO_ERROR; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | pspio_error_chain->id = pspio_error_tmp_id; |
|---|
| 38 | pspio_error_chain->line = line; |
|---|
| 39 | pspio_error_chain->next = NULL; |
|---|
| 40 | s = strlen(filename); |
|---|
| 41 | pspio_error_chain->filename = (char *)malloc(s + 1); |
|---|
| 42 | if ( pspio_error_chain->filename == NULL ) { |
|---|
| 43 | return PSPIO_ERROR; |
|---|
| 44 | } |
|---|
| 45 | memcpy(pspio_error_chain->filename,filename,s); |
|---|
| 46 | pspio_error_chain->filename[s] = 0; |
|---|
| 47 | pspio_error_tmp_id = 0; |
|---|
| 48 | |
|---|
| 49 | return PSPIO_SUCCESS; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | last_err = pspio_error_chain; |
|---|
| 53 | while ( last_err->next != NULL ) { |
|---|
| 54 | last_err = last_err->next; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | last_err->next = (pspio_error_t *)malloc(sizeof(pspio_error_t)); |
|---|
| 58 | if ( last_err->next == NULL ) { |
|---|
| 59 | return PSPIO_ERROR; |
|---|
| 60 | } |
|---|
| 61 | last_err = last_err->next; |
|---|
| 62 | last_err->id = pspio_error_tmp_id; |
|---|
| 63 | last_err->line = line; |
|---|
| 64 | last_err->next = NULL; |
|---|
| 65 | s = strlen(filename); |
|---|
| 66 | last_err->filename = (char *)malloc(s + 1); |
|---|
| 67 | if ( last_err->filename == NULL ) { |
|---|
| 68 | return PSPIO_ERROR; |
|---|
| 69 | } |
|---|
| 70 | memcpy(last_err->filename,filename,s); |
|---|
| 71 | last_err->filename[s] = 0; |
|---|
| 72 | pspio_error_tmp_id = 0; |
|---|
| 73 | |
|---|
| 74 | return PSPIO_SUCCESS; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | int pspio_error_free(void) { |
|---|
| 78 | pspio_error_t *first_err; |
|---|
| 79 | |
|---|
| 80 | while ( pspio_error_chain != NULL ) { |
|---|
| 81 | first_err = pspio_error_chain; |
|---|
| 82 | pspio_error_chain = pspio_error_chain->next; |
|---|
| 83 | free(first_err); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | return PSPIO_SUCCESS; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | pspio_error_t *pspio_error_pop(void) { |
|---|
| 90 | pspio_error_t *first_error = NULL; |
|---|
| 91 | |
|---|
| 92 | if ( pspio_error_chain != NULL ) { |
|---|
| 93 | first_error = pspio_error_chain; |
|---|
| 94 | pspio_error_chain = pspio_error_chain->next; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | return first_error; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | void pspio_error_show(const int error_id, const char *filename, |
|---|
| 102 | const int line) { |
|---|
| 103 | printf("libpspio: ERROR:\n %s\n",pspio_error_str(error_id)); |
|---|
| 104 | if ( filename != NULL ) { |
|---|
| 105 | printf(" in file %s:%d\n",filename,line); |
|---|
| 106 | } |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | const char *pspio_error_str(const int pspio_errorid) { |
|---|
| 111 | switch (pspio_errorid) |
|---|
| 112 | { |
|---|
| 113 | case PSPIO_SUCCESS: |
|---|
| 114 | return "success" ; |
|---|
| 115 | case PSPIO_ERROR: |
|---|
| 116 | return "error" ; |
|---|
| 117 | case PSPIO_ENOFILE: |
|---|
| 118 | return "file does not exist" ; |
|---|
| 119 | case PSPIO_EIO: |
|---|
| 120 | return "error in I/O" ; |
|---|
| 121 | case PSPIO_EVALUE: |
|---|
| 122 | return "value error: bad value found"; |
|---|
| 123 | case PSPIO_EGSL: |
|---|
| 124 | return "error in GSL"; |
|---|
| 125 | case PSPIO_ENOMEM: |
|---|
| 126 | return "malloc failed"; |
|---|
| 127 | case PSPIO_NOSUPPORT: |
|---|
| 128 | return "No supported option in the pseudo-potential file"; |
|---|
| 129 | default: |
|---|
| 130 | return "unknown error code" ; |
|---|
| 131 | } |
|---|
| 132 | } |
|---|