]> sigrok.org Git - libsigrok.git/blob - error.c
sr: Add sr_strerror() and sr_strerror_name().
[libsigrok.git] / error.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include "libsigrok.h"
22
23 /**
24  * Return a human-readable error string for the given libsigrok error code.
25  *
26  * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
27  * @return A const string containing a short, human-readable (English)
28  *         description of the error, such as "memory allocation error".
29  *         The string must NOT be free'd by the caller!
30  * @see sr_strerror_name
31  */
32 SR_API const char *sr_strerror(int error_code)
33 {
34         const char *str;
35
36         /*
37          * Note: All defined SR_* error macros from libsigrok.h should have
38          * an entry in this function, as well as in sr_strerror_name().
39          */
40
41         switch (error_code) {
42         case SR_OK:
43                 str = "no error";
44                 break;
45         case SR_ERR:
46                 str = "generic/unspecified error";
47                 break;
48         case SR_ERR_MALLOC:
49                 str = "memory allocation error";
50                 break;
51         case SR_ERR_BUG:
52                 str = "internal error";
53                 break;
54         case SR_ERR_SAMPLERATE:
55                 str = "invalid samplerate";
56                 break;
57         default:
58                 str = "unknown error";
59                 break;
60         }
61
62         return str;
63 }
64
65 /**
66  * Return the "name" string of the given libsigrok error code.
67  *
68  * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC",
69  * the name of the SR_OK code is "SR_OK", and so on.
70  *
71  * This function can be used for various purposes where the "name" string of
72  * a libsigrok error code is useful.
73  *
74  * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC.
75  * @return A const string containing the "name" of the error code as string.
76  *         The string must NOT be free'd by the caller!
77  * @see sr_strerror
78  */
79 SR_API const char *sr_strerror_name(int error_code)
80 {
81         const char *str;
82
83         /*
84          * Note: All defined SR_* error macros from libsigrok.h should have
85          * an entry in this function, as well as in sr_strerror().
86          */
87
88         switch (error_code) {
89         case SR_OK:
90                 str = "SR_OK";
91                 break;
92         case SR_ERR:
93                 str = "SR_ERR";
94                 break;
95         case SR_ERR_MALLOC:
96                 str = "SR_ERR_MALLOC";
97                 break;
98         case SR_ERR_BUG:
99                 str = "SR_ERR_BUG";
100                 break;
101         case SR_ERR_SAMPLERATE:
102                 str = "SR_ERR_SAMPLERATE";
103                 break;
104         default:
105                 str = "unknown error code";
106                 break;
107         }
108
109         return str;
110 }