File: input.c

package info (click to toggle)
msort 8.53-2.2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,360 kB
  • sloc: sh: 10,138; ansic: 10,031; makefile: 51
file content (332 lines) | stat: -rw-r--r-- 8,154 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Time-stamp: <2008-11-02 12:10:41 poser> */
/*
 * Copyright (C) 2005-2007 William J. Poser.
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 3 of the GNU General Public License as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"
#include "compdefs.h"

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <wchar.h>
#ifdef LOCALE_GETTEXT
#include <libintl.h>
#define _(x) gettext(x)
#else
#define _(x) (x)
#endif
#ifdef HAVE_UNINUM_UNICODE_H
#include <uninum/unicode.h>
#else
#include "unicode.h"
#endif
#include "input.h"
#include "limits.h"
#include "utf8error.h"

#define MSGSIZE 128
#define MAXUTF8LEN 6

static char msg [MSGSIZE];

int
ReportReadError(
	FILE *fp,
	UTF32 c, 		/* Error code */
	unsigned char *rp,	/* Pointer to raw input sequence */
	unsigned long RecordNumber,
	unsigned long ByteCnt)
{

  extern void ExplicateBadUTF8(FILE *, unsigned char *);

  switch (c)
    { 
    case UTF8_NOTENOUGHBYTES:
      fprintf(fp,_("Truncated UTF-8 sequence encountered at record %ld, byte %ld.\n"),
	      RecordNumber, ByteCnt);
      exit(1);
      break;
    case UTF8_BADINCODE:
      fprintf(fp,_("Invalid UTF-8 code encountered at record %ld, byte %ld.\n"),
	      RecordNumber, ByteCnt);
      ExplicateBadUTF8(fp,rp);
      exit(1);
      break;
    case UTF8_BADOUTCODE:
      fprintf(fp,_("Encountered invalid Unicode at record %ld, byte %ld.\n"),
	      RecordNumber, ByteCnt);
      exit(1);
      break;
    case UTF8_IOERROR:
      snprintf(msg,MSGSIZE-1,_("Error reading input at record %ld, byte %ld.\n"),
	       RecordNumber,ByteCnt);
      perror(msg);
      exit(1);
      break;
    default:			/* Normal EOF */
      return(0);
      break;			/* NOTREACHED */
    }
}

/*
 * Read a block of UTF-8 text terminated by an extra newline into a buffer,
 * reallocating storage as necessary. The variable BufferSize is updated
 * if the size of the buffer is increased.
 * Returns the block of text.
 * The number of characters put into the buffer, not including
 * the terminating null is returned in the variable status if everything goes well.
 * Otherwise, the error code is placed in status:  INPUT_BUFOVERFLOW if storage cannot be allocated.
 * On end of input a count of zero characters is returned in status.
 */

UTF8 *
GetNNBlockRAUTF8(FILE *fp,
	 UTF8 *buf,
	 int *status,
	 int *BufferSize,
	 wchar_t t,
	 unsigned long RecordNumber,
	 unsigned long ByteCnt)
{
  int cnt = 0;
  int c;
  int state = 0;
  int eol;
  static int done = 0;

  if(done){
    done = 0;
    *status =	INPUT_ENDOFINPUT;
    return(buf);
  }

  eol = (int) t;
  while( (c = getc(fp)) != EOF){
    if(cnt == *BufferSize){
      *BufferSize = 2 * *BufferSize;
      buf = (unsigned char *) realloc( (void *) buf, (size_t) (*BufferSize * sizeof(unsigned char)));
      if(buf == NULL){
	*status=INPUT_BUFOVERFLOW;
	return(buf);
      }
    }
    if(c == eol) {
      if(state==0){
	buf[cnt++]=eol;
	state=1;
      }
      else if(state == 1){
	buf[cnt]='\0';
	*status=cnt;
	return(buf);
      }
    }
    else {
      if(state==1) state=0;
      buf[cnt++]=c;
    }
  } /* End of while */
	
  buf[cnt] = '\0';
  done = 1;
  *status=cnt;
  return(buf);
}

void
ucstrappend(unsigned char *tgt, unsigned char *src,int slen) {
  int i;
  for(i = 0; i <slen; i++){
    *tgt++ = *src++;
  }
  *tgt = '\0';
}

/*
 * Read a block of UTF-8 text terminated by a specified character
 * from a stream into a buffer, checking to make
 * sure that the size of the buffer is not exceeded.
 * Returns the block of text.
 * The number of characters put into the buffer, not including
 * the terminating null is returned in the variable status if everything goes well.
 * Otherwise, the error code is placed in status:  INPUT_BUFOVERFLOW if storage cannot be allocated.
 * On end of input a count of zero characters is returned in status.
 */

