1
0
mirror of https://github.com/rfivet/uemacs.git synced 2024-12-18 07:16:23 -05:00

Handle all types of eol when reading files.

This commit is contained in:
Renaud 2013-06-08 17:32:48 +08:00
parent 319957e8e0
commit 30b1b06acb
3 changed files with 172 additions and 131 deletions

21
file.c
View File

@ -231,6 +231,7 @@ int readin(char *fname, int lockfl)
struct window *wp; struct window *wp;
struct buffer *bp; struct buffer *bp;
int s; int s;
int eoltype ;
int nbytes; int nbytes;
int nline; int nline;
char mesg[NSTRING]; char mesg[NSTRING];
@ -294,6 +295,7 @@ int readin(char *fname, int lockfl)
lputc(lp1, i, fline[i]); lputc(lp1, i, fline[i]);
++nline; ++nline;
} }
eoltype = ftype ;
ffclose(); /* Ignore errors. */ ffclose(); /* Ignore errors. */
strcpy(mesg, "("); strcpy(mesg, "(");
if (s == FIOERR) { if (s == FIOERR) {
@ -307,6 +309,25 @@ int readin(char *fname, int lockfl)
sprintf(&mesg[strlen(mesg)], "Read %d line", nline); sprintf(&mesg[strlen(mesg)], "Read %d line", nline);
if (nline != 1) if (nline != 1)
strcat(mesg, "s"); strcat(mesg, "s");
strcat( mesg, ", eol = ") ;
switch( eoltype) {
case FTYPE_DOS:
strcat( mesg, "DOS") ;
break ;
case FTYPE_UNIX:
strcat( mesg, "UNIX") ;
break ;
case FTYPE_MAC:
strcat( mesg, "MAC") ;
break ;
case FTYPE_NONE:
strcat( mesg, "NONE") ;
break ;
default:
strcat( mesg, "MIXED") ;
}
strcat(mesg, ")"); strcat(mesg, ")");
mlwrite(mesg); mlwrite(mesg);

View File

@ -19,14 +19,17 @@
static FILE *ffp; /* File pointer, all functions. */ static FILE *ffp; /* File pointer, all functions. */
static int eofflag; /* end-of-file flag */ static int eofflag; /* end-of-file flag */
int ftype ;
/* /*
* Open a file for reading. * Open a file for reading.
*/ */
int ffropen( const char *fn) int ffropen( const char *fn)
{ {
if ((ffp = fopen(fn, "rt")) == NULL) if ((ffp = fopen(fn, "r")) == NULL)
return FIOFNF; return FIOFNF;
eofflag = FALSE; eofflag = FALSE;
ftype = FTYPE_NONE ;
return FIOSUC; return FIOSUC;
} }
@ -61,6 +64,7 @@ int ffclose(void)
fline = NULL; fline = NULL;
} }
eofflag = FALSE; eofflag = FALSE;
ftype = FTYPE_NONE ;
#if MSDOS & CTRLZ #if MSDOS & CTRLZ
fputc(26, ffp); /* add a ^Z at the end of the file */ fputc(26, ffp); /* add a ^Z at the end of the file */
@ -103,6 +107,7 @@ int ffputline(char *buf, int nbuf)
fputc(buf[i] & 0xFF, ffp); fputc(buf[i] & 0xFF, ffp);
#endif #endif
fputc( '\r', ffp) ;
fputc('\n', ffp); fputc('\n', ffp);
if (ferror(ffp)) { if (ferror(ffp)) {
@ -141,7 +146,7 @@ int ffgetline(void)
return FIOMEM; return FIOMEM;
/* read the line in */ /* read the line in */
#if PKCODE #if 0 /* PKCODE */
if (!nullflag) { if (!nullflag) {
if (fgets(fline, NSTRING, ffp) == (char *) NULL) { /* EOF ? */ if (fgets(fline, NSTRING, ffp) == (char *) NULL) { /* EOF ? */
i = 0; i = 0;
@ -161,9 +166,9 @@ int ffgetline(void)
while (c != EOF && c != '\n') { while (c != EOF && c != '\n') {
#else #else
i = 0; i = 0;
while ((c = fgetc(ffp)) != EOF && c != '\n') { while ((c = fgetc(ffp)) != EOF && c != '\r' && c != '\n') {
#endif #endif
#if PKCODE #if 0 /* PKCODE */
if (c) { if (c) {
#endif #endif
fline[i++] = c; fline[i++] = c;
@ -177,7 +182,7 @@ int ffgetline(void)
free(fline); free(fline);
fline = tmpline; fline = tmpline;
} }
#if PKCODE #if 0 /* PKCODE */
} }
c = fgetc(ffp); c = fgetc(ffp);
#endif #endif
@ -194,7 +199,15 @@ int ffgetline(void)
eofflag = TRUE; eofflag = TRUE;
else else
return FIOEOF; return FIOEOF;
} } else if( c == '\r') {
c = fgetc( ffp) ;
if( c != '\n') {
ftype |= FTYPE_MAC ;
ungetc( c, ffp) ;
} else
ftype |= FTYPE_DOS ;
} else /* c == '\n' */
ftype |= FTYPE_UNIX ;
/* terminate and decrypt the string */ /* terminate and decrypt the string */
fline[i] = 0; fline[i] = 0;

View File

@ -1,6 +1,13 @@
#ifndef _FILEIO_H_ #ifndef _FILEIO_H_
#define _FILEIO_H_ #define _FILEIO_H_
#define FTYPE_NONE 0
#define FTYPE_UNIX 1
#define FTYPE_DOS 2
#define FTYPE_MAC 4
extern int ftype ;
int fexist( const char *fname) ; int fexist( const char *fname) ;
int ffclose( void) ; int ffclose( void) ;
int ffgetline( void) ; int ffgetline( void) ;