Skip to content

Commit

Permalink
Source char_traits<unsigned char> from GCC C++ Library directly to cl…
Browse files Browse the repository at this point in the history
…arify license.

http://gcc.gnu.org/onlinedocs/libstdc++/
Previously borrowed code from id3lib was considered shady.
Placed in its own file for style reasons.
  • Loading branch information
gitfoxi committed Jan 28, 2012
1 parent 1050ffe commit 9631d91
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 93 deletions.
96 changes: 3 additions & 93 deletions src/blob.hpp
Expand Up @@ -23,99 +23,9 @@

#include <string>

// Borrowed from id3lib_strings.h:
// They seem to be doing something for MSC, but since I only have gcc, I'll just do that
// Assuming this is uneccessary on GCC 4
// #if (defined(__GNUC__) && (__GNUC__ >= 3) || (defined(_MSC_VER) && _MSC_VER > 1000))
#if (defined(__GNUC__) && (__GNUC__ >= 3) && (__GNUC__ <= 4))
namespace std
{
template<>
struct char_traits<unsigned char>
{
typedef unsigned char char_type;
// Unsigned as wint_t in unsigned.
typedef unsigned long int_type;
typedef streampos pos_type;
typedef streamoff off_type;
typedef mbstate_t state_type;

static void
assign(char_type& __c1, const char_type& __c2)
{ __c1 = __c2; }

static bool
eq(const char_type& __c1, const char_type& __c2)
{ return __c1 == __c2; }

static bool
lt(const char_type& __c1, const char_type& __c2)
{ return __c1 < __c2; }

static int
compare(const char_type* __s1, const char_type* __s2, size_t __n)
{
for (size_t __i = 0; __i < __n; ++__i)
if (!eq(__s1[__i], __s2[__i]))
return lt(__s1[__i], __s2[__i]) ? -1 : 1;
return 0;
}

static size_t
length(const char_type* __s)
{
const char_type* __p = __s;
while (__p)
++__p;
return (__p - __s);
}

static const char_type*
find(const char_type* __s, size_t __n, const char_type& __a)
{
for (const char_type* __p = __s; size_t(__p - __s) < __n; ++__p)
if (*__p == __a) return __p;
return 0;
}

static char_type*
move(char_type* __s1, const char_type* __s2, size_t __n)
{ return (char_type*) memmove(__s1, __s2, __n * sizeof(char_type)); }

static char_type*
copy(char_type* __s1, const char_type* __s2, size_t __n)
{ return (char_type*) memcpy(__s1, __s2, __n * sizeof(char_type)); }

static char_type*
assign(char_type* __s, size_t __n, char_type __a)
{
for (char_type* __p = __s; __p < __s + __n; ++__p)
assign(*__p, __a);
return __s;
}

static char_type
to_char_type(const int_type& __c)
{ return char_type(); }

static int_type
to_int_type(const char_type& __c) { return int_type(); }

static bool
eq_int_type(const int_type& __c1, const int_type& __c2)
{ return __c1 == __c2; }

static int_type
eof() { return static_cast<int_type>(-1); }

static int_type
not_eof(const int_type& __c)
{ return eq_int_type(__c, eof()) ? int_type(0) : __c; }
};

} // namespace std
#endif // GCC version 3

#if (defined(__GNUC__) && (__GNUC__ == 3))
#include "gcc_421_char_traits.hpp"
#endif

namespace zmq
{
Expand Down
119 changes: 119 additions & 0 deletions src/gcc_421_char_traits.hpp
@@ -0,0 +1,119 @@
// Character Traits for use by standard string and iostream -*- C++ -*-

// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
// 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// 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.

/** @file char_traits.h
* This is an internal header file, included by other library headers.
* You should not attempt to use it directly.
*/

//
// ISO C++ 14882: 21 Strings library
//

// This file has been snipped for 0mq to provide a definition for
// struct char_traits<unsigned char>
// Which is not implemented in GNU ISO C++ Library as of version 3.2.3
// And is required by blob.hpp

#ifndef _ZMQ_CHAR_TRAITS_H
#define _ZMQ_CHAR_TRAITS_H 1

namespace std
{
/// @brief 21.1.3.1 char_traits specializations
template<>
struct char_traits<unsigned char>
{
typedef unsigned char char_type;
typedef int int_type;
typedef streampos pos_type;
typedef streamoff off_type;
typedef mbstate_t state_type;

static void
assign(char_type& __c1, const char_type& __c2)
{ __c1 = __c2; }

static bool
eq(const char_type& __c1, const char_type& __c2)
{ return __c1 == __c2; }

static bool
lt(const char_type& __c1, const char_type& __c2)
{ return __c1 < __c2; }

static int
compare(const char_type* __s1, const char_type* __s2, size_t __n)
{ return memcmp(__s1, __s2, __n); }

static size_t
length(const char_type* __s)
{ return strlen((const char*)__s); }

static const char_type*
find(const char_type* __s, size_t __n, const char_type& __a)
{ return static_cast<const char_type*>(memchr(__s, __a, __n)); }

static char_type*
move(char_type* __s1, const char_type* __s2, size_t __n)
{ return static_cast<char_type*>(memmove(__s1, __s2, __n)); }

static char_type*
copy(char_type* __s1, const char_type* __s2, size_t __n)
{ return static_cast<char_type*>(memcpy(__s1, __s2, __n)); }

static char_type*
assign(char_type* __s, size_t __n, char_type __a)
{ return static_cast<char_type*>(memset(__s, __a, __n)); }

static char_type
to_char_type(const int_type& __c)
{ return static_cast<char_type>(__c); }

// To keep both the byte 0xff and the eof symbol 0xffffffff
// from ending up as 0xffffffff.
static int_type
to_int_type(const char_type& __c)
{ return static_cast<int_type>(static_cast<unsigned char>(__c)); }

static bool
eq_int_type(const int_type& __c1, const int_type& __c2)
{ return __c1 == __c2; }

static int_type
eof() { return static_cast<int_type>(EOF); }

static int_type
not_eof(const int_type& __c)
{ return (__c == eof()) ? 0 : __c; }
};
}

#endif

0 comments on commit 9631d91

Please sign in to comment.