UTF8 *
GetBlockSepCharRAUTF8(
      FILE *stream,
      UTF8 *buf,
      int *status,
      int *BufferSize,
      wchar_t t,
      unsigned long RecordNumber,
      unsigned long ByteCnt
)
{
  UTF32 c;
  int cnt = 0;
  int UCBytes;
  unsigned char *rawptr;
  int infd;
  static int done = 0;
  extern UTF32 UTF8in (int,int *,unsigned char **);

  infd = fileno(stream);
  if(done){
    done = 0;
    *status=INPUT_ENDOFINPUT;
    return(buf);
  }

  while ( (c = UTF8in(infd,&UCBytes,&rawptr)) <= UNI_MAX_UTF32){
    if(cnt >= (*BufferSize -MAXUTF8LEN)){
      *BufferSize = 2 * *BufferSize;
      buf = (UTF8 *) realloc( (void *) buf, (size_t) (((*BufferSize) +1) * sizeof(UTF8)));
      if(buf == NULL){
	fprintf(stderr,"null buf ptr on request for %u bytes\n",*BufferSize);fflush(stderr);
	*status=INPUT_BUFOVERFLOW;
	return(buf);
      }
    }
    if(c == t){
      *status=cnt;
      return(buf);
    }
    else {
      ucstrappend(buf+cnt,rawptr,UCBytes);
      cnt += UCBytes;
    }
  }
  if(c > UNI_MAX_UTF32){
    ReportReadError(stderr,c,rawptr,RecordNumber,ByteCnt-UCBytes);
  }
  done = 1;
  *status=cnt;
  return(buf);
}

/*
 * Read a line into buffer from a file of UTF-8 characters, converting
 * en passant to UTF32.
 * Returns the number of characters in the line, not counting the
 * terminating null. Returns:
 *	 
 *     INPUT_ENDOFINPUT     if there is no more input
 *     INPUT_BUFOVERFLOW    if the length of the line exceeds the buffer size 
 *     	 
 */

#define DEFAULT 0
#define OVERFLOW 1
#define MSGSIZE 128

int
ugetline(FILE *fp,wchar_t *buf, int size)
{

  wchar_t c;
  int cnt = 0;
  int state = DEFAULT;
  static short GetLineDone = 0;
  char msg[MSGSIZE];
  /* These two are not used here but I don't want to include two versions of Get_UTF32...*/
  int UCBytes;		
  unsigned char *rawptr;

  extern UTF32 UTF8in (int,int *,unsigned char **);

  if(GetLineDone){
    GetLineDone = 0;
    return(INPUT_ENDOFINPUT);
  }
   
  while( (c = UTF8in(fileno(fp),&UCBytes,&rawptr)) <= UNI_MAX_UTF32){
    if(cnt == size){
      buf[cnt] = '\0';
      state = OVERFLOW;
    }
    if(c == L'\n'){
      if(state == OVERFLOW) return(INPUT_BUFOVERFLOW);
      else{
	buf[cnt] = L'\0';
	return(cnt);
      }
    }
    else if(state == DEFAULT) buf[cnt++] = c;
  }
  GetLineDone = 1;
  switch (c){ 
     case UTF8_NOTENOUGHBYTES:
       fprintf(stderr,"Truncated UTF-8 sequence encountered.\n");
       exit(1);
       break;
     case UTF8_BADINCODE:
       fprintf(stderr,"Invalid UTF-8 code encountered.\n");
       exit(1);
       break;
     case UTF8_BADOUTCODE:
       fprintf(stderr,"Encountered invalid Unicode.\n");
       exit(1);
       break;
     case UTF8_IOERROR:
       snprintf(msg,MSGSIZE-1,"Error reading input.\n");
       perror(msg);
       exit(1);
       break;
     default:			/* Normal EOF */
       break;
  }
  if(state == OVERFLOW) return(INPUT_BUFOVERFLOW);
  else{
    buf[cnt] = '\0';
    GetLineDone = 1;
    return(cnt);
  }
}


UTF8 *
GetFixedLengthRecord(
     FILE *fp,	/* Stream from which to read */
     UTF8 *buf,  /* Address of buffer */
     int *bread, /* Pointer to number of bytes actually read - return parameter */
     int *lenptr, /* Pointer to intended length of record, in bytes */
     wchar_t dummy2, /* Unused argument needed for consistency with other functions  */
     unsigned long RecordNumber, /* Dummy */
     unsigned long ByteCnt	/* Dummy */

) {
  int BytesRead;
  BytesRead = read(fileno(fp),buf,*lenptr);
  if(BytesRead == 0) {
    *bread = INPUT_ENDOFINPUT;
  }
  else if (BytesRead < *lenptr) {
    *bread = INPUT_SHORTRECORD;
  } else {
    *bread = BytesRead;
    buf[*lenptr] = '\0';
  }
  return buf;
}