Blame lib/mbsinit.c

Packit Service a2489d
/* Test for initial conversion state.
Packit Service a2489d
   Copyright (C) 2008-2018 Free Software Foundation, Inc.
Packit Service a2489d
   Written by Bruno Haible <bruno@clisp.org>, 2008.
Packit Service a2489d
Packit Service a2489d
   This program is free software: you can redistribute it and/or modify
Packit Service a2489d
   it under the terms of the GNU General Public License as published by
Packit Service a2489d
   the Free Software Foundation; either version 3 of the License, or
Packit Service a2489d
   (at your option) any later version.
Packit Service a2489d
Packit Service a2489d
   This program is distributed in the hope that it will be useful,
Packit Service a2489d
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2489d
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2489d
   GNU General Public License for more details.
Packit Service a2489d
Packit Service a2489d
   You should have received a copy of the GNU General Public License
Packit Service a2489d
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service a2489d
Packit Service a2489d
#include <config.h>
Packit Service a2489d
Packit Service a2489d
/* Specification.  */
Packit Service a2489d
#include <wchar.h>
Packit Service a2489d
Packit Service a2489d
#include "verify.h"
Packit Service a2489d
Packit Service a2489d
#if GNULIB_defined_mbstate_t
Packit Service a2489d
Packit Service a2489d
/* Platforms that lack mbsinit() also lack mbrlen(), mbrtowc(), mbsrtowcs()
Packit Service a2489d
   and wcrtomb(), wcsrtombs().
Packit Service a2489d
   We assume that
Packit Service a2489d
     - sizeof (mbstate_t) >= 4,
Packit Service a2489d
     - only stateless encodings are supported (such as UTF-8 and EUC-JP, but
Packit Service a2489d
       not ISO-2022 variants),
Packit Service a2489d
     - for each encoding, the number of bytes for a wide character is <= 4.
Packit Service a2489d
       (This maximum is attained for UTF-8, GB18030, EUC-TW.)
Packit Service a2489d
   We define the meaning of mbstate_t as follows:
Packit Service a2489d
     - In mb -> wc direction, mbstate_t's first byte contains the number of
Packit Service a2489d
       buffered bytes (in the range 0..3), followed by up to 3 buffered bytes.
Packit Service a2489d
       See mbrtowc.c.
Packit Service a2489d
     - In wc -> mb direction, mbstate_t contains no information. In other
Packit Service a2489d
       words, it is always in the initial state.  */
Packit Service a2489d
Packit Service a2489d
verify (sizeof (mbstate_t) >= 4);
Packit Service a2489d
Packit Service a2489d
int
Packit Service a2489d
mbsinit (const mbstate_t *ps)
Packit Service a2489d
{
Packit Service a2489d
  const char *pstate = (const char *)ps;
Packit Service a2489d
Packit Service a2489d
  return pstate == NULL || pstate[0] == 0;
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
#else
Packit Service a2489d
Packit Service a2489d
int
Packit Service a2489d
mbsinit (const mbstate_t *ps)
Packit Service a2489d
{
Packit Service a2489d
# if defined _WIN32 && !defined __CYGWIN__
Packit Service a2489d
  /* Native Windows.  */
Packit Service a2489d
#  ifdef __MINGW32__
Packit Service a2489d
  /* On mingw, 'mbstate_t' is defined as 'int'.  */
Packit Service a2489d
  return ps == NULL || *ps == 0;
Packit Service a2489d
#  else
Packit Service a2489d
  /* MSVC defines 'mbstate_t' as an 8-byte struct; the first 4-bytes matter.  */
Packit Service a2489d
  return ps == NULL || *(const unsigned int *)ps == 0;
Packit Service a2489d
#  endif
Packit Service a2489d
# else
Packit Service a2489d
  /* Minix, HP-UX 11.00, Solaris 2.6, Interix, ...  */
Packit Service a2489d
  /* Maybe this definition works, maybe not...  */
Packit Service a2489d
  return ps == NULL || *(const char *)ps == 0;
Packit Service a2489d
# endif
Packit Service a2489d
}
Packit Service a2489d
Packit Service a2489d
#endif