]>
Commit | Line | Data |
---|---|---|
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 | * @file | |
25 | * | |
26 | * Error handling in libsigrok. | |
27 | */ | |
28 | ||
29 | /** | |
30 | * @defgroup grp_error Error handling | |
31 | * | |
32 | * Error handling in libsigrok. | |
33 | * | |
34 | * libsigrok functions usually return @ref SR_OK upon success, or a negative | |
35 | * error code on failure. | |
36 | * | |
37 | * @{ | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Return a human-readable error string for the given libsigrok error code. | |
42 | * | |
43 | * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC. | |
44 | * @return A const string containing a short, human-readable (English) | |
45 | * description of the error, such as "memory allocation error". | |
46 | * The string must NOT be free'd by the caller! | |
47 | * @see sr_strerror_name | |
48 | */ | |
49 | SR_API const char *sr_strerror(int error_code) | |
50 | { | |
51 | const char *str; | |
52 | ||
53 | /* | |
54 | * Note: All defined SR_* error macros from libsigrok.h should have | |
55 | * an entry in this function, as well as in sr_strerror_name(). | |
56 | */ | |
57 | ||
58 | switch (error_code) { | |
59 | case SR_OK: | |
60 | str = "no error"; | |
61 | break; | |
62 | case SR_ERR: | |
63 | str = "generic/unspecified error"; | |
64 | break; | |
65 | case SR_ERR_MALLOC: | |
66 | str = "memory allocation error"; | |
67 | break; | |
68 | case SR_ERR_BUG: | |
69 | str = "internal error"; | |
70 | break; | |
71 | case SR_ERR_SAMPLERATE: | |
72 | str = "invalid samplerate"; | |
73 | break; | |
74 | default: | |
75 | str = "unknown error"; | |
76 | break; | |
77 | } | |
78 | ||
79 | return str; | |
80 | } | |
81 | ||
82 | /** | |
83 | * Return the "name" string of the given libsigrok error code. | |
84 | * | |
85 | * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC", | |
86 | * the name of the SR_OK code is "SR_OK", and so on. | |
87 | * | |
88 | * This function can be used for various purposes where the "name" string of | |
89 | * a libsigrok error code is useful. | |
90 | * | |
91 | * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC. | |
92 | * @return A const string containing the "name" of the error code as string. | |
93 | * The string must NOT be free'd by the caller! | |
94 | * @see sr_strerror | |
95 | */ | |
96 | SR_API const char *sr_strerror_name(int error_code) | |
97 | { | |
98 | const char *str; | |
99 | ||
100 | /* | |
101 | * Note: All defined SR_* error macros from libsigrok.h should have | |
102 | * an entry in this function, as well as in sr_strerror(). | |
103 | */ | |
104 | ||
105 | switch (error_code) { | |
106 | case SR_OK: | |
107 | str = "SR_OK"; | |
108 | break; | |
109 | case SR_ERR: | |
110 | str = "SR_ERR"; | |
111 | break; | |
112 | case SR_ERR_MALLOC: | |
113 | str = "SR_ERR_MALLOC"; | |
114 | break; | |
115 | case SR_ERR_BUG: | |
116 | str = "SR_ERR_BUG"; | |
117 | break; | |
118 | case SR_ERR_SAMPLERATE: | |
119 | str = "SR_ERR_SAMPLERATE"; | |
120 | break; | |
121 | default: | |
122 | str = "unknown error code"; | |
123 | break; | |
124 | } | |
125 | ||
126 | return str; | |
127 | } | |
128 | ||
129 | /** @} */ |