diff --git a/contrib/backends/nntpchan-daemon/src/mustach.h b/contrib/backends/nntpchan-daemon/src/mustach.h deleted file mode 100644 index 8196679..0000000 --- a/contrib/backends/nntpchan-daemon/src/mustach.h +++ /dev/null @@ -1,112 +0,0 @@ -/* - Author: José Bollo - Author: José Bollo - - https://gitlab.com/jobol/mustach - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -#ifndef _mustach_h_included_ -#define _mustach_h_included_ - -/** - * mustach_itf - interface for callbacks - * - * All of this function should return a negative value to stop - * the mustache processing. The returned negative value will be - * then returned to the caller of mustach as is. - * - * The functions enter and next should return 0 or 1. - * - * All other functions should normally return 0. - * - * @start: Starts the mustach processing of the closure - * 'start' is optional (can be NULL) - * - * @put: Writes the value of 'name' to 'file' with 'escape' or not - * - * @enter: Enters the section of 'name' if possible. - * Musts return 1 if entered or 0 if not entered. - * When 1 is returned, the function 'leave' will always be called. - * Conversely 'leave' is never called when enter returns 0 or - * a negative value. - * When 1 is returned, the function must activate the first - * item of the section. - * - * @next: Activates the next item of the section if it exists. - * Musts return 1 when the next item is activated. - * Musts return 0 when there is no item to activate. - * - * @leave: Leaves the last entered section - */ -struct mustach_itf { - int (*start)(void *closure); - int (*put)(void *closure, const char *name, int escape, FILE *file); - int (*enter)(void *closure, const char *name); - int (*next)(void *closure); - int (*leave)(void *closure); -}; - -#define MUSTACH_OK 0 -#define MUSTACH_ERROR_SYSTEM -1 -#define MUSTACH_ERROR_UNEXPECTED_END -2 -#define MUSTACH_ERROR_EMPTY_TAG -3 -#define MUSTACH_ERROR_TAG_TOO_LONG -4 -#define MUSTACH_ERROR_BAD_SEPARATORS -5 -#define MUSTACH_ERROR_TOO_DEPTH -6 -#define MUSTACH_ERROR_CLOSING -7 -#define MUSTACH_ERROR_BAD_UNESCAPE_TAG -8 - -/** - * fmustach - Renders the mustache 'template' in 'file' for 'itf' and 'closure'. - * - * @template: the template string to instanciate - * @itf: the interface to the functions that mustach calls - * @closure: the closure to pass to functions called - * @file: the file where to write the result - * - * Returns 0 in case of success, -1 with errno set in case of system error - * a other negative value in case of error. - */ -extern int fmustach(const char *template, struct mustach_itf *itf, void *closure, FILE *file); - -/** - * fmustach - Renders the mustache 'template' in 'fd' for 'itf' and 'closure'. - * - * @template: the template string to instanciate - * @itf: the interface to the functions that mustach calls - * @closure: the closure to pass to functions called - * @fd: the file descriptor number where to write the result - * - * Returns 0 in case of success, -1 with errno set in case of system error - * a other negative value in case of error. - */ -extern int fdmustach(const char *template, struct mustach_itf *itf, void *closure, int fd); - -/** - * fmustach - Renders the mustache 'template' in 'result' for 'itf' and 'closure'. - * - * @template: the template string to instanciate - * @itf: the interface to the functions that mustach calls - * @closure: the closure to pass to functions called - * @result: the pointer receiving the result when 0 is returned - * @size: the size of the returned result - * - * Returns 0 in case of success, -1 with errno set in case of system error - * a other negative value in case of error. - */ -extern int mustach(const char *template, struct mustach_itf *itf, void *closure, char **result, size_t *size); - -#endif - diff --git a/contrib/backends/nntpchan-daemon/src/mustach.c b/contrib/backends/nntpchan-daemon/src/mustache.cpp similarity index 85% rename from contrib/backends/nntpchan-daemon/src/mustach.c rename to contrib/backends/nntpchan-daemon/src/mustache.cpp index 03f3cb1..0bf86a3 100644 --- a/contrib/backends/nntpchan-daemon/src/mustach.c +++ b/contrib/backends/nntpchan-daemon/src/mustache.cpp @@ -17,19 +17,23 @@ limitations under the License. */ -#define _GNU_SOURCE - #include #include #include #include #include -#include "mustach.h" +#include "mustache.hpp" #define NAME_LENGTH_MAX 1024 #define DEPTH_MAX 256 +namespace nntpchan +{ +namespace mustache +{ + + static int getpartial(struct mustach_itf *itf, void *closure, const char *name, char **result) { int rc; @@ -54,7 +58,7 @@ static int getpartial(struct mustach_itf *itf, void *closure, const char *name, return rc; } -static int process(const char *template, struct mustach_itf *itf, void *closure, FILE *file, const char *opstr, const char *clstr) +static int process(const char *templ, struct mustach_itf *itf, void *closure, FILE *file, const char *opstr, const char *clstr) { char name[NAME_LENGTH_MAX + 1], *partial, c; const char *beg, *term; @@ -67,20 +71,20 @@ static int process(const char *template, struct mustach_itf *itf, void *closure, cllen = strlen(clstr); depth = 0; for(;;) { - beg = strstr(template, opstr); + beg = strstr(templ, opstr); if (beg == NULL) { /* no more mustach */ if (emit) - fwrite(template, strlen(template), 1, file); + fwrite(templ, strlen(templ), 1, file); return depth ? MUSTACH_ERROR_UNEXPECTED_END : 0; } if (emit) - fwrite(template, (size_t)(beg - template), 1, file); + fwrite(templ, (size_t)(beg - templ), 1, file); beg += oplen; term = strstr(beg, clstr); if (term == NULL) return MUSTACH_ERROR_UNEXPECTED_END; - template = term + cllen; + templ = term + cllen; len = (size_t)(term - beg); c = *beg; switch(c) { @@ -96,7 +100,7 @@ static int process(const char *template, struct mustach_itf *itf, void *closure, } else { if (term[l] != '}') return MUSTACH_ERROR_BAD_UNESCAPE_TAG; - template++; + templ++; } c = '&'; case '^': @@ -153,7 +157,7 @@ static int process(const char *template, struct mustach_itf *itf, void *closure, return rc; } stack[depth].name = beg; - stack[depth].again = template; + stack[depth].again = templ; stack[depth].length = len; stack[depth].emit = emit; stack[depth].entered = rc; @@ -169,7 +173,7 @@ static int process(const char *template, struct mustach_itf *itf, void *closure, if (rc < 0) return rc; if (rc) { - template = stack[depth++].again; + templ = stack[depth++].again; } else { emit = stack[depth].emit; if (emit && stack[depth].entered) @@ -200,15 +204,15 @@ static int process(const char *template, struct mustach_itf *itf, void *closure, } } -int fmustach(const char *template, struct mustach_itf *itf, void *closure, FILE *file) +int fmustach(const char *templ, struct mustach_itf *itf, void *closure, FILE *file) { int rc = itf->start ? itf->start(closure) : 0; if (rc == 0) - rc = process(template, itf, closure, file, "{{", "}}"); + rc = process(templ, itf, closure, file, "{{", "}}"); return rc; } -int fdmustach(const char *template, struct mustach_itf *itf, void *closure, int fd) +int fdmustach(const char *templ, struct mustach_itf *itf, void *closure, int fd) { int rc; FILE *file; @@ -218,13 +222,13 @@ int fdmustach(const char *template, struct mustach_itf *itf, void *closure, int rc = MUSTACH_ERROR_SYSTEM; errno = ENOMEM; } else { - rc = fmustach(template, itf, closure, file); + rc = fmustach(templ, itf, closure, file); fclose(file); } return rc; } -int mustach(const char *template, struct mustach_itf *itf, void *closure, char **result, size_t *size) +int mustach(const char *templ, struct mustach_itf *itf, void *closure, char **result, size_t *size) { int rc; FILE *file; @@ -238,7 +242,7 @@ int mustach(const char *template, struct mustach_itf *itf, void *closure, char * rc = MUSTACH_ERROR_SYSTEM; errno = ENOMEM; } else { - rc = fmustach(template, itf, closure, file); + rc = fmustach(templ, itf, closure, file); if (rc == 0) /* adds terminating null */ rc = fputc(0, file) ? MUSTACH_ERROR_SYSTEM : 0; @@ -254,4 +258,5 @@ int mustach(const char *template, struct mustach_itf *itf, void *closure, char * } return rc; } - +} +} diff --git a/contrib/backends/nntpchan-daemon/src/mustache.hpp b/contrib/backends/nntpchan-daemon/src/mustache.hpp new file mode 100644 index 0000000..68d4277 --- /dev/null +++ b/contrib/backends/nntpchan-daemon/src/mustache.hpp @@ -0,0 +1,96 @@ +#ifndef NNTPCHAN_MUSTACHE +#define NNTPCHAN_MUSTACHE + +/* + Author: José Bollo + Author: José Bollo + + https://gitlab.com/jobol/mustach + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + + + +#define MUSTACH_OK 0 +#define MUSTACH_ERROR_SYSTEM -1 +#define MUSTACH_ERROR_UNEXPECTED_END -2 +#define MUSTACH_ERROR_EMPTY_TAG -3 +#define MUSTACH_ERROR_TAG_TOO_LONG -4 +#define MUSTACH_ERROR_BAD_SEPARATORS -5 +#define MUSTACH_ERROR_TOO_DEPTH -6 +#define MUSTACH_ERROR_CLOSING -7 +#define MUSTACH_ERROR_BAD_UNESCAPE_TAG -8 + +#include + + +namespace nntpchan +{ + namespace mustache + { + + /** + * mustach_itf - interface for callbacks + * + * All of this function should return a negative value to stop + * the mustache processing. The returned negative value will be + * then returned to the caller of mustach as is. + * + * The functions enter and next should return 0 or 1. + * + * All other functions should normally return 0. + * + * @start: Starts the mustach processing of the closure + * 'start' is optional (can be NULL) + * + * @put: Writes the value of 'name' to 'file' with 'escape' or not + * + * @enter: Enters the section of 'name' if possible. + * Musts return 1 if entered or 0 if not entered. + * When 1 is returned, the function 'leave' will always be called. + * Conversely 'leave' is never called when enter returns 0 or + * a negative value. + * When 1 is returned, the function must activate the first + * item of the section. + * + * @next: Activates the next item of the section if it exists. + * Musts return 1 when the next item is activated. + * Musts return 0 when there is no item to activate. + * + * @leave: Leaves the last entered section + */ + struct mustach_itf { + int (*start)(void *closure); + int (*put)(void *closure, const char *name, int escape, FILE *file); + int (*enter)(void *closure, const char *name); + int (*next)(void *closure); + int (*leave)(void *closure); + }; + /** + * fmustach - Renders the mustache 'template' in 'file' for 'itf' and 'closure'. + * + * @template: the template string to instanciate + * @itf: the interface to the functions that mustach calls + * @closure: the closure to pass to functions called + * @file: the file where to write the result + * + * Returns 0 in case of success, -1 with errno set in case of system error + * a other negative value in case of error. + */ + int fmustach(const char *templ, struct mustach_itf *itf, void *closure, FILE *file); + + } +} + +#endif