Review file read and write into buffers.

This commit is contained in:
Renaud 2016-03-30 17:44:46 +08:00
parent 06eba37779
commit ac3c2aa6dd
3 changed files with 226 additions and 260 deletions

342
file.c
View File

@ -28,11 +28,6 @@
#include "mlout.h" #include "mlout.h"
#include "window.h" #include "window.h"
#if PKCODE
/* Max number of lines from one file. */
#define MAXNLINE 10000000
#endif
typedef enum { typedef enum {
EOL_NONE, EOL_NONE,
EOL_UNIX, EOL_UNIX,
@ -254,15 +249,10 @@ int getfile( const char *fname, boolean lockfl)
*/ */
int readin(const char *fname, boolean lockfl) int readin(const char *fname, boolean lockfl)
{ {
struct line *lp1;
struct line *lp2;
struct window *wp; struct window *wp;
struct buffer *bp; struct buffer *bp;
int s; int status ;
eoltype found_eol ; fio_code s ;
int nbytes;
int nline;
char *errmsg ;
#if (FILOCK && BSD) || SVR4 #if (FILOCK && BSD) || SVR4
if (lockfl && lockchk(fname) == ABORT) if (lockfl && lockchk(fname) == ABORT)
@ -278,8 +268,9 @@ int readin(const char *fname, boolean lockfl)
#endif #endif
#endif #endif
bp = curbp; /* Cheap. */ bp = curbp; /* Cheap. */
if ((s = bclear(bp)) != TRUE) /* Might be old. */ if( (status = bclear( bp)) != TRUE) /* Might be old. */
return s; return status ;
bp->b_flag &= ~(BFINVS | BFCHG); bp->b_flag &= ~(BFINVS | BFCHG);
strncpy( bp->b_fname, fname, sizeof( fname_t) - 1) ; strncpy( bp->b_fname, fname, sizeof( fname_t) - 1) ;
bp->b_fname[ sizeof( fname_t) - 1] = '\0' ; bp->b_fname[ sizeof( fname_t) - 1] = '\0' ;
@ -287,82 +278,77 @@ int readin(const char *fname, boolean lockfl)
/* let a user macro get hold of things...if he wants */ /* let a user macro get hold of things...if he wants */
execute(META | SPEC | 'R', FALSE, 1); execute(META | SPEC | 'R', FALSE, 1);
if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */ s = ffropen( bp->b_fname) ; /* Always use the name associated to buffer */
goto out; if( s == FIOFNF) /* File not found. */
mloutstr( "(New file)") ;
if (s == FIOFNF) { /* File not found. */ else if( s == FIOSUC) {
mloutstr( "(New file)") ; char *errmsg ;
goto out; eoltype found_eol ;
} int nline = 0 ;
/* read the file in */ /* read the file in */
mloutstr( "(Reading file)") ; mloutstr( "(Reading file)") ;
nline = 0; while ((s = ffgetline()) == FIOSUC) {
while ((s = ffgetline()) == FIOSUC) { line_p lp ;
nbytes = fpayload ;
#if PKCODE
if (nline > MAXNLINE) {
s = FIOMEM;
break;
}
#endif
if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM; /* Keep message on the */
break; /* display. */
}
lp2 = lback(curbp->b_linep);
lp2->l_fp = lp1;
lp1->l_fp = curbp->b_linep;
lp1->l_bp = lp2;
curbp->b_linep->l_bp = lp1;
memcpy( lp1->l_text, fline, nbytes) ;
++nline;
}
if( s == FIOERR)
mloutstr( "File read error") ;
switch( ftype) { if( nline >= 10000000 /* MAXNLINE Maximum # of lines from one file */
case FTYPE_DOS: || (lp = lalloc( fpayload)) == NULL) {
found_eol = EOL_DOS ; s = FIOMEM ; /* Keep message on the */
curbp->b_mode |= MDDOS ; break ; /* display. */
break ; }
case FTYPE_UNIX:
found_eol = EOL_UNIX ; memcpy( lp->l_text, fline, fpayload) ;
break ; lp->l_fp = curbp->b_linep ; /* insert before end of buffer */
case FTYPE_MAC: lp->l_bp = lp->l_fp->l_bp ;
found_eol = EOL_MAC ; lp->l_fp->l_bp = lp ;
break ; lp->l_bp->l_fp = lp ;
case FTYPE_NONE: nline += 1 ;
found_eol = EOL_NONE ; }
break ;
default: if( s == FIOERR)
found_eol = EOL_MIXED ; mloutstr( "File read error") ;
curbp->b_mode |= MDVIEW ; /* force view mode as we have lost
** EOL information */ switch( ftype) {
case FTYPE_DOS:
found_eol = EOL_DOS ;
curbp->b_mode |= MDDOS ;
break ;
case FTYPE_UNIX:
found_eol = EOL_UNIX ;
break ;
case FTYPE_MAC:
found_eol = EOL_MAC ;
break ;
case FTYPE_NONE:
found_eol = EOL_NONE ;
break ;
default:
found_eol = EOL_MIXED ;
curbp->b_mode |= MDVIEW ; /* force view mode as we have lost
** EOL information */
}
if( fcode == FCODE_UTF_8)
curbp->b_mode |= MDUTF8 ;
if( s == FIOERR) {
errmsg = "I/O ERROR, " ;
curbp->b_flag |= BFTRUNC ;
} else if( s == FIOMEM) {
errmsg = "OUT OF MEMORY, " ;
curbp->b_flag |= BFTRUNC ;
} else
errmsg = "" ;
mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)",
errmsg,
nline,
&"s"[ nline == 1],
codename[ fcode & (FCODE_MASK - 1)],
eolname[ found_eol]) ;
ffclose() ; /* Ignore errors. */
} }
if( fcode == FCODE_UTF_8)
curbp->b_mode |= MDUTF8 ;
if( s == FIOERR) {
errmsg = "I/O ERROR, " ;
curbp->b_flag |= BFTRUNC ;
} else if( s == FIOMEM) {
errmsg = "OUT OF MEMORY, " ;
curbp->b_flag |= BFTRUNC ;
} else
errmsg = "" ;
mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)",
errmsg,
nline,
(nline != 1) ? "s" : "",
codename[ fcode & (FCODE_MASK -1)],
eolname[ found_eol]) ;
ffclose(); /* Ignore errors. */
out:
for (wp = wheadp; wp != NULL; wp = wp->w_wndp) { for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
if (wp->w_bufp == curbp) { if (wp->w_bufp == curbp) {
wp->w_linep = lforw(curbp->b_linep); wp->w_linep = lforw(curbp->b_linep);
@ -373,9 +359,8 @@ int readin(const char *fname, boolean lockfl)
wp->w_flag |= WFMODE | WFHARD; wp->w_flag |= WFMODE | WFHARD;
} }
} }
if (s == FIOERR || s == FIOFNF) /* False if error. */
return FALSE; return (s == FIOERR || s == FIOFNF) ? FALSE : TRUE ;
return TRUE;
} }
/* /*
@ -487,7 +472,7 @@ int filewrite( int f, int n) {
/* /*
* Save the contents of the current * Save the contents of the current
* buffer in its associatd file. No nothing * buffer in its associated file. Do nothing
* if nothing has changed (this may be a bug, not a * if nothing has changed (this may be a bug, not a
* feature). Error if there is no remembered file * feature). Error if there is no remembered file
* name for the buffer. Bound to "C-X C-S". May * name for the buffer. Bound to "C-X C-S". May
@ -535,43 +520,38 @@ int filesave(int f, int n)
* a macro for this. Most of the grief is error * a macro for this. Most of the grief is error
* checking of some sort. * checking of some sort.
*/ */
int writeout( const char *fn) int writeout( const char *fn) {
{ fio_code s ;
int s;
struct line *lp;
int nline;
if ((s = ffwopen(fn)) != FIOSUC) { /* Open writes message. */ s = ffwopen( fn) ; /* Open writes message. */
mloutstr( "Cannot open file for writing") ; if( s != FIOSUC)
return FALSE; mloutstr( "Cannot open file for writing") ;
} else {
mloutstr( "(Writing...)") ; /* tell us were writing */ line_p lp ;
lp = lforw(curbp->b_linep); /* First line. */ fio_code s2 ;
nline = 0; /* Number of lines. */ int nline = 0 ;
while (lp != curbp->b_linep) {
s = ffputline( &lp->l_text[0], llength(lp), curbp->b_mode & MDDOS) ; mloutstr( "(Writing...)") ; /* tell us we are writing */
if( s != FIOSUC) { for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) {
mloutstr( "Write I/O error") ; s = ffputline( lp->l_text, llength( lp), curbp->b_mode & MDDOS) ;
break; if( s != FIOSUC)
} break ;
++nline; nline += 1 ;
lp = lforw(lp); }
}
if (s == FIOSUC) { /* No write error. */ s2 = ffclose() ;
s = ffclose(); if( s != FIOSUC)
if (s == FIOSUC) { /* No close error. */ mloutstr( "Write I/O error") ;
if (nline == 1) else if( s2 != FIOSUC)
mloutstr( "(Wrote 1 line)") ;
else
mloutfmt( "(Wrote %d lines)", nline) ;
} else
mloutstr( "Error closing file") ; mloutstr( "Error closing file") ;
} else /* Ignore close error */ else { /* Successfull write and close. */
ffclose(); /* if a write error. */ mloutfmt( "(Wrote %d line%s)", nline, &"s"[ nline == 1]) ;
if (s != FIOSUC) /* Some sort of error. */ return TRUE ;
return FALSE; }
return TRUE; }
return FALSE ;
} }
/* /*
@ -620,81 +600,75 @@ int filename( int f, int n) {
* status of the read. * status of the read.
*/ */
static int ifile( const char *fname) { static int ifile( const char *fname) {
struct line *lp0; fio_code s ;
struct line *lp1;
struct line *lp2;
struct buffer *bp;
int s;
int nbytes;
int nline;
char *errmsg ;
bp = curbp; /* Cheap. */ curbp->b_flag |= BFCHG ; /* we have changed */
bp->b_flag |= BFCHG; /* we have changed */ curbp->b_flag &= ~BFINVS ; /* and are not temporary */
bp->b_flag &= ~BFINVS; /* and are not temporary */ s = ffropen( fname) ;
if ((s = ffropen(fname)) == FIOERR) /* Hard file open. */ if( s == FIOFNF) { /* File not found. */
goto out;
if (s == FIOFNF) { /* File not found. */
mloutstr( "(No such file)") ; mloutstr( "(No such file)") ;
return FALSE; return FALSE ;
} }
mloutstr( "(Inserting file)") ;
if( s == FIOSUC) { /* Hard file open. */
int nline = 0 ; /* number of line read */
char *errmsg ;
mloutstr( "(Inserting file)") ;
/* back up a line and save the mark here */ /* back up a line and save the mark here */
curwp->w_dotp = lback(curwp->w_dotp); curwp->w_dotp = lback(curwp->w_dotp);
curwp->w_doto = 0; curwp->w_doto = 0;
curwp->w_markp = curwp->w_dotp; curwp->w_markp = curwp->w_dotp;
curwp->w_marko = 0; curwp->w_marko = 0;
nline = 0; while( (s = ffgetline()) == FIOSUC) {
while ((s = ffgetline()) == FIOSUC) { line_p lpp, lp, lpn ;
nbytes = fpayload ;
if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM; /* Keep message on the */
break; /* display. */
}
lp0 = curwp->w_dotp; /* line previous to insert */
lp2 = lp0->l_fp; /* line after insert */
/* re-link new line between lp0 and lp2 */ if( (lp = lalloc( fpayload)) == NULL) {
lp2->l_bp = lp1; s = FIOMEM ; /* Keep message on the */
lp0->l_fp = lp1; break ; /* display. */
lp1->l_bp = lp0; }
lp1->l_fp = lp2;
/* and advance and write out the current line */ memcpy( lp->l_text, fline, fpayload) ;
curwp->w_dotp = lp1; lp->l_bp = lpp = curwp->w_dotp ; /* insert after dot line */
memcpy( lp1->l_text, fline, nbytes) ; lp->l_fp = lpn = lpp->l_fp ; /* line after insert */
++nline;
}
ffclose(); /* Ignore errors. */
curwp->w_markp = lforw(curwp->w_markp);
if( s == FIOERR) {
errmsg = "I/O ERROR, " ;
curbp->b_flag |= BFTRUNC ;
} else if( s == FIOMEM) {
errmsg = "OUT OF MEMORY, " ;
curbp->b_flag |= BFTRUNC;
} else
errmsg = "" ;
mloutfmt( "(%sInserted %d line%s)",
errmsg,
nline,
(nline > 1) ? "s" : "") ;
out: /* re-link new line between lpp and lpn */
/* advance to the next line and mark the window for changes */ lpn->l_bp = lp ;
lpp->l_fp = lp ;
/* and advance and write out the current line */
curwp->w_dotp = lp ;
nline += 1 ;
}
ffclose() ; /* Ignore errors. */
curwp->w_markp = lforw(curwp->w_markp);
if( s == FIOERR) {
errmsg = "I/O ERROR, " ;
curbp->b_flag |= BFTRUNC ;
} else if( s == FIOMEM) {
errmsg = "OUT OF MEMORY, " ;
curbp->b_flag |= BFTRUNC;
} else
errmsg = "" ;
mloutfmt( "(%sInserted %d line%s)",
errmsg,
nline,
&"s"[ nline == 1]) ;
}
/* advance to the next line and mark the window for changes */
curwp->w_dotp = lforw(curwp->w_dotp); curwp->w_dotp = lforw(curwp->w_dotp);
curwp->w_flag |= WFHARD | WFMODE; curwp->w_flag |= WFHARD | WFMODE;
/* copy window parameters back to the buffer structure */ /* copy window parameters back to the buffer structure */
curbp->b_dotp = curwp->w_dotp; curbp->b_dotp = curwp->w_dotp;
curbp->b_doto = curwp->w_doto; curbp->b_doto = curwp->w_doto;
curbp->b_markp = curwp->w_markp; curbp->b_markp = curwp->w_markp;
curbp->b_marko = curwp->w_marko; curbp->b_marko = curwp->w_marko;
if (s == FIOERR) /* False if error. */ return (s == FIOERR) ? FALSE : TRUE ;
return FALSE;
return TRUE;
} }

