1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-06-11 14:20:41 +00:00

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

180
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,36 +278,31 @@ 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. */
if (s == FIOFNF) { /* File not found. */
mloutstr( "(New file)") ; mloutstr( "(New file)") ;
goto out; else if( s == FIOSUC) {
} char *errmsg ;
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) {
nbytes = fpayload ; line_p lp ;
#if PKCODE
if (nline > MAXNLINE) { if( nline >= 10000000 /* MAXNLINE Maximum # of lines from one file */
s = FIOMEM; || (lp = lalloc( fpayload)) == NULL) {
break;
}
#endif
if ((lp1 = lalloc(nbytes)) == NULL) {
s = FIOMEM ; /* Keep message on the */ s = FIOMEM ; /* Keep message on the */
break ; /* display. */ break ; /* display. */
} }
lp2 = lback(curbp->b_linep);
lp2->l_fp = lp1; memcpy( lp->l_text, fline, fpayload) ;
lp1->l_fp = curbp->b_linep; lp->l_fp = curbp->b_linep ; /* insert before end of buffer */
lp1->l_bp = lp2; lp->l_bp = lp->l_fp->l_bp ;
curbp->b_linep->l_bp = lp1; lp->l_fp->l_bp = lp ;
memcpy( lp1->l_text, fline, nbytes) ; lp->l_bp->l_fp = lp ;
++nline; nline += 1 ;
} }
if( s == FIOERR) if( s == FIOERR)
@ -357,12 +343,12 @@ int readin(const char *fname, boolean lockfl)
mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)", mloutfmt( "(%sRead %d line%s, code/eol: %s/%s)",
errmsg, errmsg,
nline, nline,
(nline != 1) ? "s" : "", &"s"[ nline == 1],
codename[ fcode & (FCODE_MASK - 1)], codename[ fcode & (FCODE_MASK - 1)],
eolname[ found_eol]) ; eolname[ found_eol]) ;
ffclose() ; /* Ignore errors. */ 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,44 +520,39 @@ 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. */
if( s != FIOSUC)
mloutstr( "Cannot open file for writing") ; mloutstr( "Cannot open file for writing") ;
return FALSE; else {
} line_p lp ;
mloutstr( "(Writing...)") ; /* tell us were writing */ fio_code s2 ;
lp = lforw(curbp->b_linep); /* First line. */ int nline = 0 ;
nline = 0; /* Number of lines. */
while (lp != curbp->b_linep) { mloutstr( "(Writing...)") ; /* tell us we are writing */
s = ffputline( &lp->l_text[0], llength(lp), curbp->b_mode & MDDOS) ; for( lp = lforw( curbp->b_linep) ; lp != curbp->b_linep ; lp = lforw( lp)) {
if( s != FIOSUC) { s = ffputline( lp->l_text, llength( lp), curbp->b_mode & MDDOS) ;
mloutstr( "Write I/O error") ; if( s != FIOSUC)
break ; break ;
nline += 1 ;
} }
++nline; s2 = ffclose() ;
lp = lforw(lp); if( s != FIOSUC)
} mloutstr( "Write I/O error") ;
if (s == FIOSUC) { /* No write error. */ else if( s2 != FIOSUC)
s = ffclose();
if (s == FIOSUC) { /* No close error. */
if (nline == 1)
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 FALSE;
return TRUE ; return TRUE ;
} }
}
return FALSE ;
}
/* /*
* The command allows the user * The command allows the user
@ -620,24 +600,20 @@ 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. */
goto out;
if( s == FIOFNF) { /* File not found. */ if( s == FIOFNF) { /* File not found. */
mloutstr( "(No such file)") ; mloutstr( "(No such file)") ;
return FALSE ; return FALSE ;
} }
if( s == FIOSUC) { /* Hard file open. */
int nline = 0 ; /* number of line read */
char *errmsg ;
mloutstr( "(Inserting file)") ; mloutstr( "(Inserting file)") ;
/* back up a line and save the mark here */ /* back up a line and save the mark here */
@ -646,27 +622,27 @@ static int ifile( const char *fname) {
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) {
nbytes = fpayload ; line_p lpp, lp, lpn ;
if ((lp1 = lalloc(nbytes)) == NULL) {
if( (lp = lalloc( fpayload)) == NULL) {
s = FIOMEM ; /* Keep message on the */ s = FIOMEM ; /* Keep message on the */
break ; /* display. */ 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 */ memcpy( lp->l_text, fline, fpayload) ;
lp2->l_bp = lp1; lp->l_bp = lpp = curwp->w_dotp ; /* insert after dot line */
lp0->l_fp = lp1; lp->l_fp = lpn = lpp->l_fp ; /* line after insert */
lp1->l_bp = lp0;
lp1->l_fp = lp2; /* re-link new line between lpp and lpn */
lpn->l_bp = lp ;
lpp->l_fp = lp ;
/* and advance and write out the current line */ /* and advance and write out the current line */
curwp->w_dotp = lp1; curwp->w_dotp = lp ;
memcpy( lp1->l_text, fline, nbytes) ; nline += 1 ;
++nline;
} }
ffclose() ; /* Ignore errors. */ ffclose() ; /* Ignore errors. */
curwp->w_markp = lforw(curwp->w_markp); curwp->w_markp = lforw(curwp->w_markp);
if( s == FIOERR) { if( s == FIOERR) {
@ -681,9 +657,9 @@ static int ifile( const char *fname) {
mloutfmt( "(%sInserted %d line%s)", mloutfmt( "(%sInserted %d line%s)",
errmsg, errmsg,
nline, nline,
(nline > 1) ? "s" : "") ; &"s"[ nline == 1]) ;
}
out:
/* advance to the next line and mark the window for changes */ /* 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;
@ -694,7 +670,5 @@ out:
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;
} }

View File

@ -118,8 +118,7 @@ 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 */
@ -142,13 +141,10 @@ fio_code ffgetline(void)
/* 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 */
lcode |= c ;
/* if it's longer, get more room */
if( i >= flen) { if( i >= flen) {
char *tmpline ; /* temp storage for expanding line */ char *tmpline ; /* temp storage for expanding line */
fpayload = i ;
tmpline = malloc( flen + NSTRING) ; tmpline = malloc( flen + NSTRING) ;
if( tmpline == NULL) if( tmpline == NULL)
return FIOMEM ; return FIOMEM ;
@ -158,17 +154,21 @@ fio_code ffgetline(void)
free( fline) ; free( fline) ;
fline = tmpline ; fline = tmpline ;
} }
fline[ i++] = c ;
lcode |= c ;
} }
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 */
@ -199,7 +199,5 @@ fio_code ffgetline(void)
} else /* c == '\n' */ } else /* c == '\n' */
ftype |= FTYPE_UNIX ; ftype |= FTYPE_UNIX ;
/* terminate the string */
fline[i] = 0;
return FIOSUC ; return FIOSUC ;
} }

28
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_size = size ;
lp->l_used = used ; 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,10 +641,8 @@ char *getctext(void)
} }
/* copy it across */ /* copy it across */
dp = rline; memcpy( rline, lp->l_text, size) ;
while (size--) rline[ size] = 0 ;
*dp++ = *sp++;
*dp = 0;
return rline ; return rline ;
} }