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

📄 fileedit.c

📁 FileEdit can edit file ,save file ,open file ,have basic simple functions for file .INSERT, BACKSPAC
💻 C
字号:
 /*******************************************
 * FileEdit   Programme:           *
 *   file  open          *
 *   file  edit            *
 *   file save            *
 *       -- by Peter_Hu 11/20/08 --   *
 *                                *
 *******************************************/
#include "stdio.h"
#include "windows.h"
#include "crtdbg.h"
#include "string.h" 
#include <time.h>

#define DIR_UP -1
#define DIR_DOWN 1

#define FORE_BLACK 0
#define FORE_BLUE FOREGROUND_BLUE
#define FORE_GREEN FOREGROUND_GREEN
#define FORE_RED FOREGROUND_RED
#define FORE_WHITE (FORE_BLUE|FORE_GREEN|FORE_RED)
#define FORE_MASK (FORE_BLUE|FORE_GREEN|FORE_RED|FOREGROUND_INTENSITY)

#define BACK_BLACK 0
#define BACK_BLUE BACKGROUND_BLUE
#define BACK_GREEN BACKGROUND_GREEN
#define BACK_RED BACKGROUND_RED
#define BACK_WHITE (BACK_BLUE|BACK_GREEN|BACK_RED)
#define BACK_MASK (BACK_BLUE|BACK_GREEN|BACK_RED|BACKGROUND_INTENSITY)

static HANDLE stdinput=INVALID_HANDLE_VALUE , stdoutput=INVALID_HANDLE_VALUE;

void InitIoSystem();
void GetChar(char * pscancode , char * pasciicode);  /* get char */
void DisplayChar(char ch);
void SetCursorXY(int x , int y);

void GetCursorXY(int * px ,int * py);
unsigned short GetTextForeColor(void);
unsigned short GetTextBackColor(void);

void SetTextForeColor(unsigned short forecolor);
void SetTextBackColor(unsigned short backcolor);

void ScrollScreen(int dir , unsigned int linecount ,
                  int left , int top , int right , int bottom);

void DisplayCursor(int flag);
void DisplayString(char *str);
/* ========================================================================= */
void InitIoSystem()
{
 COORD cursorinitpos={0 , 0};
 COORD buffersize={80 , 25};
 stdinput=GetStdHandle(STD_INPUT_HANDLE);
 _ASSERTE(stdinput!=INVALID_HANDLE_VALUE);
 stdoutput=GetStdHandle(STD_OUTPUT_HANDLE);
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(SetConsoleCursorPosition(stdoutput , cursorinitpos)!=0);
 SetTextForeColor(FORE_WHITE);
 SetTextBackColor(BACK_BLACK);
 _ASSERTE(SetConsoleScreenBufferSize(stdoutput , buffersize)!=0);
}

/* ========================================================================= */

void GetChar(char * pscancode , char * pasciicode)
{
 INPUT_RECORD inputrecord;
 unsigned long inputcount;
 _ASSERTE(pscancode!=NULL);
 _ASSERTE(pasciicode!=NULL);
 while (1)
 {
  _ASSERTE(stdinput!=INVALID_HANDLE_VALUE);
  _ASSERTE(ReadConsoleInput(stdinput , &inputrecord , 1 , &inputcount)!=0);
  if (inputrecord.EventType==KEY_EVENT && inputrecord.Event.KeyEvent.bKeyDown)
  {
    *pscancode = (char)inputrecord.Event.KeyEvent.wVirtualScanCode,
    *pasciicode = inputrecord.Event.KeyEvent.uChar.AsciiChar;
if (inputrecord.Event.KeyEvent.dwControlKeyState&
(RIGHT_ALT_PRESSED|RIGHT_CTRL_PRESSED|LEFT_ALT_PRESSED|LEFT_CTRL_PRESSED))
     continue;
    else if (*pasciicode==0&&
    (inputrecord.Event.KeyEvent.dwControlKeyState&SHIFT_PRESSED))
     continue;
    else
     break;
  }
 }
}

/* ======================================================================== */

void DisplayChar(char ch)
{
 unsigned long outputcount;
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(WriteConsole(stdoutput , &ch , 1 , &outputcount , NULL)!=0);
}

/* ======================================================================== */
void SetCursorXY(int x ,int y)
{
 COORD cursorpos={x , y};
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(x>=0&&x<80&&y>=0&&y<25);
 _ASSERTE(SetConsoleCursorPosition(stdoutput , cursorpos)!=0);
}

