From: Uwe Hermann Date: Thu, 12 Jul 2012 19:30:49 +0000 (+0200) Subject: sr: Add sr_strerror() and sr_strerror_name(). X-Git-Tag: dsupstream~864 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=b5a8e848256f54b32ca988be5a1ec2db5048873a;p=libsigrok.git sr: Add sr_strerror() and sr_strerror_name(). --- diff --git a/Makefile.am b/Makefile.am index f37c2e5d..e2ccabb6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -37,6 +37,7 @@ libsigrok_la_SOURCES = \ strutil.c \ log.c \ version.c + error.c libsigrok_la_LIBADD = \ $(LIBOBJS) \ diff --git a/error.c b/error.c new file mode 100644 index 00000000..d0697bc8 --- /dev/null +++ b/error.c @@ -0,0 +1,110 @@ +/* + * This file is part of the sigrok project. + * + * Copyright (C) 2012 Uwe Hermann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libsigrok.h" + +/** + * Return a human-readable error string for the given libsigrok error code. + * + * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC. + * @return A const string containing a short, human-readable (English) + * description of the error, such as "memory allocation error". + * The string must NOT be free'd by the caller! + * @see sr_strerror_name + */ +SR_API const char *sr_strerror(int error_code) +{ + const char *str; + + /* + * Note: All defined SR_* error macros from libsigrok.h should have + * an entry in this function, as well as in sr_strerror_name(). + */ + + switch (error_code) { + case SR_OK: + str = "no error"; + break; + case SR_ERR: + str = "generic/unspecified error"; + break; + case SR_ERR_MALLOC: + str = "memory allocation error"; + break; + case SR_ERR_BUG: + str = "internal error"; + break; + case SR_ERR_SAMPLERATE: + str = "invalid samplerate"; + break; + default: + str = "unknown error"; + break; + } + + return str; +} + +/** + * Return the "name" string of the given libsigrok error code. + * + * For example, the "name" of the SR_ERR_MALLOC error code is "SR_ERR_MALLOC", + * the name of the SR_OK code is "SR_OK", and so on. + * + * This function can be used for various purposes where the "name" string of + * a libsigrok error code is useful. + * + * @param error_code A libsigrok error code number, such as SR_ERR_MALLOC. + * @return A const string containing the "name" of the error code as string. + * The string must NOT be free'd by the caller! + * @see sr_strerror + */ +SR_API const char *sr_strerror_name(int error_code) +{ + const char *str; + + /* + * Note: All defined SR_* error macros from libsigrok.h should have + * an entry in this function, as well as in sr_strerror(). + */ + + switch (error_code) { + case SR_OK: + str = "SR_OK"; + break; + case SR_ERR: + str = "SR_ERR"; + break; + case SR_ERR_MALLOC: + str = "SR_ERR_MALLOC"; + break; + case SR_ERR_BUG: + str = "SR_ERR_BUG"; + break; + case SR_ERR_SAMPLERATE: + str = "SR_ERR_SAMPLERATE"; + break; + default: + str = "unknown error code"; + break; + } + + return str; +} diff --git a/proto.h b/proto.h index a00c6ba7..cb5c211c 100644 --- a/proto.h +++ b/proto.h @@ -141,4 +141,9 @@ SR_API int sr_lib_version_revision_get(void); SR_API int sr_lib_version_age_get(void); SR_API const char *sr_lib_version_string_get(void); +/*--- error.c ---------------------------------------------------------------*/ + +SR_API const char *sr_strerror(int error_code); +SR_API const char *sr_strerror_name(int error_code); + #endif