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

📄 block.c

📁 包括文件操作、图形系统、并口串口编程、鼠标编程、小型cad系统、编译器、病毒防火墙、海底大战等多个实例。
💻 C
📖 第 1 页 / 共 4 页
字号:


#include "tdestr.h"
#include "common.h"
#include "tdefunc.h"
#include "define.h"



int  mark_block( WINDOW *window )
{
int type;
int num;
long lnum;
register file_infos *file;      /* 临时文件 */
register WINDOW *win;           /* 把当前窗口指针放到一个临时寄存器里面 */
int rc;

   win  = window;
   file = win->file_info;
   if (win->rline > file->length || win->ll->len == EOF)
      return( ERROR );
   if (g_status.marked == FALSE) {
      g_status.marked = TRUE;
      g_status.marked_file = file;
   }
   if (g_status.command == MarkBox)
      type = BOX;
   else if (g_status.command == MarkLine)
      type = LINE;
   else if (g_status.command == MarkStream)
      type = STREAM;
   else
      return( ERROR );

   rc = OK;
   /*
    * 仅对于一个文件定义块。用户可以在这个文件的任何窗口进行操作。
    */
   if (file == g_status.marked_file) {

      /*
       * 不管块的模式标识块的起始和中止位置
       */
      if (file->block_type == NOTMARKED) {
         file->block_ec  = file->block_bc = win->rcol;
         file->block_er  = file->block_br = win->rline;
      } else {
         if (file->block_br > win->rline) {
            file->block_br = win->rline;
            if (file->block_bc < win->rcol && type != STREAM)
               file->block_ec = win->rcol;
            else
               file->block_bc = win->rcol;
         } else {
            if (type != STREAM) {
               file->block_ec = win->rcol;
               file->block_er = win->rline;
            } else {
               if (win->rline == file->block_br &&
                   win->rline == file->block_er) {
                  if (win->rcol < file->block_bc)
                     file->block_bc = win->rcol;
                  else
                     file->block_ec = win->rcol;
               } else if (win->rline == file->block_br)
                  file->block_bc = win->rcol;
               else {
                  file->block_ec = win->rcol;
                  file->block_er = win->rline;
               }
            }
         }

         /*
          * 如果用户标识的块的终止位置在起始位置前,那么交换两个位置。
          */
         if (file->block_er < file->block_br) {
            lnum = file->block_er;
            file->block_er = file->block_br;
            file->block_br = lnum;
         }

         /*
          * 如果用户标识的块的终止列在起始列前,那么交换两个位置。
          */
         if ((file->block_ec < file->block_bc) && (type != STREAM ||
              (type == STREAM && file->block_br == file->block_er))) {
            num = file->block_ec;
            file->block_ec = file->block_bc;
            file->block_bc = num;
         }
      }

      /*
       * 如果块类型已经被定义,但是如果用户使用混和模式,那么块的
	   * 类型被置为当前块的类型
       
       */
      if (file->block_type != NOTMARKED) {
         /*
          * 如果块的类型是矩形块,那么要保证左上角小于右下脚
          * 如果块的类型是stream块,那么保证起始列小于中止列
          */
         if (type == BOX) {
            if (file->block_ec < file->block_bc) {
               num = file->block_ec;
               file->block_ec = file->block_bc;
               file->block_bc = num;
            }
         }
      }

      assert( file->block_er >= file->block_br );

      file->block_type = type;
      file->dirty = GLOBAL;
   } else {
      /*
       * 已经定义好块
       */
      error( WARNING, win->bottom_line, block1 );
      rc = ERROR;
   }
   return( rc );
}



int  unmark_block( WINDOW *arg_filler )
{
register file_infos *marked_file;

   if (g_status.marked == TRUE) {
      marked_file              = g_status.marked_file;
      g_status.marked          = FALSE;
      g_status.marked_file     = NULL;
      marked_file->block_start = NULL;
      marked_file->block_end   = NULL;
      marked_file->block_bc    = marked_file->block_ec = 0;
      marked_file->block_br    = marked_file->block_er = 0l;
      if (marked_file->block_type)
         marked_file->dirty = GLOBAL;
      marked_file->block_type  = NOTMARKED;
   }
   return( OK );
}



void restore_marked_block( WINDOW *window, int net_change )
{
long length;
register file_infos *marked_file;

   if (g_status.marked == TRUE && net_change != 0) {
      marked_file = g_status.marked_file;
      length = marked_file->length;

     
      if (marked_file == window->file_info) {

        
         if (marked_file->block_br > window->rline) {
            marked_file->block_br += net_change;
            marked_file->block_er += net_change;
            marked_file->dirty = GLOBAL;
        
         } else if (marked_file->block_er >= window->rline)
            marked_file->dirty = GLOBAL;

         
         if (marked_file->block_br > length)
            unmark_block( window );
         else if (marked_file->block_er > length) {
            marked_file->block_er = length;
            marked_file->dirty = GLOBAL;
         }
      }
   }
}



