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

📄 fileswap.h

📁 简单的Dos界面的文本编辑器
💻 H
📖 第 1 页 / 共 3 页
字号:
		{
			if(j>i &&signfile[checkfile[j]][1]!=0)
			{
				signfile[checkfile[j]][0]-=2;
			}
		}
	}	
	if(SumLNow()!=0 && lnum!=SumLNow()+1)
		current=lnum;
	if(SumLNow()!=0 && lnum==SumLNow()+1)
		current=lnum-1;
	if(SumLNow()==0)
		current=0;
	return 0;
}
/////NOTE: after call Position(),you must free the memory of the return str!!!!!!!!!!
//move current point to lnum if success return the line point ,else return NULL
//or load it in memory from file
char* FileSwap::Position(long int lnum=current)
{
	int i=0;
	char* str;
	if(lnum<signfile[0][0]+signfile[0][1] && lnum>=signfile[0][0])
	{   //when the line lnum is in memory , get it directly
		str=GetLine(lnum-signfile[0][0]+1);
		current=lnum;
	}
	else
	{
		Insert("",lnum);   //to load line lnum into memory
		DeleteLine(lnum+1);		
		str=GetLine(lnum-signfile[0][0]+1);	
	}	
	return str;
} 
//Display the line lnum to screen
int FileSwap::Display(long int lnum=current)
{
	int i;
	long j;
	char *str,*st=new char[128];
	for(i=2; i>=0; i--)
	{
		if(signfile[i][1]>0)
			break;
	}
	if( lnum<1 || lnum>(signfile[checkfile[i]][0]+signfile[checkfile[i]][1]-1) )
	{
		printf("--------line %4d for display is invalid-----",lnum);
		return -1;
	}
	//find the line lnum in which part
	for(i=0; i<3; i++ )
	{
		if( signfile[checkfile[i]][0]+signfile[checkfile[i]][1] > lnum )
			break;
	}
	if(checkfile[i]==0)  //line lnum is in memory
	{
		str=GetLine(lnum-signfile[checkfile[i]][0]+1);
		printf("  %s",str);
		delete str;
		return 0;
	}
	else
	{
		rewind(tempfp[checkfile[i]-1]);
		for(j=signfile[checkfile[i]][0]; j<=lnum; j++) 
		{
			fgets(st,128,tempfp[checkfile[i]-1]);
			if(j==lnum)
			{
				cout<<st;
				delete st;
				return 0;
			}
		}
	}
	return -1;
}
///////write line in file *fp after line lnum 
int FileSwap::ReadInto(FILE *fp,long int lnum=current)
{
	char *str=new char[128];
	for(int i=2; i>=0; i--)
	{
		if(signfile[checkfile[i]][1]!=0)
			break;
	}
	if( lnum<1 || lnum>(signfile[checkfile[i]][0]+signfile[checkfile[i]][1]-1) )
	{
		//cout<<"line"<<lnum<<"for display is invalid"<<endl;
		return -1;
	}
	
	while(str=fgets(str,128,fp))
	{		
		Insert(str,lnum++);
		//if(lnum==2000)
		//	cout<<"here"<<endl;
	}
	return 0;
}

