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

📄 fileswap.h

📁 简单的Dos界面的文本编辑器
💻 H
📖 第 1 页 / 共 3 页
字号:
////////////////////////////////////////////
///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!the use of function Optimize
/////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <io.h>
#include "blockfil.h"

#define line_in_memory 400     //the max lines operated in memory each time
#define line_in_swap 200       //lines write into memory during each file swap

class FileSwap:public BlockFile
{
   private:
	   long int signfile[3][2];   //signfile[i][0] sign tempfile i 's beganning linenum
	                              //signfile[i][1] sign tempfile i 's total line number
	   long int currentfp[2];     // save the end position of the tempfile after each file operation
	   int checkfile[3];          //using the index to maintain memory,temp1,temp2 's order
	   //char *filepath;
	   int file_exist;            //sign file to operation is exsit or not 
	   FILE* fpoint;              //save file point of the file opened when constructor called
   public:	   
	   static long int current;
	   FILE *tempfp[2];           //point to the two temp file
	   FileSwap(FILE *fp);
	   FileSwap(void);
	   FileSwap(FileSwap &obj);
	   ~FileSwap(void);
	   long int Insert(char* input,long int lnum); //insert input in line lnum,	return the current line							
	   int DeleteLine(long int lnum);         //delete line lnum
	   char* Position(long int lnum);         /////!!!Note: After you call function FindStr(),you must delete the reutrn value
	                                          //move current point to lnum if success return the line point ,else return NULL
	                                          //or load it in memory from file
	   long int SumLNow(void);                //count total line now of the file opened
	   int Print(long lnum,int linemark);
	   int Display(long int lnum);            //print line lnum to screen	  
	   int CopyLine(long src,long tar,int length);       //copy line src after line tar
	   void Append(char *str);                //append str at the end of file
	   long FindStr(char* str,long lnum);
	   long Find(char *str,long lnum);
	   int Replace( char *str_target,char *str_src); //replace substring str_target with new string str_src in current line
	   ////////////////////////////////////
	   int ReadInto(FILE *fp,long int lnum);    //write fp after line lnum 
	   int ReadOut(FILE *fp);                //write current file modified to fp
	   int Save();                       //save file and go no operating
	   int SaveAs(FILE *fp);
	   int BlockSaveAs(FILE *fp, long start_line, long length);//save line start_line to start_line+length to file filename
};

 long int  FileSwap::current=0;

