This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Sample conformant, clean <c...> headers


Attached are a couple of sample heders and implementation files for my
suggestion regarding the namespace-clean, conformant <c...> headers.

They aren't perfect (<clocale> does not define NULL), but they're just examples.

I used <cctype>, a very simple example, and the more complex <clocale> which
has a libC POD and some defined macros.  I added some compile-time switches
so if the configury bothers to check, a more efficient implementation could be
chosen.

I didn't bother with the usual headers to save (my) bandwidth, but I'm sure you
can use your imaginations.  The tests ran okay (before last minute changes so
your mileage may vary) on a i686-pc-linux-gnu platform with a fairly recent CVS
snapshot.

With this implementation of the <c...> headers, the whole c_std/c_shadow thing
becomes completely unnecessary.

I can whip up some more headers next week so I can try a full bootstrap and
run with the testsuites.

-- 

Stephen M. Webb
stephen at bregmasoft dot com

#include <ctype.h>

namespace 
{
    inline int clib_isalnum(int __c) { return isalnum(__c); }
    inline int clib_isalpha(int __c) { return isalpha(__c); }
    inline int clib_isblank(int __c) { return isblank(__c); }
    inline int clib_iscntrl(int __c) { return iscntrl(__c); }
    inline int clib_isdigit(int __c) { return isdigit(__c); }
    inline int clib_isgraph(int __c) { return isgraph(__c); }
    inline int clib_islower(int __c) { return islower(__c); }
    inline int clib_isprint(int __c) { return isprint(__c); }
    inline int clib_ispunct(int __c) { return ispunct(__c); }
    inline int clib_isspace(int __c) { return isspace(__c); }
    inline int clib_isupper(int __c) { return isupper(__c); }
    inline int clib_isxdigit(int __c) { return isxdigit(__c); }
    inline int clib_tolower(int __c) { return tolower(__c); }
    inline int clib_toupper(int __c) { return toupper(__c); }
}


namespace std
{
    int isalnum(int __c) { return clib_isalnum(__c); }
    int isalpha(int __c) { return clib_isalpha(__c); }
    int isblank(int __c) { return clib_isblank(__c); }
    int iscntrl(int __c) { return clib_iscntrl(__c); }
    int isdigit(int __c) { return clib_isdigit(__c); }
    int isgraph(int __c) { return clib_isgraph(__c); }
    int islower(int __c) { return clib_islower(__c); }
    int isprint(int __c) { return clib_isprint(__c); }
    int ispunct(int __c) { return clib_ispunct(__c); }
    int isspace(int __c) { return clib_isspace(__c); }
    int isupper(int __c) { return clib_isupper(__c); }
    int isxdigit(int __c) { return clib_isxdigit(__c); }
    int tolower(int __c) { return clib_tolower(__c); }
    int toupper(int __c) { return clib_toupper(__c); }
}
#include <locale.h>

/*
 * Capture functions for forwarding the libC functions.
 */
namespace
{
    inline char*
    clib_setlocale(int __cat, const char* __loc)
    { return setlocale(__cat, __loc); }

    inline lconv*
    clib_localeconv(void)
    { return localeconv(); }

}

/*
 * Undefine these in case they were redefined in locale.h/
 */
#undef setlocale
#undef localeconv

namespace std
{
    /**
     * Set the current l10n locale.
     */
    char*
    setlocale(int __cat, const char* __loc)
	{
      static int clib_lcvalues[] = {
          LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES, LC_ALL
      };
      return clib_setlocale(clib_lcvalues[__cat], __loc);
	}
}

// Include this here for C++ lconv POD definition.  Don't include this file
// at the top because it may redefine LC_*.
#include <clocale>