/* ======================================================================== */
void GetCursorXY(int * px , int * py)
{
 CONSOLE_SCREEN_BUFFER_INFO screenbufferinfo;
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(px!=NULL&&py!=NULL);
 _ASSERTE(GetConsoleScreenBufferInfo(stdoutput , &screenbufferinfo)!=0);
 *px=screenbufferinfo.dwCursorPosition.X;
 *py=screenbufferinfo.dwCursorPosition.Y;
}
/* ======================================================================= */
unsigned short GetTextForeColor(void)
{
 CONSOLE_SCREEN_BUFFER_INFO screenbufferinfo;
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(GetConsoleScreenBufferInfo(stdoutput , &screenbufferinfo)!=0);
 return screenbufferinfo.wAttributes & FORE_MASK;
}
/* ======================================================================== */
unsigned short GetTextBackColor(void)
{
 CONSOLE_SCREEN_BUFFER_INFO screenbufferinfo;
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(GetConsoleScreenBufferInfo(stdoutput , &screenbufferinfo)!=0);
 return screenbufferinfo.wAttributes & BACK_MASK;
}
/* ======================================================================== */
void SetTextForeColor(unsigned short forecolor)
{
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(SetConsoleTextAttribute(stdoutput ,
                (WORD)(forecolor|GetTextBackColor()))!=0);
}
/* ======================================================================== */

