⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codearea.c

📁 linux 下类似于TC功能的编译器,但还没有完成,希望大家共同研究
💻 C
📖 第 1 页 / 共 3 页
字号:
				multiremark=FALSE;				GETCH(pos);			}		}		else		{			line[pos]=line[pos]|COLOR_PAIR(COLOR_GREEN_BLUE);			GETCH(pos);		}				}	else if( isalpha(ch) )	{		k=0;		wordbegin=pos;		do		{			if(k < MAXIDLEN)				a[k++]=ch;			GETCH(pos);		}while( isalpha(ch) || isdigit(ch)|| ch=='_' );		wordend=pos;		a[k]=0;		keyword[0]=a;		i=NRW;		while(strcmp(a,keyword[i--]));		if(++i)		{			color=COLOR_YELLOW_BLUE;		}	//key word		else		{			color=GetColor();		}	//identifier		while(wordbegin<wordend)		{			line[wordbegin]=line[wordbegin]|COLOR_PAIR(color);			wordbegin++;		}	}	else if ( isdigit(ch) )	{		wordbegin=pos;		color=COLOR_CYAN_BLUE;		do		{			GETCH(pos);		}while(isdigit(ch));		if(ch=='.')		{			do			{				GETCH(pos);			}while(isdigit(ch));						}		else if(ch=='x')		{			if (wordbegin+1!=pos || line[wordbegin]!='0')			{//error such as 012x123456 or 1x123456				color=COLOR_CYAN_RED;			}			do			{				GETCH(pos);			}while(isdigit(ch) || ((ch>='a')&&(ch<='f')) || ((ch>='A')&&(ch<='F')) );		}		wordend=pos;		while(wordbegin<wordend)		{			line[wordbegin]=line[wordbegin]|COLOR_PAIR(color)|A_UNDERLINE;				;			wordbegin++;		}			}	else if(ch=='"')	{		wordbegin=pos;		color=COLOR_CYAN_BLUE;		do		{			line[pos]=line[pos]|COLOR_PAIR(color);			GETCH(pos);		}while(ch!='"' && ch!='\0');		if(ch=='"')		{			line[pos]=line[pos]|COLOR_PAIR(color);			GETCH(pos);		}		else if(ch=='\0')		{	//missed a ",corrected it			//line[pos]='"'|COLOR_PAIR(color);			//line[pos+1]='\0';			char word;			color=COLOR_CYAN_RED;			wordend=pos;			while(wordbegin<wordend)			{				word=line[wordbegin];				line[wordbegin]=word|COLOR_PAIR(color);				wordbegin++;			}		}	}	else if(ch=='/')	{		wordbegin=pos;		GETCH(pos);		if(ch=='/')		{			pos=wordbegin;			do			{				line[pos]=line[pos]|COLOR_PAIR(COLOR_GREEN_BLUE);				GETCH(pos);			}while(ch!='\0');		}		else if(ch=='*')		{			multiremark=TRUE;			line[pos-1]=line[pos-1]|COLOR_PAIR(COLOR_GREEN_BLUE);			line[pos]=line[pos]|COLOR_PAIR(COLOR_GREEN_BLUE);			GETCH(pos);		}		else		{			line[pos-1]=line[pos-1]|COLOR_PAIR(COLOR_MAGENTA_BLUE);			pos--;			GETCH(pos);		}	}	else if(ch=='\'')	{		wordbegin=pos;		color=COLOR_CYAN_BLUE;		do		{			prech=ch;			line[pos]=line[pos]|COLOR_PAIR(color);			GETCH(pos);		}while((ch=='\'' && prech=='\\') || (ch!='\'' && ch!='\0'));		if(ch=='\'')		{			line[pos]=line[pos]|COLOR_PAIR(color);			GETCH(pos);		}		else if(ch=='\0')		{			char word;			color=COLOR_CYAN_RED;			wordend=pos;			while(wordbegin<wordend)			{				word=line[wordbegin];				line[wordbegin]=word|COLOR_PAIR(color);				wordbegin++;			}		}	}	else	{		line[pos]=line[pos]|COLOR_PAIR(COLOR_MAGENTA_BLUE);		GETCH(pos);	}	goto again;over:	return;}BOOL CodeArea::NewFile(){	//free all memory for code	Trim();	//init member variable	return InitLocalVar();}int  CodeArea::ReadFile(char *filename){	int ret=FALSE;	int i=0;	int size=0;	char fnm[MAXFIELDLEN];	PageList dptr=pagelist;	PageList newp=NULL;	codefile file(filename);	if (file.success != ZC_OK) 	{		goto over;	}	strcpy(fnm,filenm);//backup filename in case of reading device file	if(NewFile()==FALSE)		goto over;	//read file	while(i<CODE_QSET && (size=file.getline(dptr->data[i])))//???????????	{		linecolor(dptr->data[i]);		if(dptr==pagelist && i==0)	//get colwidth of the first line			totalcol=size;		i++;		if(i==CODE_QSET && file.iseof()==FALSE)//??????????????????		{			if((newp=AllocPage())==NULL)				goto over;			InsertPage(dptr,newp);			dptr=newp;			i=0;		}		totalrow++;			maxcol=(size>maxcol)?(size):(maxcol);	}	curpage=0;	if (totalrow>1)		totalrow--;		//in order to process untitled file	ShowPage(pagelist,0,GetHeight(),0,GetWidth(),0,0);	wmove(basewin,0,0);	if( file.getmode()==ZC_REG )		strncpy(filenm,filename,MAXFIELDLEN);	else if(file.getmode()==ZC_CHR)		strncpy(filenm,fnm,MAXFIELDLEN);	ret = TRUE;over:	return ret;}BOOL CodeArea::SaveFile(char *filename){	int ret=FALSE;	int i=0,j=0;	PageList dptr;	codefile file(filename,O_CREAT|O_WRONLY|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);	if (file.success != ZC_OK) 	{		//don't forget to free memory		goto over;	}		strcpy(filenm,filename);	modified=FALSE;	dptr	=pagelist;	do	{		if (dptr->data)		{			for (i = 0; i <CODE_QSET && j<totalrow; i++)			{				if (dptr->data[i])				{					if(file.putline(dptr->data[i])==-1)						goto over;					j++;				}			}		}		dptr=dptr->next;	}while(dptr && dptr!=pagelist);		ret=TRUE;	goto over;over:	return ret;}BOOL CodeArea::SaveFile(){	return SaveFile(filenm);}BOOL CodeArea::Modified(){	return modified;}char* CodeArea::GetFileName(){	return filenm;}BOOL CodeArea::SelectText(){	BOOL ret=FALSE;	int begline=msg.seleobj.begline-1;	int begcol =msg.seleobj.begcol-1;	int endline=msg.seleobj.endline-1;	int endcol =msg.seleobj.endcol-1;	int baseln =begline%CODE_QSET;	int lines  =endline-begline+1;	int line   =baseln;	int viewln =0;	int col    =0;	int pgidx  =0;	int y,x;	int i,j;	char ch;	PageList page=pagelist;	PageList bpg =NULL;	//data check	if(begline<0 || begline>=totalrow)		goto failure;	if(begcol<0 || begcol>=CODE_QUANTUM)		goto failure;	if(endline<0 || endline>=totalrow || endline<begline)		goto failure;	if(endcol<0 || endcol >=CODE_QUANTUM)		goto failure;	seltext.selected=TRUE;	seltext.begline=begline;	seltext.begcol =begcol;	seltext.endline=endline;	seltext.endcol =endcol;	//select text	getyx(basewin,y,x);	viewln=currow-y;	pgidx=(int)(begline/CODE_QSET);	for(i=0;i<pgidx;i++)	{		page=page->next;	}	bpg=page;	//page may be scroll to following page,so keep it backup	currow=begline; //currow based on 0 and count from head to tail	curcol=begcol;	offcol=0;	for(i=0;i<lines;i++,line++)	{		if(line>=CODE_QSET)		{			if(page->next!=pagelist)			{				page=page->next;			}			else			{				break;			}			line=0;		}		j=(i==0)?(begcol):(0);		col=(i==lines-1)?(endcol+1):(CODE_QUANTUM);		for(;j<col && page->data[line][j]!='\0';j++)		{			ch=page->data[line][j];			page->data[line][j]=ch|A_REVERSE;		}	}	if(currow>=viewln && currow<=viewln + CODE_QSET -1)//block in the current visual window	{		curpage=(int)(viewln/CODE_QSET);		totalcol=GetCurColWidth(NULL,currow);		begcol=(begcol>=totalcol)?(totalcol):(begcol);		offcol=(int)((begcol-GetWidth())/TABSIZE)+1;		offcol=(offcol>=0)?(offcol):(0);		curcol=begcol;		wmove(basewin,currow-viewln,begcol-offcol*TABSIZE);	}	else//block out of current visual window	{		curpage=(int)(currow/CODE_QSET);		totalcol=GetCurColWidth(bpg,currow);		begcol=(begcol>=totalcol)?(totalcol):(begcol);		offcol=(int)((begcol-GetWidth())/TABSIZE)+1;		offcol=(offcol>=0)?(offcol):(0);		curcol=begcol;		wmove(basewin,0,begcol-offcol*TABSIZE);	}	ret=TRUE;failure:	return ret;}void CodeArea::UnSelectTxt(){	if (seltext.selected==FALSE)		return;}BOOL CodeArea::CutBlock(){	BOOL ret=FALSE;	ret=TRUE;//failure:	return ret;}BOOL CodeArea::CopyBlock(){	BOOL ret=FALSE;	ret=TRUE;//failure:	return ret;}BOOL CodeArea::PasteBlock(){	BOOL ret=FALSE;	ret=TRUE;//failure:	return ret;}BOOL CodeArea::DelBlock(){	BOOL ret=FALSE;	int i,j,k;	int ln;	PageList pg;	pg=LocatePage(NULL,seltext.begline);	ln=seltext.begline%CODE_QSET;	if (seltext.selected==FALSE)		goto over;	if (seltext.begline==seltext.endline)//block lies in a single line	{		for(k=0,i=seltext.begcol;i<=seltext.endcol && pg->data[ln][i]!='\0';i++,k++)			pg->data[ln][i]='\0';		for(j=0,i=seltext.endcol+1;i<CODE_QUANTUM && pg->data[ln][i]!='\0';i++)		{				pg->data[ln][seltext.begcol+j]=pg->data[ln][i];			pg->data[ln][i]='\0';			j++;		}		pg->data[ln][seltext.begcol+j]='\0';		totalcol=(currow!=seltext.begline)?(totalcol):(totalcol-k);	}	else//block contains multi-line	{		int len=GetCurColWidth(NULL,seltext.endline)-seltext.endcol-1;		int shift=seltext.endline-seltext.begline;		int targetbeg=seltext.begcol;		PageList ppg=LocatePage(NULL,seltext.endline);		int y,x;		getyx(basewin,y,x);				if( (currow<seltext.begline) || (currow==seltext.begline && curcol<seltext.begcol) || (currow==seltext.endline && curcol>seltext.endcol) || (currow>seltext.endline) )			goto over;		if(seltext.begcol==0 && seltext.begline>0)		{			ln=(seltext.begline-1)%CODE_QSET;			pg=LocatePage(NULL,seltext.begline-1);			targetbeg=GetCurColWidth(NULL,seltext.begline-1);		}		CopyLineUp(ppg->data[seltext.endline%CODE_QSET],seltext.endcol+1,len,pg->data[ln],targetbeg,1,TRUE);		shift=(seltext.begcol<=0 && seltext.begline>0)?(shift+1):(shift);		ShiftLinesUp(LocatePage(NULL,seltext.endline+1),seltext.endline+1,shift);		totalrow=totalrow-shift;		if(currow>=totalrow)		{			int cursor=currow-totalrow+1;			if(y>=cursor)			{				currow=totalrow-1;				wmove(basewin,y-cursor,0);			}			else			{				curpage=(int)((totalrow-1)/CODE_QSET);				currow=(abs(currow-shift-y))%CODE_QSET+curpage*CODE_QSET;				currow=(currow>=totalrow)?(totalrow-1):(currow);				wmove(basewin,0,0);			}		}		else		{			wmove(basewin,y,0);		}		totalcol=GetCurColWidth(NULL,currow);		maxcol=(maxcol>totalcol)?(maxcol):(totalcol);		curcol=0;		offcol=0;	}	seltext.selected=FALSE;	modified=TRUE;over:	ret=TRUE;	return ret;}void CodeArea::CodeIndent(){	int i=0,j=0;	PageList dptr;	char shell[256];	codefile file(MODULE_SECONAME,O_WRONLY);	if (file.success != ZC_OK) 	{		ErrMsg(OKONLYDLG,"open /dev/backinfo error!");		return;	}	dptr=pagelist;	do	{		if (dptr->data)		{			for (i = 0; i <CODE_QSET && j<totalrow; i++)			{				if (dptr->data[i])				{					if(file.putline(dptr->data[i])==-1)						;					j++;				}			}		}		dptr=dptr->next;	}while(dptr && dptr!=pagelist);	strcpy(shell,"cat ");	strcat(shell,MODULE_SECONAME);	strcat(shell,"|indent ");	strcat(shell," -o ");	strcat(shell,MODULE_PRIMNAME);	strcat(shell," 2>");	strcat(shell,MODULE_SECONAME);	system(shell);	ReadFile(MODULE_PRIMNAME);	modified=TRUE;}void CodeArea::AutoMake(){	char shell[256];	struct stat buf;	if (SaveFile()==FALSE) 	{		ErrMsg(OKONLYDLG," Save file error!");		return;	}	strcpy(shell,"gcc ");	strcat(shell,filenm);	strcat(shell," -o ");	strcat(shell,filenm);	strcat(shell,"Ex");	strcat(shell," 2>");	strcat(shell,filenm);	strcat(shell,"Mk");	system(shell);	strcpy(shell,filenm);	strcat(shell,"Mk");	if(stat(shell,&buf)==-1)	{		ErrMsg(OKONLYDLG,"Make error!");		goto over;	}	if(buf.st_size==0)	{		MakeMsg(NOTHINGDLG,"No error,no warning!");		goto over;	}	else	{		char *msgbuf=NULL;		codefile file(shell,O_RDONLY);		if (file.success != ZC_OK) 		{			goto over;		}		msgbuf=(char *)malloc(sizeof(char)*file.len);		strcpy(msgbuf,file.data);		MakeMsg(NOTHINGDLG,msgbuf);		free(msgbuf);	}over:	return;	//ReadFile(MODULE_SECONAME);}//end of file codearea.c

⌨️ 快捷键说明

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