//current line move to lnum after this function 
long FileSwap::FindStr(char *str,long lnum=current)
{
	char *st;
	st=Position(lnum);    //current line move to lnum
    int length1=strlen(str),length2=strlen(st);
	for( int i=0; i<=length2-length1; i++ )
	{
		if( *(st+i)==*str && *(st+i+length1-1)==*(str+length1-1) )
		{
			*(st+i+length1)=NULL;
			if( !strcmp( (st+i), str ) )
			{
				delete st;
				return lnum;
			}				
		}
	}
	delete st;
	return -1;
}
//replace all substring str_target with new string str_src in current line 
//unless the total characters no more than line_in_memory bytes
//success return 0; else return -1
int FileSwap::Replace(char *str_target,char *str_src)
{
	char *strr,*st,*new_line=new char[512];
	int flag=0;    //sign whether str_target was found in current line ,sucess 1,else 0;
	*new_line=NULL;
	char *string=new char[128];
	//////////////////////////////////////////////////////////////////
	st=Position();//current line move to lnum
	strr=st;       //for free the memory called in Position()
	int origin_len=strlen(strr);
    int length1=strlen(str_target),length3=0;
	int sign=0;
	while((signed)strlen(st) >= length1 )
	{
		sign+=1;
		//if(sign==6)
		//	cout<<"here"<<endl;
		int length2=strlen(st);
		for( int i=0; i<=length2-length1; i++ )
		{
			if( *(st+i)==*str_target && *(st+i+length1-1)==*(str_target+length1-1) )
			{
				char ch=*(st+i+length1);
				*(st+i+length1)=NULL;
				if( !strcmp( (st+i), str_target ) )
				{
					flag=1;
					*(st+i+length1)=ch;			
					strcpy(string,st+i+length1);
					break;
				}
				else
				{
					flag=0;
					*(st+i+length1)=ch;	
				}
			}
			else
			{
				flag=0;
			}
		}
		//if(i+length1>origin_len)
		//	return -1;
		if(strlen(st)<(unsigned)origin_len && flag==0)
		{
			strcat(new_line,st);   //string is the last part of the old string of origin current line
			break;
		}
		if(flag==0 && strlen(st)==(unsigned)origin_len)
		{
			return -1;
		}
		//copy the first part to new_line
		*(st+i)=NULL;
		strcat(new_line,st);
		strcat(new_line,str_src);
		strcpy(st,string);
		length3=strlen(new_line);
		if(((line_in_memory-1)-length3)<=(signed)strlen(st) &&(unsigned)((line_in_memory-1)-length3)>=strlen(st)+strlen(str_target)-strlen(str_src))
		{
			strcat(new_line,str_src);
			strcat(new_line,st+length1);
			break;
		}
		int len1=strlen(st);
		int len2=(strlen(st)+strlen(str_target)-strlen(str_src));
		if(((line_in_memory-1)-length3)>len1 && (((line_in_memory-1)-length3)<len2 || len1<length1))
		{
			strcat(new_line,st);
			break;
		}
		if(((line_in_memory-1)-length3)>len1 && ((line_in_memory-1)-length3)== len2)
		{
			strcat(new_line,str_src);
			strcat(new_line,st+length1);
			break;
		}
	}	
	DeleteLine();
	if(SumLNow()!=0)
	{
		Insert(new_line,current-1);
	}
	else
	{
		Insert(new_line,0);
	}
	delete strr;
	delete string;
	delete new_line;
	return 0;
	
}

//current line move to lnum after this function 
long FileSwap::Find(char *str,long lnum=current)
{
	char *st;
	st=Position(lnum);    //current line move to lnum
    int length1=strlen(str),length2=strlen(st);
	for( int i=0; i<=length2-length1; i++ )
	{
		if( *(st+i)==*str && *(st+i+length1-1)==*(str+length1-1) )
		{
			*(st+i+length1)=NULL;
			if( !strcmp( (st+i), str ) )
			{
				delete st;
				return lnum;
			}				
		}
                else
                {
                      delete st;
                      return -1;
                }
	}
	return -1;
}

