~ubuntu-branches/debian/stretch/alpine/stretch

« back to all changes in this revision

Viewing changes to pico/fileio.c

  • Committer: Bazaar Package Importer
  • Author(s): Asheesh Laroia
  • Date: 2007-02-17 13:17:42 UTC
  • Revision ID: james.westby@ubuntu.com-20070217131742-99x5c6cpg1pbkdhw
Tags: upstream-0.82+dfsg
ImportĀ upstreamĀ versionĀ 0.82+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if     !defined(lint) && !defined(DOS)
 
2
static char rcsid[] = "$Id: fileio.c 203 2006-10-26 17:23:46Z hubert@u.washington.edu $";
 
3
#endif
 
4
 
 
5
/*
 
6
 * ========================================================================
 
7
 * Copyright 2006 University of Washington
 
8
 *
 
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
 
12
 *
 
13
 *     http://www.apache.org/licenses/LICENSE-2.0
 
14
 *
 
15
 * ========================================================================
 
16
 *
 
17
 * Program:     file reading routines
 
18
 *
 
19
 */
 
20
 
 
21
/*
 
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
 
24
 * be used.
 
25
 */
 
26
#include        "headers.h"
 
27
#include        "../pith/charconv/filesys.h"
 
28
 
 
29
 
 
30
#if     defined(bsd) || defined(lnx)
 
31
extern int errno;
 
32
#endif
 
33
 
 
34
 
 
35
 
 
36
FIOINFO g_pico_fio;
 
37
 
 
38
/*
 
39
 * Open a file for reading.
 
40
 */
 
41
int
 
42
ffropen(char *fn)
 
43
{
 
44
    int status;
 
45
 
 
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)
 
49
          status = FIOFNF;
 
50
    }
 
51
 
 
52
    return (status);
 
53
}
 
54
 
 
55
 
 
56
/*
 
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.
 
60
 */
 
61
int
 
62
ffputline(CELL buf[], int nbuf)
 
63
{
 
64
    register int    i;
 
65
 
 
66
    for(i = 0; i < nbuf; ++i)
 
67
       if(write_a_wide_char((UCS) buf[i].c, g_pico_fio.fp) == EOF)
 
68
         break;
 
69
 
 
70
   if(i == nbuf)
 
71
     write_a_wide_char((UCS) '\n', g_pico_fio.fp);
 
72
 
 
73
    if(ferror(g_pico_fio.fp)){
 
74
        emlwrite("\007Write error: %s", errstr(errno));
 
75
        sleep(5);
 
76
        return FIOERR;
 
77
    }
 
78
 
 
79
    return FIOSUC;
 
80
}
 
81
 
 
82
 
 
83
/*
 
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.
 
88
 *
 
89
 * Translate the line from the user's locale charset to UCS-4.
 
90
 */
 
91
int
 
92
ffgetline(UCS buf[], size_t nbuf, int *charsreturned, int msg)
 
93
{
 
94
    int    i;
 
95
    UCS    ucs;
 
96
 
 
97
    if(charsreturned)
 
98
      *charsreturned = 0;
 
99
 
 
100
    i = 0;
 
101
 
 
102
    while((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) != CCONV_EOF && ucs != '\n'){
 
103
        /*
 
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'.
 
107
         */
 
108
        if(ucs == '\r'){
 
109
            if((ucs = read_a_wide_char(g_pico_fio.fp, input_cs)) == CCONV_EOF || ucs == '\n')
 
110
              break;
 
111
 
 
112
            if(i < nbuf-2)              /* Bare CR. Insert it and go on... */
 
113
              buf[i++] = '\r';          /* else, we're up a creek */
 
114
        }
 
115
 
 
116
        if(i >= nbuf-2){
 
117
            buf[nbuf - 2] = ucs;        /* store last char read */
 
118
            buf[nbuf - 1] = '\0';       /* and terminate it */
 
119
            if(charsreturned)
 
120
              *charsreturned = nbuf - 1;
 
121
 
 
122
            if(msg)
 
123
              emlwrite("File has long line", NULL);
 
124
 
 
125
            return FIOLNG;
 
126
        }
 
127
 
 
128
        buf[i++] = ucs;
 
129
    }
 
130
 
 
131
    if(ucs == CCONV_EOF){
 
132
        if(ferror(g_pico_fio.fp)){
 
133
            emlwrite("File read error", NULL);
 
134
            if(charsreturned)
 
135
              *charsreturned = i;
 
136
 
 
137
            return FIOERR;
 
138
        }
 
139
 
 
140
        if(i != 0)
 
141
          emlwrite("File doesn't end with newline.  Adding one.", NULL);
 
142
        else{
 
143
            if(charsreturned)
 
144
              *charsreturned = i;
 
145
            return FIOEOF;
 
146
        }
 
147
    }
 
148
 
 
149
    buf[i] = '\0';
 
150
 
 
151
    if(charsreturned)
 
152
      *charsreturned = i;
 
153
 
 
154
    return FIOSUC;
 
155
}