summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Buchanan <jonathan.russ.buchanan@gmail.com>2020-07-22 01:06:20 -0400
committerJonathan Buchanan <jonathan.russ.buchanan@gmail.com>2020-07-22 01:06:20 -0400
commit007b2a19f8d9bdf926aa584d85beb5bdac32bf9d (patch)
treeb0a7df56e904d7547a90fe57c474510f7b4bcc44
parent05d427aafa7f9e637db969e4933905be2a8d9b9c (diff)
generate c function for looking up ec hints
-rw-r--r--gnu-taler-error-codes/.gitignore5
-rw-r--r--gnu-taler-error-codes/Makefile16
-rw-r--r--gnu-taler-error-codes/c-length.template9
-rw-r--r--gnu-taler-error-codes/c.footer33
-rw-r--r--gnu-taler-error-codes/c.header47
-rw-r--r--gnu-taler-error-codes/c.template5
-rw-r--r--gnu-taler-error-codes/h.footer10
-rw-r--r--gnu-taler-error-codes/registry.rec4
8 files changed, 127 insertions, 2 deletions
diff --git a/gnu-taler-error-codes/.gitignore b/gnu-taler-error-codes/.gitignore
index 9111c0f..4331786 100644
--- a/gnu-taler-error-codes/.gitignore
+++ b/gnu-taler-error-codes/.gitignore
@@ -1,8 +1,13 @@
taler-error-codes.h
+taler-error-codes.c
taler-error-codes.ts
taler-error-codes.kt
combined.tmp
+combined-escaped.tmp
+length.tmp
taler-error-codes.h.tmp
+taler-error-codes.c.tmp
+taler-error-codes-length.c.tmp
taler-error-codes.ts.tmp
taler-error-codes.kt.tmp
diff --git a/gnu-taler-error-codes/Makefile b/gnu-taler-error-codes/Makefile
index c0bb208..2837753 100644
--- a/gnu-taler-error-codes/Makefile
+++ b/gnu-taler-error-codes/Makefile
@@ -1,4 +1,5 @@
FILES=taler-error-codes.h \
+ taler-error-codes.c \
taler-error-codes.ts \
taler-error-codes.kt
all: check $(FILES)
@@ -17,12 +18,27 @@ prep:
combined.tmp: registry.rec prep
recsel -t TalerErrorCode -j HttpStatus -p Description,Name,Value,HttpStatus,HttpStatus.Value,HttpStatus.Identifier ../http-status-codes/registry.rec registry.rec > $@
+combined-escaped.tmp: combined.tmp
+ sed 's/"/\\"/g' $^ > $@
+
+length.tmp: combined.tmp
+ recsel -p "Count(Description):ECS_LENGTH" combined.tmp > $@
+
taler-error-codes.h.tmp: combined.tmp h.template
../format.sh h.template < combined.tmp > $@
taler-error-codes.h: h.header taler-error-codes.h.tmp h.footer
cat $^ > $@
+taler-error-codes.c.tmp: combined-escaped.tmp c.template
+ ../format.sh c.template < combined-escaped.tmp > $@
+
+taler-error-codes-length.c.tmp: length.tmp c-length.template
+ ../format.sh c-length.template < length.tmp > $@
+
+taler-error-codes.c: c.header taler-error-codes.c.tmp taler-error-codes-length.c.tmp c.footer
+ cat $^ > $@
+
taler-error-codes.ts.tmp: combined.tmp ts.template
../format.sh ts.template < combined.tmp > $@
diff --git a/gnu-taler-error-codes/c-length.template b/gnu-taler-error-codes/c-length.template
new file mode 100644
index 0000000..24e3e41
--- /dev/null
+++ b/gnu-taler-error-codes/c-length.template
@@ -0,0 +1,9 @@
+
+
+};
+
+
+/**
+ * The length of @e code_hint_pairs.
+ */
+static const unsigned int code_hint_pairs_length = {{ECS_LENGTH}};
diff --git a/gnu-taler-error-codes/c.footer b/gnu-taler-error-codes/c.footer
new file mode 100644
index 0000000..c348200
--- /dev/null
+++ b/gnu-taler-error-codes/c.footer
@@ -0,0 +1,33 @@
+
+
+
+/**
+ * Returns a hint for a given error code.
+ *
+ * @param ec the error code.
+ * @return the hint if it could be found, otherwise NULL.
+ */
+const char *
+TALER_ErrorCode_get_hint (enum TALER_ErrorCode ec)
+{
+ unsigned int lower = 0;
+ unsigned int upper = code_hint_pairs_length - 1;
+ unsigned int mid = upper / 2;
+ while (lower <= upper)
+ {
+ mid = (upper + lower) / 2;
+ if (code_hint_pairs[mid].ec < ec)
+ {
+ lower = mid + 1;
+ }
+ else if (code_hint_pairs[mid].ec > ec)
+ {
+ upper = mid - 1;
+ }
+ else
+ {
+ return code_hint_pairs[mid].hint;
+ }
+ }
+ return NULL;
+}
diff --git a/gnu-taler-error-codes/c.header b/gnu-taler-error-codes/c.header
new file mode 100644
index 0000000..434bcee
--- /dev/null
+++ b/gnu-taler-error-codes/c.header
@@ -0,0 +1,47 @@
+/*
+ This file is part of GNU Taler
+ Copyright (C) 2012-2020 Taler Systems SA
+
+ GNU Taler is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
+
+ GNU Taler 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
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ SPDX-License-Identifier: LGPL3.0-or-later
+
+ Note: the LGPL does not apply to all components of GNU Taler,
+ but it does apply to this file.
+ */
+#include "taler-error-codes.h"
+#include <stddef.h>
+
+
+/**
+ * A pair containing an error code and its hint.
+ */
+struct ErrorCodeAndHint
+{
+ /**
+ * The error code.
+ */
+ enum TALER_ErrorCode ec;
+
+ /**
+ * The hint.
+ */
+ const char *hint;
+};
+
+
+/**
+ * The list of all error codes with their hints.
+ */
+static const struct ErrorCodeAndHint code_hint_pairs[] = {
diff --git a/gnu-taler-error-codes/c.template b/gnu-taler-error-codes/c.template
new file mode 100644
index 0000000..02c39ed
--- /dev/null
+++ b/gnu-taler-error-codes/c.template
@@ -0,0 +1,5 @@
+
+ {
+ .ec = TALER_EC_{{Name}},
+ .hint = "{{Description}}"
+ },
diff --git a/gnu-taler-error-codes/h.footer b/gnu-taler-error-codes/h.footer
index 5d32ee9..1357a9e 100644
--- a/gnu-taler-error-codes/h.footer
+++ b/gnu-taler-error-codes/h.footer
@@ -3,6 +3,16 @@
};
+/**
+ * Returns a hint for a given error code.
+ *
+ * @param ec the error code.
+ * @return the hint if it could be found, otherwise NULL.
+ */
+const char *
+TALER_ErrorCode_get_hint (enum TALER_ErrorCode ec);
+
+
#if 0 /* keep Emacsens' auto-indent happy */
{
#endif
diff --git a/gnu-taler-error-codes/registry.rec b/gnu-taler-error-codes/registry.rec
index 5fb2954..e7ca9e1 100644
--- a/gnu-taler-error-codes/registry.rec
+++ b/gnu-taler-error-codes/registry.rec
@@ -74,7 +74,7 @@ HttpStatus: 0
Value: 10
Name: ENDPOINT_UNKNOWN
-Description: There is no endpoint defined for the URL provided by the client (
+Description: There is no endpoint defined for the URL provided by the client.
HttpStatus: 404
Value: 11
@@ -84,7 +84,7 @@ HttpStatus: 414
Value: 12
Name: WRONG_NUMBER_OF_SEGMENTS
-Description: The number of segments included in the URI does not match the number of segments expected by the endpoint. (
+Description: The number of segments included in the URI does not match the number of segments expected by the endpoint.
HttpStatus: 404
Value: 13