/////////////////////////////////////////////////////////////////////////////////////////
//copy length lines begin line src after line target 
int FileSwap::CopyLine(long src,long tar=current,int length=1)
{
	char *str="";
	int count1=0,count2=0;
	for(int i=2; i>=0; i--)
	{
		if(signfile[checkfile[i]][1]>0)
			break;
	}
	if( tar<0 || tar>(signfile[checkfile[i]][0]+signfile[checkfile[i]][1]-1) )
	{
		//cout<<"line"<<tar<<"for display is invalid"<<endl;
		return -1;
	}
	if( src<1 || src>(signfile[checkfile[i]][0]+signfile[checkfile[i]][1]-1) )
	{
		//cout<<"line"<<src<<"for display is invalid"<<endl;
		return -1;
	}
	if(src+length-1>SumLNow())
		length=SumLNow()-src+1;
	int j=0;
	//the target line is the last line of the file
	if(tar==SumLNow())   
	{
		for(i=1;i<=length;i++)
		{
			str=Position(src+count2++);
			Append(str);
			delete str;
		}
	}
	else
	{
		for( i=1; i<=length; i++)
		{
			if(src+length>=tar && src<tar)
			{
				if(i<=tar-src+1)
				{
					str=Position(src+count2++);
					Insert(str,tar+count1++);
				}
				else
				{	
					count2++;				
					str=Position(count2+tar+j++);
					Insert(str,tar+count1++);
					
				}
			}
			else
			{
				if(tar>src)
				{
					str=Position(src+count2++);
					Insert(str,tar+count1++);
				}
				else
				{
					str=Position(count2+src++);
					Insert(str,tar++);
					count2++;
				}
			}
			delete str;
		}
	}
	return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/////append sting str at the end of the file operated
void FileSwap::Append(char *str)
{
	for(int i=2;i>=0;i--)
	{
		if(signfile[checkfile[i]][1]>0)
			break;
	}
	if(i<0)
		Insert(str,1);
	else
	{
		Insert(str,SumLNow());
	}
}
/////////////////////////////////////////////////////////////////////////////////
//Note::the source file opened when object initialized must open with "r+"
int FileSwap::Save()
{
	rewind(fpoint);
	ReadOut(fpoint);		
	return 0;
}
////////////////////////////////////////////////////////////////////////////////////
//save the source file in a new file
int FileSwap::SaveAs(FILE *fp)
{	
	if(!fp)
	{
		return -1;
	}
	else
	{
		ReadOut(fp);
		fclose(fp);
	}
	return 0;
}
//save line form start_line to line strar_line+length into file filename,if start_line is less than 1,
//evaluate it with 1,if it larger than tha maxline,make it equal to maxline
int FileSwap::BlockSaveAs(FILE *fp,long start_line,long length)
{
	char *stline;	
	if(SumLNow()==0)
	{
		fclose(fp);
		return 0;
	}
	if(start_line<1)
		start_line=1;
	if(start_line>SumLNow())
		start_line=SumLNow();
	if( start_line+length-1 > SumLNow() )
		length=SumLNow()-start_line+1;
	for(long i=0; i<length; i++)
	{
		stline=Position(start_line+i);
		fputs(stline,fp);
		//if(i==(line_in_swap-1))
		//	cout<<"here"<<endl;
		delete stline;
	}
	//fclose(fp);
	return 0; //successful
}
/////////////////////////////////////////////////////////////////////
//output file operated into file fp//but not close file fp
int FileSwap::ReadOut(FILE *fp)
{
	long int j;
	char *st,*str=new char[128];	
	if(!fp)
	{
		cout<<"open File error\n";
		return -1;
	}
	for( int i=0; i<3; i++ )
	{
		if(checkfile[i]==0)
		{
			for( j=1; j<=signfile[0][1]; j++ ) 
			{
				st=GetLine(j);				
				fputs(st,fp);				
			    //BlockFile::DeleteLine(1);
				delete st;			
			}
		}
		else
		{		
			rewind(tempfp[checkfile[i]-1]);
			for( j=1; j<=signfile[checkfile[i]][1]; j++ )
			{
				fgets(str,128,tempfp[checkfile[i]-1]);
				fputs(str,fp);
			}
		}
	}
	delete str;
	return 0;
}


int FileSwap::Print(long lnum=current,int linemark=0) //0 no linemark   1 with
{
	long begin=0,endline=0;
	int length=0;
	char *str="";
	if(lnum<1 || lnum>SumLNow())
		return -1;
	else 
	{
		if(lnum-10<1)
			begin=1;
		else
			begin=lnum-10;
	}
	if(lnum+10>SumLNow())
		endline=SumLNow();
	else
	{
		endline=lnum+10;
	}
	printf("------------------------------------------------------------------------------\n");
	for( ; begin<=endline; begin++)
	{
		str=Position(begin);
		length=strlen(str);
		if(length<=75)
		{
			if(linemark==1)
				printf("%4d:%s",begin,str);
			else
				printf("  %s",str);			
		}
		else
		{
			if(linemark==1)
			{
				printf("%4d:",begin);
				for(int j=0; j<75; j++)
					printf("%c",*(str+j));
				printf("     %s",(str+j));			
			}
			else
			{
				for(int j=0; j<75; j++)
					printf("%c",*(str+j));
				printf("     %s",(str+j));				
			}
		}
		delete str;
	}
	current=lnum;
	printf("-------------------------------------------------------------------------------\n");
	return 0;
}



⌨️ 快捷键说明

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