OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [stdio/] [fgetws.c] - Blame information for rev 387

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/*-
2
 * Copyright (c) 2002-2004 Tim J. Robbins.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 *
14
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
 * SUCH DAMAGE.
25
 */
26
 
27
/*
28
FUNCTION
29
<<fgetws>>---get wide character string from a file or stream
30
 
31
INDEX
32
        fgetws
33
INDEX
34
        _fgetws_r
35
 
36
ANSI_SYNOPSIS
37
        #include <wchar.h>
38
        wchar_t *fgetws(wchar_t *<[ws]>, int <[n]>, FILE *<[fp]>);
39
 
40
        #include <wchar.h>
41
        wchar_t *_fgetws_r(struct _reent *<[ptr]>, wchar_t *<[ws]>, int <[n]>, FILE *<[fp]>);
42
 
43
TRAD_SYNOPSIS
44
        #include <wchar.h>
45
        wchar_t *fgetws(<[ws]>,<[n]>,<[fp]>)
46
        wchar_t *<[ws]>;
47
        int <[n]>;
48
        FILE *<[fp]>;
49
 
50
        #include <wchar.h>
51
        wchar_t *_fgetws_r(<[ptr]>, <[ws]>,<[n]>,<[fp]>)
52
        struct _reent *<[ptr]>;
53
        wchar_t *<[ws]>;
54
        int <[n]>;
55
        FILE *<[fp]>;
56
 
57
DESCRIPTION
58
Reads at most <[n-1]> wide characters from <[fp]> until a newline
59
is found. The wide characters including to the newline are stored
60
in <[ws]>. The buffer is terminated with a 0.
61
 
62
The <<_fgetws_r>> function is simply the reentrant version of
63
<<fgetws>> and is passed an additional reentrancy structure
64
pointer: <[ptr]>.
65
 
66
RETURNS
67
<<fgetws>> returns the buffer passed to it, with the data
68
filled in. If end of file occurs with some data already
69
accumulated, the data is returned with no other indication. If
70
no data are read, NULL is returned instead.
71
 
72
PORTABILITY
73
C99, POSIX.1-2001
74
*/
75
 
76
#include <_ansi.h>
77
#include <reent.h>
78
#include <errno.h>
79
#include <stdio.h>
80
#include <string.h>
81
#include <wchar.h>
82
#include "local.h"
83
 
84
wchar_t *
85
_DEFUN(_fgetws_r, (ptr, ws, n, fp),
86
        struct _reent *ptr _AND
87
        wchar_t * ws _AND
88
        int n _AND
89
        FILE * fp)
90
{
91
  wchar_t *wsp;
92
  size_t nconv;
93
  const char *src;
94
  unsigned char *nl;
95
 
96
  _flockfile (fp);
97
  ORIENT (fp, 1);
98
 
99
  if (n <= 0)
100
    {
101
      errno = EINVAL;
102
      goto error;
103
    }
104
 
105
  if (fp->_r <= 0 && __srefill_r (ptr, fp))
106
    /* EOF */
107
    goto error;
108
  wsp = ws;
109
  do
110
    {
111
      src = fp->_p;
112
      nl = memchr (fp->_p, '\n', fp->_r);
113
      nconv = _mbsrtowcs_r (ptr, wsp, &src,
114
                            nl != NULL ? (nl - fp->_p + 1) : fp->_r,
115
                            &fp->_mbstate);
116
      if (nconv == (size_t) -1)
117
        /* Conversion error */
118
        goto error;
119
      if (src == NULL)
120
        {
121
          /*
122
           * We hit a null byte. Increment the character count,
123
           * since mbsnrtowcs()'s return value doesn't include
124
           * the terminating null, then resume conversion
125
           * after the null.
126
           */
127
          nconv++;
128
          src = memchr (fp->_p, '\0', fp->_r);
129
          src++;
130
        }
131
      fp->_r -= (unsigned char *) src - fp->_p;
132
      fp->_p = (unsigned char *) src;
133
      n -= nconv;
134
      wsp += nconv;
135
    }
136
  while (wsp[-1] != L'\n' && n > 1 && (fp->_r > 0
137
         || __srefill_r (ptr, fp) == 0));
138
  if (wsp == ws)
139
    /* EOF */
140
    goto error;
141
  if (!mbsinit (&fp->_mbstate))
142
    /* Incomplete character */
143
    goto error;
144
  *wsp++ = L'\0';
145
  _funlockfile (fp);
146
  return ws;
147
 
148
error:
149
  _funlockfile (fp);
150
  return NULL;
151
}
152
 
153
wchar_t *
154
_DEFUN(fgetws, (ws, n, fp),
155
        wchar_t *ws _AND
156
        int n _AND
157
        FILE *fp)
158
{
159
  CHECK_INIT (_REENT, fp);
160
  return _fgetws_r (_REENT, ws, n, fp);
161
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.