psmedley / gcc

GNU General Public License v2.0
7 stars 1 forks source link

OS/2-specific C++ locale(ctype) support #22

Closed komh closed 8 years ago

komh commented 8 years ago

Hi/2.

Currently, OS/2 uses general locale(ctype) support of libstdc++-v3. However, it crashes when using std::ctype::classic_table(), because it returns 0(NULL). The following are the test codes.

#include <iostream>
#include <vector>
#include <locale>
#include <sstream>

// This ctype facet classifies commas and endlines as whitespace
struct csv_whitespace : std::ctype<char> {
    static const mask* make_table()
    {
        // make a copy of the "C" locale table
        static std::vector<mask> v(classic_table(), classic_table() + table_size);
        v[','] |=  space;  // comma will be classified as whitespace
        v[' '] &= ~space;      // space will not be classified as whitespace
        return &v[0];
    }
    csv_whitespace(std::size_t refs = 0) : ctype(make_table(), false, refs) {}
};

int main()
{
    std::string in = "Column 1,Column 2,Column 3\n123,456,789";
    std::string token;

    std::cout << "default locale:\n";
    std::istringstream s1(in);
    while(s1 >> token)
            std::cout << "  " << token << '\n';

    std::cout << "locale with modified ctype:\n";
    std::istringstream s2(in);
    s2.imbue(std::locale(s2.getloc(), new csv_whitespace));
    while(s2 >> token)
            std::cout << "  " << token<< '\n';
}
komh commented 8 years ago

Here is the patch.

From f7079ce1c99ad6db0520259589176e97fcdc473a Mon Sep 17 00:00:00 2001
From: KO Myung-Hun <komh@chollian.net>
Date: Tue, 16 Feb 2016 15:29:28 +0900
Subject: [PATCH] OS/2: Implement OS/2-specific locale(ctype) support

    new file:   config/os/os2/ctype_base.h
    new file:   config/os/os2/ctype_configure_char.cc
    new file:   config/os/os2/ctype_inline.h
    new file:   config/os/os2/os_defines.h
    modified:   configure.host
---
 libstdc++-v3/config/os/os2/ctype_base.h            |  58 ++++++++
 libstdc++-v3/config/os/os2/ctype_configure_char.cc |  99 +++++++++++++
 libstdc++-v3/config/os/os2/ctype_inline.h          | 159 +++++++++++++++++++++
 libstdc++-v3/config/os/os2/os_defines.h            |  36 +++++
 libstdc++-v3/configure.host                        |   3 +
 5 files changed, 355 insertions(+)
 create mode 100644 libstdc++-v3/config/os/os2/ctype_base.h
 create mode 100644 libstdc++-v3/config/os/os2/ctype_configure_char.cc
 create mode 100644 libstdc++-v3/config/os/os2/ctype_inline.h
 create mode 100644 libstdc++-v3/config/os/os2/os_defines.h

