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:
parent
319957e8e0
commit
30b1b06acb
21
file.c
21
file.c
@ -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);
|
||||||
|
|
||||||
|
275
fileio.c
275
fileio.c
@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
#include "fileio.h"
|
#include "fileio.h"
|
||||||
|
|
||||||
/* FILEIO.C
|
/* FILEIO.C
|
||||||
*
|
*
|
||||||
* The routines in this file read and write ASCII files from the disk. All of
|
* The routines in this file read and write ASCII files from the disk. All of
|
||||||
* the knowledge about files are here.
|
* the knowledge about files are here.
|
||||||
*
|
*
|
||||||
* modified by Petri Kutvonen
|
* modified by Petri Kutvonen
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -16,18 +16,21 @@
|
|||||||
#include "estruct.h"
|
#include "estruct.h"
|
||||||
#include "edef.h"
|
#include "edef.h"
|
||||||
|
|
||||||
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;
|
||||||
return FIOSUC;
|
ftype = FTYPE_NONE ;
|
||||||
|
return FIOSUC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -37,17 +40,17 @@ int ffropen( const char *fn)
|
|||||||
int ffwopen( const char *fn)
|
int ffwopen( const char *fn)
|
||||||
{
|
{
|
||||||
#if VMS
|
#if VMS
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if ((fd = creat(fn, 0666, "rfm=var", "rat=cr")) < 0
|
if ((fd = creat(fn, 0666, "rfm=var", "rat=cr")) < 0
|
||||||
|| (ffp = fdopen(fd, "w")) == NULL) {
|
|| (ffp = fdopen(fd, "w")) == NULL) {
|
||||||
#else
|
#else
|
||||||
if ((ffp = fopen(fn, "w")) == NULL) {
|
if ((ffp = fopen(fn, "w")) == NULL) {
|
||||||
#endif
|
#endif
|
||||||
mlwrite("Cannot open file for writing");
|
mlwrite("Cannot open file for writing");
|
||||||
return FIOERR;
|
return FIOERR;
|
||||||
}
|
}
|
||||||
return FIOSUC;
|
return FIOSUC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -55,26 +58,27 @@ int ffwopen( const char *fn)
|
|||||||
*/
|
*/
|
||||||
int ffclose(void)
|
int ffclose(void)
|
||||||
{
|
{
|
||||||
/* free this since we do not need it anymore */
|
/* free this since we do not need it anymore */
|
||||||
if (fline) {
|
if (fline) {
|
||||||
free(fline);
|
free(fline);
|
||||||
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 */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if V7 | USG | BSD | (MSDOS & (MSC | TURBO))
|
#if V7 | USG | BSD | (MSDOS & (MSC | TURBO))
|
||||||
if (fclose(ffp) != FALSE) {
|
if (fclose(ffp) != FALSE) {
|
||||||
mlwrite("Error closing file");
|
mlwrite("Error closing file");
|
||||||
return FIOERR;
|
return FIOERR;
|
||||||
}
|
}
|
||||||
return FIOSUC;
|
return FIOSUC;
|
||||||
#else
|
#else
|
||||||
fclose(ffp);
|
fclose(ffp);
|
||||||
return FIOSUC;
|
return FIOSUC;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,32 +89,33 @@ int ffclose(void)
|
|||||||
*/
|
*/
|
||||||
int ffputline(char *buf, int nbuf)
|
int ffputline(char *buf, int nbuf)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
#if CRYPT
|
#if CRYPT
|
||||||
char c; /* character to translate */
|
char c; /* character to translate */
|
||||||
|
|
||||||
if (cryptflag) {
|
if (cryptflag) {
|
||||||
for (i = 0; i < nbuf; ++i) {
|
for (i = 0; i < nbuf; ++i) {
|
||||||
c = buf[i] & 0xff;
|
c = buf[i] & 0xff;
|
||||||
myencrypt(&c, 1);
|
myencrypt(&c, 1);
|
||||||
fputc(c, ffp);
|
fputc(c, ffp);
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
for (i = 0; i < nbuf; ++i)
|
for (i = 0; i < nbuf; ++i)
|
||||||
fputc(buf[i] & 0xFF, ffp);
|
fputc(buf[i] & 0xFF, ffp);
|
||||||
#else
|
#else
|
||||||
for (i = 0; i < nbuf; ++i)
|
for (i = 0; i < nbuf; ++i)
|
||||||
fputc(buf[i] & 0xFF, ffp);
|
fputc(buf[i] & 0xFF, ffp);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fputc('\n', ffp);
|
fputc( '\r', ffp) ;
|
||||||
|
fputc('\n', ffp);
|
||||||
|
|
||||||
if (ferror(ffp)) {
|
if (ferror(ffp)) {
|
||||||
mlwrite("Write I/O error");
|
mlwrite("Write I/O error");
|
||||||
return FIOERR;
|
return FIOERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return FIOSUC;
|
return FIOSUC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -121,107 +126,115 @@ int ffputline(char *buf, int nbuf)
|
|||||||
*/
|
*/
|
||||||
int ffgetline(void)
|
int ffgetline(void)
|
||||||
{
|
{
|
||||||
int c; /* current character read */
|
int c; /* current character read */
|
||||||
int i; /* current index into fline */
|
int i; /* current index into fline */
|
||||||
char *tmpline; /* temp storage for expanding line */
|
char *tmpline; /* temp storage for expanding line */
|
||||||
|
|
||||||
/* 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 */
|
||||||
#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;
|
||||||
c = EOF;
|
c = EOF;
|
||||||
} else {
|
} else {
|
||||||
i = strlen(fline);
|
i = strlen(fline);
|
||||||
c = 0;
|
c = 0;
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
c = fline[i - 1];
|
c = fline[i - 1];
|
||||||
i--;
|
i--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i = 0;
|
i = 0;
|
||||||
c = fgetc(ffp);
|
c = fgetc(ffp);
|
||||||
}
|
}
|
||||||
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;
|
||||||
/* if it's longer, get more room */
|
/* if it's longer, get more room */
|
||||||
if (i >= flen) {
|
if (i >= flen) {
|
||||||
if ((tmpline =
|
if ((tmpline =
|
||||||
malloc(flen + NSTRING)) == NULL)
|
malloc(flen + NSTRING)) == NULL)
|
||||||
return FIOMEM;
|
return FIOMEM;
|
||||||
strncpy(tmpline, fline, flen);
|
strncpy(tmpline, fline, flen);
|
||||||
flen += NSTRING;
|
flen += NSTRING;
|
||||||
free(fline);
|
free(fline);
|
||||||
fline = tmpline;
|
fline = tmpline;
|
||||||
}
|
}
|
||||||
#if PKCODE
|
#if 0 /* PKCODE */
|
||||||
}
|
}
|
||||||
c = fgetc(ffp);
|
c = fgetc(ffp);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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)) {
|
||||||
mlwrite("File read error");
|
mlwrite("File read error");
|
||||||
return FIOERR;
|
return FIOERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i != 0)
|
if (i != 0)
|
||||||
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;
|
||||||
#if CRYPT
|
#if CRYPT
|
||||||
if (cryptflag)
|
if (cryptflag)
|
||||||
myencrypt(fline, strlen(fline));
|
myencrypt(fline, strlen(fline));
|
||||||
#endif
|
#endif
|
||||||
return FIOSUC;
|
return FIOSUC;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* does <fname> exist on disk?
|
* does <fname> exist on disk?
|
||||||
*
|
*
|
||||||
* char *fname; file to check for existance
|
* char *fname; file to check for existance
|
||||||
*/
|
*/
|
||||||
int fexist( const char *fname)
|
int fexist( const char *fname)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
|
||||||
/* try to open the file for reading */
|
/* try to open the file for reading */
|
||||||
fp = fopen(fname, "r");
|
fp = fopen(fname, "r");
|
||||||
|
|
||||||
/* if it fails, just return false! */
|
/* if it fails, just return false! */
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
/* otherwise, close it and report true */
|
/* otherwise, close it and report true */
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
7
fileio.h
7
fileio.h
@ -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) ;
|
||||||
|
Loading…
Reference in New Issue
Block a user