1
#if !defined(lint) && !defined(DOS)
2
static char rcsid[] = "$Id: fileio.c 203 2006-10-26 17:23:46Z hubert@u.washington.edu $";
6
* ========================================================================
7
* Copyright 2006 University of Washington
9
* Licensed under the Apache License, Version 2.0 (the "License");
10
* you may not use this file except in compliance with the License.
11
* You may obtain a copy of the License at
13
* http://www.apache.org/licenses/LICENSE-2.0
15
* ========================================================================
17
* Program: file reading routines
22
* The routines in this file read and write ASCII files from the disk. All of
23
* the knowledge about files are here. A better message writing scheme should
27
#include "../pith/charconv/filesys.h"
30
#if defined(bsd) || defined(lnx)
39
* Open a file for reading.
46
if ((status = fexist(g_pico_fio.name = fn, "r", (off_t *)NULL)) == FIOSUC){
47
g_pico_fio.flags = FIOINFO_READ;
48
if((g_pico_fio.fp = our_fopen(g_pico_fio.name, "r")) == NULL)
57
* Write a line to the already opened file. The "buf" points to the buffer,
58
* and the "nbuf" is its length, less the free newline. Return the status.
59
* Check only at the newline.
62
ffputline(CELL buf[], int nbuf)
66
for(i = 0; i < nbuf; ++i)
67
if(write_a_wide_char((UCS) buf[i].c, g_pico_fio.fp) == EOF)
71
write_a_wide_char((UCS) '\n', g_pico_fio.fp);
73
if(ferror(g_pico_fio.fp)){
74
emlwrite("\007Write error: %s", errstr(errno));
84
* Read a line from a file, and store the bytes in the supplied buffer. The
85
* "nbuf" is the length of the buffer. Complain about long lines and lines
86
* at the end of the file that don't have a newline present. Check for I/O
87
* errors too. Return status.
89
* Translate the line from the user's locale charset to UCS-4.
92
ffgetline(UCS buf[], size_t nbuf, int *charsreturned, int msg)
102
while((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) != CCONV_EOF && ucs != '\n'){
104
* Don't blat the CR should the newline be CRLF and we're
105
* running on a unix system. NOTE: this takes care of itself
106
* under DOS since the non-binary open turns newlines into '\n'.
109
if((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) == CCONV_EOF || ucs == '\n')
112
if(i < nbuf-2) /* Bare CR. Insert it and go on... */
113
buf[i++] = '\r'; /* else, we're up a creek */
117
buf[nbuf - 2] = ucs; /* store last char read */
118
buf[nbuf - 1] = '\0'; /* and terminate it */
120
*charsreturned = nbuf - 1;
123
emlwrite("File has long line", NULL);
131
if(ucs == CCONV_EOF){
132
if(ferror(g_pico_fio.fp)){
133
emlwrite("File read error", NULL);
141
emlwrite("File doesn't end with newline. Adding one.", NULL);