namespace std
{
    /** 
     * Get local numeric formatting information. 
     *
     * Note that the _c-to_l copy could be replaced with a simple
     * pointer copy (with reinterpret_cast) if it is known that
     * the C library lconv structure matches the C++ library lconv
     * structure exactly.  Later configury changes can provide that;
     * for now everyone must copy explicitly.
     */
    lconv*
    localeconv(void)
    {
#ifdef _GLIBCPP_LCONV_MATCHES
        return reinterpret_cast<lconv*>(clib_localeconv());
#else
        ::lconv* _c = clib_localeconv();
        static lconv _l;

        _l.decimal_point = _c->decimal_point;
        _l.thousands_sep = _c->thousands_sep;
        _l.grouping = _c->grouping;
        _l.int_curr_symbol = _c->int_curr_symbol;
        _l.currency_symbol = _c->currency_symbol;
        _l.mon_decimal_point = _c->mon_decimal_point;
        _l.mon_thousands_sep = _c->mon_thousands_sep;
        _l.mon_grouping = _c->mon_grouping;
        _l.positive_sign = _c->positive_sign;
        _l.negative_sign = _c->negative_sign;
        _l.int_frac_digits = _c->int_frac_digits;
        _l.frac_digits = _c->frac_digits;
        _l.p_cs_precedes = _c->p_cs_precedes;
        _l.p_sep_by_space = _c->p_sep_by_space;
        _l.n_cs_precedes = _c->n_cs_precedes;
        _l.n_sep_by_space = _c->n_sep_by_space;
        _l.p_sign_posn = _c->p_sign_posn;
        _l.n_sign_posn = _c->n_sign_posn;

        return &_l;
#endif
    }
}

// -*- C++ -*- forwarding header.

// Copyright (C) 1997, 1998, 1999, 2000, 2001 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 2, 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.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

//
// ISO/IEC C++ 14882:1998 21.4 Null-terminated sequence utilities [lib.c.strings]
//

#ifndef _CPP_CCTYPE
#define _CPP_CCTYPE 1

#include <bits/c++config.h>

#pragma GCC system_header

// Get rid of those macros defined in <ctype.h> in lieu of real functions.
#undef isalnum
#undef isalpha
#undef isblank
#undef iscntrl
#undef isdigit
#undef isgraph
#undef islower
#undef isprint
#undef ispunct
#undef isspace
#undef isupper
#undef isxdigit
#undef tolower
#undef toupper

namespace std
{
  extern int isalnum(int);
  extern int isalpha(int);
  extern int isblank(int);
  extern int iscntrl(int);
  extern int isdigit(int);
  extern int isgraph(int);
  extern int islower(int);
  extern int isprint(int);
  extern int ispunct(int);
  extern int isspace(int);
  extern int isupper(int);
  extern int isxdigit(int);
  extern int tolower(int);
  extern int toupper(int);
}

#endif 












// -*- C++ -*- forwarding header.

// Copyright (C) 1997, 1998, 1999, 2000, 2001 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 2, 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.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

// As a special exception, you may use this file as part of a free software
// library without restriction.  Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License.  This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.

//
// ISO/IEC C++ 14882:1998 22.3 C Library Locales [lib.c.locales]
//

// Note: This is not a conforming implementation.
// In order to confirm, it needs to define NULL.

#ifndef _CPP_CLOCALE
#define _CPP_CLOCALE 1

#include <bits/c++config.h>

#pragma GCC system_header

// Get rid of those macros defined in <locale.h> in lieu of real functions.
#undef setlocale
#undef localeconv

// Get rid of any C library definitions so they can superceded by the C++ library.
#undef LC_CTYPE 
#undef LC_NUMERIC
#undef LC_TIME  
#undef LC_COLLATE
#undef LC_MONETARY
#undef LC_MESSAGES
#undef LC_ALL

struct lconv;

namespace std
{
  /**
   * This structure contains numeric and monetary formatting localization
   * parameters and must confirm to the definition in ISO C clause 7.4.
   */
  struct lconv
  {
    char* decimal_point;
    char* thousands_sep;
    char* grouping;
    char* int_curr_symbol;
    char* currency_symbol;
    char* mon_decimal_point;
    char* mon_thousands_sep;
    char* mon_grouping;
    char* positive_sign;
    char* negative_sign;
    char  int_frac_digits;
    char  frac_digits;
    char  p_cs_precedes;
    char  p_sep_by_space;
    char  n_cs_precedes;
    char  n_sep_by_space;
    char  p_sign_posn;
    char  n_sign_posn;
  };

  extern char* setlocale(int, const char*); 
  extern lconv* localeconv(void);
}

// 17.4.4.2 [lib.res.on.macro.definitions] says these must resolve
// to integral values.
#define LC_CTYPE    0
#define LC_NUMERIC  1
#define LC_TIME     2
#define LC_COLLATE  3
#define LC_MONETARY 4
#define LC_MESSAGES 5
#define	LC_ALL		6

#endif



#include <cctype>
#include <ctype.h>

int
main(int argc, char* argv[])
{
    using std::isalpha;

    int k = 1;
    if (isalpha('k'))
    {
        k = 0;
    }
    return k;
}
#include <clocale>

int
main(int argc, char* argv[])
{
    char* oldLocale = std::setlocale(LC_ALL, "C");
    return LC_ALL;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]