1 // SPDX-License-Identifier: GPL-2.0
2 /************************************************************************
5 * (c) Copyright 2023, FSF. Inc., Hangzhou, China
6 * (c) Copyright 2023, 自由软件基金会. Inc., 杭州, 中国
7 ************************************************************************
10 * createdate: 2023-06-19
11 * author: devenkong@126.com
12 * note: it extract string buffer code in menuconfig program,
13 * and append new style interface. this file is wirten for various
14 * String Buff functions.
16 ************************************************************************/
18 /************************************************************************
23 ************************************************************************
24 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
25 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
26 ************************************************************************/
38 /*******************************
39 *******************************
41 *******************************
42 *******************************/
43 #define MODULE_NAME MODULE_CLASS_SYSLIB
45 /* System include files */
56 /* Project module include files */
61 /* YYSTYPE & yylval */
62 #include "gmr/parser.tab.h"
64 /* Private include files */
65 //#include <XXX_Private.h>
68 /*******************************
69 *******************************
70 * 2. Global Variables &
74 *******************************
75 *******************************/
77 #define START_STRSIZE 16
79 CACHING_STR strbuff
= {
80 NULL
, START_STRSIZE
, 0
85 /*******************************
86 *******************************
87 * 3. Private Functions Implement
88 *******************************
89 *******************************/
91 /*********************************************************************
93 * 功能: StrBuff功能模块的初始化函数。
94 * 参数: i32Value1:32位带符号整形值参数1
95 * 返回: 0表示运行ok,负数表示err id。
96 *********************************************************************/
97 static int32
StrBuff_Example (int32 i32Value1
)
103 /*******************************
104 *******************************
105 * 4. Public Functions Implement
106 *******************************
107 *******************************/
109 /*********************************************************************
111 *********************************************************************/
113 /* Allocate initial growable string */
114 struct gstr
str_new(void)
117 gs
.s
= malloc(sizeof(char) * 64);
124 /* Free storage for growable string */
125 void str_free(struct gstr
*gs
)
133 /* Append to growable string */
134 void str_append(struct gstr
*gs
, const char *s
)
138 l
= strlen(gs
->s
) + strlen(s
) + 1;
140 gs
->s
= realloc(gs
->s
, l
);
147 /* Append printf formatted string to growable string */
148 void str_printf(struct gstr
*gs
, const char *fmt
, ...)
151 char s
[10000]; /* big enough... */
153 vsnprintf(s
, sizeof(s
), fmt
, ap
);
158 /* Retrieve value of growable string */
159 const char *str_get(struct gstr
*gs
)
164 CACHING_STR
* pstBuffList
[3] = {0};
165 uint32 u32BuffListFlag
= 0;
167 /*********************************************************************
168 * caching string buffer for T_WORD and others.
169 *********************************************************************/
171 void init_strbuff (CACHING_STR
*pstr_cache
)
173 pstr_cache
->text
= NULL
;
174 pstr_cache
->text_asize
= 0;
175 pstr_cache
->text_size
= 0;
181 * @ memblk store allocated mem ptr,
182 * @ do not free mem when ptr cnt less then MIN_BUFF_CNT, store it in ptrbuff.
183 * @ alloc new buff from ptrbuff first.
184 * @ it avoid frequently memory alloc/free.
186 CACHING_STR
* alloc_strbuff (void)
191 if (u32BuffListFlag
> 0x07)
193 ptr
= malloc(sizeof(CACHING_STR
));
200 pstr_cache
->text
= NULL
;
201 pstr_cache
->text_asize
= 0;
202 pstr_cache
->text_size
= 0;
207 bool free_strbuff (CACHING_STR
*pstr_cache
)
213 void new_string (CACHING_STR
*pstr_cache
)
215 pstr_cache
->text
= malloc(START_STRSIZE
);
216 pstr_cache
->text_asize
= START_STRSIZE
;
217 pstr_cache
->text_size
= 0;
218 *pstr_cache
->text
= 0;
222 * at first time, it alloc string len size memory,
223 * secondly, it alloc START_STRSIZE size.
225 void alloc_string (CACHING_STR
*pstr_cache
, const char *str
, int size
)
227 new_string(pstr_cache
);
228 append_string(pstr_cache
, str
, size
);
230 pstr_cache->text = xmalloc(size + 1);
231 memcpy(pstr_cache->text, str, size);
232 pstr_cache->text[size] = 0;
233 pstr_cache->text_size = size;
237 void free_string (CACHING_STR
*pstr_cache
)
240 if (pstr_cache
->text
)
241 free((char *)pstr_cache
->text
);
243 pstr_cache
->text_asize
= 0;
244 pstr_cache
->text_size
= 0;
245 pstr_cache
->text
= NULL
;
248 void append_string (CACHING_STR
*pstr_cache
, const char *str
, int size
)
250 if (size
== 0) return;
251 if (size
== -1) size
= strlen(str
);
253 if (pstr_cache
->text
== NULL
)
255 pstr_cache
->text
= (char *)malloc(size
);
256 pstr_cache
->text_asize
= size
;
257 pstr_cache
->text_size
= 0;
260 int new_size
= pstr_cache
->text_size
+ size
+ 1;
261 if (new_size
> pstr_cache
->text_asize
) {
262 new_size
+= START_STRSIZE
- 1;
263 new_size
&= -START_STRSIZE
;
264 pstr_cache
->text
= (char *)realloc(pstr_cache
->text
, new_size
);
265 pstr_cache
->text_asize
= new_size
;
267 memcpy(pstr_cache
->text
+ pstr_cache
->text_size
, str
, size
);
268 pstr_cache
->text_size
+= size
;
269 pstr_cache
->text
[pstr_cache
->text_size
] = 0;
272 CACHING_STR
* dup_string (CACHING_STR
*pstr_cache
, CACHING_STR
*p_dst_str_cache
)
274 if (!pstr_cache
) return NULL
;
276 if (p_dst_str_cache
->text
)
277 free((char *)p_dst_str_cache
->text
);
279 p_dst_str_cache
->text
= (char *)strdup(pstr_cache
->text
);
280 p_dst_str_cache
->text_asize
= pstr_cache
->text_asize
;
281 p_dst_str_cache
->text_size
= pstr_cache
->text_size
;
284 int is_empty_string (CACHING_STR
*pstr_cache
)
286 return (pstr_cache
->text_size
== 0 && pstr_cache
->text
== NULL
);
289 int is_unallocated_string (CACHING_STR
*pstr_cache
)
291 return (pstr_cache
->text
== NULL
);
296 * \desc: remove ln char at the end of string.
298 void ln_string (CACHING_STR
*pstr_cache
)
300 int i
= pstr_cache
->text_size
- 1;
307 if (pstr_cache
->text
[i
] == '\n')
313 pstr_cache
->text
[i
] = '\n';
314 pstr_cache
->text_size
= i
+1;
315 pstr_cache
->text
[i
+1] = 0;
319 /*********************************************************************
320 * 名字: StrBuffIF_Testing
324 *********************************************************************/
325 void StrBuffIF_Testing (void)
332 void StrBuffIF_Testing (void){}
335 /*******************************
336 *******************************
338 *******************************
339 *******************************/