stmt.c

来自「CForms, by Lars Berntzon (Stockholm, Swe」· C语言 代码 · 共 159 行

C
159
字号
#include "config.h"#include "token.h"#include "comp.h"#define istext(ch) (isalnum(ch) || (ch) == '_')static void stmt_add_char(struct stmt **stmt, char ch);/****************************************************************** *		S T M T _ A D D *		--------------- * Description: *	Add text to statement list. * * Arguments: *	stmt	- Pointer to pointer of list. *	str	- String to add. * * Return: *	none * ******************************************************************/voidstmt_add(struct stmt **stmt, char *str, char *space){    if (str == NULL) return;#ifdef STANDALONE    fprintf(stderr, "stmt_add('%s', '%s')\n", str, space);#endif    /*     * Find last but one statement.     */    for(; (*stmt) != NULL && (*stmt)->next != NULL; stmt = &(*stmt)->next) 	;    /*     * Append the text.     */    while(*space)    {	stmt_add_char(stmt, *space++);    }    while(*str)    {	stmt_add_char(stmt, *str++);    }}/****************************************************************** *		S T M T _ A D D _ C H A R *		------------------------- * Description: *	Add one character to a statement. If statement is empty or *	overfilled a new block is automatically allocated. * * Arguments: *	stmt	- Pointer to list head. *	ch	- Character to add. * * Return: *	none * ******************************************************************/static voidstmt_add_char(struct stmt **stmt, char ch){    /*     * If this one is full, point to end of list.     */    if (((*stmt) != NULL) && (*stmt)->pos >= MAXPOS) {	stmt = &(*stmt)->next;    }    /*     * If end of list (empty list), make entry.     */    if (*stmt == NULL) {#ifdef STANDALONE	(*stmt) = (struct stmt *)malloc(sizeof **stmt);#else	(*stmt) = memalloc(sizeof **stmt);#endif	(*stmt)->next = NULL;	(*stmt)->pos = 0;	(*stmt)->line = line;    }    (*stmt)->txt[(*stmt)->pos++] = ch;}/******************************************************************* *              S T M T _ W R I T E *              ------------------- * *******************************************************************/voidstmt_write(struct stmt *stmt, FILE *fp){    char *p;    if (stmt == NULL) return;#ifndef STANDALONE    fprintf(fp, "#line %d \"%s\"\n", stmt->line, filename);#endif    /*     * Skip first new line.     */    if (stmt)    {	/*	 * Check that line begins with a newline (possibly 	 * leading blanks).	 */	p = stmt->txt;	while(p < stmt->txt + stmt->pos && isspace(*p) && *p != '\n')	{	    p++;	}	/*	 * If that is the case then remove them from output.	 */	if (p < stmt->txt + stmt->pos && *p == '\n') {	    p++;	    stmt->pos -= p - stmt->txt;	    fprintf(fp, "%.*s", stmt->pos, p);	}	else {	    fprintf(fp, "%.*s", stmt->pos, stmt->txt);	}	stmt = stmt->next;    }    for(; stmt != NULL; stmt = stmt->next) {	fprintf(fp, "%.*s", stmt->pos, stmt->txt);    }}#ifdef STANDALONEmain(){    char token[1000];    char space[1000];    struct stmt *stmt = NULL;    in = stdin;    while(GetTok(token, space))    {	stmt_add(&stmt, token, space);    }    stmt_write(stmt, stdout);}#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?