From 007b2a19f8d9bdf926aa584d85beb5bdac32bf9d Mon Sep 17 00:00:00 2001 From: Jonathan Buchanan Date: Wed, 22 Jul 2020 01:06:20 -0400 Subject: generate c function for looking up ec hints --- gnu-taler-error-codes/.gitignore | 5 ++++ gnu-taler-error-codes/Makefile | 16 +++++++++++ gnu-taler-error-codes/c-length.template | 9 +++++++ gnu-taler-error-codes/c.footer | 33 +++++++++++++++++++++++ gnu-taler-error-codes/c.header | 47 +++++++++++++++++++++++++++++++++ gnu-taler-error-codes/c.template | 5 ++++ gnu-taler-error-codes/h.footer | 10 +++++++ gnu-taler-error-codes/registry.rec | 4 +-- 8 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 gnu-taler-error-codes/c-length.template create mode 100644 gnu-taler-error-codes/c.footer create mode 100644 gnu-taler-error-codes/c.header create mode 100644 gnu-taler-error-codes/c.template (limited to 'gnu-taler-error-codes') 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 . + + 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 + + +/** + * 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 -- cgit v1.2.3