| 1 | /* |
| 2 | Author: José Bollo <[email protected]> |
| 3 | Author: José Bollo <[email protected]> |
| 4 | |
| 5 | https://gitlab.com/jobol/mustach |
| 6 | |
| 7 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | you may not use this file except in compliance with the License. |
| 9 | You may obtain a copy of the License at |
| 10 | |
| 11 | http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | |
| 13 | Unless required by applicable law or agreed to in writing, software |
| 14 | distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | See the License for the specific language governing permissions and |
| 17 | limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #ifndef _mustach_h_included_ |
| 21 | #define _mustach_h_included_ |
| 22 | |
| 23 | /** |
| 24 | * mustach_itf - interface for callbacks |
| 25 | * |
| 26 | * All of this function should return a negative value to stop |
| 27 | * the mustache processing. The returned negative value will be |
| 28 | * then returned to the caller of mustach as is. |
| 29 | * |
| 30 | * The functions enter and next should return 0 or 1. |
| 31 | * |
| 32 | * All other functions should normally return 0. If it returns |
| 33 | * a negative value, it means an error that stop the process |
| 34 | * and that is reported to the caller. |
| 35 | * |
| 36 | * @start: Starts the mustach processing of the closure |
| 37 | * 'start' is optional (can be NULL) |
| 38 | * |
| 39 | * @put: Writes the value of 'name' to 'file' with 'escape' or not |
| 40 | * As an extension (see NO_ALLOW_EMPTY_TAG), the 'name' can be |
| 41 | * the empty string. In that later case an implemntation can |
| 42 | * return MUSTACH_ERROR_EMPTY_TAG to refuse empty names. |
| 43 | * |
| 44 | * @enter: Enters the section of 'name' if possible. |
| 45 | * Musts return 1 if entered or 0 if not entered. |
| 46 | * When 1 is returned, the function 'leave' will always be called. |
| 47 | * Conversely 'leave' is never called when enter returns 0 or |
| 48 | * a negative value. |
| 49 | * When 1 is returned, the function must activate the first |
| 50 | * item of the section. |
| 51 | * |
| 52 | * @next: Activates the next item of the section if it exists. |
| 53 | * Musts return 1 when the next item is activated. |
| 54 | * Musts return 0 when there is no item to activate. |
| 55 | * |
| 56 | * @leave: Leaves the last entered section |
| 57 | */ |
| 58 | struct mustach_itf { |
| 59 | int (*start)(void *closure); |
| 60 | int (*put)(void *closure, const char *name, int escape, FILE *file); |
| 61 | int (*enter)(void *closure, const char *name); |
| 62 | int (*next)(void *closure); |
| 63 | int (*leave)(void *closure); |
| 64 | }; |
| 65 | |
| 66 | #define MUSTACH_OK 0 |
| 67 | #define MUSTACH_ERROR_SYSTEM -1 |
| 68 | #define MUSTACH_ERROR_UNEXPECTED_END -2 |
| 69 | #define MUSTACH_ERROR_EMPTY_TAG -3 |
| 70 | #define MUSTACH_ERROR_TAG_TOO_LONG -4 |
| 71 | #define MUSTACH_ERROR_BAD_SEPARATORS -5 |
| 72 | #define MUSTACH_ERROR_TOO_DEEP -6 |
| 73 | #define MUSTACH_ERROR_CLOSING -7 |
| 74 | #define MUSTACH_ERROR_BAD_UNESCAPE_TAG -8 |
| 75 | |
| 76 | /* compatibility with older bad name */ |
| 77 | #define MUSTACH_ERROR_TOO_DEPTH MUSTACH_ERROR_TOO_DEEP |
| 78 | |
| 79 | /** |
| 80 | * fmustach - Renders the mustache 'template' in 'file' for 'itf' and 'closure'. |
| 81 | * |
| 82 | * @template: the template string to instanciate |
| 83 | * @itf: the interface to the functions that mustach calls |
| 84 | * @closure: the closure to pass to functions called |
| 85 | * @file: the file where to write the result |
| 86 | * |
| 87 | * Returns 0 in case of success, -1 with errno set in case of system error |
| 88 | * a other negative value in case of error. |
| 89 | */ |
| 90 | extern int fmustach(const char *template, struct mustach_itf *itf, void *closure, FILE *file); |
| 91 | |
| 92 | /** |
| 93 | * fmustach - Renders the mustache 'template' in 'fd' for 'itf' and 'closure'. |
| 94 | * |
| 95 | * @template: the template string to instanciate |
| 96 | * @itf: the interface to the functions that mustach calls |
| 97 | * @closure: the closure to pass to functions called |
| 98 | * @fd: the file descriptor number where to write the result |
| 99 | * |
| 100 | * Returns 0 in case of success, -1 with errno set in case of system error |
| 101 | * a other negative value in case of error. |
| 102 | */ |
| 103 | extern int fdmustach(const char *template, struct mustach_itf *itf, void *closure, int fd); |
| 104 | |
| 105 | /** |
| 106 | * fmustach - Renders the mustache 'template' in 'result' for 'itf' and 'closure'. |
| 107 | * |
| 108 | * @template: the template string to instanciate |
| 109 | * @itf: the interface to the functions that mustach calls |
| 110 | * @closure: the closure to pass to functions called |
| 111 | * @result: the pointer receiving the result when 0 is returned |
| 112 | * @size: the size of the returned result |
| 113 | * |
| 114 | * Returns 0 in case of success, -1 with errno set in case of system error |
| 115 | * a other negative value in case of error. |
| 116 | */ |
| 117 | extern int mustach(const char *template, struct mustach_itf *itf, void *closure, char **result, size_t *size); |
| 118 | |
| 119 | #endif |
| 120 |