]> sigrok.org Git - libsigrokdecode.git/commitdiff
Add srd_strerror() and srd_strerror_name().
authorUwe Hermann <redacted>
Fri, 3 May 2013 21:28:10 +0000 (23:28 +0200)
committerUwe Hermann <redacted>
Sat, 4 May 2013 10:05:49 +0000 (12:05 +0200)
Makefile.am
NEWS
error.c [new file with mode: 0644]
libsigrokdecode.h.in

index d3f91b89feaea549d6027619064c60562f4c4194..15b5310d6ef3f2c3108f4bfdc8c62287149377ef 100644 (file)
@@ -25,7 +25,7 @@ SUBDIRS = contrib decoders
 lib_LTLIBRARIES = libsigrokdecode.la
 
 libsigrokdecode_la_SOURCES = controller.c decoder.c log.c util.c exception.c \
 lib_LTLIBRARIES = libsigrokdecode.la
 
 libsigrokdecode_la_SOURCES = controller.c decoder.c log.c util.c exception.c \
-       module_sigrokdecode.c type_decoder.c type_logic.c version.c
+       module_sigrokdecode.c type_decoder.c type_logic.c version.c error.c
 
 libsigrokdecode_la_CPPFLAGS = $(CPPFLAGS_PYTHON) \
                              -DDECODERS_DIR='"$(DECODERS_DIR)"'
 
 libsigrokdecode_la_CPPFLAGS = $(CPPFLAGS_PYTHON) \
                              -DDECODERS_DIR='"$(DECODERS_DIR)"'
diff --git a/NEWS b/NEWS
index 101b16e24377ba7cd1928c64d0fd314dc702981e..09cd51fdf8ad16dc4ed0d7491e6253e2df414f76 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -7,7 +7,10 @@
    - Dallas DS1307 RTC
  * Library: Decoders now expose their options via the GSList *options field
    in struct srd_decoder.
    - Dallas DS1307 RTC
  * Library: Decoders now expose their options via the GSList *options field
    in struct srd_decoder.
- * Various API documentation fixes.
+ * API related changes:
+   - Various API documentation fixes.
+   - srd_decoder_list() now returns 'const GSList *' instead of 'GSList *'.
+   - Added new srd_strerror() and srd_strerror_name() API calls.
  * Added support for optional probes for PDs.
 
 0.1.1 (2013-01-27)
  * Added support for optional probes for PDs.
 
 0.1.1 (2013-01-27)
diff --git a/error.c b/error.c
new file mode 100644 (file)
index 0000000..e6abd4b
--- /dev/null
+++ b/error.c
@@ -0,0 +1,152 @@
+/*
+ * This file is part of the libsigrokdecode project.
+ *
+ * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * 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 "libsigrokdecode.h"
+
+/**
+ * @file
+ *
+ * Error handling in libsigrokdecode.
+ */
+
+/**
+ * @defgroup grp_error Error handling
+ *
+ * Error handling in libsigrokdecode.
+ *
+ * libsigrokdecode functions usually return @ref SRD_OK upon success, or a
+ * negative error code on failure.
+ *
+ * @{
+ */
+
+/**
+ * Return a human-readable error string for the given libsigrokdecode error
+ * code.
+ *
+ * @param error_code A libsigrokdecode error code number, such as
+ *                   SRD_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 srd_strerror_name
+ *
+ * @since 0.2.0
+ */
+SRD_API const char *srd_strerror(int error_code)
+{
+       const char *str;
+
+       /*
+        * Note: All defined SRD_* error macros from libsigrokdecode.h must
+        * have an entry in this function, as well as in srd_strerror_name().
+        */
+
+       switch (error_code) {
+       case SRD_OK:
+               str = "no error";
+               break;
+       case SRD_ERR:
+               str = "generic/unspecified error";
+               break;
+       case SRD_ERR_MALLOC:
+               str = "memory allocation error";
+               break;
+       case SRD_ERR_ARG:
+               str = "invalid argument";
+               break;
+       case SRD_ERR_BUG:
+               str = "internal error";
+               break;
+       case SRD_ERR_PYTHON:
+               str = "Python API error";
+               break;
+       case SRD_ERR_DECODERS_DIR:
+               str = "decoders directory access error";
+               break;
+       default:
+               str = "unknown error";
+               break;
+       }
+
+       return str;
+}
+
+/**
+ * Return the "name" string of the given libsigrokdecode error code.
+ *
+ * For example, the "name" of the SRD_ERR_MALLOC error code is
+ * "SRD_ERR_MALLOC", the name of the SRD_OK code is "SRD_OK", and so on.
+ *
+ * This function can be used for various purposes where the "name" string of
+ * a libsigrokdecode error code is useful.
+ *
+ * @param error_code A libsigrokdecode error code number, such as
+ *                   SRD_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 srd_strerror
+ *
+ * @since 0.2.0
+ */
+SRD_API const char *srd_strerror_name(int error_code)
+{
+       const char *str;
+
+       /*
+        * Note: All defined SRD_* error macros from libsigrokdecode.h must
+        * have an entry in this function, as well as in srd_strerror().
+        */
+
+       switch (error_code) {
+       case SRD_OK:
+               str = "SRD_OK";
+               break;
+       case SRD_ERR:
+               str = "SRD_ERR";
+               break;
+       case SRD_ERR_MALLOC:
+               str = "SRD_ERR_MALLOC";
+               break;
+       case SRD_ERR_ARG:
+               str = "SRD_ERR_ARG";
+               break;
+       case SRD_ERR_BUG:
+               str = "SRD_ERR_BUG";
+               break;
+       case SRD_ERR_PYTHON:
+               str = "SRD_ERR_PYTHON";
+               break;
+       case SRD_ERR_DECODERS_DIR:
+               str = "SRD_ERR_DECODERS_DIR";
+               break;
+       default:
+               str = "unknown error code";
+               break;
+       }
+
+       return str;
+}
+
+/** @} */
index 877aeb15401d45e8f44e12f0098647fa16bb7963..c18e2c4e6aec0e4df423c180b462f2791ac0e1a8 100644 (file)
@@ -323,6 +323,11 @@ SRD_API int srd_lib_version_revision_get(void);
 SRD_API int srd_lib_version_age_get(void);
 SRD_API const char *srd_lib_version_string_get(void);
 
 SRD_API int srd_lib_version_age_get(void);
 SRD_API const char *srd_lib_version_string_get(void);
 
+/*--- error.c ---------------------------------------------------------------*/
+
+SRD_API const char *srd_strerror(int error_code);
+SRD_API const char *srd_strerror_name(int error_code);
+
 #ifdef __cplusplus
 }
 #endif
 #ifdef __cplusplus
 }
 #endif