/* * ReplaceCurrentLine - replace current line with work line */ vi_rc ReplaceCurrentLine( void ) { int extra; line *tmp; /* * add extra space to fcb */ FetchFcb( CurrentFcb ); extra = WorkLine->len - CurrentLine->len; CurrentFcb->byte_cnt += extra; CurrentFcb->nullfcb = false; /* * copy new data in */ tmp = LineAlloc( WorkLine->data, WorkLine->len ); tmp->u.ld.mark = CurrentLine->u.ld.mark; ReplaceLLItem( (ss **)&CurrentFcb->lines.head, (ss **)&CurrentFcb->lines.tail, (ss *)CurrentLine, (ss *)tmp ); MemFree( CurrentLine ); CurrentLine = tmp; WorkLine->len = -1; return( CheckCurrentFcbCapacity() ); } /* ReplaceCurrentLine */
/* * duplicateFcb - do just that */ static void duplicateFcb( fcb *cfcb, fcb **dfcb ) { line *cline, *nline; /* * get fcb and create a new one */ FetchFcb( cfcb ); cfcb->non_swappable = TRUE; *dfcb = FcbAlloc( NULL ); (*dfcb)->start_line = cfcb->start_line; (*dfcb)->end_line = cfcb->end_line; (*dfcb)->byte_cnt = cfcb->byte_cnt; (*dfcb)->lines.head = (*dfcb)->lines.tail = NULL; /* * copy all lines */ for( cline = cfcb->lines.head; cline != NULL; cline = cline->next ) { nline = LineAlloc( cline->data, cline->len ); AddLLItemAtEnd( (ss **)&((*dfcb)->lines.head), (ss **)&((*dfcb)->lines.tail), (ss *)nline ); } cfcb->non_swappable = FALSE; (*dfcb)->non_swappable = FALSE; } /* duplicateFcb */
/* * CreateNullLine - put a single null line in an fcb */ void CreateNullLine( fcb *cfcb ) { line *cline; cline = LineAlloc( NULL, 0 ); FetchFcb( cfcb ); AddLLItemAtEnd( (ss **)&(cfcb->lines.head), (ss **)&(cfcb->lines.tail), (ss *)cline ); cfcb->byte_cnt = 1; cfcb->start_line = cfcb->end_line = 1; cfcb->nullfcb = TRUE; } /* CreateNullLine */
/* * CurrentLineReplaceUndoStart - set up undo for replacement of current line */ void CurrentLineReplaceUndoStart( void ) { if( !EditFlags.Undo || UndoStack == NULL ) { return; } cLine = CurrentPos.line; cCol = CurrentPos.column; pageTop = LeftTopPos.line; lineSave = LineAlloc( CurrentLine->data, CurrentLine->len ); lineSave->inf.ld.mark = CurrentLine->inf.ld.mark; } /* CurrentLineReplaceUndoStart */
/* * InsertNewLine - do just that */ void InsertNewLine( line *who, line_list *linelist, char *data, int copylen, insert_dir dir ) { line *cl; if( copylen <= 0 ) { data = NULL; copylen = 0; } cl = LineAlloc( data, copylen ); if( linelist->head == NULL ) { AddLLItemAtEnd( (ss **)&linelist->head, (ss **)&linelist->tail, (ss *)cl ); } else { if( dir == INSERT_AFTER ) { InsertLLItemAfter( (ss **)&linelist->tail, (ss *)who, (ss *)cl ); } else { InsertLLItemBefore( (ss **)&linelist->head, (ss *)who, (ss *)cl ); } } } /* InsertNewLine */
/* * ReadFcbData - read fcb data */ vi_rc ReadFcbData( file *f, bool *crlf_reached ) { int cnt, used, linecnt; bool eofflag; fcb *cfcb; vi_rc rc; f->bytes_pending = false; /* * go to appropriate location in file */ if( f->is_stdio ) { if( extraData != NULL ) { memcpy( ReadBuffer, extraData, extraDataSize ); MemFreePtr( &extraData ); } } else { rc = FileSeek( f->handle, f->curr_pos ); if( rc != ERR_NO_ERR ) { return( rc ); } } /* * read file data */ if( f->is_stdio ) { cnt = fread( ReadBuffer + extraDataSize, 1, MAX_IO_BUFFER - extraDataSize, stdin ); cnt += extraDataSize; extraDataSize = 0; if( ferror( stdin ) ) { return( ERR_READ ); } } else { cnt = read( f->handle, ReadBuffer, MAX_IO_BUFFER ); if( cnt == -1 ) { return( ERR_READ ); } } /* * get new fcb */ cfcb = FcbAlloc( f ); AddLLItemAtEnd( (ss **)&(f->fcbs.head), (ss **)&(f->fcbs.tail), (ss *)cfcb ); /* * create lines from buffer info */ eofflag = CreateLinesFromFileBuffer( cnt, &cfcb->lines, &used, &linecnt, &(cfcb->byte_cnt), crlf_reached ); if( used == 0 ) { CreateNullLine( cfcb ); eofflag = true; } else { /* * update position */ f->curr_pos += used; if( f->fcbs.tail->prev == NULL ) { cfcb->start_line = 1; } else { cfcb->start_line = f->fcbs.tail->prev->end_line + 1; } if( f->is_stdio && feof( stdin ) && used >= cnt || !f->is_stdio && f->curr_pos >= f->size ) { eofflag = true; if( !EditFlags.LastEOL && ReadBuffer[used - 1] == LF ) { ++linecnt; AddLLItemAtEnd( (ss **)&(cfcb->lines.head), (ss **)&(cfcb->lines.tail), (ss *)LineAlloc( NULL, 0 ) ); } } /* * update line numbers */ cfcb->end_line = cfcb->start_line + linecnt - 1; cfcb->non_swappable = false; } if( eofflag ) { if( !f->is_stdio ) { close( f->handle ); f->handle = -1; } return( END_OF_FILE ); } f->bytes_pending = true; if( f->is_stdio ) { extraDataSize = cnt - used; extraData = MemAlloc( extraDataSize ); memcpy( extraData, ReadBuffer + used, extraDataSize ); } return( ERR_NO_ERR ); } /* ReadFcbData */