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

📄 def.h

📁 本人上程序设计课时
💻 H
📖 第 1 页 / 共 3 页
字号:
        }
        DrawSelectText();
    setStateWindow();
	return returnvalue;
}

/*显示消息,tape==0显示错误消息,tape==1显示y/n消息,tape==2显示Replace message消息*/
int MessageBox(char *msg,int tape)
{
    char *buf;
    int key,n,r=0;
    char *title[]={"Error","Message","Replace message"};
    char yn[]="(y/n)";

    n=strlen(msg)+4;
    if(tape)
        n+=5;
    buf=(char*)malloc((n+1)*3*2);
    gettext((80-n)/2,8,(80+n)/2,10,buf);
    window((80-n)/2,8,(80+n)/2,10);
    textbackground(LIGHTGRAY);
    textcolor(RED);
    clrscr();

    gotoxy(3,2);
    cputs(msg);
    if(1==tape)
        cputs(yn);

    box(1,1,n,3);
    gotoxy(4,1);
    cputs(title[tape]);

   while(1)
    {
        key=bioskey(0);
        if(tape==0||tape==2)
        {
        if(key==Key_ESC)
            break;
        }else{
            if(key==Key_Y||key==Key_UY)
            {r= 1;  break;}

            if(key==Key_N||key==Key_UN)
            {break;}
    }
    }
    puttext((80-n)/2,8,(80+n)/2,10,buf);
    setTextWindow();
    return r;
}


/*以下定义文件各项操作*/

struct FileLine * NewLine()
{
    struct FileLine *temp;
    if(temp=(struct FileLine * )malloc(sizeof(struct FileLine)))
    {
        temp->lineNR=NULL;
        temp->pro=NULL;
        temp->next=NULL;
		temp->num=0;
    }
    return temp;
}

int AppendLine()
{
    int restnum=0,col=0,i;
    struct FileLine *temp;

    if((temp=NewLine())==0)
    {
        MessageBox("short of memory!",0);
        return 0;
    }
    iTotalLine++;      /**/
    temp->pro=current; /*新建行插入到当前行之后*/
    if(current->next)
		current->next->pro=temp;
    temp->next=current->next;
    current->next=temp;

    col=iStartCol+txtGB.x;
    if(current->lineNR&&strlen(current->lineNR)>col-1)/*如果插入点后有文本,移到下一行*/
    {
      restnum=strlen(current->lineNR)-col+1;
        if(restnum>temp->num-1)
        {
            free(temp->lineNR);
            temp->lineNR=(char*)malloc(restnum+1);
            if(!temp->lineNR)
            {
                free(temp);
                MessageBox("short of memory!",0);
                return 0;
            }
            temp->num=restnum+1;
            memset(temp->lineNR,0,restnum+1);
        }
        strcpy(temp->lineNR,current->lineNR+col-1);
        memset(current->lineNR+col-1,0,restnum);
    }
    current=temp;  /*point to the new line*/
    txtGB.x=1;     /*设置光标位置*/
    iStartCol=0;

    if(txtGB.y<21)
    {
      txtGB.y++;
    }else{
    
        top=top->next;
        iStartLine++;
    }
    DrawView();
	return 1;
}

