use strbuff instead of str_buff & str_idx
[build-config.git] / src / config / lxrgmr-code / StrBuff.c
blob4957bbf359a8cf98a623cd789ecfd380d5885d78
1 // SPDX-License-Identifier: GPL-2.0
2 /************************************************************************
3 * reglxgmr
5 * (c) Copyright 2023, FSF. Inc., Hangzhou, China
6 * (c) Copyright 2023, 自由软件基金会. Inc., 杭州, 中国
7 ************************************************************************
8 * filename: StrBuff.h
9 * function: StrBuff模块。
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 ************************************************************************/
17 /* Modify record */
18 /************************************************************************
19 * date: $DATE
20 * author: $AUTHOR
21 * note:
23 ************************************************************************
24 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
25 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
26 ************************************************************************/
29 模块引入函数:
34 数据接口:
38 /*******************************
39 *******************************
40 * 1. Include Files
41 *******************************
42 *******************************/
43 #define MODULE_NAME MODULE_CLASS_SYSLIB
45 /* System include files */
46 //#include "type.h"
47 #include <stdio.h>
48 #include <errno.h>
49 #include <memory.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
54 #include <stdarg.h>
56 /* Project module include files */
57 #include "StrBuff.h"
58 #include "AQStack.h"
59 //#include "lkglob.h"
61 /* YYSTYPE & yylval */
62 #include "gmr/parser.tab.h"
64 /* Private include files */
65 //#include <XXX_Private.h>
68 /*******************************
69 *******************************
70 * 2. Global Variables &
71 * Micro Defines &
72 * Type Defines &
73 * Type Proto
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 /*********************************************************************
92 * 名字: StrBuff_Example
93 * 功能: StrBuff功能模块的初始化函数。
94 * 参数: i32Value1:32位带符号整形值参数1
95 * 返回: 0表示运行ok,负数表示err id。
96 *********************************************************************/
97 static int32 StrBuff_Example (int32 i32Value1)
99 return 0;
103 /*******************************
104 *******************************
105 * 4. Public Functions Implement
106 *******************************
107 *******************************/
109 /*********************************************************************
110 * string operation.
111 *********************************************************************/
113 /* Allocate initial growable string */
114 struct gstr str_new(void)
116 struct gstr gs;
117 gs.s = malloc(sizeof(char) * 64);
118 gs.len = 64;
119 gs.max_width = 0;
120 strcpy(gs.s, "\0");
121 return gs;
124 /* Free storage for growable string */
125 void str_free(struct gstr *gs)
127 if (gs->s)
128 free(gs->s);
129 gs->s = NULL;
130 gs->len = 0;
133 /* Append to growable string */
134 void str_append(struct gstr *gs, const char *s)
136 size_t l;
137 if (s) {
138 l = strlen(gs->s) + strlen(s) + 1;
139 if (l > gs->len) {
140 gs->s = realloc(gs->s, l);
141 gs->len = l;
143 strcat(gs->s, s);
147 /* Append printf formatted string to growable string */
148 void str_printf(struct gstr *gs, const char *fmt, ...)
150 va_list ap;
151 char s[10000]; /* big enough... */
152 va_start(ap, fmt);
153 vsnprintf(s, sizeof(s), fmt, ap);
154 str_append(gs, s);
155 va_end(ap);
158 /* Retrieve value of growable string */
159 const char *str_get(struct gstr *gs)
161 return gs->s;
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;
178 #if 0
180 * XXX:
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)
188 void * ptr;
190 //for
191 if (u32BuffListFlag > 0x07)
193 ptr = malloc(sizeof(CACHING_STR));
195 else
200 pstr_cache->text = NULL;
201 pstr_cache->text_asize = 0;
202 pstr_cache->text_size = 0;
204 return ptr;
207 bool free_strbuff (CACHING_STR *pstr_cache)
209 return false;
211 #endif
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)
239 #ifdef LXRGMR_CODE
240 if (pstr_cache->text)
241 free((char *)pstr_cache->text);
242 #endif
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);
295 * \func: ln_string
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;
301 int idx = 0;
303 while (1)
305 if (i <= 0) break;
307 if (pstr_cache->text[i] == '\n')
308 i--;
309 else
310 break;
313 pstr_cache->text[i] = '\n';
314 pstr_cache->text_size = i+1;
315 pstr_cache->text[i+1] = 0;
318 #if 0
319 /*********************************************************************
320 * 名字: StrBuffIF_Testing
321 * 功能: StrBuff模块测试函数。
322 * 参数: 无
323 * 返回: 无
324 *********************************************************************/
325 void StrBuffIF_Testing (void)
327 StrBuffIF_Init ();
329 StrBuff_Example (1);
331 #else
332 void StrBuffIF_Testing (void){}
333 #endif
335 /*******************************
336 *******************************
337 * End Of File
338 *******************************
339 *******************************/