diff --git a/libstdc++-v3/config/os/os2/ctype_base.h b/libstdc++-v3/config/os/os2/ctype_base.h
new file mode 100644
index 0000000..65af744
--- /dev/null
+++ b/libstdc++-v3/config/os/os2/ctype_base.h
@@ -0,0 +1,58 @@
+// Locale support -*- C++ -*-
+
+// Copyright (C) 2000-2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+//
+// ISO C++ 14882: 22.1  Locales
+//
+
+// Information as gleaned from usr/include/_ctype.h
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  /// @brief  Base class for ctype.
+  struct ctype_base
+  {
+    // Non-standard typedefs.
+    typedef const int* __to_type;
+
+    // NB: Offsets into ctype<char>::_M_table force a particular size
+    // on the mask type. Because of this, we don't use an enum.
+    typedef unsigned mask;
+    static const mask upper     = __CT_UPPER;
+    static const mask lower     = __CT_LOWER;
+    static const mask alpha     = __CT_ALPHA;
+    static const mask digit     = __CT_DIGIT;
+    static const mask xdigit    = __CT_XDIGIT;
+    static const mask space     = __CT_SPACE;
+    static const mask print     = __CT_PRINT;
+    static const mask graph     = __CT_ALPHA | __CT_DIGIT | __CT_PUNCT;
+    static const mask cntrl     = __CT_CNTRL;
+    static const mask punct     = __CT_PUNCT;
+    static const mask alnum     = __CT_ALPHA | __CT_DIGIT;
+  };
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
diff --git a/libstdc++-v3/config/os/os2/ctype_configure_char.cc b/libstdc++-v3/config/os/os2/ctype_configure_char.cc
new file mode 100644
index 0000000..8857450
--- /dev/null
+++ b/libstdc++-v3/config/os/os2/ctype_configure_char.cc
@@ -0,0 +1,99 @@
+// Locale support -*- C++ -*-
+
+// Copyright (C) 2011-2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file ctype_configure_char.cc */
+
+//
+// ISO C++ 14882: 22.1  Locales
+//
+
+#include <locale>
+#include <cstdlib>
+#include <cstring>
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+// Information as gleaned from usr/include/ctype.h
+
+  const ctype_base::mask*
+  ctype<char>::classic_table() throw()
+  { return __libc_GLocaleCtypeDefault.aufType; }
+
+  ctype<char>::ctype(__c_locale, const mask* __table, bool __del,
+                     size_t __refs)
+  : facet(__refs), _M_del(__table != 0 && __del),
+  _M_toupper(NULL), _M_tolower(NULL),
+  _M_table(__table ? __table : classic_table())
+  {
+    memset(_M_widen, 0, sizeof(_M_widen));
+    _M_widen_ok = 0;
+    memset(_M_narrow, 0, sizeof(_M_narrow));
+    _M_narrow_ok = 0;
+  }
+
+  ctype<char>::ctype(const mask* __table, bool __del, size_t __refs)
+  : facet(__refs), _M_del(__table != 0 && __del),
+  _M_toupper(NULL), _M_tolower(NULL),
+  _M_table(__table ? __table : classic_table())
+  {
+    memset(_M_widen, 0, sizeof(_M_widen));
+    _M_widen_ok = 0;
+    memset(_M_narrow, 0, sizeof(_M_narrow));
+    _M_narrow_ok = 0;
+  }
+
+  char
+  ctype<char>::do_toupper(char __c) const
+  { return ::toupper((int) __c); }
+
+  const char*
+  ctype<char>::do_toupper(char* __low, const char* __high) const
+  {
+    while (__low < __high)
+      {
+        *__low = ::toupper((int) *__low);
+        ++__low;
+      }
+    return __high;
+  }
+
+  char
+  ctype<char>::do_tolower(char __c) const
+  { return ::tolower((int) __c); }
+
+  const char*
+  ctype<char>::do_tolower(char* __low, const char* __high) const
+  {
+    while (__low < __high)
+      {
+        *__low = ::tolower((int) *__low);
+        ++__low;
+      }
+    return __high;
+  }
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
diff --git a/libstdc++-v3/config/os/os2/ctype_inline.h b/libstdc++-v3/config/os/os2/ctype_inline.h
new file mode 100644
index 0000000..51611b9
--- /dev/null
+++ b/libstdc++-v3/config/os/os2/ctype_inline.h
@@ -0,0 +1,159 @@
+// Locale support -*- C++ -*-
+
+// Copyright (C) 2000-2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file bits/ctype_inline.h
+ *  This is an internal header file, included by other library headers.
+ *  Do not attempt to use it directly. @headername{locale}
+ */
+
+//
+// ISO C++ 14882: 22.1  Locales
+//
+
+// ctype bits to be inlined go here. Non-inlinable (ie virtual do_*)
+// functions go in ctype.cc
+
+namespace std _GLIBCXX_VISIBILITY(default)
+{
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+  bool
+  ctype<char>::
+  is(mask __m, char __c) const
+  {
+    if (_M_table)
+      return _M_table[static_cast<unsigned char>(__c)] & __m;
+    else
+      return __istype(__c, __m);
+  }
+
+  const char*
+  ctype<char>::
+  is(const char* __low, const char* __high, mask* __vec) const
+  {
+    if (_M_table)
+      while (__low < __high)
+        *__vec++ = _M_table[static_cast<unsigned char>(*__low++)];
+    else
+      for (;__low < __high; ++__vec, ++__low)
+        {
+          mask __m = 0;
+          if (this->is(upper, *__low)) __m |= upper;
+          if (this->is(lower, *__low)) __m |= lower;
+          if (this->is(alpha, *__low)) __m |= alpha;
+          if (this->is(digit, *__low)) __m |= digit;
+          if (this->is(xdigit, *__low)) __m |= xdigit;
+          if (this->is(space, *__low)) __m |= space;
+          if (this->is(print, *__low)) __m |= print;
+          if (this->is(graph, *__low)) __m |= graph;
+          if (this->is(cntrl, *__low)) __m |= cntrl;
+          if (this->is(punct, *__low)) __m |= punct;
+          // Do not include explicit line for alnum mask since it is a
+          // pure composite of masks on OS/2 kLIBC.
+          *__vec = __m;
+        }
+    return __high;
+  }
+
+  const char*
+  ctype<char>::
+  scan_is(mask __m, const char* __low, const char* __high) const
+  {
+    if (_M_table)
+      while (__low < __high
+             && !(_M_table[static_cast<unsigned char>(*__low)] & __m))
+        ++__low;
+    else
+      while (__low < __high && !this->is(__m, *__low))
+        ++__low;
+    return __low;
+  }
+
+  const char*
+  ctype<char>::
+  scan_not(mask __m, const char* __low, const char* __high) const
+  {
+    if (_M_table)
+      while (__low < __high
+             && (_M_table[static_cast<unsigned char>(*__low)] & __m) != 0)
+        ++__low;
+    else
+      while (__low < __high && this->is(__m, *__low) != 0)
+        ++__low;
+    return __low;
+  }
+
+#ifdef _GLIBCXX_USE_WCHAR_T
+  inline bool
+  ctype<wchar_t>::
+  do_is(mask __m, wchar_t __c) const
+  {
+    return __istype (__c, __m);
+  }
+
+  inline const wchar_t*
+  ctype<wchar_t>::
+  do_is(const wchar_t* __lo, const wchar_t* __hi, mask* __vec) const
+  {
+    for (;__lo < __hi; ++__vec, ++__lo)
+      {
+        mask __m = 0;
+        if (this->do_is(upper, *__lo)) __m |= upper;
+        if (this->do_is(lower, *__lo)) __m |= lower;
+        if (this->do_is(alpha, *__lo)) __m |= alpha;
+        if (this->do_is(digit, *__lo)) __m |= digit;
+        if (this->do_is(xdigit, *__lo)) __m |= xdigit;
+        if (this->do_is(space, *__lo)) __m |= space;
+        if (this->do_is(print, *__lo)) __m |= print;
+        if (this->do_is(graph, *__lo)) __m |= graph;
+        if (this->do_is(cntrl, *__lo)) __m |= cntrl;
+        if (this->do_is(punct, *__lo)) __m |= punct;
+        // Do not include explicit line for alnum mask since it is a
+        // pure composite of masks on OS/2 kLIBC.
+        *__vec = __m;
+      }
+    return __hi;
+  }
+
+  inline const wchar_t*
+  ctype<wchar_t>::
+  do_scan_is(mask __m, const wchar_t* __lo, const wchar_t* __hi) const
+  {
+    while (__lo < __hi && ! __istype (*__lo, __m))
+      ++__lo;
+    return __lo;
+  }
+
+  inline const wchar_t*
+  ctype<wchar_t>::
+  do_scan_not(mask __m, const char_type* __lo, const char_type* __hi) const
+  {
+    while (__lo < __hi && __istype (*__lo, __m))
+      ++__lo;
+    return __lo;
+  }
+#endif
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace
diff --git a/libstdc++-v3/config/os/os2/os_defines.h b/libstdc++-v3/config/os/os2/os_defines.h
new file mode 100644
index 0000000..50ea322
--- /dev/null
+++ b/libstdc++-v3/config/os/os2/os_defines.h
@@ -0,0 +1,36 @@
+// Specific definitions for OS/2  -*- C++ -*-
+
+// Copyright (C) 2000-2014 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file bits/os_defines.h
+ *  This is an internal header file, included by other library headers.
+ *  Do not attempt to use it directly. @headername{iosfwd}
+ */
+
+#ifndef _GLIBCXX_OS_DEFINES
+#define _GLIBCXX_OS_DEFINES 1
+
+// System-specific #define, typedefs, corrections, etc, go here.  This
+// file will come before all others.
+
+#endif
diff --git a/libstdc++-v3/configure.host b/libstdc++-v3/configure.host
index c168454..b80df37 100644
--- a/libstdc++-v3/configure.host
+++ b/libstdc++-v3/configure.host
@@ -294,6 +294,9 @@ case "${host_os}" in
   openbsd*)
     os_include_dir="os/bsd/openbsd"
     ;;
+  os2*)
+    os_include_dir="os/os2"
+    ;;
   qnx6.[12]*)
     os_include_dir="os/qnx/qnx6.1"
     c_model=c
-- 
2.7.0
komh commented 8 years ago

Pushed as commit abbd4b1a7087fd066353eff2646d1d5a0c30c5a4.

dmik commented 8 years ago

@komh Thanks, what is this patch based on? Some other platform or purely from scratch?

komh commented 8 years ago

Why ?

dmik commented 8 years ago

Just to know its origin, e.g. to keep it in sync in the future.

komh commented 8 years ago

Primarily FreeBSD.

dmik commented 8 years ago

Okay, makes sense. Thanks.