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

📄 preprocessor.c.old1

📁 51模拟器 51模拟器 51模拟器
💻 OLD1
字号:
#include"gas51.h"#include<string.h>#define IS_NOT_DEFINED 0#define IS_DEFINED 1char dest[80];char tmpnambuff[12]="gas51";void gas51_perror();char * include_file(char *arg,char *file,long pos);int pat_index(char *,char *);char * remove_all_spaces(char *);int preprocess(char *file_to_preprocess)   //function to prprocess the assembly file...{	FILE *fp,newfp;	char buff[80]={0},line[80],tempbuff[40];	char *buff_cleaned,newfilebuff[20],*newfile;	char directive_arg[15],directive_expansion[15],directive_name[15],directive_symbol[15]  ;	int i,j,l;	newfile=newfilebuff;	fp=fopen(file_to_preprocess,"r");	gas51_line_no = 0;	strcpy(newfilebuff,file_to_preprocess);	while( fgets(buff,80,fp) )  {		gas51_line_no++;		for(i=0;i<=strlen(buff);i++)		{			    if(buff[i]==';')		    	break;	    		}		buff[i]='\0' ;            if(pat_index(buff,"#include")) /****EXTREMELY IMPORTANT!!!  there must be NO SPACE BETWEEN the '#' and 'include' ...**/			{			buff_cleaned = remove_all_spaces(buff) ;   			for(i=9,j=0;i<=strlen(buff_cleaned);i++,j++) // in this case filename starts from 10th location or 9th index...				directive_arg[j]=buff_cleaned[i] ;						directive_arg[strlen(directive_arg)-1] = '\0' ;  //to replace the last '>' with NULL...						include_file(directive_arg,newfile,ftell(fp));   			fclose(fp);			fp=fopen(tmpnambuff,"r");			//tmpnambuff[strlen(tmpnambuff)-6]='\0';			newfile=tmpnambuff;			}					    else            	if(pat_index(buff,"#define")) /****EXTREMELY IMPORTANT!!!  there must be NO SPACE BETWEEN the '#' and 'define' ...**/	    	{				    	}				}}int pat_index(char *src_str,char *substr){	int i=0,j=0,k=0,retval=0;	int len_substr , len_src_str ;	char temp_str[10];	len_substr = strlen(substr) ;	len_src_str = strlen(src_str) ;	for(i=0;i<(len_src_str - len_substr);i++)    {    		for(j=0,k=i;k<i+len_substr;j++,k++)  {		temp_str[j]=src_str[k] ;	}		temp_str[j] = '\0' ;	if(!strcmp(temp_str,substr))	{		retval=i+1;  //we have to increment i here coz arrays start with 0 but column no. in		break;	     //a file starts with 1...		}	else 		retval=0;				} 			return retval;}char * include_file(char *file_to_include,char *newfile, long pos){	FILE *fpsrc,*fptemp,*fpdest;	  	int i=0,newfd;		char *fname1, *fname2 ,buff[80] ,tempbuff[40];//tempnambuff[12]="gas51XXXXXX";		char *retptr;	char const1[]="/usr/include/gas51/" ;		//extern int gas51_sys_errlist[100];	char const2[]="./" ;		fname1=strcat(const1 ,file_to_include)  ;		fname2=strcat(const2 ,file_to_include) ;	// fptemp points to .asm file, fpdest points to .asm.tmp file,	if ( ((fpsrc=fopen(fname1 ,"r")) )  ||  ( (fpsrc=fopen(fname2,"r")) ))	{       		// code to include file contents goes here...				fptemp=fopen(newfile,"r");		tmpnambuff[strlen(tmpnambuff)-6]='\0';		newfd=mkstemp(strcat(tmpnambuff,"XXXXXX"));		retptr=tmpnambuff;		//newname = tmpnam(tempbuff);		//strcpy(tempbuff,newfile);		//strcat(tempbuff,".tmp");		//fpdest=fopen(tempbuff,"w");		fpdest=fdopen(newfd,"w");		while(1)		{			fgets(buff,80,fptemp) ;			if(  (ftell(fptemp)) == pos  )				break;			else				fprintf(fpdest,"%s",buff);			}						//fseek(fptemp,pos,SEEK_SET);						while( fgets(buff,80,fpsrc)  )		{			fprintf(fpdest,"%s",buff);						}							fseek(fptemp,pos,SEEK_SET);				while( fgets(buff,80,fptemp)  )		{			fprintf(fpdest,"%s",buff);						}							fclose(fpsrc);		fclose(fpdest);		fclose(fptemp);				return retptr;					}				else			{		gas51_errno = 0 ;  // 0 -> code for file not found!!		gas51_perror() ;		exit(0) ;			}		}char * remove_all_spaces(char *buff_to_clean){	int i,j,k;	static char cleaned_buff[80];	for(i=0,j=0;i<strlen(buff_to_clean);i++)	{		if(buff_to_clean[i]!=' ' && buff_to_clean[i]!='\t' && buff_to_clean[i]!='\n' && buff_to_clean[i]!='\r' )			{				cleaned_buff[j] = buff_to_clean[i];				j++;			}		else 			continue;	}	cleaned_buff[j]= '\0' ;	return cleaned_buff ;}			void gas51_perror(){	printf("gas51: %s : %d : %s \n", gas51_file_being_assembled, gas51_line_no, gas51_errlist[gas51_errno]);}//void insert_into_define_list(char *Directive_Symbol, char *Directive_Expansion , int flag )//{//	gas51_define_list *tempStruct,*tempPtr;			/* forming the define structure prior to insertion into the linked list...*/	/*	tempStruct = (gas51_define_list *)malloc(sizeof(gas51_define_list)) ;	tempStruct->symbol = Directive_Symbol ;	tempStruct->expansion = Directive_Expansion ; 	tempStruct->flag = IS_DEFINED ;	tempStruct->next = NULL ;	tempPtr = &head ;	while(tempPtr->next)	{	   tempPtr = tempPtr->next ;	}			tempPtr = tempStruct ;	head.next = tempPtr ;			}*/

⌨️ 快捷键说明

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