| |
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 <cx/string.h> |
| |
33 #include <cx/buffer.h> |
| |
34 |
| |
35 #ifdef __cplusplus |
| |
36 extern "C" { |
| |
37 #endif |
| |
38 |
| |
39 typedef enum StringSegmentType { |
| |
40 /* |
| |
41 * Static string segment |
| |
42 */ |
| |
43 STRING_SEGMENT_STR = 0, |
| |
44 /* |
| |
45 * Placeholder referenced by number, e.g., $1, $2, ... |
| |
46 */ |
| |
47 STRING_SEGMENT_NUM_PLACEHOLDER, |
| |
48 /* |
| |
49 * Placeholder referenced by name, e.g., ${varname} or $varname |
| |
50 */ |
| |
51 STRING_SEGMENT_VAR_PLACEHOLDER |
| |
52 } StringSegmentType; |
| |
53 |
| |
54 typedef struct StringTemplateSegment StringTemplateSegment; |
| |
55 typedef struct StringTemplate StringTemplate; |
| |
56 |
| |
57 struct StringTemplate { |
| |
58 const CxAllocator *a; |
| |
59 StringTemplateSegment *segments; |
| |
60 }; |
| |
61 |
| |
62 struct StringTemplateSegment { |
| |
63 /* |
| |
64 * STRING_SEGMENT_STR: static string segment |
| |
65 * STRING_SEGMENT_NUM_PLACEHOLDER: null |
| |
66 * STRING_SEGMENT_VAR_PLACEHOLDER: variable name |
| |
67 */ |
| |
68 cxstring str; |
| |
69 |
| |
70 /* |
| |
71 * Segment type |
| |
72 */ |
| |
73 StringSegmentType type; |
| |
74 |
| |
75 /* |
| |
76 * reference number if type is STRING_SEGMENT_NUM_PLACEHOLDER |
| |
77 */ |
| |
78 int num; |
| |
79 |
| |
80 /* |
| |
81 * Next segment |
| |
82 */ |
| |
83 StringTemplateSegment *next; |
| |
84 }; |
| |
85 |
| |
86 /* |
| |
87 * Callback for converting a var segment into a string |
| |
88 * |
| |
89 * a: The allocator passed to string_template_write_to |
| |
90 * seg: Placeholder segment |
| |
91 * userdata: The userdata pointer passed to string_template_write_to |
| |
92 * free_str: If set to true, the returned string pointer will be freed |
| |
93 * using the allocator's free function. |
| |
94 */ |
| |
95 typedef cxmutstr (*strtpl_var_func)(const CxAllocator *a, StringTemplateSegment *seg, void *userdata, WSBool *free_str); |
| |
96 |
| |
97 /* |
| |
98 * Compiles a string template |
| |
99 * |
| |
100 * a: The allocator to use for building the compiled template |
| |
101 * tpl: Semplate string |
| |
102 * delim: Delimiter chars for variable names |
| |
103 */ |
| |
104 StringTemplate string_template_compile(const CxAllocator *a, cxstring tpl, char *delim); |
| |
105 |
| |
106 /* |
| |
107 * Builds a string using the provided template and writes it to the stream. |
| |
108 * |
| |
109 * tpl: Template |
| |
110 * a: aThe allocator passed to the strtpl_var_func callback |
| |
111 * varfunc: The callback used for converting |
| |
112 * STRING_SEGMENT_NUM_PLACEHOLDER and STRING_SEGMENT_VAR_PLACEHOLDER segments |
| |
113 * userdata: The userdata pointer passed to the strtpl_var_func callback |
| |
114 * stream: The stream object to which the resulting string should be written |
| |
115 * writef: Stream write function |
| |
116 */ |
| |
117 int string_template_write_to(StringTemplate *tpl, const CxAllocator *a, strtpl_var_func varfunc, void *userdata, void *stream, cx_write_func writef); |
| |
118 |
| |
119 /* |
| |
120 * Builds a string, using the provided template and allocator |
| |
121 */ |
| |
122 cxmutstr string_template_build_string(StringTemplate *tpl, const CxAllocator *a, strtpl_var_func varfunc, void *userdata); |
| |
123 |
| |
124 #ifdef __cplusplus |
| |
125 } |
| |
126 #endif |
| |
127 |
| |
128 #endif /* STRINGREPLACE_H */ |
| |
129 |