lxrgmr
[build-config.git] / src / config / lxrgmr-code / util-bak.c
blobaba1b88c302e0aac8e4dc5387c32e7cbffbfc940
1 /************************************************************************
2 * $PROJECT Project
4 * (c) Copyright 2009, $COMPANY_EN. Inc., $CITY_EN, $COUNTRY_EN
5 * (c) Copyright 2009, $COMPANY_CN. Inc., $CITY_CN, $COUNTRY_CN
6 ************************************************************************
7 * filename: Util.c
8 * function: Util模块。
9 * createdate: 2023-04-03
10 * author: devenkong@126.com
11 * note:this file is wirten for various util functions.
14 ************************************************************************/
15 /* Modify record */
16 /************************************************************************
17 * date: $DATE
18 * author: $AUTHOR
19 * note:
21 ************************************************************************/
24 模块引入函数:
29 数据接口:
33 /*******************************
34 *******************************
35 * 1. Include Files
36 *******************************
37 *******************************/
38 #define MODULE_NAME MODULE_CLASS_SYSLIB
40 /* System include files */
41 //#include "type.h"
42 #include <stdio.h>
43 #include <errno.h>
44 #include <memory.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
49 /* Project module include files */
50 #include "Util.h"
51 #include "QStack.h"
53 /* YYSTYPE & yylval */
54 #include "gmr/parser.tab.h"
56 /* Private include files */
57 //#include <XXX_Private.h>
60 /*******************************
61 *******************************
62 * 2. Global Variables &
63 * Micro Defines &
64 * Type Defines &
65 * Type Proto
66 *******************************
67 *******************************/
69 extern int last_token;
72 * set content flag automatically.
73 * referenced from script.l(script.lex.c).
75 extern int g_content_before_cmnt;
76 #define RETURN(token) g_content_before_cmnt=1; return token;
78 /* those variables are defined in script.lex.c */
79 extern char *yytext;
80 extern int yylineno;
81 extern size_t yyleng;
83 /* it is defined in menu.c */
84 extern struct file *file_list;
87 #define START_STRSIZE 16
89 static int curr_state = 0;
90 static int init_state = 0;
92 static volatile uint8 gau8StateQStackBuf[128];
93 static QSTACK gstStateQStack;
95 CACHING_STR strbuff = {
96 NULL, START_STRSIZE, 0
101 /*******************************
102 *******************************
103 * 3. Private Functions Implement
104 *******************************
105 *******************************/
107 /*********************************************************************
108 * 名字: Util_Example
109 * 功能: util功能模块的初始化函数。
110 * 参数: i32Value1:32位带符号整形值参数1
111 * 返回: 0表示运行ok,负数表示err id。
112 *********************************************************************/
113 static int32 Util_Example (int32 i32Value1)
115 return 0;
119 /*******************************
120 *******************************
121 * 4. Public Functions Implement
122 *******************************
123 *******************************/
125 void *xmalloc(size_t size)
127 void *p = malloc(size);
128 if (p)
129 return p;
130 fprintf(stderr, "Out of memory.\n");
131 exit(1);
134 void *xcalloc(size_t nmemb, size_t size)
136 void *p = calloc(nmemb, size);
137 if (p)
138 return p;
139 fprintf(stderr, "Out of memory.\n");
140 exit(1);
143 void *xrealloc(void *p, size_t size)
145 p = realloc(p, size);
146 if (p)
147 return p;
148 fprintf(stderr, "Out of memory.\n");
149 exit(1);
152 char *xstrdup(const char *s)
154 char *p;
156 p = strdup(s);
157 if (p)
158 return p;
159 fprintf(stderr, "Out of memory.\n");
160 exit(1);
163 char *xstrndup(const char *s, size_t n)
165 char *p;
167 p = strndup(s, n);
168 if (p)
169 return p;
170 fprintf(stderr, "Out of memory.\n");
171 exit(1);
174 /* it's just a wrap for interface name. */
175 void xfree(const char *s)
177 if (s)
178 free((void *)s);
181 /* file already present in list? If not add it */
182 struct file *file_lookup(const char *name)
184 struct file *file;
185 // TBD: append expand func if link with corresonding code.
186 char *file_name = (char *)name;//sym_expand_string_value(name);
188 printf("name = %s\n\n", name);
189 printf("file_name = %s\n\n", file_name);
190 for (file = file_list; file; file = file->next) {
191 if (!strcmp(name, file->name)) {
192 free(file_name);
193 return file;
197 file = xmalloc(sizeof(*file));
198 memset(file, 0, sizeof(*file));
199 file->name = file_name;
200 file->next = file_list;
201 file_list = file;
202 return file;
205 /*********************************************************************
206 * state operation.
207 *********************************************************************/
209 void STATE_INIT (int state)
211 curr_state = state;
212 init_state = state;
214 QStackIF_InitQueue (&gstStateQStack, (char *)&gau8StateQStackBuf, 128);
215 STATE_SET(curr_state);
218 void STATE_ENTER (int state)
220 QStackIF_PushU32(&gstStateQStack, (unsigned int)curr_state);
221 curr_state=state;
222 STATE_SET(curr_state);
225 int STATE_RESTORE (void)
227 if (QStackIF_IsEmpty(&gstStateQStack))
229 printf("err: can not pop state from stack.\n");
230 return init_state;
233 curr_state = (int)QStackIF_PopU32(&gstStateQStack);
234 STATE_SET(curr_state);
236 return curr_state;
239 /*********************************************************************
240 * caching string buffer for T_WORD and others.
241 *********************************************************************/
243 int is_empty_string (CACHING_STR *pstr_cache)
245 return (pstr_cache->text_size == 0 && pstr_cache->text == NULL);
248 int is_unallocated_string (CACHING_STR *pstr_cache)
250 return (pstr_cache->text == NULL);
253 void init_string (CACHING_STR *pstr_cache)
255 pstr_cache->text = NULL;
256 pstr_cache->text_asize = 0;
257 pstr_cache->text_size = 0;
260 void new_string (CACHING_STR *pstr_cache)
262 pstr_cache->text = xmalloc(START_STRSIZE);
263 pstr_cache->text_asize = START_STRSIZE;
264 pstr_cache->text_size = 0;
265 *pstr_cache->text = 0;
268 void append_string (CACHING_STR *pstr_cache, const char *str, int size)
270 if (pstr_cache->text == NULL)
272 pstr_cache->text = xmalloc(START_STRSIZE);
273 pstr_cache->text_asize = START_STRSIZE;
274 pstr_cache->text_size = 0;
277 int new_size = pstr_cache->text_size + size + 1;
278 if (new_size > pstr_cache->text_asize) {
279 new_size += START_STRSIZE - 1;
280 new_size &= -START_STRSIZE;
281 pstr_cache->text = xrealloc(pstr_cache->text, new_size);
282 pstr_cache->text_asize = new_size;
284 memcpy(pstr_cache->text + pstr_cache->text_size, str, size);
285 pstr_cache->text_size += size;
286 pstr_cache->text[pstr_cache->text_size] = 0;
289 void alloc_string (CACHING_STR *pstr_cache, const char *str, int size)
291 new_string(pstr_cache);
292 append_string(pstr_cache, str, size);
294 pstr_cache->text = xmalloc(size + 1);
295 memcpy(pstr_cache->text, str, size);
296 pstr_cache->text[size] = 0;
297 pstr_cache->text_size = size;
301 int const_string (CACHING_STR *pstr_cache, int token)
303 if (is_unallocated_string(&strbuff))
305 alloc_string(&strbuff, yytext, yyleng);
306 yylval.string = strbuff.text;
308 last_token = token;
309 return token;
311 else
312 append_string(&strbuff, yytext, yyleng);
314 RETURN( T_WORD );
317 CACHING_STR * dup_string (CACHING_STR *pstr_cache, CACHING_STR *p_dst_str_cache)
319 if (!pstr_cache) return NULL;
321 if (p_dst_str_cache->text)
322 xfree((const char *)p_dst_str_cache->text);
324 p_dst_str_cache->text = xstrdup(pstr_cache->text);
325 p_dst_str_cache->text_asize = pstr_cache->text_asize;
326 p_dst_str_cache->text_size = pstr_cache->text_size;
329 void free_string (CACHING_STR *pstr_cache)
331 if (pstr_cache->text)
332 xfree(pstr_cache->text);
333 pstr_cache->text_asize = 0;
334 pstr_cache->text_size = 0;
335 pstr_cache->text = NULL;
338 #if 0
339 /*********************************************************************
340 * 名字: UtilIF_Testing
341 * 功能: util模块测试函数。
342 * 参数: 无
343 * 返回: 无
344 *********************************************************************/
345 void UtilIF_Testing (void)
347 UtilIF_Init ();
349 Util_Example (1);
351 #else
352 void UtilIF_Testing (void){}
353 #endif
355 /*******************************
356 *******************************
357 * End Of File
358 *******************************
359 *******************************/