Refactor out some stuff to CMake tests

This commit is contained in:
Tsuda kageyu 2013-04-21 22:24:12 +09:00
parent 529d78f54b
commit 49b07a2662
26 changed files with 478 additions and 486 deletions

View File

@ -21,7 +21,6 @@ option(BUILD_EXAMPLES "Build the examples" OFF)
option(NO_ITUNES_HACKS "Disable workarounds for iTunes bugs" OFF)
add_definitions(-DHAVE_CONFIG_H)
set(TESTS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/tests/")
## the following are directories where stuff will be installed to

View File

@ -5,8 +5,170 @@ include(CheckFunctionExists)
include(CheckLibraryExists)
include(CheckTypeSize)
include(CheckCXXSourceCompiles)
include(TestBigEndian)
# Determine the CPU byte order.
test_big_endian(TAGLIB_BIG_ENDIAN)
if(NOT ${TAGLIB_BIG_ENDIAN})
set(TAGLIB_LITTLE_ENDIAN 1)
endif()
# Determine the size of integral types.
check_type_size("short" SIZEOF_SHORT)
check_type_size("int" SIZEOF_INT)
check_type_size("long long" SIZEOF_LONGLONG)
check_type_size("wchar_t" SIZEOF_WCHAR_T)
# Determine if your compiler supports std::wstring.
check_cxx_source_compiles("
#include <string>
int main() {
std::wstring x(L\"ABC\");
return 0;
}
" HAVE_STD_WSTRING)
# Determine which kind of atomic operations your compiler supports.
check_cxx_source_compiles("
#include <atomic>
int main() {
std::atomic<unsigned int> x;
x.fetch_add(1);
x.fetch_sub(1);
return 0;
}
" HAVE_STD_ATOMIC)
check_cxx_source_compiles("
#include <boost/atomic.hpp>
int main() {
boost::atomic<unsigned int> x(1);
x.fetch_add(1);
x.fetch_sub(1);
return 0;
}
" HAVE_BOOST_ATOMIC)
check_cxx_source_compiles("
int main() {
volatile int x;
__sync_add_and_fetch(&x, 1);
int y = __sync_sub_and_fetch(&x, 1);
return 0;
}
" HAVE_GCC_ATOMIC)
check_cxx_source_compiles("
#include <libkern/OSAtomic.h>
int main() {
volatile int32_t x;
OSAtomicIncrement32Barrier(&x);
int32_t y = OSAtomicDecrement32Barrier(&x);
return 0;
}
" HAVE_MAC_ATOMIC)
check_cxx_source_compiles("
#include <windows.h>
int main() {
volatile LONG x;
InterlockedIncrement(&x);
LONG y = InterlockedDecrement(&x);
return 0;
}
" HAVE_WIN_ATOMIC)
check_cxx_source_compiles("
#include <ia64intrin.h>
int main() {
volatile int x;
__sync_add_and_fetch(&x, 1);
int y = __sync_sub_and_fetch(&x, 1);
return 0;
}
" HAVE_IA64_ATOMIC)
# Determine which kind of byte swap functions your compiler supports.
# Some of them can be missing depends on the GCC version.
check_cxx_source_compiles("
int main() {
__builtin_bswap16(0);
return 0;
}
" HAVE_GCC_BYTESWAP_16)
check_cxx_source_compiles("
int main() {
__builtin_bswap32(0);
return 0;
}
" HAVE_GCC_BYTESWAP_32)
check_cxx_source_compiles("
int main() {
__builtin_bswap64(0);
return 0;
}
" HAVE_GCC_BYTESWAP_64)
check_cxx_source_compiles("
#include <stdlib.h>
int main() {
_byteswap_ushort(0);
_byteswap_ulong(0);
_byteswap_uint64(0);
return 0;
}
" HAVE_MSC_BYTESWAP)
check_cxx_source_compiles("
#include <byteswap.h>
int main() {
__bswap_16(0);
__bswap_32(0);
__bswap_64(0);
return 0;
}
" HAVE_GLIBC_BYTESWAP)
check_cxx_source_compiles("
#include <libkern/OSByteOrder.h>
int main() {
OSSwapInt16(0);
OSSwapInt32(0);
OSSwapInt64(0);
return 0;
}
" HAVE_MAC_BYTESWAP)
check_cxx_source_compiles("
#include <sys/endian.h>
int main() {
swap16(0);
swap32(0);
swap64(0);
return 0;
}
" HAVE_OPENBSD_BYTESWAP)
# Determine whether your compiler supports codecvt.
check_cxx_source_compiles("
#include <codecvt>
int main() {
std::codecvt_utf8_utf16<wchar_t> x;
return 0;
}
" HAVE_STD_CODECVT)
# Check for libz using the cmake supplied FindZLIB.cmake
# check for libz using the cmake supplied FindZLIB.cmake
find_package(ZLIB)
if(ZLIB_FOUND)
set(HAVE_ZLIB 1)
@ -14,12 +176,6 @@ else()
set(HAVE_ZLIB 0)
endif()
# Determine whether your compiler supports codecvt header.
check_cxx_source_compiles("
#include <codecvt>
int main() { std::codecvt_utf8_utf16<wchar_t> x; return 0; }
" HAVE_CODECVT)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
find_package(CppUnit)

View File

@ -1,10 +1,40 @@
/* config-taglib.h. Generated by cmake from config-taglib.h.cmake */
/* Define if you have libz */
#cmakedefine HAVE_ZLIB 1
/* Indicates the endianness of your target system */
#cmakedefine TAGLIB_LITTLE_ENDIAN 1
#cmakedefine TAGLIB_BIG_ENDIAN 1
/* Defined if your compiler has <codecvt> header */
#cmakedefine HAVE_CODECVT 1
/* Size of integral types */
#cmakedefine SIZEOF_SHORT ${SIZEOF_SHORT}
#cmakedefine SIZEOF_INT ${SIZEOF_INT}
#cmakedefine SIZEOF_LONGLONG ${SIZEOF_LONGLONG}
#cmakedefine SIZEOF_WCHAR_T ${SIZEOF_WCHAR_T}
/* Defined if your compiler supports std::wstring */
#cmakedefine HAVE_STD_WSTRING 1
/* Defined if your compiler supports some atomic operations */
#cmakedefine HAVE_STD_ATOMIC 1
#cmakedefine HAVE_BOOST_ATOMIC 1
#cmakedefine HAVE_GCC_ATOMIC 1
#cmakedefine HAVE_MAC_ATOMIC 1
#cmakedefine HAVE_WIN_ATOMIC 1
#cmakedefine HAVE_IA64_ATOMIC 1
/* Defined if your compiler supports some byte swap functions */
#cmakedefine HAVE_GCC_BYTESWAP_16 1
#cmakedefine HAVE_GCC_BYTESWAP_32 1
#cmakedefine HAVE_GCC_BYTESWAP_64 1
#cmakedefine HAVE_MSC_BYTESWAP 1
#cmakedefine HAVE_GLIBC_BYTESWAP 1
#cmakedefine HAVE_MAC_BYTESWAP 1
#cmakedefine HAVE_OPENBSD_BYTESWAP 1
/* Defined if your compiler supports codecvt */
#cmakedefine HAVE_STD_CODECVT 1
/* Defined if you have libz */
#cmakedefine HAVE_ZLIB 1
#cmakedefine NO_ITUNES_HACKS 1
#cmakedefine WITH_ASF 1

View File

@ -50,7 +50,6 @@ set(tag_HDRS
toolkit/tmap.h
toolkit/tmap.tcc
toolkit/tpropertymap.h
toolkit/tbyteswap.h
mpeg/mpegfile.h
mpeg/mpegproperties.h
mpeg/mpegheader.h
@ -290,7 +289,6 @@ set(toolkit_SRCS
toolkit/tfilestream.cpp
toolkit/tdebug.cpp
toolkit/tpropertymap.cpp
toolkit/tbyteswap.cpp
toolkit/unicode.cpp
)

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <taglib.h>
#include <tdebug.h>
#include "asfattribute.h"

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <tdebug.h>
#include <tbytevectorlist.h>
#include <tpropertymap.h>

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <taglib.h>
#include <tdebug.h>
#include "asfattribute.h"

View File

@ -23,9 +23,7 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "config.h"
#include <tdebug.h>
#include <tstring.h>

View File

@ -23,9 +23,7 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "config.h"
#include <tpropertymap.h>
#include "asftag.h"

View File

@ -27,9 +27,7 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <config.h>
#ifdef _WIN32
# include <Shlwapi.h>

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <taglib.h>
#include <tdebug.h>
#include "flacmetadatablock.h"

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <taglib.h>
#include <tdebug.h>
#include "flacpicture.h"

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <taglib.h>
#include <tdebug.h>
#include <tstring.h>

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <tdebug.h>
#include <tstring.h>
#include "mp4atom.h"

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <taglib.h>
#include <tdebug.h>
#include "mp4coverart.h"

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <tdebug.h>
#include <tstring.h>
#include <tpropertymap.h>

View File

@ -23,10 +23,6 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <taglib.h>
#include <tdebug.h>
#include "mp4item.h"

View File

@ -23,9 +23,7 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "config.h"
#include <tdebug.h>
#include <tstring.h>

View File

@ -23,9 +23,7 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "config.h"
#include <tdebug.h>
#include <tstring.h>

View File

@ -23,9 +23,7 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "config.h"
#if HAVE_ZLIB
#include <zlib.h>

View File

@ -23,9 +23,7 @@
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "config.h"
#include <tdebug.h>

View File

@ -26,6 +26,8 @@
#ifndef TAGLIB_H
#define TAGLIB_H
#include "config.h"
#define TAGLIB_MAJOR_VERSION 1
#define TAGLIB_MINOR_VERSION 8
#define TAGLIB_PATCH_VERSION 0
@ -45,47 +47,110 @@
#include <string>
#include <climits>
#ifdef __APPLE__
# include <libkern/OSAtomic.h>
# define TAGLIB_ATOMIC_MAC
#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
# if !defined(NOMINMAX)
# define NOMINMAX
# endif
# include <windows.h>
# define TAGLIB_ATOMIC_WIN
#elif defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 401) \
&& (defined(__i386__) || defined(__i486__) || defined(__i586__) || \
defined(__i686__) || defined(__x86_64) || defined(__ia64)) \
&& !defined(__INTEL_COMPILER)
# define TAGLIB_ATOMIC_GCC
#elif defined(__ia64) && defined(__INTEL_COMPILER)
# include <ia64intrin.h>
# define TAGLIB_ATOMIC_GCC
#endif
// Check the widths of integral types.
#if UCHAR_MAX != 255U
# error TagLib assumes that char is 8-bit wide.
#if SIZEOF_SHORT != 2
# error TagLib requires that short is 16-bit wide.
#endif
#if USHRT_MAX != 65535U
# error TagLib assumes that short is 16-bit wide.
#if SIZEOF_INT != 4
# error TagLib requires that int is 32-bit wide.
#endif
#if UINT_MAX != 4294967295U
# error TagLib assumes that int is 32-bit wide.
#if SIZEOF_LONGLONG != 8
# error TagLib requires that long long is 64-bit wide.
#endif
#if !defined(ULLONG_MAX) && !defined(ULONGLONG_MAX) && !defined(ULONG_LONG_MAX)
# error TagLib assumes that long long is 64-bit wide.
#elif defined(ULLONG_MAX) && ULLONG_MAX != 18446744073709551615ULL
# error TagLib assumes that long long is 64-bit wide.
#elif defined(ULONGLONG_MAX) && ULONGLONG_MAX != 18446744073709551615ULL
# error TagLib assumes that long long is 64-bit wide.
#elif defined(ULONG_LONG_MAX) && ULONG_LONG_MAX != 18446744073709551615ULL
# error TagLib assumes that long long is 64-bit wide.
#if SIZEOF_WCHAR_T < 2
# error TagLib requires that wchar_t is sufficient to store a UTF-16 char.
#endif
// Atomic increment/decrement operations
#if defined(HAVE_STD_ATOMIC)
# include <atomic>
#elif defined(HAVE_BOOST_ATOMIC)
# include <boost/atomic.hpp>
#elif defined(HAVE_MAC_ATOMIC)
# include <libkern/OSAtomic.h>
#elif defined(HAVE_WIN_ATOMIC)
# include <windows.h>
#elif defined(HAVE_IA64_ATOMIC)
# include <ia64intrin.h>
#endif
#if defined(HAVE_STD_ATOMIC)
# define TAGLIB_ATOMIC_INT std::atomic<unsigned int>
# define TAGLIB_ATOMIC_INC(x) x.fetch_add(1)
# define TAGLIB_ATOMIC_DEC(x) (x.fetch_sub(1) - 1)
#elif defined(HAVE_BOOST_ATOMIC)
# define TAGLIB_ATOMIC_INT boost::atomic<unsigned int>
# define TAGLIB_ATOMIC_INC(x) x.fetch_add(1)
# define TAGLIB_ATOMIC_DEC(x) (x.fetch_sub(1) - 1)
#elif defined(HAVE_GCC_ATOMIC)
# define TAGLIB_ATOMIC_INT volatile int
# define TAGLIB_ATOMIC_INC(x) __sync_add_and_fetch(&x, 1)
# define TAGLIB_ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1)
#elif defined(HAVE_MAC_ATOMIC)
# define TAGLIB_ATOMIC_INT volatile int32_t
# define TAGLIB_ATOMIC_INC(x) OSAtomicIncrement32Barrier(&x)
# define TAGLIB_ATOMIC_DEC(x) OSAtomicDecrement32Barrier(&x)
#elif defined(HAVE_IA64_ATOMIC)
# define TAGLIB_ATOMIC_INT volatile int
# define TAGLIB_ATOMIC_INC(x) __sync_add_and_fetch(&x, 1)
# define TAGLIB_ATOMIC_DEC(x) __sync_sub_and_fetch(&x, 1)
#else
# define TAGLIB_ATOMIC_INT volatile int
# define TAGLIB_ATOMIC_INC(x) (++x)
# define TAGLIB_ATOMIC_DEC(x) (--x)
#endif
// Optimized byte swap functions.
#if defined(HAVE_MSC_BYTESWAP)
# include <stdlib.h>
#elif defined(HAVE_GLIBC_BYTESWAP)
# include <byteswap.h>
#elif defined(HAVE_MAC_BYTESWAP)
# include <libkern/OSByteOrder.h>
#elif defined(HAVE_OPENBSD_BYTESWAP)
# include <sys/endian.h>
#endif
#if defined(HAVE_GCC_BYTESWAP16)
# define TAGLIB_BYTESWAP_16(x) __builtin_bswap16(x)
#elif defined(HAVE_MSC_BYTESWAP)
# define TAGLIB_BYTESWAP_16(x) _byteswap_ushort(x)
#elif defined(HAVE_GLIBC_BYTESWAP)
# define TAGLIB_BYTESWAP_16(x) __bswap_16(x)
#elif defined(HAVE_MAC_BYTESWAP)
# define TAGLIB_BYTESWAP_16(x) OSSwapInt16(x)
#elif defined(HAVE_OPENBSD_BYTESWAP)
# define TAGLIB_BYTESWAP_16(x) swap16(x)
#endif
#if defined(HAVE_GCC_BYTESWAP32)
# define TAGLIB_BYTESWAP_32(x) __builtin_bswap32(x)
#elif defined(HAVE_MSC_BYTESWAP)
# define TAGLIB_BYTESWAP_32(x) _byteswap_ulong(x)
#elif defined(HAVE_GLIBC_BYTESWAP)
# define TAGLIB_BYTESWAP_32(x) __bswap_32(x)
#elif defined(HAVE_MAC_BYTESWAP)
# define TAGLIB_BYTESWAP_32(x) OSSwapInt32(x)
#elif defined(HAVE_OPENBSD_BYTESWAP)
# define TAGLIB_BYTESWAP_32(x) swap32(x)
#endif
#if defined(HAVE_GCC_BYTESWAP64)
# define TAGLIB_BYTESWAP_64(x) __builtin_bswap64(x)
#elif defined(HAVE_MSC_BYTESWAP)
# define TAGLIB_BYTESWAP_64(x) _byteswap_uint64(x)
#elif defined(HAVE_GLIBC_BYTESWAP)
# define TAGLIB_BYTESWAP_64(x) __bswap_64(x)
#elif defined(HAVE_MAC_BYTESWAP)
# define TAGLIB_BYTESWAP_64(x) OSSwapInt64(x)
#elif defined(HAVE_OPENBSD_BYTESWAP)
# define TAGLIB_BYTESWAP_64(x) swap64(x)
#endif
//! A namespace for all TagLib related classes and functions
@ -115,7 +180,11 @@ namespace TagLib {
* Unfortunately std::wstring isn't defined on some systems, (i.e. GCC < 3)
* so I'm providing something here that should be constant.
*/
#ifdef HAVE_STD_WSTRING
typedef std::wstring wstring;
#else
typedef std::basic_string<wchar> wstring;
#endif
#ifndef DO_NOT_DOCUMENT // Tell Doxygen to skip this class.
/*!
@ -130,32 +199,12 @@ namespace TagLib {
public:
RefCounter() : refCount(1) {}
#ifdef TAGLIB_ATOMIC_MAC
void ref() { OSAtomicIncrement32Barrier(const_cast<int32_t*>(&refCount)); }
bool deref() { return ! OSAtomicDecrement32Barrier(const_cast<int32_t*>(&refCount)); }
int32_t count() { return refCount; }
private:
volatile int32_t refCount;
#elif defined(TAGLIB_ATOMIC_WIN)
void ref() { InterlockedIncrement(&refCount); }
bool deref() { return ! InterlockedDecrement(&refCount); }
long count() { return refCount; }
private:
volatile long refCount;
#elif defined(TAGLIB_ATOMIC_GCC)
void ref() { __sync_add_and_fetch(&refCount, 1); }
bool deref() { return ! __sync_sub_and_fetch(&refCount, 1); }
void ref() { TAGLIB_ATOMIC_INC(refCount); }
bool deref() { return (TAGLIB_ATOMIC_DEC(refCount) == 0); }
int count() { return refCount; }
private:
volatile int refCount;
#else
void ref() { refCount++; }
bool deref() { return ! --refCount; }
int count() { return refCount; }
private:
uint refCount;
#endif
private:
TAGLIB_ATOMIC_INT refCount;
};
#endif // DO_NOT_DOCUMENT

View File

@ -1,197 +0,0 @@
/***************************************************************************
copyright : (C) 2013 by Tsuda Kageyu
email : tsuda.kageyu@gmail.com
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#include "taglib.h"
#include "tbyteswap.h"
// Determines if compiler intrinsic functions are available.
// MSVC: Intrinsic _byteswap_* functions.
#if defined(_MSC_VER) && _MSC_VER >= 1400
# include <stdlib.h>
# define TAGLIB_BYTESWAP_MSC
// GCC 4.8 or above: __builtin_bswap16(), 32 and 64.
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8))
# define TAGLIB_BYTESWAP_GCC 2
// GCC 4.3 or above: __builtin_bswap16 is missing.
#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3))
# define TAGLIB_BYTESWAP_GCC 1
#endif
// Determines if platform or library specific functions are available.
#if defined(__APPLE__)
# include <libkern/OSByteOrder.h>
# define TAGLIB_BYTESWAP_MAC
#elif defined(__OpenBSD__)
# include <sys/endian.h>
# define TAGLIB_BYTESWAP_OPENBSD
#elif defined(__GLIBC__)
# include <byteswap.h>
# define TAGLIB_BYTESWAP_GLIBC
#endif
// Determines CPU byte order at compile time rather than run time if possible.
#if (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))) \
|| (defined(__GNUC__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)) \
|| (defined(__clang__) && defined(__LITTLE_ENDIAN__))
# define TAGLIB_LITTLE_ENDIAN
#elif (defined(__GNUC__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)) \
|| (defined(__clang__) && defined(__BIG_ENDIAN__))
# define TAGLIB_BIG_ENDIAN
#else
namespace {
bool isLittleEndian()
{
TagLib::ushort x = 1;
return (*reinterpret_cast<TagLib::uchar*>(&x) == 1);
}
}
#endif
namespace TagLib
{
ushort byteSwap16(ushort x)
{
#if defined(TAGLIB_BYTESWAP_MSC)
return _byteswap_ushort(x);
#elif defined(TAGLIB_BYTESWAP_GCC) && TAGLIB_BYTESWAP_GCC == 2
return __builtin_bswap16(x);
#elif defined(TAGLIB_BYTESWAP_MAC)
return OSSwapInt16(x);
#elif defined(TAGLIB_BYTESWAP_OPENBSD)
return swap16(x);
#elif defined(TAGLIB_BYTESWAP_GLIBC)
return __bswap_16(x);
#else
return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
#endif
}
uint byteSwap32(uint x)
{
#if defined(TAGLIB_BYTESWAP_MSC)
return _byteswap_ulong(x);
#elif defined(TAGLIB_BYTESWAP_GCC)
return __builtin_bswap32(x);
#elif defined(TAGLIB_BYTESWAP_MAC)
return OSSwapInt32(x);
#elif defined(TAGLIB_BYTESWAP_OPENBSD)
return swap32(x);
#elif defined(TAGLIB_BYTESWAP_GLIBC)
return __bswap_32(x);
#else
return ((x & 0xff000000) >> 24)
| ((x & 0x00ff0000) >> 8)
| ((x & 0x0000ff00) << 8)
| ((x & 0x000000ff) << 24);
#endif
}
ulonglong byteSwap64(ulonglong x)
{
#if defined(TAGLIB_BYTESWAP_MSC)
return _byteswap_uint64(x);
#elif defined(TAGLIB_BYTESWAP_GCC)
return __builtin_bswap64(x);
#elif defined(TAGLIB_BYTESWAP_MAC)
return OSSwapInt64(x);
#elif defined(TAGLIB_BYTESWAP_OPENBSD)
return swap64(x);
#elif defined(TAGLIB_BYTESWAP_GLIBC)
return __bswap_64(x);
#else
return ((x & 0xff00000000000000ull) >> 56)
| ((x & 0x00ff000000000000ull) >> 40)
| ((x & 0x0000ff0000000000ull) >> 24)
| ((x & 0x000000ff00000000ull) >> 8)
| ((x & 0x00000000ff000000ull) << 8)
| ((x & 0x0000000000ff0000ull) << 24)
| ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56);
#endif
}
#if defined(TAGLIB_LITTLE_ENDIAN)
const bool isLittleEndianSystem = true;
#elif defined(TAGLIB_BIG_ENDIAN)
const bool isLittleEndianSystem = false;
#else
const bool isLittleEndianSystem = isLittleEndian();
#endif
}

View File

@ -1,55 +0,0 @@
/***************************************************************************
copyright : (C) 2013 by Tsuda Kageyu
email : tsuda.kageyu@gmail.com
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301 USA *
* *
* Alternatively, this file is available under the Mozilla Public *
* License Version 1.1. You may obtain a copy of the License at *
* http://www.mozilla.org/MPL/ *
***************************************************************************/
#ifndef TAGLIB_BYTESWAP_H
#define TAGLIB_BYTESWAP_H
namespace TagLib
{
// Cross-platform byte order conversion functions.
/*!
* Converts the byte order of \a x as a 16-bit unsigned integer.
*/
ushort byteSwap16(ushort x);
/*!
* Converts the byte order of \a x as a 32-bit unsigned integer.
*/
uint byteSwap32(uint x);
/*!
* Converts the byte order of \a x as a 64-bit unsigned integer.
*/
ulonglong byteSwap64(ulonglong x);
/*!
* Indicates the system byte order.
* \a true if little endian, \a false if big endian.
*/
extern const bool isLittleEndianSystem;
}
#endif

View File

@ -30,7 +30,6 @@
#include <tdebug.h>
#include "tbytevector.h"
#include "tbyteswap.h"
// This is a bit ugly to keep writing over and over again.
@ -186,19 +185,53 @@ T byteSwap(T x)
template <>
ushort byteSwap<ushort>(ushort x)
{
return byteSwap16(x);
#ifdef TAGLIB_BYTESWAP_16
return TAGLIB_BYTESWAP_16(x);
#else
return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
#endif
}
template <>
uint byteSwap<uint>(uint x)
{
return byteSwap32(x);
#ifdef TAGLIB_BYTESWAP_32
return TAGLIB_BYTESWAP_32(x);
#else
return ((x & 0xff000000u) >> 24)
| ((x & 0x00ff0000u) >> 8)
| ((x & 0x0000ff00u) << 8)
| ((x & 0x000000ffu) << 24);
#endif
}
template <>
ulonglong byteSwap<ulonglong>(ulonglong x)
{
return byteSwap64(x);
#ifdef TAGLIB_BYTESWAP_64
return TAGLIB_BYTESWAP_64(x);
#else
return (x & 0xff00000000000000ull) >> 56)
| (x & 0x00ff000000000000ull) >> 40)
| (x & 0x0000ff0000000000ull) >> 24)
| (x & 0x000000ff00000000ull) >> 8)
| (x & 0x00000000ff000000ull) << 8)
| (x & 0x0000000000ff0000ull) << 24)
| (x & 0x000000000000ff00ull) << 40)
| (x & 0x00000000000000ffull) << 56);
#endif
}
template <class T>
@ -213,7 +246,12 @@ T toNumber(const ByteVector &v, size_t offset, bool mostSignificantByteFirst)
T tmp;
::memcpy(&tmp, v.data() + offset, sizeof(T));
if(isLittleEndianSystem == mostSignificantByteFirst)
#ifdef TAGLIB_LITTLE_ENDIAN
const bool swap = mostSignificantByteFirst;
#else
const bool swap != mostSignificantByteFirst;
#endif
if(swap)
return byteSwap<T>(tmp);
else
return tmp;
@ -241,7 +279,12 @@ ByteVector fromNumber(T value, bool mostSignificantByteFirst)
{
const size_t size = sizeof(T);
if(isLittleEndianSystem == mostSignificantByteFirst)
#ifdef TAGLIB_LITTLE_ENDIAN
const bool swap = mostSignificantByteFirst;
#else
const bool swap != mostSignificantByteFirst;
#endif
if(swap)
value = byteSwap<T>(value);
return ByteVector(reinterpret_cast<const char *>(&value), size);

View File

@ -25,33 +25,128 @@
// This class assumes that std::basic_string<T> has a contiguous and null-terminated buffer.
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "tstring.h"
#include "tdebug.h"
#include "tstringlist.h"
#include "tbyteswap.h"
#include <iostream>
#include <string.h>
#ifdef HAVE_CODECVT
#ifdef HAVE_STD_CODECVT
# include <codecvt>
namespace {
typedef std::codecvt_utf8_utf16<wchar_t> utf8_utf16_t;
}
#else
# include "unicode.h"
#endif
namespace {
namespace
{
inline TagLib::ushort byteSwap(TagLib::ushort x)
{
#ifdef TAGLIB_BYTESWAP_16
return TAGLIB_BYTESWAP_16(x);
#else
return((x >> 8) & 0xff) | ((x & 0xff) << 8);
#endif
}
inline unsigned short combine(unsigned char c1, unsigned char c2)
{
return (c1 << 8) | c2;
}
void UTF16toUTF8(const wchar_t *src, size_t srcLength, char *dst, size_t dstLength)
{
#ifdef HAVE_STD_CODECVT
typedef std::codecvt_utf8_utf16<wchar_t> utf8_utf16_t;
using namespace TagLib;
const wchar_t *srcBegin = src;
const wchar_t *srcEnd = srcBegin + srcLength;
char *dstBegin = dst;
char *dstEnd = dstBegin + dstLength;
std::mbstate_t st = 0;
const wchar_t *source;
char *target;
std::codecvt_base::result result = utf8_utf16_t().out(
st, srcBegin, srcEnd, source, dstBegin, dstEnd, target);
if(result != utf8_utf16_t::ok) {
debug("String::copyFromUTF8() - Unicode conversion error.");
}
#else
using namespace Unicode;
using namespace TagLib;
const Unicode::UTF16 *srcBegin = src;
const Unicode::UTF16 *srcEnd = srcBegin + srcLength;
Unicode::UTF8 *dstBegin = reinterpret_cast<Unicode::UTF8*>(dst);
Unicode::UTF8 *dstEnd = dstBegin + dstLength;
Unicode::ConversionResult result = Unicode::ConvertUTF16toUTF8(
&srcBegin, srcEnd, &dstBegin, dstEnd, Unicode::lenientConversion);
if(result != Unicode::conversionOK) {
debug("String::to8Bit() - Unicode conversion error.");
}
#endif
}
void UTF8toUTF16(const char *src, size_t srcLength, wchar_t *dst, size_t dstLength)
{
#ifdef HAVE_STD_CODECVT
typedef std::codecvt_utf8_utf16<wchar_t> utf8_utf16_t;
using namespace TagLib;
const char *srcBegin = src;
const char *srcEnd = srcBegin + srcLength;
wchar_t *dstBegin = dst;
wchar_t *dstEnd = dstBegin + dstLength;
std::mbstate_t st = 0;
const char *source;
wchar_t *target;
std::codecvt_base::result result = utf8_utf16_t().in(
st, srcBegin, srcEnd, source, dstBegin, dstEnd, target);
if(result != utf8_utf16_t::ok) {
debug("String::copyFromUTF8() - Unicode conversion error.");
}
#else
using namespace Unicode;
using namespace TagLib;
const Unicode::UTF8 *srcBegin = reinterpret_cast<const Unicode::UTF8*>(src);
const Unicode::UTF8 *srcEnd = srcBegin + srcLength;
Unicode::UTF16 *dstBegin = dst;
Unicode::UTF16 *dstEnd = dstBegin + dstLength;
Unicode::ConversionResult result = Unicode::ConvertUTF8toUTF16(
&srcBegin, srcEnd, &dstBegin, dstEnd, Unicode::lenientConversion);
if(result != Unicode::conversionOK) {
debug("String::copyFromUTF8() - Unicode conversion error.");
}
#endif
}
}
namespace TagLib {
@ -202,34 +297,7 @@ std::string String::to8Bit(bool unicode) const
else {
s.resize(d->data.size() * 4 + 1);
#ifdef HAVE_CODECVT
std::mbstate_t st = 0;
const wchar_t *source;
char *target;
std::codecvt_base::result result = utf8_utf16_t().out(
st, &d->data[0], &d->data[d->data.size()], source, &s[0], &s[s.size()], target);
if(result != utf8_utf16_t::ok) {
debug("String::copyFromUTF8() - Unicode conversion error.");
}
#else
const Unicode::UTF16 *source = &d->data[0];
Unicode::UTF8 *target = reinterpret_cast<Unicode::UTF8*>(&s[0]);
Unicode::ConversionResult result = Unicode::ConvertUTF16toUTF8(
&source, source + d->data.size(),
&target, target + s.size(),
Unicode::lenientConversion);
if(result != Unicode::conversionOK) {
debug("String::to8Bit() - Unicode conversion error.");
}
#endif
UTF16toUTF8(&d->data[0], d->data.size(), &s[0], s.size());
s.resize(::strlen(s.c_str()));
}
@ -371,34 +439,7 @@ ByteVector String::data(Type t) const
{
ByteVector v(size() * 4 + 1, 0);
#ifdef HAVE_CODECVT
std::mbstate_t st = 0;
const wchar_t *source;
char *target;
std::codecvt_base::result result = utf8_utf16_t().out(
st, &d->data[0], &d->data[d->data.size()], source, v.data(), v.data() + v.size(), target);
if(result != utf8_utf16_t::ok) {
debug("String::data() - Unicode conversion error.");
}
#else
const Unicode::UTF16 *source = &d->data[0];
Unicode::UTF8 *target = reinterpret_cast<Unicode::UTF8*>(v.data());
Unicode::ConversionResult result = Unicode::ConvertUTF16toUTF8(
&source, source + d->data.size(),
&target, target + v.size(),
Unicode::lenientConversion);
if(result != Unicode::conversionOK) {
debug("String::data() - Unicode conversion error.");
}
#endif
UTF16toUTF8(&d->data[0], d->data.size(), v.data(), v.size());
v.resize(::strlen(v.data()));
return v;
@ -730,34 +771,7 @@ void String::copyFromUTF8(const char *s, size_t length)
{
d->data.resize(length);
#ifdef HAVE_CODECVT
std::mbstate_t st = 0;
const char *source;
wchar_t *target;
std::codecvt_base::result result = utf8_utf16_t().in(
st, s, s + length, source, &d->data[0], &d->data[d->data.size()], target);
if(result != utf8_utf16_t::ok) {
debug("String::copyFromUTF8() - Unicode conversion error.");
}
#else
const Unicode::UTF8 *source = reinterpret_cast<const Unicode::UTF8 *>(s);
Unicode::UTF16 *target = &d->data[0];
Unicode::ConversionResult result = Unicode::ConvertUTF8toUTF16(
&source, source + length,
&target, target + length,
Unicode::lenientConversion);
if(result != Unicode::conversionOK) {
debug("String::copyFromUTF8() - Unicode conversion error.");
}
#endif
UTF8toUTF16(s, length, &d->data[0], d->data.size());
d->data.resize(::wcslen(d->data.c_str()));
}
@ -785,7 +799,7 @@ void String::copyFromUTF16(const wchar_t *s, size_t length, Type t)
if(swap) {
for(size_t i = 0; i < length; ++i)
d->data[i] = byteSwap16(static_cast<ushort>(s[i]));
d->data[i] = byteSwap(static_cast<ushort>(s[i]));
}
}
@ -836,7 +850,16 @@ void String::copyFromUTF16(const char *s, size_t length, Type t)
#endif
}
const String::Type String::WCharByteOrder = isLittleEndianSystem ? String::UTF16LE : String::UTF16BE;
#ifdef TAGLIB_LITTLE_ENDIAN
const String::Type String::WCharByteOrder = String::UTF16LE;
#else
const String::Type String::WCharByteOrder = String::UTF16BE;
#endif
}
////////////////////////////////////////////////////////////////////////////////