Back to index

glibc  2.9
iogetwline.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,1997-2000,2002,2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.
00018 
00019    As a special exception, if you link the code in this file with
00020    files compiled with a GNU compiler to produce an executable,
00021    that does not cause the resulting executable to be covered by
00022    the GNU Lesser General Public License.  This exception does not
00023    however invalidate any other reasons why the executable file
00024    might be covered by the GNU Lesser General Public License.
00025    This exception applies to code released by its copyright holders
00026    in files containing the exception.  */
00027 
00028 #include "libioP.h"
00029 #include <string.h>
00030 #include <wchar.h>
00031 
00032 #ifdef _LIBC
00033 # define wmemcpy __wmemcpy
00034 #endif
00035 
00036 #if defined _LIBC || !_G_HAVE_IO_GETLINE_INFO
00037 
00038 _IO_size_t
00039 _IO_getwline (fp, buf, n, delim, extract_delim)
00040      _IO_FILE *fp;
00041      wchar_t *buf;
00042      _IO_size_t n;
00043      wint_t delim;
00044      int extract_delim;
00045 {
00046   return _IO_getwline_info (fp, buf, n, delim, extract_delim, (wint_t *) 0);
00047 }
00048 
00049 /* Algorithm based on that used by Berkeley pre-4.4 fgets implementation.
00050 
00051    Read chars into buf (of size n), until delim is seen.
00052    Return number of chars read (at most n).
00053    Does not put a terminating '\0' in buf.
00054    If extract_delim < 0, leave delimiter unread.
00055    If extract_delim > 0, insert delim in output. */
00056 
00057 _IO_size_t
00058 _IO_getwline_info (fp, buf, n, delim, extract_delim, eof)
00059      _IO_FILE *fp;
00060      wchar_t *buf;
00061      _IO_size_t n;
00062      wint_t delim;
00063      int extract_delim;
00064      wint_t *eof;
00065 {
00066   wchar_t *ptr = buf;
00067   if (eof != NULL)
00068     *eof = 0;
00069   if (__builtin_expect (fp->_mode, 1) == 0)
00070     _IO_fwide (fp, 1);
00071   while (n != 0)
00072     {
00073       _IO_ssize_t len = (fp->_wide_data->_IO_read_end
00074                       - fp->_wide_data->_IO_read_ptr);
00075       if (len <= 0)
00076        {
00077          wint_t wc = __wuflow (fp);
00078          if (wc == WEOF)
00079            {
00080              if (eof)
00081               *eof = wc;
00082              break;
00083            }
00084          if (wc == delim)
00085            {
00086              if (extract_delim > 0)
00087               *ptr++ = wc;
00088              else if (extract_delim < 0)
00089               INTUSE(_IO_sputbackc) (fp, wc);
00090              if (extract_delim > 0)
00091               ++len;
00092              return ptr - buf;
00093            }
00094          *ptr++ = wc;
00095          n--;
00096        }
00097       else
00098        {
00099          wchar_t *t;
00100          if ((_IO_size_t) len >= n)
00101            len = n;
00102          t = wmemchr ((void *) fp->_wide_data->_IO_read_ptr, delim, len);
00103          if (t != NULL)
00104            {
00105              _IO_size_t old_len = ptr - buf;
00106              len = t - fp->_wide_data->_IO_read_ptr;
00107              if (extract_delim >= 0)
00108               {
00109                 ++t;
00110                 if (extract_delim > 0)
00111                   ++len;
00112               }
00113              wmemcpy ((void *) ptr, (void *) fp->_wide_data->_IO_read_ptr,
00114                      len);
00115              fp->_wide_data->_IO_read_ptr = t;
00116              return old_len + len;
00117            }
00118          wmemcpy ((void *) ptr, (void *) fp->_wide_data->_IO_read_ptr, len);
00119          fp->_wide_data->_IO_read_ptr += len;
00120          ptr += len;
00121          n -= len;
00122        }
00123     }
00124   return ptr - buf;
00125 }
00126 
00127 #endif /* Defined _LIBC || !_G_HAVE_IO_GETLINE_INFO */