2013-05-17 10:58:27 -04:00
|
|
|
/* fileio.c -- implements fileio.h */
|
|
|
|
#include "fileio.h"
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* The routines in this file read and write ASCII files from the disk. All
|
|
|
|
of the knowledge about files are here.
|
|
|
|
|
|
|
|
modified by Petri Kutvonen
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
|
|
|
|
2013-05-18 01:40:11 -04:00
|
|
|
#include <stdio.h>
|
2013-06-10 01:30:06 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-09-20 06:10:30 -04:00
|
|
|
#include "defines.h"
|
2013-10-01 06:27:57 -04:00
|
|
|
#include "retcode.h"
|
2015-02-15 00:30:54 -05:00
|
|
|
#include "utf8.h"
|
2013-06-10 01:30:06 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
char *fline = NULL ; /* dynamic return line */
|
|
|
|
static int flen = 0 ; /* current allocated length of fline */
|
|
|
|
int ftype ;
|
|
|
|
int fcode ; /* encoding type FCODE_xxxxx */
|
|
|
|
int fpayload ; /* actual length of fline content */
|
2013-06-10 01:30:06 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
static FILE *ffp ; /* File pointer, all functions. */
|
|
|
|
static boolean eofflag ; /* end-of-file flag */
|
2013-06-08 05:32:48 -04:00
|
|
|
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* Open a file for reading. */
|
|
|
|
fio_code ffropen( const char *fn) {
|
|
|
|
eofflag = FALSE ;
|
2013-06-08 05:32:48 -04:00
|
|
|
ftype = FTYPE_NONE ;
|
2015-02-15 00:30:54 -05:00
|
|
|
fcode = FCODE_ASCII ;
|
2021-08-13 23:45:41 -04:00
|
|
|
ffp = fopen( fn, "r") ;
|
|
|
|
return (ffp == NULL) ? FIOFNF : FIOSUC ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
|
|
|
|
/* Open a file for writing. Return TRUE if all is well, and FALSE on error
|
|
|
|
(cannot create).
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2019-07-03 20:06:13 -04:00
|
|
|
fio_code ffwopen( const char *fn) {
|
|
|
|
ffp = fopen( fn, "w") ;
|
2021-08-13 23:45:41 -04:00
|
|
|
return (ffp == NULL) ? FIOERR : FIOSUC ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
|
|
|
|
/* Close a file. Should look at the status in all systems.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2021-08-13 23:45:41 -04:00
|
|
|
fio_code ffclose( void) {
|
2013-06-08 05:32:48 -04:00
|
|
|
/* free this since we do not need it anymore */
|
2021-08-13 23:45:41 -04:00
|
|
|
if( fline) {
|
|
|
|
free( fline) ;
|
|
|
|
fline = NULL ;
|
2013-06-08 05:32:48 -04:00
|
|
|
}
|
2021-08-13 23:45:41 -04:00
|
|
|
|
|
|
|
eofflag = FALSE ;
|
2013-06-08 05:32:48 -04:00
|
|
|
ftype = FTYPE_NONE ;
|
2015-02-15 00:30:54 -05:00
|
|
|
fcode = FCODE_ASCII ;
|
2013-06-08 05:32:48 -04:00
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
return (fclose( ffp) != FALSE) ? FIOERR : FIOSUC ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
|
|
|
|
/* Write a line to the already opened file. The "buf" points to the
|
|
|
|
buffer, and the "nbuf" is its length, less the free newline. Return the
|
|
|
|
status. Check only at the newline.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2013-06-10 21:51:29 -04:00
|
|
|
fio_code ffputline( char *buf, int nbuf, int dosflag) {
|
2021-08-13 23:45:41 -04:00
|
|
|
fwrite( buf, 1, nbuf, ffp) ;
|
|
|
|
|
|
|
|
if( dosflag)
|
|
|
|
fputc( '\r', ffp) ;
|
2013-06-08 23:21:35 -04:00
|
|
|
|
2013-06-10 00:09:39 -04:00
|
|
|
fputc( '\n', ffp) ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-10 23:53:08 -04:00
|
|
|
if( ferror( ffp))
|
2013-06-10 00:09:39 -04:00
|
|
|
return FIOERR ;
|
2005-05-31 11:50:56 -04:00
|
|
|
|
2013-06-10 00:09:39 -04:00
|
|
|
return FIOSUC ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
|
|
|
|
2021-08-13 23:45:41 -04:00
|
|
|
/* Read a line from a file, and store the bytes in the supplied buffer.
|
|
|
|
The "nbuf" is the length of the buffer. Complain about long lines and
|
|
|
|
lines at the end of the file that don't have a newline present. Check
|
|
|
|
for I/O errors too. Return status.
|
2005-05-31 11:50:56 -04:00
|
|
|
*/
|
2016-03-30 05:44:46 -04:00
|
|
|
fio_code ffgetline( void) {
|
2021-08-13 23:45:41 -04:00
|
|
|
int c ; /* current character read */
|
|
|
|
int lcode = FCODE_ASCII ; /* line encoding, defaults to ASCII */
|
2016-03-30 05:44:46 -04:00
|
|
|
|
|
|
|
/* if we are at the end...return it */
|
2021-08-13 23:45:41 -04:00
|
|
|
if( eofflag)
|
|
|
|
return FIOEOF ;
|
2016-03-30 05:44:46 -04:00
|
|
|
|
|
|
|
/* dump fline if it ended up too big */
|
2021-08-13 23:45:41 -04:00
|
|
|
if( flen > NSTRING) {
|
|
|
|
free( fline) ;
|
|
|
|
fline = NULL ;
|
|
|
|
}
|
2013-06-08 05:32:48 -04:00
|
|
|
|
2016-03-30 05:44:46 -04:00
|
|
|
/* if we don't have an fline, allocate one */
|
2021-08-13 23:45:41 -04:00
|
|
|
if( fline == NULL)
|
|
|
|
if( (fline = malloc( flen = NSTRING)) == NULL)
|
|
|
|
return FIOMEM ;
|
2016-03-30 05:44:46 -04:00
|
|
|
|
|
|
|
/* read the line in */
|
2021-08-13 23:45:41 -04:00
|
|
|
int i = 0 ; /* current index into fline */
|
|
|
|
while( (c = fgetc( ffp)) != EOF && c != '\r' && c != '\n') {
|
|
|
|
/* 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 ;
|
|
|
|
}
|
|
|
|
|
|
|
|
fpayload = i ;
|
|
|
|
if( lcode & 0x80 /* line contains extended chars */
|
|
|
|
&& (fcode != FCODE_MIXED)) {
|
|
|
|
/* Check if consistent UTF-8 encoding */
|
|
|
|
lcode = FCODE_ASCII ;
|
|
|
|
int pos = 0 ;
|
|
|
|
while( (pos < i) && (lcode != FCODE_MIXED)) {
|
|
|
|
unicode_t uc ;
|
|
|
|
|
|
|
|
int bytes = utf8_to_unicode( fline, pos, i, &uc) ;
|
|
|
|
pos += bytes ;
|
|
|
|
if( bytes > 1) /* Multi byte UTF-8 sequence */
|
|
|
|
lcode |= FCODE_UTF_8 ;
|
|
|
|
else if( uc > 127) /* Extended ASCII */
|
|
|
|
lcode |= FCODE_EXTND ;
|
|
|
|
}
|
|
|
|
|
|
|
|
fcode |= lcode ;
|
|
|
|
}
|
2013-06-10 02:39:19 -04:00
|
|
|
|
2016-03-30 05:44:46 -04:00
|
|
|
/* test for any errors that may have occured */
|
2021-08-13 23:45:41 -04:00
|
|
|
if( c == EOF) {
|
|
|
|
if( ferror( ffp))
|
|
|
|
return FIOERR ;
|
|
|
|
|
|
|
|
if( i != 0)
|
|
|
|
eofflag = TRUE ;
|
|
|
|
else
|
|
|
|
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 ;
|
2016-03-30 05:44:46 -04:00
|
|
|
|
|
|
|
return FIOSUC ;
|
2005-05-31 11:50:56 -04:00
|
|
|
}
|
2021-08-13 23:45:41 -04:00
|
|
|
|
|
|
|
/* end of fileio.c */
|