114
fileio.c
View File

@ -118,57 +118,57 @@ fio_code ffputline( char *buf, int nbuf, int dosflag) {
* at the end of the file that don't have a newline present. Check for I/O * at the end of the file that don't have a newline present. Check for I/O
* errors too. Return status. * errors too. Return status.
*/ */
fio_code ffgetline(void) fio_code ffgetline( void) {
{ int c ; /* current character read */
int c; /* current character read */ int i ; /* current index into fline */
int i; /* current index into fline */ int lcode = FCODE_ASCII ; /* line encoding, defaults to ASCII */
int lcode = FCODE_ASCII ; /* line encoding, defaults to ASCII */
/* if we are at the end...return it */ /* if we are at the end...return it */
if (eofflag) if( eofflag)
return FIOEOF; return FIOEOF ;
/* dump fline if it ended up too big */ /* dump fline if it ended up too big */
if (flen > NSTRING) { if( flen > NSTRING) {
free(fline); free( fline) ;
fline = NULL; fline = NULL ;
} }
/* if we don't have an fline, allocate one */ /* if we don't have an fline, allocate one */
if (fline == NULL) if( fline == NULL)
if ((fline = malloc(flen = NSTRING)) == NULL) if( (fline = malloc( flen = NSTRING)) == NULL)
return FIOMEM; return FIOMEM ;
/* read the line in */ /* read the line in */
i = 0; i = 0 ;
while ((c = fgetc(ffp)) != EOF && c != '\r' && c != '\n') { while( (c = fgetc( ffp)) != EOF && c != '\r' && c != '\n') {
fline[i++] = c; /* if line is full, get more room */
if( i >= flen) {
char *tmpline ; /* temp storage for expanding line */
tmpline = malloc( flen + NSTRING) ;
if( tmpline == NULL)
return FIOMEM ;
memcpy( tmpline, fline, flen) ;
flen += NSTRING ;
free( fline) ;
fline = tmpline ;
}
fline[ i++] = c ;
lcode |= c ; lcode |= c ;
/* if it's longer, get more room */ }
if (i >= flen) {
char *tmpline; /* temp storage for expanding line */
fpayload = i ;
tmpline = malloc(flen + NSTRING) ;
if( tmpline == NULL)
return FIOMEM ;
memcpy( tmpline, fline, flen) ;
flen += NSTRING;
free(fline);
fline = tmpline;
}
}
fpayload = i ; fpayload = i ;
lcode &= FCODE_MASK ; lcode &= FCODE_MASK ;
if( lcode && (fcode != FCODE_MIXED)) { /* line contains extended chars */ if( lcode && (fcode != FCODE_MIXED)) { /* line contains extended chars */
/* Check if consistent UTF-8 encoding */ /* Check if consistent UTF-8 encoding */
int bytes ;
int pos = 0 ; int pos = 0 ;
unicode_t uc ;
while( (pos < i) && (lcode != FCODE_MIXED)) { while( (pos < i) && (lcode != FCODE_MIXED)) {
unicode_t uc ;
int bytes ;
bytes = utf8_to_unicode( fline, pos, i, &uc) ; bytes = utf8_to_unicode( fline, pos, i, &uc) ;
pos += bytes ; pos += bytes ;
if( bytes > 1) /* Multi byte UTF-8 sequence */ if( bytes > 1) /* Multi byte UTF-8 sequence */
@ -180,26 +180,24 @@ fio_code ffgetline(void)
fcode |= lcode ; fcode |= lcode ;
} }
/* test for any errors that may have occured */ /* test for any errors that may have occured */
if (c == EOF) { if( c == EOF) {
if( ferror( ffp)) if( ferror( ffp))
return FIOERR ; return FIOERR ;
if (i != 0) if( i != 0)
eofflag = TRUE; eofflag = TRUE ;
else else
return FIOEOF; return FIOEOF ;
} else if( c == '\r') { } else if( c == '\r') {
c = fgetc( ffp) ; c = fgetc( ffp) ;
if( c != '\n') { if( c != '\n') {
ftype |= FTYPE_MAC ; ftype |= FTYPE_MAC ;
ungetc( c, ffp) ; ungetc( c, ffp) ;
} else } else
ftype |= FTYPE_DOS ; ftype |= FTYPE_DOS ;
} else /* c == '\n' */ } else /* c == '\n' */
ftype |= FTYPE_UNIX ; ftype |= FTYPE_UNIX ;
/* terminate the string */ return FIOSUC ;
fline[i] = 0;
return FIOSUC;
} }

30
line.c
View File

@ -179,15 +179,16 @@ line_p lalloc( int used) {
line_p lp ; line_p lp ;
int size ; int size ;
size = used + BLOCK_SIZE - used % BLOCK_SIZE ; /* size = used + BLOCK_SIZE - used % BLOCK_SIZE ; */
size = (used + BLOCK_SIZE) & ~(BLOCK_SIZE - 1) ; /* as BLOCK_SIZE is power of 2 */
lp = (line_p) malloc( offsetof( struct line, l_text) + size) ; lp = (line_p) malloc( offsetof( struct line, l_text) + size) ;
if( lp == NULL) { if( lp == NULL)
mloutstr( "(OUT OF MEMORY)") ; mloutstr( "(OUT OF MEMORY)") ;
return NULL ; else {
lp->l_size = size ;
lp->l_used = used ;
} }
lp->l_size = size ;
lp->l_used = used ;
return lp ; return lp ;
} }
@ -197,8 +198,7 @@ line_p lalloc( int used) {
* might be in. Release the memory. The buffers are updated too; the magic * might be in. Release the memory. The buffers are updated too; the magic
* conditions described in the above comments don't hold here. * conditions described in the above comments don't hold here.
*/ */
void lfree(struct line *lp) void lfree( line_p lp) {
{
struct buffer *bp; struct buffer *bp;
struct window *wp; struct window *wp;
@ -619,18 +619,14 @@ int ldelete(long n, int kflag)
* getctext: grab and return a string with the text of * getctext: grab and return a string with the text of
* the current line * the current line
*/ */
char *getctext(void) char *getctext( void) {
{ line_p lp ; /* line to copy */
struct line *lp; /* line to copy */
int size; /* length of line to return */ int size; /* length of line to return */
char *sp; /* string pointer into line */
char *dp; /* string pointer into returned line */
static int rsize = 0 ; static int rsize = 0 ;
static char *rline ; /* line to return */ static char *rline ; /* line to return */
/* find the contents of the current line and its length */ /* find the contents of the current line and its length */
lp = curwp->w_dotp; lp = curwp->w_dotp;
sp = lp->l_text;
size = lp->l_used; size = lp->l_used;
if( size >= rsize) { if( size >= rsize) {
if( rsize) if( rsize)
@ -645,11 +641,9 @@ char *getctext(void)
} }
/* copy it across */ /* copy it across */
dp = rline; memcpy( rline, lp->l_text, size) ;
while (size--) rline[ size] = 0 ;
*dp++ = *sp++; return rline ;
*dp = 0;
return rline;
} }
/* /*