int  prepare_block( WINDOW *window, line_list_ptr ll, int bc )
{
register int pad = 0;  
register int len;

   assert( bc >= 0 );
   assert( bc < MAX_LINE_LENGTH );
   assert( ll->len != EOF );
   assert( g_status.copied == FALSE );

   copy_line( ll );
   detab_linebuff( );

   len = g_status.line_buff_len;
   pad = bc - len;
   if (pad > 0) {
      

      assert( pad >= 0 );
      assert( pad < MAX_LINE_LENGTH );

      memset( g_status.line_buff+len, ' ', pad );
      g_status.line_buff_len += pad;
   }
   
   return( un_copy_line( ll, window, FALSE ) );
}



int  pad_dest_line( WINDOW *window, file_infos *dest_file, line_list_ptr ll )
{
int rc;
text_ptr l;
line_list_ptr new_node;

   rc = OK;

   l = NULL;
   new_node = (line_list_ptr)my_malloc( sizeof(line_list_struc), &rc );
   if (rc == OK) {
      new_node->len   = 0;
      new_node->dirty = FALSE;
      new_node->line  = l;
      if (ll->next != NULL) {
         ll->next->prev = new_node;
         new_node->next = ll->next;
         ll->next = new_node;
         new_node->prev = ll;
      } else {
         new_node->next = ll;
         if (ll->prev != NULL)
            ll->prev->next = new_node;
         new_node->prev = ll->prev;
         ll->prev = new_node;
         if (new_node->prev == NULL)
            window->file_info->line_list = new_node;
      }
      ++dest_file->length;
   } else {
     
      error( WARNING, window->bottom_line, block4 );
      if (new_node != NULL)
         my_free( new_node );
      rc = ERROR;
   }
   return( rc );
}