void SetTextBackColor(unsigned short backcolor)
{
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(SetConsoleTextAttribute(stdoutput ,
                     (WORD)(GetTextForeColor()|backcolor))!=0);
}
/* ======================================================================== */
void ScrollScreen(int dir , unsigned int linecount ,
                         int left ,int top ,int right , int bottom)
{
 SMALL_RECT rect={left , top , right , bottom};
 COORD destcoord={left , top+dir*linecount};
 CHAR_INFO fillinfo={' ' ,};
 _ASSERTE(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERTE(left>=0&&top>=0&&right>=0&&bottom>=0&&
                   left<80&&right<80&&top<25&&bottom<25);
 fillinfo.Attributes=GetTextForeColor() | GetTextBackColor();
 _ASSERTE(ScrollConsoleScreenBuffer(stdoutput,&rect,
                               NULL,destcoord,&fillinfo)!=0);
}

/* ========================================================================= */
void DisplayCursor(int flag)
{
 CONSOLE_CURSOR_INFO curinfo;
 _ASSERT(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERT(GetConsoleCursorInfo(stdoutput , &curinfo)!=0);
 curinfo.bVisible=(flag?TRUE:FALSE);
 _ASSERT(SetConsoleCursorInfo(stdoutput , &curinfo)!=0);
}

/* ====================================================================== */
void DisplayString(char *str)
{
 unsigned long outputcount;
 _ASSERT(stdoutput!=INVALID_HANDLE_VALUE);
 _ASSERT(str!=NULL);
 _ASSERT(WriteConsole(stdoutput , str , strlen(str) , &outputcount , NULL)!=0);
}

/* ====================================================================== */

#define MaxSize 79
#define MaxLine 24
#define Insert 1
#define Overwrite (-1)

FILE *fp;
char filename[20]="newfile";
char filename1[20];

typedef struct SNode 
{
 struct SNode *next,*prior;
 char String[MaxSize+1];
}SNode;
                      /* define  functions */

void openfile();      /* open file */
void newfile();
void savefile();
void quit();
void datashow();      /* show datatime */
int nextrow();
int backspace();
int uprow();
int downrow();
int downpage();
int uppage();   
int deletechar();
int insertchar(char b);



SNode * HeadNode,*TailNode,*rowp;
int State=Insert;
int sign=0,flag=0,flag1=0,sign1=0,sign2=0,sign3=0;
int x0=0;
/* =============================MAIN  BEGINNING=========================== */
void main(void)
{
 int x=0,y=0,i;
 char a,b;
 InitIoSystem();
 if(!(HeadNode=TailNode=rowp=(SNode*)malloc(sizeof(SNode))))
  exit(0);
 HeadNode->String [0]=0;
 HeadNode->next=HeadNode->prior=NULL;
 
 SetCursorXY(0,0);
 
 SetTextBackColor(BACK_BLUE);
 for(i=0;i<MaxSize+1;i++)
  printf(" ");
 SetCursorXY(0,0);
 printf("%s",filename);

 SetCursorXY(0,MaxLine);

 SetTextBackColor(BACK_BLUE);
 for(i=0;i<MaxSize;i++)
  printf(" ");
 SetCursorXY(0,MaxLine);
 printf("insert       F1-OPEN F2-NEW F3-SAVE F4-CLOSE F5-QUIT");

 datashow();
 
 SetCursorXY(0,1);
 SetTextBackColor(BACK_BLACK);
 while(1)
 {
  sign=0;
  flag=0;
  sign1=0;
  sign2=0;
  sign3=0;
  
  GetChar(&a,&b);
  GetCursorXY(&x,&y);
  if(a==1)/* ESC */
   ;
  
  else if(a==28)/* ENTER */
   nextrow();
  else if(a==14)
   backspace();
  else if(a==59)/* F1 */
   openfile();
  else if(a==60)/* F2 */
   newfile();
  else if(a==61)/* F3 */
   savefile();
  else if(a==62)/* F4 */
   newfile();
  else if(a==63)/* F5 */
   
  {
   quit();
   if(flag1==1)
    return;
  }
  else if(a==71)/* HOME */
   SetCursorXY(0,y);
  else if(a==79)/* END */
   if(rowp->String[0]<MaxSize)
    SetCursorXY(rowp->String[0],y );
   else 
    SetCursorXY(MaxSize-1,y );
   else if(a==72)/* UP */
    uprow();
   else if(a==80)/* DOWN */
    downrow();
   else if(a==75)/* LEFT */
   {
    if(x==0)
    {
     int i;
     i=uprow();/* if i=0,top in the screen and can scroll */
     if(i!=-1)/* if i=-1,can't scroll */
      if(rowp->String[0]<MaxSize)
       SetCursorXY(rowp->String[0],y-i);
      else 
       SetCursorXY(MaxSize-1,y-i);
    }
    else 
     SetCursorXY(x-1,y);
   }  /*  left  */  
   
   else if(a==77)/* RIGHT */
   {       
    if(x==rowp->String[0]-1&&!rowp->next)
     SetCursorXY(x+1,y);
    else if(x>=rowp->String[0]-1)
    {
     int i;
     i=downrow();/* if i=1 common;if  i=0 bottom on the screen */
     if(i!=-1)/* if i=-1,can't scroll */
      SetCursorXY(0,y+i);
    }     
    else
     SetCursorXY(x+1,y);
   }
   else if(a==81)/* PAGE DOWN */
   {
    DisplayCursor(0);
    downpage();  
    DisplayCursor(1);
   }
   else if(a==73)/* PAGE UP */
   { 
    DisplayCursor(0);
    uppage();
    DisplayCursor(1);   
   }
   else if(a==82)/* INSERT */
    
   {
    State*=-1;
    if(State!=Insert)
    {
     SetCursorXY(0,MaxLine);
     SetTextBackColor(BACK_BLUE);
     printf("overwrite");
     SetCursorXY(x,y);
     SetTextBackColor(BACK_BLACK);
    }
    else
    {
     SetCursorXY(0,MaxLine);
     SetTextBackColor(BACK_BLUE);
     printf("insert   ");
     SetCursorXY(x,y);
     SetTextBackColor(BACK_BLACK);
    }
   }
   else if(a==83)/* DELETE */
    deletechar();
   
   
   
   else if(b)
    
    insertchar(b);  
   
   
 }/* while(1) */
}/* main() */

int insertchar(char ch)
{ 
 int i;   
 int x=0,y=0; 
 char ch1;                                              
 GetCursorXY(&x,&y);
 if(sign3)
  x=x0;
 
 if(!rowp)
 {
  if(!(TailNode->next=rowp=(SNode *)malloc(sizeof(SNode))))
   exit(0);
  TailNode->next->prior=TailNode; 
  TailNode->next->next=NULL;
  TailNode->next->String[0]=0;
  TailNode=TailNode->next;
  
 }
 if(State==Overwrite&&ch!='\t')
 {
  rowp->String[x+1]=ch;
  DisplayChar(ch);
  if(x==rowp->String[0])
   rowp->String[0]++;
  if(x==MaxSize-1)
  {
   if(y<MaxLine-1)
    SetCursorXY(0,y+1);
   else
   {
    ScrollScreen(DIR_UP,1,0,2,MaxSize-1,MaxLine-1);
    SetCursorXY(0,MaxLine-1);
   }
   rowp=rowp->next;
   
  }
  return 0 ;
 }
 
 if(ch=='\t')/*   deal with TAB key,when thisline >=8insert  8  blank   */
 {
  if(State==Overwrite)
   return 0;
  if(x<MaxSize-8)
   for(i=0;i<8;i++)
    insertchar(' ');
   else 
    for(i=x;i<MaxSize-1;i++)
     insertchar(' ');
    return 0 ;
 }
 if(rowp->String[0]<MaxSize)
 {
  for(i=rowp->String[0];i>x;i--)/*    back shift  */
   rowp->String[i+1]=rowp->String[i];
  rowp->String[x+1]=ch;
  rowp->String[0]++;
  if(sign3)
   return 1;
  
  DisplayChar(ch);
  for(i=x+2;i<=rowp->String[0];i++)
   DisplayChar(rowp->String[i]);
  
  if(x<MaxSize-1)
   SetCursorXY(x+1,y);
  else if(y<MaxLine-1)
  {
   SetCursorXY(0,y+1);
   rowp=rowp->next;
  } 
  else
  { 
   ScrollScreen(DIR_UP,1,0,2,MaxSize-1,MaxLine-1);
   SetCursorXY(0,MaxLine-1);
   rowp=rowp->next;
  }
  return 1;
 }
 else if(rowp->String[0]==MaxSize)
 {char ch2;
 if(y!=MaxLine-1)/* not insert  in the last line  on the screen*/
 { 
  ch1=rowp->String[rowp->String[0]];
  SetCursorXY(0,y+1);
  rowp=rowp->next;
  insertchar(ch1);
  rowp=rowp->prior;
  rowp->String[0]--;
  if(flag&&sign1)
   y--;
  sign1=1;
  if(y==0)
   sign3=1;
  if(!sign3)
   SetCursorXY(x,y);
  else x0=x;
  
  insertchar(ch);
  if(sign3)
   rowp=rowp->next;
 }
 else    
 {
  if(flag)
  {
   
   SNode *p1=rowp;
   sign2=1;
   DisplayChar(ch);
   
   if(rowp->String[0]<MaxSize)
   {
    for(i=1;i<=rowp->String[0];i++)
     
     DisplayChar(rowp->String[i]);
    
    for(i=rowp->String[0];i>0;i--)
     rowp->String[i+1]=rowp->String[i];
    
    rowp->String[1]=ch;
    rowp->String[0]++;
   }
   else
   {
    for(i=1;i<MaxSize;i++)
     DisplayChar(rowp->String[i]);
    ch2=rowp->String[MaxSize];  
    for(i=MaxSize-1;i>0;i--)
     rowp->String[i+1]=rowp->String[i];
    rowp->String[1]=ch;
    
    rowp=rowp->next;
pp:;
   
   if(!rowp)
   {
    if(!(TailNode->next=rowp=(SNode *)malloc(sizeof(SNode))))
     exit(0);
    TailNode->next->prior=TailNode; 
    TailNode->next->next=NULL;
    TailNode->next->String[0]=0;
    TailNode=TailNode->next;
    rowp->String[0]=1;
    rowp->String[1]=ch2;
    rowp=p1;
    return 1;
   }
   
   if(rowp->String[0]<MaxSize)
    
   { for(i=rowp->String[0];i>0;i--)
   rowp->String[i+1]=rowp->String[i];
   rowp->String[1]=ch2;
   rowp->String[0]++;
   rowp=p1;
   return 1;
   } 
   else 
   {
    char ch3=rowp->String[MaxSize];
    for(i=MaxSize-1;i>0;i--)
     rowp->String[i+1]=rowp->String[i];
    rowp->String[1]=ch2;
    ch2=ch3;
    rowp=rowp->next;
    goto pp;
   }
   
   }
  }
  ScrollScreen(DIR_UP,1,0,2,MaxSize-1,MaxLine-1);
  flag=1;
  if(rowp->next)
  { 
   SetCursorXY(0,MaxLine-1);
   for(i=0;i<rowp->next->String[0];i++)
    DisplayChar(rowp->next->String[i+1]);
  }
  SetCursorXY(x,y-1);
  insertchar(ch);

 }
 return 1;
}
return 1;
}

/* ====================================================================== */
int backspace(void)
{
 int x=0,y=0;
 int i;
 GetCursorXY(&x,&y);
 if(x!=0)
 {
  SetCursorXY(x-1,y);
  DisplayChar(' ');
  SetCursorXY(x-1,y);
  for(i=x;i<rowp->String[0];i++)
  {
   rowp->String[i]=rowp->String[i+1];
   DisplayChar(rowp->String[i+1]);
  }
  DisplayChar(' ');
  SetCursorXY(x-1,y);
  rowp->String[0]--;
 }/* if */
 
 else/* x=0 */
 { if(!rowp&&TailNode)
 {
  if(TailNode->String[0]<MaxSize)
   
   SetCursorXY(TailNode->String[0],y-1);
  else 
  {
   TailNode->String[0]--;
   SetCursorXY(TailNode->String[0],y-1);
   DisplayChar(' ');
   SetCursorXY(TailNode->String[0],y-1);
  }
  rowp=TailNode;
  return 1;
 }
 else if(!rowp->prior)
  return 1; 
 
    /*the length is not zero,  have line before ,not scroll  screen 

⌨️ 快捷键说明

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