UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2025 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef STRINGREPLACE_H 30 #define STRINGREPLACE_H 31 32 #include "../public/nsapi.h" 33 34 #include <cx/string.h> 35 #include <cx/buffer.h> 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 typedef enum StringSegmentType { 42 /* 43 * Static string segment 44 */ 45 STRING_SEGMENT_STR = 0, 46 /* 47 * Placeholder referenced by number, e.g., $1, $2, ... 48 */ 49 STRING_SEGMENT_NUM_PLACEHOLDER, 50 /* 51 * Placeholder referenced by name, e.g., ${varname} or $varname 52 */ 53 STRING_SEGMENT_VAR_PLACEHOLDER 54 } StringSegmentType; 55 56 typedef struct StringTemplateSegment StringTemplateSegment; 57 typedef struct StringTemplate StringTemplate; 58 59 struct StringTemplate { 60 const CxAllocator *a; 61 StringTemplateSegment *segments; 62 }; 63 64 struct StringTemplateSegment { 65 /* 66 * STRING_SEGMENT_STR: static string segment 67 * STRING_SEGMENT_NUM_PLACEHOLDER: null 68 * STRING_SEGMENT_VAR_PLACEHOLDER: variable name 69 */ 70 cxmutstr str; 71 72 /* 73 * Segment type 74 */ 75 StringSegmentType type; 76 77 /* 78 * reference number if type is STRING_SEGMENT_NUM_PLACEHOLDER 79 */ 80 int num; 81 82 /* 83 * Next segment 84 */ 85 StringTemplateSegment *next; 86 }; 87 88 /* 89 * Callback for converting a var segment into a string 90 * 91 * a: The allocator passed to string_template_write_to 92 * seg: Placeholder segment 93 * userdata: The userdata pointer passed to string_template_write_to 94 * free_str: If set to true, the returned string pointer will be freed 95 * using the allocator's free function. 96 */ 97 typedef cxmutstr (*strtpl_var_func)(const CxAllocator *a, StringTemplateSegment *seg, void *userdata, WSBool *free_str); 98 99 /* 100 * Compiles a string template 101 * 102 * a: The allocator to use for building the compiled template 103 * tpl: Semplate string 104 */ 105 StringTemplate* string_template_compile(const CxAllocator *a, cxstring tpl); 106 107 /* 108 * Builds a string using the provided template and writes it to the stream. 109 * 110 * tpl: Template 111 * a: aThe allocator passed to the strtpl_var_func callback 112 * varfunc: The callback used for converting 113 * STRING_SEGMENT_NUM_PLACEHOLDER and STRING_SEGMENT_VAR_PLACEHOLDER segments 114 * userdata: The userdata pointer passed to the strtpl_var_func callback 115 * stream: The stream object to which the resulting string should be written 116 * writef: Stream write function 117 * 118 * returns the number of written bytes or -1 on error 119 */ 120 ssize_t string_template_write_to(StringTemplate *tpl, const CxAllocator *a, strtpl_var_func varfunc, void *userdata, void *stream, cx_write_func writef); 121 122 /* 123 * Builds a string, using the provided template and allocator 124 */ 125 cxmutstr string_template_build_string(StringTemplate *tpl, const CxAllocator *a, strtpl_var_func varfunc, void *userdata); 126 127 void string_template_free(StringTemplate *tpl); 128 129 #ifdef __cplusplus 130 } 131 #endif 132 133 #endif /* STRINGREPLACE_H */ 134 135