//file exist,copy the first line line_in_swap to memory,the last write to tempfp[0]
FileSwap::FileSwap(FILE *fp):BlockFile()
{
	long int cnt=0;	
	char str[128];  //save a line read from *fp	
	file_exist=1;
	fpoint=fp;
	tempfp[0]=tmpfile();//fopen("t1m_x_z1.xtc","w+");
	if(tempfp[0]==NULL)
	{   
		cout<<"Creat template file Error\n";
		exit(1);
	}
	tempfp[1]=tmpfile();//fopen("t1m_x_z2.xtc","w+");
	if(tempfp[1]==NULL)
	{
		cout<<"Creat template file Error\n";
		exit(1);
	}
    //read the first line_in_swap lines to memory operating
	while(cnt<line_in_swap)
	{
		fgets(str,128,fp);	
		InsertLine(str,cnt++);		
	}	
	current=1;
	signfile[1][1]=0;	
	while(fgets(str,128,fp)!= NULL )//fomat fp ,a line character is less than 127
	{                                   //each line writing to memory is end with '\n'
		if(strlen(str)==127 && str[126]!='\n')
		{
			str[126]='\n';
			str[127]=NULL;
			fseek(fp,-1L,SEEK_CUR);// not safe to fseek here!!!!!!!!!!!such as CR/LF
		}
		fputs(str,tempfp[0]);
		signfile[1][1]++;
	}
	currentfp[0]=ftell(tempfp[0]);
	currentfp[1]=ftell(tempfp[1]);
	checkfile[0]=0;       //the first part is in memory
	checkfile[1]=1;       //the second part is in tempfp[0]
	checkfile[2]=2;       //the third part is in tempfp[1]
	signfile[0][0]=1;     // line[0] is the first line in memory
	signfile[0][1]=GetLcnt();
	if(signfile[1][1]!=0)
	{
		signfile[1][0]=GetLcnt()+1;
	}
	else
	{
		signfile[1][0]=0;
	}
	signfile[2][0]=0;
	signfile[2][1]=0;
	current=1;
	//fcloseall();
	//cout<<"construtor ok\n";
}
FileSwap::FileSwap(void):BlockFile()
{
	file_exist=0;	
	currentfp[0]=0;
	currentfp[1]=0;
	tempfp[0]=tmpfile();//fopen("t1m_x_z1.xtc","w+");
	if(tempfp[0]==NULL)
	{   
		cout<<"Creat template file Error\n";
		exit(1);
	}
	tempfp[1]=tmpfile();//fopen("t1m_x_z2.xtc","w+");
	if(tempfp[1]==NULL)
	{
		cout<<"Creat template file Error\n";
		exit(1);
	}
	checkfile[0]=0; //the first part is in memory
	checkfile[1]=1; //tempfp[0] is empty
	checkfile[2]=2; //tempfp[1] is empty
	signfile[0][0]=1;  //line[0] is the first line in memory
	signfile[0][1]=0;
	for(int i=1; i<=2; i++ )
	{
		for(int j=0; j<2; j++ )
			signfile[i][j]=0;
		//cout<<"construtor ok\n";
	}
}
FileSwap::FileSwap(FileSwap &obj)
{
	for(int i=0; i<3; i++)
	{
		checkfile[i]=obj.checkfile[i];
		signfile[i][0]=obj.signfile[i][0];
		signfile[i][1]=obj.signfile[i][1];
	}
	current=obj.current;
	currentfp[0]=obj.currentfp[0];
	currentfp[1]=obj.currentfp[1];
	file_exist=obj.file_exist;
	fpoint=obj.fpoint;
	tempfp[0]=obj.tempfp[0];
	tempfp[1]=obj.tempfp[1];
}
FileSwap::~FileSwap(void)
{
    //fclose(tempfp[0]);
    //fclose(tempfp[1]);
    ///fcloseall();
    //cout<<"distructor operation ok!\n"<<endl;
    //system("del t1m_x_z1.xtc");
    //system("del t1m_x_z2.xtc");
}
long int FileSwap::SumLNow(void)
{
	//long totalline=signfile[0][1]+signfile[1][1]+signfile[2][1];
	return signfile[0][1]+signfile[1][1]+signfile[2][1];//totalline;
	//access is valid
}
///insert a string at line lnum, use memory of line_in_memory lines and two tempfie
long int FileSwap::Insert(char *input,long int lnum=current)
{
   int ipflag;//ipflag sign the indes of the array checkfile[3]                       
   long linen=0;
   char str[128],*strp;   
   for(ipflag=0; ipflag<3; ipflag++ )
   {	      
	   if( lnum>=0 && signfile[checkfile[ipflag]][1]!=0 && (signfile[checkfile[ipflag]][0]+signfile[checkfile[ipflag]][1])>lnum )
	   { 		  
		   break; //checkfile[ipflag] sign which part is to insert,memory or tempfile
	   }      	   
	   else if(ipflag>2)
	   {
			   return -1;              //operation is failed
		  
	   }
   } 
   if( signfile[0][1]==0 && signfile[1][1]==0 && signfile[2][1]==0 ) 
   {
	   ipflag=0;
	   checkfile[ipflag]=-1;   //used to run to the end condition directly
   }
   //insert operation in memory
   if(checkfile[ipflag]==0)   
   {  
	   int length=strlen(input);
	  // str[128]="";
	   if(length<128)
	   {
		   //memory is available
		   if(signfile[0][1]<line_in_memory )
		   {
			   InsertLine(input,lnum-signfile[0][0]+1);
			   for( int j=0; j<3 ; j++ )
			   {
				   if(j>ipflag && signfile[checkfile[j]][1]!=0)
					   signfile[checkfile[j]][0]++;
			   }
			   signfile[0][1]++;
			   current=lnum+1;
			   return lnum;
		   }
		   //memory is full when insert
		   else if(signfile[0][1]==line_in_memory)
		   {
			   //memory is the first part
			   //char c='';
			   long int tmplnum,tmpnum;
			   if(checkfile[0]==0)
			   {
				   fseek(tempfp[checkfile[1]-1],currentfp[checkfile[1]-1],0);
				   rewind(tempfp[checkfile[2]-1]);
				   for(int i=1; i<=signfile[checkfile[2]][1]; i++ )
				   {					
					   fgets(str,128,tempfp[checkfile[2]-1]);
					   fputs(str,tempfp[checkfile[1]-1]);
				   }
				   currentfp[checkfile[1]-1]=ftell(tempfp[checkfile[1]-1]);//save the changed point's pos				   
				   rewind(tempfp[checkfile[2]-1]);
				   tmplnum=lnum-signfile[0][0]+1;           //convert lnum to corresponding tmplnum in memory
				   tmpnum=tmplnum;
				   //write the line after lnum-1 to tempfp[checkfile[2]-1]
				   while(tmplnum<line_in_memory)
				   {
					   strp=GetLine(tmpnum);
					   fputs(strp,tempfp[checkfile[2]-1]);
					   BlockFile::DeleteLine(tmpnum);
					   delete strp;
					   tmplnum++;
				   }
				   if(tmplnum==line_in_memory)
					   fputs(input,tempfp[checkfile[2]-1]);
				   else
					   InsertLine(input,lnum-signfile[0][0]+1);
				   currentfp[checkfile[2]-1]=ftell(tempfp[checkfile[2]-1]);
				   				   
				   int swap;
				   swap=checkfile[1];
				   checkfile[1]=checkfile[2];
				   checkfile[2]=swap;
				   ////a file empty is controlled by signfile[i][1]==0
				   if(signfile[checkfile[2]][1]!=0)           //when tmepfp[checkfile[2]] is not empty
				   {
					   signfile[checkfile[2]][0]+=1;
				   }
				   else if(signfile[checkfile[2]][0]==0)
				   {
					   signfile[checkfile[2]][0]=signfile[checkfile[1]][0];
				   }
				   signfile[checkfile[1]][0]=lnum+1;				
				   signfile[checkfile[1]][1]=signfile[0][0]+signfile[0][1]-lnum;				
				   signfile[0][1]=lnum;	
				   current=lnum+1;
				   return lnum;
			   }
			   //memory is the second part
			   else if(checkfile[1]==0)
			   {
				   //copy others into tempfp[checkfile[0]]
				   fseek(tempfp[checkfile[0]-1],currentfp[checkfile[0]-1],0);
				   rewind(tempfp[checkfile[2]-1]);
				   for( int i=1; i<=line_in_memory; i++ )
				   {        
					   strp=GetLine(1);
					   fputs(strp,tempfp[checkfile[0]-1]);
					   delete strp;
					   BlockFile::DeleteLine(1);
				   }				   
				   for( i=1; i<=signfile[checkfile[2]][1]; i++ )
				   {					
					   fgets(str,128,tempfp[checkfile[2]-1]);
					   fputs(str,tempfp[checkfile[0]-1]);
				   }
				   long int sumline=signfile[0][1]+signfile[1][1]+signfile[2][1];
				   //////////////////////////////////////////
				   long int cntl=signfile[0][0];        //for more effect
				   fseek(tempfp[checkfile[0]-1],currentfp[checkfile[0]-1],0);
				   for( long int cnt=signfile[0][0]; cnt<lnum; cnt++ )  //find the line lnum beginning fpoint to move to memory
				   {
					   if( fgets(str,128,tempfp[checkfile[0]-1]) )
						   cntl++;
				   }//after this ,file pointer point to head of lnum in tempfp[checkfilep0]-1]
				   currentfp[checkfile[0]-1]=ftell( tempfp[checkfile[0]-1] );//the true end in this insert of tempfp[checkfile[0]-1]
				   ///write <=line_in_swap lines to memory 
				   //str="";
				   i=0;///////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!
				   while(cntl<=sumline)
				   {						
					   if( cntl<lnum+line_in_swap )
					   {							   
						   if( fgets(str,128,tempfp[checkfile[0]-1]) )
						   {
							   cntl++;								   
							   InsertLine(str,i++);								   
						   }
					   }
					   else
						   break;
				   }				  
				   //write the last to tempfp[checkfile[0]-1] if have
				   rewind(tempfp[checkfile[2]-1]);
				   long int fflag=cntl;				   
				   while( cntl<=sumline )
				   {
					   fgets(str,128,tempfp[checkfile[0]-1]);						   
					   fputs(str,tempfp[checkfile[2]-1]);
					   cntl++;
				   }
				   currentfp[checkfile[2]-1]=ftell(tempfp[checkfile[2]-1]);					   
				  
				   InsertLine(input,1);////////////////
				   signfile[checkfile[0]][1]=lnum-1;

				   signfile[checkfile[0]][0]=1;
				   signfile[checkfile[1]][0]=lnum;
				   if(fflag<=sumline)//tempfp[checkfile[2]-1] is not empty
				   {
					   signfile[checkfile[1]][1]=(line_in_swap+1);
					   signfile[checkfile[2]][0]=lnum+(line_in_swap+1);
					   signfile[checkfile[2]][1]=sumline-lnum-(line_in_swap-1);
				   }
				   else
				   {
					   signfile[checkfile[1]][1]=sumline-lnum+2;
					   signfile[checkfile[2]][0]=0;
					   signfile[checkfile[2]][1]=0;
				   }
				   current=lnum+1;
				   return lnum;
			   }
			   //memory is the third part
			   else		   
			   {
				   //copy others into tempfp[checkfile[0]-1]
				   fseek(tempfp[checkfile[0]-1],currentfp[checkfile[0]-1],0);
				   rewind(tempfp[checkfile[1]-1]);			   
				   for(long int i=1; i<=signfile[checkfile[2]][1]; i++ )
				   {					
					   fgets(str,128,tempfp[checkfile[1]-1]);
					   fputs(str,tempfp[checkfile[0]-1]);
				   }
				   long int tmpfpt=ftell(tempfp[checkfile[0]-1]);  //for more effect
				   for( i=1; i<=line_in_memory; i++ )
				   {
					   strp=GetLine(1);
					   fputs(strp,tempfp[checkfile[0]-1]);
					   delete strp;
					   DeleteLine(1);
				   }				   
				   long int sumline=signfile[0][1]+signfile[1][1]+signfile[2][1];
				   //////////////////////////////////////////
				   long int cntl=signfile[1][1]+signfile[2][1]+1;        //the first line num of old memory				   
				   fseek(tempfp[checkfile[0]-1],tmpfpt,0);
				   for( int cnt=cntl; cnt<lnum; cnt++ )  //find the line to move to memory
				   {
					   if( fgets(str,128,tempfp[checkfile[0]-1]) )
						   cntl++;
				   }//after it ,file pointer point to head of line lnum in tempfp[checkfile[0]-1]
				   currentfp[checkfile[0]-1]=ftell(tempfp[checkfile[0]-1]);//the true end in this insert of tempfp[checkfile[0]-1]					   
				   i=0;					   
				   while(cntl<=sumline)
				   {						
					   if( cntl<lnum+line_in_swap )
					   {							   
						   if( fgets(str,128,tempfp[checkfile[0]-1]) )
						   {
							   cntl++;								   
							   InsertLine(str,i++);								   
						   }
					   }
				   }
				   InsertLine(input,lnum-signfile[0][0]+1);/////////////////////////
				   if(cntl<=sumline)
				   {
					   //write the last to tempfp[checkfile[0]-1] if have
					   rewind(tempfp[checkfile[2]-1]);
					   while( cntl<=sumline )
					   {
						   fgets(str,128,tempfp[checkfile[0]-1]);
						   fputs(str,tempfp[checkfile[1]-1]);
						   cntl++;							   
					   }
					   currentfp[checkfile[1]-1]=ftell(tempfp[checkfile[1]-1]);
					   signfile[checkfile[0]][0]=1;
					   signfile[checkfile[0]][1]=lnum-1;
					   signfile[checkfile[1]][0]=lnum+(line_in_swap+1);
					   signfile[checkfile[1]][1]=sumline-lnum-(line_in_swap-1);
					   signfile[checkfile[2]][0]=lnum;
					   signfile[checkfile[2]][1]=(line_in_swap+1);
					   int swap=checkfile[2];
					   checkfile[2]=checkfile[1];
					   checkfile[1]=swap;						   
				   }
				   else
				   {
					   rewind(tempfp[checkfile[1]-1]);
					   currentfp[checkfile[1]-1]=ftell(tempfp[checkfile[1]-1]);
					   signfile[checkfile[0]][0]=1;
					   signfile[checkfile[0]][1]=lnum-1;
					   signfile[checkfile[1]][0]=0;
					   signfile[checkfile[1]][1]=0;
					   signfile[checkfile[2]][0]=lnum;
					   signfile[checkfile[2]][1]=sumline-lnum+2;
				   }	
				   current=lnum+1;
				   return lnum;
			   }
		   }
	   }
	   if(length>=128)
	   {
		   char str[128]="";
		   while( strlen(input)>=128 )
		   {
			   int offset=127;       //sign how many characters input to move after each insert
			   strncpy(str,input,127);
			   if(str[126]!='\n')
			   {
				   str[126]='\n';
				   str[127]=NULL;
				   offset=126;
			   }			  
			   else
			   {
				   str[127]=NULL;
			   }
			   //memory is available
			   if(signfile[0][1]<line_in_memory)
			   {
				   if( InsertLine(str,lnum-signfile[0][0]+1)==0 )
				   {
					   //for( int j=0; j>ipflag && j<3; j++ )
					   //{
						//   signfile[checkfile[j]][0]++;//part after memory first line num+1
					   //}
					   signfile[0][1]++;
					   for(int i=0;i<3;i++)
					   {
						   if(checkfile[i]==0)//find memory in which part
							   break;
					   }
					   for(int j=0;j<3;j++)
					   {
						   if( j>i && signfile[checkfile[j]][1]!=0 )
							   signfile[checkfile[j]][0]++;

					   }
					   lnum++;
					   input+=offset;					   
				   }
				   else 
					   return -1;
			   }
			   //memory is exceed,using swapfile to solve
			   else
			   {
				   Insert(str,lnum);
				   lnum++;
				   input+=offset;				   
			   }
		   }
		   //solve input being less than 128 or is "\x0" 
		   if(strlen(input)>0)
		   {
			   Insert(input,lnum);
			   current=lnum+1;
		   }
	   }	   
	   return lnum;
   }
   //insert in tempfile
   else if( checkfile[ipflag]!=-1 )

⌨️ 快捷键说明

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