Back to index

glibc  2.9
wcstok.c
Go to the documentation of this file.
00001 /* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <wchar.h>
00021 #include <errno.h>
00022 
00023 
00024 /* Parse WCS into tokens separated by characters in DELIM.  If WCS is
00025    NULL, the last string wcstok() was called with is used.  */
00026 wchar_t *
00027 wcstok (wcs, delim, save_ptr)
00028      wchar_t *wcs;
00029      const wchar_t *delim;
00030      wchar_t **save_ptr;
00031 {
00032   wchar_t *result;
00033 
00034   if (wcs == NULL)
00035     {
00036       if (*save_ptr == NULL)
00037        {
00038          __set_errno (EINVAL);
00039          return NULL;
00040        }
00041       else
00042        wcs = *save_ptr;
00043     }
00044 
00045   /* Scan leading delimiters.  */
00046   wcs += wcsspn (wcs, delim);
00047   if (*wcs == L'\0')
00048     {
00049       *save_ptr = NULL;
00050       return NULL;
00051     }
00052 
00053   /* Find the end of the token.     */
00054   result = wcs;
00055   wcs = wcspbrk (result, delim);
00056   if (wcs == NULL)
00057     /* This token finishes the string.    */
00058     *save_ptr = NULL;
00059   else
00060     {
00061       /* Terminate the token and make *SAVE_PTR point past it.  */
00062       *wcs = L'\0';
00063       *save_ptr = wcs + 1;
00064     }
00065   return result;
00066 }