int OnModify(char ch,int flag) /*flag ==0 为插入,flag==1为改写状态*/
{
    char *tempstr;
    int col,n2,i;
    bNeedSave=1;

    col=txtGB.x+iStartCol;
	if(!current->lineNR)
	{
		current->lineNR=malloc(10);
		if(!current->lineNR)
		{
			MessageBox("short of memory!",0);
			return 0;
		}
		memset(current->lineNR,0,10);
		current->num=10;
		n2=0;
	}else{
		n2=strlen(current->lineNR);}

    switch(flag)
    {
    case 0:
        if(n2==current->num-1)/*空间不足*/
            {
                current->num+=10;
                if((tempstr=(char*)malloc(current->num))==0)
                {
                    MessageBox("Short of memory!",0);
                    return 0;
                }

                memset(tempstr,0,current->num);
                strcpy(tempstr,current->lineNR);
                free(current->lineNR);
                current->lineNR=tempstr;
            }
            
        if(n2==col-1)/*行末插入字符*/
        {
                current->lineNR[n2]=ch;
                current->lineNR[n2+1]='\0';
            }else{   /*行中插入字符*/
                i=n2;
                while(i>col-1)
                {
					current->lineNR[i]=current->lineNR[i-1];
					i--;
                }
                current->lineNR[i]=ch;
            }
            break;

    case 1:

        if(n2==col-1)/*光标位于行末尾*/
            {
                if(n2==current->num-1)/*空间不足*/
                    {
                        current->num+=10;
                        /*tempstr=(char*)malloc(current->num);*/
                        if((tempstr=(char*)malloc(current->num))==0)
                        {
                            MessageBox("Short of memory!",0);
                            return -1;
                        }

                        memset(tempstr,0,current->num);
                        strcpy(tempstr,current->lineNR);
                        free(current->lineNR);
                        current->lineNR=tempstr;
                    }
                current->lineNR[n2]=ch;
                current->lineNR[n2+1]='\0';

            }else{   /*行中插入字符*/
                current->lineNR[col-1]=ch;
            }
            break;
    }
    if(txtGB.x==78)
    {
        iStartCol++;
        DrawView();
    }else{
        txtGB.x++;
        ClearCurrentLine();
        DrawCurrentLine();
    }
    setStateWindow();
    return 1;
}

int OnDeleteCh()
{
    int n,col=txtGB.x+iStartCol;
    char *p;
    struct FileLine *temp;

    if(current->lineNR&&strlen(current->lineNR)>col-1)  /*光标不处于行尾时*/
    {
       	strcpy(current->lineNR+col-1,current->lineNR+col);

    }else{
        if(current->next)                /*光标处于行尾时,合并下行到本行*/
        {
			if(current->next->lineNR&&strlen(current->next->lineNR)) 
			{
	    		if(current->lineNR)
					n=strlen(current->lineNR);
	   			else n=0;
	 
	   			n+=strlen(current->next->lineNR);
	            
           		if(!(p=(char*)malloc(n+1)))
              		{
						MessageBox("Short of memory!",0);
						return -1;
             		}
			
				if(current->lineNR)
          			strcpy(p,current->lineNR);
           		strcat(p,current->next->lineNR);
				p[n]='\0';

          		free(current->lineNR);
          		current->lineNR=p;
			}

			temp=current->next;/*将下行从链表中删除,并释放内存*/
			current->next=temp->next;
			temp->next->pro=current;
			free(temp->lineNR);
			free(temp);
			iTotalLine--;
        }
    }
    DrawView();
}

int OnBackspace()
{
    if(txtGB.x==1&&txtGB.y==1&&current==top)/*光标位于文件头退出*/
      if(iStartCol==0)    return 0;
      else
      {
    ScrollWindow(2);
    txtGB.x++;
    }
    
    if(txtGB.x>1)
    {
        txtGB.x--;
    }else{             /*txtGB.x==1*/
    if(iStartCol>0)
            ScrollWindow(2);
        else{        /*iStartCol==0,*/
                TranslateKey(Key_UP,0);
                OnEnd();
            }
    }
    OnDeleteCh();
}

int OnEnd()
{
    int n=0;
    if(current->lineNR)
	n=strlen(current->lineNR);
    if(iStartCol+78>n)
    {
        txtGB.x=n-iStartCol+1;
        gotoxy(txtGB.x,txtGB.y);
    }else{
    txtGB.x=78;
        iStartCol=n-77;
        DrawView();
    }
}

int OnHome()
{
    txtGB.x=1;
    iStartCol=0;
    DrawView();
    return 0;
}

int OnCtrlHome()
{
    txtGB.x=1;
    txtGB.y=1;
    current=top=head;
    iStartLine=0;
    DrawView();
    return 0;
}

