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

📄 newpad.c

📁 自己写的记事本,发上来大家一起看看再说 有什么不组的大家帮忙
💻 C
📖 第 1 页 / 共 2 页
字号:
char str[80];
register char *p;
int len,i;
clrline(MAX_LINES);
gotoxy(1,MAX_LINES);
printf("search string :");
edit_gets(str);
if(! *str)
{
  help();
  return;
}
p=curloc;
len=strlen(str);
while(*p&&strncmp(str,p,len)) p++;
if(! *p)
{
  gotoxy(1,MAX_LINES);
  printf("\7No found string %s!",str);
  getch();
  help();
  return;
}   /* not found */
i=0;
while(p>buf&&*p!='\r')
{
  p--;
  i++;
}
p++;
i--;
/* reposition curren location to start of match  */
curloc=p+i;
scrnx=i;
scrny=0;
clrscr();
display_scrn(0,0,p);
help();
}

/*****************************************************************************
   Move up a line.
*****************************************************************************/
void upline(void)
{
register int i;
char *p;
if(curloc==buf)return;
p=curloc;
if(*p=='\r') p--;
for(;*p!='\r'&&p<buf;p--);
if(*p=='\r')return;
curloc=p;
curloc--;
i=scrnx;  /* save X coordinate */
/* find start of next line */
while(*curloc!='\r'&&curloc>buf) curloc--;
scrny--;
scrnx=0;
curloc++;
/* if at top of screen ,must stop */
if(scrny<0)
{
  scrolldn(1,1);
  scrny=0;
  gotoxy(1,1);
  printline(curloc);
}
while(i&&*curloc!='\r')
{
  curloc++;
  scrnx++;
  i--;
}
}

/***************************************************************************
   Move down one line,keep previous scrnx location if possible
**************************************************************************/
void downline(void)
{
register int i;
char *p;
i=scrnx;
p=curloc;
while(*p!='\r'&&p<endloc) p++;
if(p==endloc) return;
p++;
curloc=p;
scrny++;
scrnx=0;
if(scrny==MAX_LINES-1)
{
  scrny=MAX_LINES-2;
  scrollup(1,1,LINE_LEN,MAX_LINES-1);
  gotoxy(scrnx+1,scrny+1);
  printline(curloc);
}
while(i&& *curloc!='\r' && curloc<endloc)
{
  curloc++;
  scrnx++;
  i--;
}
}

/****************************************************************************
   Display a screen full of text (up to 24 lines)
***************************************************************************/
void display_scrn(int x,int y,char *p)
{
register int i,j;
gotoxy(x+1,y+1);
textcolor(WHITE);
textbackground(BLUE);
i=0;
while(y<MAX_LINES-1&&*p)
{
  switch(*p)
  {
   case '\r': printf("\n");
    y++;
    gotoxy(1,y+1);
    i=0;
    break;
   case '\x09':
    for(j=0;j<8;j++)
    {
     printf(" ");
     i++;
    }
   default:
    if(i<LINE_LEN) putch(*p);
    i++;
  }
  p++;
}
}

/****************************************************************************
    Page down MAX_LINES lines
*****************************************************************************/
void pagedown(void)
{
register int i;
clrscr();
for(i=0;i<MAX_LINES&&curloc<endloc;)
{
  if(*curloc=='\r') i++;
  curloc++;
}
help();
scrnx=0;
scrny=0;
display_scrn(0,0,curloc);
}

/***************************************************************************
    Page up MAX_LINES line
***************************************************************************/
void pageup(void)
{
register int i;
clrscr();
for(i=0;i<MAX_LINES&&curloc>buf;)
{
  if(*curloc=='\r') i++;
  curloc--;
}
scrnx=scrny=0;
display_scrn(0,0,curloc);
help();
}

/**************************************************************************
    Go to the end of the file
**************************************************************************/
void gotoend(void)
{
int i;
char *p;
p=curloc;
for(i=scrnx;*p!='\r'&&p<endloc;)
{
  scrnx++;
  p++;
  i++;
}
gotoxy(scrnx+1,scrny+1);
curloc=p;
}

/****************************************************************************
    Load a file
****************************************************************************/
int load(char *fname)
{
FILE *fp;
char ch,*p;
int i;
draw_border(0,0,78,24,0x2f);
clrscr();
if((fp=fopen(fname,"rb"))==NULL) return 0;
gotoxy(1,1);
p=buf;
while(!feof(fp)&&p!=buf+BUF_SIZE-2)
{
  ch=getc(fp);
  if(ch=='\x09')
   for(i=0;i<8;i++,p++) *p='\x20';
  else if(ch!='\n'&&ch!=EOF)
   {
    *p=ch;
    p++;
   }
}
*p='\0';
fclose(fp);
curloc=endloc=p;
clrscr();
curloc=buf;
help();
scrnx=scrny=0;
display_scrn(0,0,curloc);
}

