1 /************************************************************************
4 * (c) Copyright 2009, $COMPANY_EN. Inc., $CITY_EN, $COUNTRY_EN
5 * (c) Copyright 2009, $COMPANY_CN. Inc., $CITY_CN, $COUNTRY_CN
6 ************************************************************************
9 * createdate: 2023-04-03
10 * author: devenkong@126.com
11 * note:this file is wirten for various util functions.
14 ************************************************************************/
16 /************************************************************************
21 ************************************************************************/
33 /*******************************
34 *******************************
36 *******************************
37 *******************************/
38 #define MODULE_NAME MODULE_CLASS_SYSLIB
40 /* System include files */
49 /* Project module include files */
53 /* YYSTYPE & yylval */
54 #include "gmr/parser.tab.h"
56 /* Private include files */
57 //#include <XXX_Private.h>
60 /*******************************
61 *******************************
62 * 2. Global Variables &
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 */
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 /*********************************************************************
109 * 功能: util功能模块的初始化函数。
110 * 参数: i32Value1:32位带符号整形值参数1
111 * 返回: 0表示运行ok,负数表示err id。
112 *********************************************************************/
113 static int32
Util_Example (int32 i32Value1
)
119 /*******************************
120 *******************************
121 * 4. Public Functions Implement
122 *******************************
123 *******************************/
125 void *xmalloc(size_t size
)
127 void *p
= malloc(size
);
130 fprintf(stderr
, "Out of memory.\n");
134 void *xcalloc(size_t nmemb
, size_t size
)
136 void *p
= calloc(nmemb
, size
);
139 fprintf(stderr
, "Out of memory.\n");
143 void *xrealloc(void *p
, size_t size
)
145 p
= realloc(p
, size
);
148 fprintf(stderr
, "Out of memory.\n");
152 char *xstrdup(const char *s
)
159 fprintf(stderr
, "Out of memory.\n");
163 char *xstrndup(const char *s
, size_t n
)
170 fprintf(stderr
, "Out of memory.\n");
174 /* it's just a wrap for interface name. */
175 void xfree(const char *s
)
181 /* file already present in list? If not add it */
182 struct file
*file_lookup(const char *name
)
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
)) {
197 file
= xmalloc(sizeof(*file
));
198 memset(file
, 0, sizeof(*file
));
199 file
->name
= file_name
;
200 file
->next
= file_list
;
205 /*********************************************************************
207 *********************************************************************/
209 void STATE_INIT (int 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
);
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");
233 curr_state
= (int)QStackIF_PopU32(&gstStateQStack
);
234 STATE_SET(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
;
312 append_string(&strbuff
, yytext
, yyleng
);
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
;
339 /*********************************************************************
344 *********************************************************************/
345 void UtilIF_Testing (void)
352 void UtilIF_Testing (void){}
355 /*******************************
356 *******************************
358 *******************************
359 *******************************/