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

Subversion Repositories openrisc_me

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

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

Line No. Rev Author Line
1 207 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
  __sfp_lock_acquire ();
97
  _flockfile (fp);
98
  ORIENT (fp, 1);
99
 
100
  if (n <= 0)
101
    {
102
      errno = EINVAL;
103
      goto error;
104
    }
105
 
106
  if (fp->_r <= 0 && __srefill_r (ptr, fp))
107
    /* EOF */
108
    goto error;
109
  wsp = ws;
110
  do
111
    {
112
      src = (char *) fp->_p;
113
      nl = memchr (fp->_p, '\n', fp->_r);
114
      nconv = _mbsrtowcs_r (ptr, wsp, &src,
115
                            nl != NULL ? (nl - fp->_p + 1) : fp->_r,
116
                            &fp->_mbstate);
117
      if (nconv == (size_t) -1)
118
        /* Conversion error */
119
        goto error;
120
      if (src == NULL)
121
        {
122
          /*
123
           * We hit a null byte. Increment the character count,
124
           * since mbsnrtowcs()'s return value doesn't include
125
           * the terminating null, then resume conversion
126
           * after the null.
127
           */
128
          nconv++;
129
          src = memchr (fp->_p, '\0', fp->_r);
130
          src++;
131
        }
132
      fp->_r -= (unsigned char *) src - fp->_p;
133
      fp->_p = (unsigned char *) src;
134
      n -= nconv;
135
      wsp += nconv;
136
    }
137
  while (wsp[-1] != L'\n' && n > 1 && (fp->_r > 0
138
         || __srefill_r (ptr, fp) == 0));
139
  if (wsp == ws)
140
    /* EOF */
141
    goto error;
142
  if (!mbsinit (&fp->_mbstate))
143
    /* Incomplete character */
144
    goto error;
145
  *wsp++ = L'\0';
146
  _funlockfile (fp);
147
  __sfp_lock_release ();
148
  return ws;
149
 
150
error:
151
  _funlockfile (fp);
152
  __sfp_lock_release ();
153
  return NULL;
154
}
155
 
156
wchar_t *
157
_DEFUN(fgetws, (ws, n, fp),
158
        wchar_t *ws _AND
159
        int n _AND
160
        FILE *fp)
161
{
162
  CHECK_INIT (_REENT, fp);
163
  return _fgetws_r (_REENT, ws, n, fp);
164
}

powered by: WebSVN 2.1.0

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