int  move_copy_delete_overlay_block( WINDOW *window )
{
int  action;
WINDOW *source_window;          
line_list_ptr source;           
line_list_ptr dest;             
long number;                    
int  lens;                      
int  lend;                      
int  add;                       
int  block_len;                 
line_list_ptr block_start;      
line_list_ptr block_end;  
int  prompt_line;
int  same;                      
int  source_first;              
file_infos *source_file;
file_infos *dest_file;
int  rcol, bc, ec;              
long rline;                     
long br, er;                    
long block_num;                 
long block_inc;                 
int  block_just;                
int  block_type;
int  fill_char;
int  rc;

   entab_linebuff( );
   rc = un_copy_line( window->ll, window, TRUE );
   if (g_status.marked == FALSE || rc == ERROR)
      return( ERROR );
   switch (g_status.command) {
      case MoveBlock :
         action = MOVE;
         break;
      case DeleteBlock :
         action = DELETE;
         break;
      case CopyBlock :
         action = COPY;
         break;
      case KopyBlock :
         action = KOPY;
         break;
      case FillBlock :
         action = FILL;
         break;
      case OverlayBlock :
         action = OVERLAY;
         break;
      case NumberBlock :
         action = NUMBER;
         break;
      case SwapBlock :
         action = SWAP;
         break;
      default :
         return( ERROR );
   }
   source_file = g_status.marked_file;
   source_window = g_status.window_list;
   for (; ptoul( source_window->file_info ) != ptoul( source_file );)
      source_window = source_window->next;
   prompt_line = window->bottom_line;
   dest_file = window->file_info;
   check_block( );
   if (g_status.marked == FALSE)
      return( ERROR );
   block_start = source_file->block_start;
   block_end = source_file->block_end;
   if (block_start == NULL  ||  block_end == NULL)
      return( ERROR );

   block_type = source_file->block_type;
   if (block_type != LINE  &&  block_type != STREAM  &&  block_type != BOX)
      return( ERROR );

   dest = window->ll;
   rline = window->rline;
   if (dest->len == EOF)
      return( ERROR );
   rc = OK;

   bc = source_file->block_bc;
   ec = source_file->block_ec;
   br = source_file->block_br;
   er = source_file->block_er;

  
   rcol =  (action == FILL || action == NUMBER) ? bc : window->rcol;

   
   source_first = same = FALSE;
   if (action == FILL) {
      if (block_type == BOX) {
         if (get_block_fill_char( window, &fill_char ) == ERROR)
            return( ERROR );
         dest = block_start;
         same = TRUE;
      } else {
        
         error( WARNING, prompt_line, block2 );
         return( ERROR );
      }
   }
   block_inc = 1;
   if (action == NUMBER) {
      if (block_type == BOX) {
         if (get_block_numbers( window, &block_num, &block_inc, &block_just )
              == ERROR)
            return( ERROR );
         dest = block_start;
         same = TRUE;
      } else {
        
         error( WARNING, prompt_line, block3a );
         return( ERROR );
      }
   }
   if (action == SWAP) {
      if (block_type != BOX) {
        
         error( WARNING, prompt_line, block3b );
         return( ERROR );
      }
   }
   if (source_file == dest_file && action != DELETE && action != FILL) {
      same = TRUE;
      if (block_type == BOX && action == MOVE) {
         if (rline == br  &&  (rcol >= bc && rcol <= ec))
            
            return( ERROR );
      } else if (block_type == LINE || block_type == STREAM) {
         if (rline >= br && rline <= er) {
            if (block_type == LINE) {
               
               if (action == COPY || action == KOPY)
                  dest = block_end;
                
               else if (action == MOVE)
                  return( ERROR );
            } else {

              
               if ((rline > br && rline < er) ||
                   (br == er && rcol >= bc && rcol <= ec) ||
                   (br != er && ((rline == br && rcol >= bc) ||
                                 (rline == er && rcol <= ec)))) {

               
                  if (action == COPY || action == KOPY) {
                     dest = block_end;
                     rcol = ec + 1;
                     rline = er;
                  } else if (action == MOVE)
                     return( ERROR );
               }
            }
         }
      }
   }
   if (br < rline)
      source_first = TRUE;

   block_len = (ec+1) - bc;
   if (block_type == BOX) {
      if (action != DELETE && action != FILL) {
         if (rcol + block_len > MAX_LINE_LENGTH) {
          
            error( WARNING, prompt_line, ltol );
            return( ERROR );
         }
      }
   } else if (block_type == LINE) {
      block_len = 0;
      if (action == OVERLAY) {
        
         error( WARNING, prompt_line, block5 );
         return( ERROR );
      }
   } else if (block_type == STREAM) {

      if (action == OVERLAY) {
         
         error( WARNING, prompt_line, block5 );
         return( ERROR );
      }

      lend = block_end->len;
      if (action == DELETE || action == MOVE) {

        
         if (lend > ec)
            lend -= ec;
         else
            lend = 0;
         if (bc + lend > MAX_LINE_LENGTH) {
           
            error( WARNING, prompt_line, ltol );
            return( ERROR );
         }
      }

      if (action != DELETE) {

         
         lens = block_start->len;

        
         if (dest->line == window->ll->line)
            add = dest->len;
         else
            add = lens;

         
         if (lens > bc)
            lens -= bc;
         else
            lens = 0;
         if (rcol + lens > MAX_LINE_LENGTH) {
           
            error( WARNING, prompt_line, ltol );
            return( ERROR );
         }

       
         if (add > bc)
            add -= bc;
         else
            add = 0;
         if (lend > ec)
            lend -= ec;
         else
            lend = 0;
         if (add + lend > MAX_LINE_LENGTH) {
           
            error( WARNING, prompt_line, ltol );
            return( ERROR );
         }
      }
      if (ptoul( block_start ) == ptoul( block_end )) {
         block_type = BOX;
         block_len = (ec+1) - bc;
      }
   }

   if (mode.do_backups == TRUE) {
      switch (action) {
         case MOVE :
         case DELETE :
         case COPY :
         case KOPY :
         case SWAP :
            window->file_info->modified = TRUE;
            rc = backup_file( window );
            break;
      }
      switch (action) {
         case MOVE :
         case DELETE :
         case FILL :
         case NUMBER :
         case SWAP :
            source_window->file_info->modified = TRUE;
            if (rc != ERROR)
               rc = backup_file( source_window );
            break;
      }
   }
   source = block_start;

   assert( block_start != NULL );
   assert( block_start->len != EOF );
   assert( block_end != NULL );
   assert( block_end->len != EOF );

   if (block_type == LINE)
      do_line_block( window,  source_window,  action,
                     source_file,  dest_file,  block_start,  block_end,
                     source,  dest,  br,  er,  &rc );

   else if (block_type == STREAM)
      do_stream_block( window,  source_window,  action,
                       source_file,  dest_file,  block_start,  block_end,
                       source,  dest,  rline,  br,  er,  bc,  ec,  rcol,  &rc );

⌨️ 快捷键说明

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