int OnCtrlEnd()
{
    int i=0;
    while(current->next)
    {
        current=current->next;
        if(txtGB.y<21)
            txtGB.y++;
    }

    if(iTotalLine<iStartLine+21) /*当前页已显示文件结尾*/
    {
        OnEnd();
        DrawView();
        return 0;
    }

    top=current;
    while(top->pro&&i<20)/*一页21行*/
    {
        top=top->pro;
        i++;
    }
    iStartLine=iTotalLine-21;
    OnEnd();
    DrawView();
    return 0;
}

int OnPageDown()
{
    int i=0;
    if(iTotalLine<iStartLine+21) /*当前页已显示文件结尾*/
        return 0;

    for(i=0;i<20;i++)
    {
        TranslateKey(Key_DOWN,bioskey(2));
    }
}
int OnPageUp()
{
    int i=0;
    if(0==iStartLine) /*当前页已显示文件头*/
        return 0;
    for(i=0;i<20&&iStartLine>0;i++)
    {
        TranslateKey(Key_UP,bioskey(2));
    }
    return 0;
}

int DrawView()
{
    struct FileLine *oldcur;
    int i,x,y;

    window(2,3,79,23);
    textbackground(BLUE);
    clrscr();
    textcolor(WHITE);

    oldcur=current;
    current=top;
    y=txtGB.y;
    x=txtGB.x;
   for(i=0;i<21;i++)
    {
        txtGB.y=i+1;
        DrawCurrentLine();
        if(0==current->next)
            break;
        current=current->next;
    }
    current=oldcur;
    txtGB.y=y;
    txtGB.x=x;
    gotoxy(txtGB.x,txtGB.y);
}

int DrawCurrentLine()
{
    int i,n;
    char temp[80];
    memset(temp,0,80);

    gotoxy(1,txtGB.y);
    if(!current->lineNR)
		return 0;

    n=strlen(current->lineNR);
    if(n>(78+iStartCol))
    {
        for(i=0;i<78;i++)
            temp[i]=current->lineNR[iStartCol+i];
        cputs(temp);
        return 0;
    }
    if(n>iStartCol)        /*n<=iStarCol时,本行显示为空*/
         cputs(current->lineNR+iStartCol);
	return 1;
}

int ClearCurrentLine()
{
    window(2,txtGB.y+2,79,txtGB.y+2);
    textbackground(BLUE);
    clrscr();
    setTextWindow();
}

/*direction=1,2,3,4分别表示屏幕向左,右,上,下滚动一行)*/
/*txtwindow(2,3,79,23)*/
int ScrollWindow(int direction)
{
    int i;
    switch(direction)
    {
    case 1:
        iStartCol++;
    break;
    case 2:
        iStartCol--;
        break;
    case 3:
        iStartLine++;
        top=top->next;
        break;
    case 4:
        iStartLine--;
        top=top->pro;
        break;
    }
    DrawView();
}

long getfilesize(FILE *file)
{
    long size=0;
    rewind(file);
    while(fgetc(file)!=EOF) size++; 
    rewind(file);
    return size;
}