/*********************************************************************************
    Go to top the file
******************************************************************************/
void home(void)
{
curloc-=scrnx;
scrnx=0;
gotoxy(scrnx+1,scrny);
}

/******************************************************************************
    Save a file
*****************************************************************************/
int save(char *fname)
{
int i;
FILE *fp;
char *p,name[80];
if(!*fname)
{
  clrline(MAX_LINES);
  gotoxy(1,MAX_LINES);
  printf("filename:");
  gets(name);
}
else strcpy(name,fname);
if((fp=fopen(name,"wb"))==NULL) return 0;
p=buf;
while(p!=endloc)
{
  if(*p!='\r')
  putc(*p,fp);
  else
  {
   putc('\r',fp);
   putc('\n',fp);
  }
  p++;
}
fclose(fp);
clrline(MAX_LINES);
printf("file %s already saved.",name);
getch();
help();
return 1;
}
/******************************************************************************
    Read a string from the keyboard
*****************************************************************************/
void edit_gets(char *str)
{
char *p;
p=str;
for(;;)
{
  *str=getch();
  if(*str=='\r')
  {
   *str='\0';
   return;
  }
  if(*str=='\b')
  {
   if(str>p)
   {
    str--;
    putch('\b');
    putch(' ');
    putch('\b');
   }
  }
  else
  {
   putch(*str);
   str++;
  }
}
}

/*******************************************************************************
    Read and save cursor coordinates
******************************************************************************/
void cursor_pos(void)
{
union REGS i,o;
i.h.bh=0;
i.h.ah=3;
int86(16,&i,&o);
}

/********************************************************************************
    Send cursor to specified X,Y(0,0 is upper left corner).
*****************************************************************************/
void gotoxy(int x,int y)
{
union REGS i;
i.h.dh=y;
i.h.dl=x;
i.h.ah=2;
i.h.bh=0;
int86(16,&i,&i);
}

/*****************************************************************************
    Clear entire line given its Y coordinate
*****************************************************************************/
void clrline(int y)
{
register int i;
gotoxy(1,y);
for(i=0;i<LINE_LEN;i++) putch(' ');
textcolor(WHITE);
textbackground(BLUE);
}

/****************************************************************************
    Clear to end of specified line.
****************************************************************************/
void edit_clr_col(int x,int y)
{
char *p;
p=curloc;
gotoxy(x+1,y+1);
for(;x<LINE_LEN&&*p!='\r'&&*p;x++,p++)
{
  printf(" ");
}
}

/****************************************************************************
    Clear the screen.
****************************************************************************/
void clrscr(void)
{
union REGS r;
r.h.ah=6;
r.h.al=0;
r.h.ch=1;
r.h.cl=1;
r.h.dh=MAX_LINES-1;
r.h.dl=LINE_LEN;
r.h.bh=0x1f;
int86(0x10,&r,&r);
}
/***************************************************************************
    Scroll down the screen.
***************************************************************************/
void scrolldn(int x,int y)
{
union REGS r;
r.h.ah=7;
r.h.al=1;
r.h.ch=y;
r.h.cl=x;
r.h.dh=MAX_LINES-1;
r.h.dl=LINE_LEN;
r.h.bh=0x1f;
int86(0x10,&r,&r);
}

/****************************************************************************
    Srcoll up the screen.
****************************************************************************/
void scrollup(int topx,int topy,int endx,int endy)
{
union REGS r;
r.h.ah=6;
r.h.al=1;
r.h.ch=topy;
r.h.cl=topx;
r.h.dh=endy;
r.h.dl=endx;
r.h.bh=0x1f;
int86(0x10,&r,&r);
}

/**************************************************************************/
void draw_border(int beginx,int beginy,int endx,int endy,int attr)
{
int i;
union REGS r;
r.h.ah=6;
r.h.al=0;
r.h.ch=beginy;
r.h.cl=beginx;
r.h.dh=endy;
r.h.dl=endx+1;
r.h.bh=attr;
int86(0x10,&r,&r);
for(i=beginx;i<endx;i++)
{ gotoxy(i,beginy);
  putchar(196);
  gotoxy(i,endy);
  putchar(196);
}
for(i=beginy;i<=endy;i++)
{
  gotoxy(beginx,i);
  putchar(179);
  gotoxy(endx,i);
  putchar(179);
}
gotoxy(beginx,beginy);
putchar(218);
gotoxy(beginx,endy);
putchar(192);
gotoxy(endx,beginy);
putchar(191);
gotoxy(endx,endy);
putchar(217);
}

⌨️ 快捷键说明

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