-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstr_replace.h
More file actions
247 lines (198 loc) · 8.87 KB
/
str_replace.h
File metadata and controls
247 lines (198 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/***************************************************************************
* Copyright (C) 2007 by Stoian Ivanov *
* sdr@tera-com.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License version *
* 2 as published by the Free Software Foundation; *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef str_replace_h_SDR_321234msdaa_included
#define str_replace_h_SDR_321234msdaa_included
/** \file
\brief String replacement functions.
\author Stoian Ivanov sdr@tera-com.com
\example str_replace.example.c
\example str_replace.example1.c
*/
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
///malloc() proto as type
typedef void * (malloc_ft) (size_t SIZE);
///free() proto as type
typedef void (free_ft) (void *PTR);
///pattern replace context
typedef struct {
malloc_ft* malloc; ///<use this malloc implementation
free_ft* free; ///<use this free implementation
int prepared;
int pattern_free;
int pattern_len;
char *pattern;
int ph_names_count; ///<current element count in ph_names,ph_values,ph_names_l
int ph_names_size; ///<currrent size of ph_names,ph_values,ph_names_l
char **ph_names;
char **ph_values;
int *ph_names_l; ///<names lengths
int *ph_values_l; ///<values lengths
int max_name_l;
int max_value_l;
int ph_order_count; ///<current element count in ph_order, ph_offset
int ph_order_size; ///<current size of ph_order, ph_offset
int *ph_order;
int *ph_offset;
int bucket_names; ///<incremetntal step for ph_names_size
int bucket_order; ///<incremetntal step for ph_order_size
char delimiter; ///<placeholder delimiter
} phctx_t;
///user values holder
typedef struct {
char **ph_values;
int *ph_values_l;
int max_value_l;
} phctx_user_values_t;
///name-value bundle structure
///one memory block to hold em'all :)
typedef struct {
char **ph_names;
char **ph_values;
int ph_names_count;
int *ph_names_l;
int *ph_values_l;
int max_name_l;
int max_value_l;
} phctx_namevalue_bundle_t;
///allocate and initialize a replace_ph context
phctx_t * str_replace_ph_init (
int bucket_names, ///<expected placeholders count
int bucket_order, ///<expected placeholders occurrences in pattern
char delimiter, ///<place holder delimiter
malloc_ft*use_malloc/*=NULL*/, ///<use this malloc implementation NULL for default
free_ft* use_free/*=NULL*/ ///<use this free implementation NULL for default
);
///initialize a replace_ph context
void str_replace_ph_init_static (
phctx_t * ctx,
int bucket_names, ///<expected placeholders count
int bucket_order, ///<expected placeholders occurrences in pattern
char delimiter, ///<place holder delimiter
malloc_ft*use_malloc/*=NULL*/, ///<use this malloc implementation NULL for default
free_ft* use_free/*=NULL*/ ///<use this free implementation NULL for default
);
///initialize a phctx_user_values_t from a ctx having space just for the allready set placeholders
phctx_user_values_t * str_replace_ph_init_uv (
phctx_t *ctx
);
///clear placeholders data
void str_replace_ph_reset_ph( phctx_t *ctx );
///deiinit replace_ph context used in str_replace_ph_free
void str_replace_ph_deinit ( phctx_t *ctx);
///deiinit and free replace_ph context memory
void str_replace_ph_free ( phctx_t *ctx);
///free phctx_user_values valuesdata
void str_replace_ph_free_uv_valuesdata (phctx_t *ctx, phctx_user_values_t *uv) ;
///free phctx_user_values
void str_replace_ph_free_uv (phctx_t *ctx, phctx_user_values_t *uv);
///add/set a place holder and a value for it
int str_replace_ph_set_ph (
phctx_t *ctx, ///<context
char *ph_name, ///<placeholder name
int ph_nlen/*=-1*/, ///<place holder name length if known else -1
char *ph_value/*=NULL*/, ///<place hoder value if known
int ph_vlen/*=-1*/ ///<place holder value length if known else -1
);
///set (NOT ADD) a place holder user value
int str_replace_ph_set_ph_uv (
phctx_t *ctx, ///<context
phctx_user_values_t *uv,
char *ph_name, ///<placeholder name
int ph_nlen/*=-1*/, ///< place holder name length if known else -1
char *ph_value/*=NULL*/, ///< place hoder value if known
int ph_vlen/*=-1*/, ///< place holder value length if known else -1
char **oldvalue/*=NULL*/ ///< write return old value here if not NULL
);
///removews a place holder and its value
int str_replace_ph_remove_ph (
phctx_t *ctx, ///< context
char *ph_name, ///< placeholder name
int ph_nlen/*=-1*/ ///< place holder name length if known else -1
);
///add a pattern to context
int str_replace_ph_config_pattern(
phctx_t *ctx, ///< context to change
char *pattern, ///< pattern to add asciiZ
int copy, ///< copy(1) data or just use(0) the pointer
int plen/*=-1*/ ///< data len if known -1 to autofindout
);
///prepare context for susbstitutions. e.g. find placeholders in pattern
int str_replace_ph_prepare (phctx_t *ctx);
///fid maximal destionation size with given pattern and placeholders. This will automaticaly call str_replace_ph_prepare() if needed
int str_replace_ph_subst_maxsize (phctx_t *ctx);
///fid maximal destionation size with given pattern, user values and placeholders. This will automaticaly call str_replace_ph_prepare() if needed
int str_replace_ph_subst_maxsize_uv (phctx_t *ctx,phctx_user_values_t *uv);
///make the substitutions in current pattern with current placeholders. This will automaticaly call str_replace_ph_prepare() if needed
int str_replace_ph_subst (
phctx_t *ctx,
char *dst, ///< where to store result plus a terminateig 0
int dstmaxlen ///< space availible in dst
);
///make the substitutions in current pattern with current placeholders. This will automaticaly call str_replace_ph_prepare() if needed
///this will also use values from provided arrays
int str_replace_ph_subst_user (
phctx_t *ctx,
char **user_ph_values, ///< values to use
int *user_ph_values_l, ///< values lengths to use
char *dst, ///< where to store result plus a terminateig 0
int dstmaxlen ///< space availible in dst
);
///directly uses str_replace_ph_subst_user
int str_replace_ph_subst_uv (
phctx_t *ctx,
phctx_user_values_t *uv, ///< values lengths to use
char *dst, ///< where to store result plus a terminateig 0
int dstmaxlen ///< space availible in dst
);
///bundle a COPY of all name/values in a single memory block lead by phctx_namevalue_bundle_t struct
phctx_namevalue_bundle_t* str_replace_ph_bundle (
phctx_t *ctx,
phctx_user_values_t *uv ///< optional, might be NULL
);
///copy bundle name-values to context
int str_replace_ph_import_bundle (phctx_t *ctx,phctx_namevalue_bundle_t* bundle);
///crete uv based on ctx and bundle
phctx_user_values_t * str_replace_ph_bundle2uv (phctx_t *ctx, phctx_namevalue_bundle_t * bundle, int ignoremissing);
///free bundel in proper way
void str_replace_ph_bundle_free (phctx_t *ctx,phctx_namevalue_bundle_t *bundle);
///simple search-and-replace replace first found
int str_replace_single (const char *haystack,const char *needle,const char *changeto, char* dst , int dstmaxlen, int hslen, int nlen, int chtolen);
///simple search-and-replace replace all found - based on str_replace_single()
int str_replace_multiple (const char *haystack,const char *needle,const char *changeto, char* dst , int dstmaxlen, int hslen, int nlen, int chtolen);
///escapes quotes (`"') and slash with a slash good for mysql_real_escape_string() replacement dest must be 2*+1 size of original string
///no bound checking is done!
int str_replace_add_slashes (
char *from, ///< where to read from 0
int fromsz, ///< size of from (if 0 strlen will be used)
char *to ///< where to store result plus a terminateig 0
);
///url decodes string + to space %xx to proper charecter replacement dest must be atleast the size of original string
///no bound checking is done!
int str_replace_urldecode (
char *from, ///< where to read from 0
int fromsz, ///< size of from (if 0 strlen will be used)
char *to ///< where to store result plus a terminateig 0
);
#ifdef __cplusplus
}
#endif
#endif