int NewFile()/*初始化新文件*/
{
    if(bNeedSave)
    {
        if(MessageBox("Do you want to save the file?",1))
            SaveFile(0);
    }

    if(!top)
    FreeMemory();

    strcpy(path,"");
    lFileSize=0;
    iTotalLine=1;
    iStartLine=0;
    iStartCol=0;
    top=current=head=NewLine();
    txtGB.x=1;
    txtGB.y=1;
    DrawView();
}
/*取文件当前行的长度(含'\n')*/
int getfilelinelength(FILE *file)
{

  char ch;
  int i=0;
  fpos_t pos;
  fgetpos(file,&pos);
  while((ch=fgetc(file))!='\n'&&ch!=EOF)
  {
     i++;
  }
  fsetpos(file,&pos);
  if(i==0&&ch==EOF)
      return -1;
  if(ch=='\n')
	  i++;

  return i;
}
int OpenTheFile(char *name)
{
    FILE *file;
    char *tempfilebuf,ch;
    int  i,first=1,linelength; 
    struct FileLine *t,*pro;
    file=fopen(name,"r");
    if(!file)
    {
        return -1;
     }
    strcpy(path,name);/*把文件路径保存起来*/
    lFileSize=getfilesize(file);
    FreeMemory();
	if(!lFileSize)
	{
		NewFile();
		strcpy(path,name);/*把文件路径保存起来*/
		return 1;
	}
    iStartLine=0;
    iStartCol=0;
    iTotalLine=0;
    
    while(-1!=(linelength=getfilelinelength(file)))
    {
        t=NewLine();
        if(!t)
        {
            fclose(file);
            return 0;
        }

	    t->lineNR=(char *)malloc(linelength+1);
	    if(!t->lineNR)
	    {
		free(t);
		fclose(file);
		return 0;
	    }
	    t->num=linelength+1;
	    fgets(t->lineNR,linelength+1,file);
	    while(t->lineNR[strlen(t->lineNR)-1]=='\n')
	    {
		t->lineNR[strlen(t->lineNR)-1]='\0';
	    }


        if(first)/*附加到链表*/
        {
            pro=current=top=head=t;
            iStartLine=0;
            first=0;
        }else{         
            t->pro=pro;
            pro->next=t;
        }
        iTotalLine++;
        
        pro=t;
  }
    fclose(file);
    return 1;
}

int FreeMemory()
{
    struct FileLine *p,*temp;
    p=head;
    while(p)
    {
    temp=p->next;
        free(p->lineNR);
        free(p);
        p=temp;
    }
}

/*消息输入对话框
msg:返回输入内容,需用户预先分配
num: msg的大小
title:对话框标题
dis:对话框中初始显示内容*/
int InputMessageBox(char *msg,int num,char *title)
{
     char buf[41*3*2];
     int i,n;
     char ch;

    gettext(10,5,50,7,buf);
    window(10,5,50,7);
    textbackground(LIGHTGRAY);
    clrscr();
    box(1,1,40,3);
    gotoxy(5,1);
    cputs(title);
    gotoxy(2,2);
    window(11,6,48,6);
    textbackground(LIGHTGRAY);
    clrscr();
	textcolor(BLACK);

    cputs(msg);
    n=strlen(msg);
    
    while((ch=getch())!=13)
    {
        if(27==ch)
        {
        puttext(10,5,50,7,buf);
            return 0;
        }        
        if(ch!=8&&ch<32)
            continue;

        if(n==num-1&&ch!=8)
        {continue;}

        if(ch==8)
        {
            n--;
            if(n<0)
                n=0;
            msg[n]='\0';
        }else{
        msg[n++]=ch;
		msg[n]='\0';
    }
         clrscr();
         gotoxy(1,1);
		 i=n<38?0:n-37;
         cputs(msg+i);
        
    }
    puttext(10,5,50,7,buf);
    return 1;
}
/*取当前路径*/
char *current_directory(char *path)
{
   strcpy(path, "X:\\");      /* fill string with form of response: X:\ */
   path[0] = 'A' + getdisk();    /* replace X with current drive letter */
   getcurdir(0, path+3);  /* fill rest of string with current directory */
   return(path);
}

int OpenFile()/*打开文件*/
{
     char name[MAXPATH+1];/*MAXPATH在dir.h中定义为80*/
     int flag;
      memset(name, 0,MAXPATH+1); 
      if(!strlen(path))
        {
			current_directory(name);
			strcat(name,"\\");
			flag= InputMessageBox(name,MAXPATH,"Input filename");
		}else {
			strcpy(name,path);
			if( !(flag=InputMessageBox(name,MAXPATH,"Input filename")))
				return 0;
		}
    if(!strcmp(name,path)) 

⌨️ 快捷键说明

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