From 49b07a26626a2d1741c7b04073fd2016963f3766 Mon Sep 17 00:00:00 2001 From: Tsuda kageyu Date: Sun, 21 Apr 2013 22:24:12 +0900 Subject: [PATCH] Refactor out some stuff to CMake tests --- CMakeLists.txt | 1 - ConfigureChecks.cmake | 170 +++++++++++++++++- config-taglib.h.cmake | 38 +++- taglib/CMakeLists.txt | 2 - taglib/asf/asfattribute.cpp | 4 - taglib/asf/asffile.cpp | 4 - taglib/asf/asfpicture.cpp | 4 - taglib/asf/asfproperties.cpp | 4 +- taglib/asf/asftag.cpp | 4 +- taglib/fileref.cpp | 4 +- taglib/flac/flacmetadatablock.cpp | 4 - taglib/flac/flacpicture.cpp | 4 - taglib/flac/flacunknownmetadatablock.cpp | 4 - taglib/mp4/mp4atom.cpp | 4 - taglib/mp4/mp4coverart.cpp | 4 - taglib/mp4/mp4file.cpp | 4 - taglib/mp4/mp4item.cpp | 4 - taglib/mp4/mp4properties.cpp | 4 +- taglib/mp4/mp4tag.cpp | 4 +- taglib/mpeg/id3v2/id3v2frame.cpp | 4 +- taglib/mpeg/id3v2/id3v2framefactory.cpp | 4 +- taglib/toolkit/taglib.h | 161 +++++++++++------ taglib/toolkit/tbyteswap.cpp | 197 --------------------- taglib/toolkit/tbyteswap.h | 55 ------ taglib/toolkit/tbytevector.cpp | 55 +++++- taglib/toolkit/tstring.cpp | 215 +++++++++++++---------- 26 files changed, 477 insertions(+), 485 deletions(-) delete mode 100644 taglib/toolkit/tbyteswap.cpp delete mode 100644 taglib/toolkit/tbyteswap.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f29e463..14f00956 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index 4b8f512e..11db70d8 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -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 + 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 + int main() { + std::atomic x; + x.fetch_add(1); + x.fetch_sub(1); + return 0; + } +" HAVE_STD_ATOMIC) + +check_cxx_source_compiles(" + #include + int main() { + boost::atomic 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 + int main() { + volatile int32_t x; + OSAtomicIncrement32Barrier(&x); + int32_t y = OSAtomicDecrement32Barrier(&x); + return 0; + } +" HAVE_MAC_ATOMIC) + +check_cxx_source_compiles(" + #include + int main() { + volatile LONG x; + InterlockedIncrement(&x); + LONG y = InterlockedDecrement(&x); + return 0; + } +" HAVE_WIN_ATOMIC) + +check_cxx_source_compiles(" + #include + 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 + int main() { + _byteswap_ushort(0); + _byteswap_ulong(0); + _byteswap_uint64(0); + return 0; + } +" HAVE_MSC_BYTESWAP) + +check_cxx_source_compiles(" + #include + int main() { + __bswap_16(0); + __bswap_32(0); + __bswap_64(0); + return 0; + } +" HAVE_GLIBC_BYTESWAP) + +check_cxx_source_compiles(" + #include + int main() { + OSSwapInt16(0); + OSSwapInt32(0); + OSSwapInt64(0); + return 0; + } +" HAVE_MAC_BYTESWAP) + +check_cxx_source_compiles(" + #include + int main() { + swap16(0); + swap32(0); + swap64(0); + return 0; + } +" HAVE_OPENBSD_BYTESWAP) + +# Determine whether your compiler supports codecvt. + +check_cxx_source_compiles(" + #include + int main() { + std::codecvt_utf8_utf16 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 - int main() { std::codecvt_utf8_utf16 x; return 0; } -" HAVE_CODECVT) set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) find_package(CppUnit) diff --git a/config-taglib.h.cmake b/config-taglib.h.cmake index 9fcc29a4..bb25bb86 100644 --- a/config-taglib.h.cmake +++ b/config-taglib.h.cmake @@ -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 + +/* 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 has header */ -#cmakedefine HAVE_CODECVT 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 diff --git a/taglib/CMakeLists.txt b/taglib/CMakeLists.txt index a940caf5..72712ca2 100644 --- a/taglib/CMakeLists.txt +++ b/taglib/CMakeLists.txt @@ -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 ) diff --git a/taglib/asf/asfattribute.cpp b/taglib/asf/asfattribute.cpp index 2cfada7b..937a8816 100644 --- a/taglib/asf/asfattribute.cpp +++ b/taglib/asf/asfattribute.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include "asfattribute.h" diff --git a/taglib/asf/asffile.cpp b/taglib/asf/asffile.cpp index 56e93a76..241998ca 100644 --- a/taglib/asf/asffile.cpp +++ b/taglib/asf/asffile.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include diff --git a/taglib/asf/asfpicture.cpp b/taglib/asf/asfpicture.cpp index 35e52a7d..3db695a4 100644 --- a/taglib/asf/asfpicture.cpp +++ b/taglib/asf/asfpicture.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include "asfattribute.h" diff --git a/taglib/asf/asfproperties.cpp b/taglib/asf/asfproperties.cpp index 835cbdf9..11d43b9d 100644 --- a/taglib/asf/asfproperties.cpp +++ b/taglib/asf/asfproperties.cpp @@ -23,9 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #include #include diff --git a/taglib/asf/asftag.cpp b/taglib/asf/asftag.cpp index 1cbd16eb..70881209 100644 --- a/taglib/asf/asftag.cpp +++ b/taglib/asf/asftag.cpp @@ -23,9 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #include #include "asftag.h" diff --git a/taglib/fileref.cpp b/taglib/fileref.cpp index 46e59ff1..6da560b7 100644 --- a/taglib/fileref.cpp +++ b/taglib/fileref.cpp @@ -27,9 +27,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -# include -#endif +#include #ifdef _WIN32 # include diff --git a/taglib/flac/flacmetadatablock.cpp b/taglib/flac/flacmetadatablock.cpp index 7d161c27..17ab05f3 100644 --- a/taglib/flac/flacmetadatablock.cpp +++ b/taglib/flac/flacmetadatablock.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include "flacmetadatablock.h" diff --git a/taglib/flac/flacpicture.cpp b/taglib/flac/flacpicture.cpp index 95eeb6ab..a2a9000b 100644 --- a/taglib/flac/flacpicture.cpp +++ b/taglib/flac/flacpicture.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include "flacpicture.h" diff --git a/taglib/flac/flacunknownmetadatablock.cpp b/taglib/flac/flacunknownmetadatablock.cpp index 1265affb..dcd5d651 100644 --- a/taglib/flac/flacunknownmetadatablock.cpp +++ b/taglib/flac/flacunknownmetadatablock.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include diff --git a/taglib/mp4/mp4atom.cpp b/taglib/mp4/mp4atom.cpp index fac593b1..7b87a479 100644 --- a/taglib/mp4/mp4atom.cpp +++ b/taglib/mp4/mp4atom.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include "mp4atom.h" diff --git a/taglib/mp4/mp4coverart.cpp b/taglib/mp4/mp4coverart.cpp index 928e3c4a..5ccc76d6 100644 --- a/taglib/mp4/mp4coverart.cpp +++ b/taglib/mp4/mp4coverart.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include "mp4coverart.h" diff --git a/taglib/mp4/mp4file.cpp b/taglib/mp4/mp4file.cpp index f41ee888..aab1a2be 100644 --- a/taglib/mp4/mp4file.cpp +++ b/taglib/mp4/mp4file.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include diff --git a/taglib/mp4/mp4item.cpp b/taglib/mp4/mp4item.cpp index af2cc65c..fdb96451 100644 --- a/taglib/mp4/mp4item.cpp +++ b/taglib/mp4/mp4item.cpp @@ -23,10 +23,6 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif - #include #include #include "mp4item.h" diff --git a/taglib/mp4/mp4properties.cpp b/taglib/mp4/mp4properties.cpp index e4e95072..f5d7ed21 100644 --- a/taglib/mp4/mp4properties.cpp +++ b/taglib/mp4/mp4properties.cpp @@ -23,9 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #include #include diff --git a/taglib/mp4/mp4tag.cpp b/taglib/mp4/mp4tag.cpp index df83796d..e16a994b 100644 --- a/taglib/mp4/mp4tag.cpp +++ b/taglib/mp4/mp4tag.cpp @@ -23,9 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #include #include diff --git a/taglib/mpeg/id3v2/id3v2frame.cpp b/taglib/mpeg/id3v2/id3v2frame.cpp index c5c5585d..a444b9eb 100644 --- a/taglib/mpeg/id3v2/id3v2frame.cpp +++ b/taglib/mpeg/id3v2/id3v2frame.cpp @@ -23,9 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #if HAVE_ZLIB #include diff --git a/taglib/mpeg/id3v2/id3v2framefactory.cpp b/taglib/mpeg/id3v2/id3v2framefactory.cpp index c7d74214..54b37ce8 100644 --- a/taglib/mpeg/id3v2/id3v2framefactory.cpp +++ b/taglib/mpeg/id3v2/id3v2framefactory.cpp @@ -23,9 +23,7 @@ * http://www.mozilla.org/MPL/ * ***************************************************************************/ -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #include diff --git a/taglib/toolkit/taglib.h b/taglib/toolkit/taglib.h index dd4ee69d..81d23f04 100755 --- a/taglib/toolkit/taglib.h +++ b/taglib/toolkit/taglib.h @@ -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 #include -#ifdef __APPLE__ -# include -# define TAGLIB_ATOMIC_MAC -#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) -# if !defined(NOMINMAX) -# define NOMINMAX -# endif -# include -# 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 -# define TAGLIB_ATOMIC_GCC +// Check the widths of integral types. + +#if SIZEOF_SHORT != 2 +# error TagLib requires that short is 16-bit wide. #endif -// Check the widths of integral types. +#if SIZEOF_INT != 4 +# error TagLib requires that int is 32-bit wide. +#endif + +#if SIZEOF_LONGLONG != 8 +# error TagLib requires that long long is 64-bit wide. +#endif + +#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 +#elif defined(HAVE_BOOST_ATOMIC) +# include +#elif defined(HAVE_MAC_ATOMIC) +# include +#elif defined(HAVE_WIN_ATOMIC) +# include +#elif defined(HAVE_IA64_ATOMIC) +# include +#endif + +#if defined(HAVE_STD_ATOMIC) +# define TAGLIB_ATOMIC_INT std::atomic +# 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 +# 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 -#if UCHAR_MAX != 255U -# error TagLib assumes that char is 8-bit wide. +// Optimized byte swap functions. + +#if defined(HAVE_MSC_BYTESWAP) +# include +#elif defined(HAVE_GLIBC_BYTESWAP) +# include +#elif defined(HAVE_MAC_BYTESWAP) +# include +#elif defined(HAVE_OPENBSD_BYTESWAP) +# include #endif -#if USHRT_MAX != 65535U -# error TagLib assumes that short is 16-bit wide. +#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 UINT_MAX != 4294967295U -# error TagLib assumes that int is 32-bit wide. +#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(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 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 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(&refCount)); } - bool deref() { return ! OSAtomicDecrement32Barrier(const_cast(&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 diff --git a/taglib/toolkit/tbyteswap.cpp b/taglib/toolkit/tbyteswap.cpp deleted file mode 100644 index cee2772d..00000000 --- a/taglib/toolkit/tbyteswap.cpp +++ /dev/null @@ -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 -# 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 -# define TAGLIB_BYTESWAP_MAC - -#elif defined(__OpenBSD__) -# include -# define TAGLIB_BYTESWAP_OPENBSD - -#elif defined(__GLIBC__) -# include -# 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(&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 -} diff --git a/taglib/toolkit/tbyteswap.h b/taglib/toolkit/tbyteswap.h deleted file mode 100644 index 012a3173..00000000 --- a/taglib/toolkit/tbyteswap.h +++ /dev/null @@ -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 diff --git a/taglib/toolkit/tbytevector.cpp b/taglib/toolkit/tbytevector.cpp index fafaf68f..a4b12672 100644 --- a/taglib/toolkit/tbytevector.cpp +++ b/taglib/toolkit/tbytevector.cpp @@ -30,7 +30,6 @@ #include #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 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 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 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 @@ -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(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(value); return ByteVector(reinterpret_cast(&value), size); diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 22590c89..72f4a24a 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -25,33 +25,128 @@ // This class assumes that std::basic_string 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 #include -#ifdef HAVE_CODECVT +#ifdef HAVE_STD_CODECVT # include -namespace { - typedef std::codecvt_utf8_utf16 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 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(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 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(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(&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(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(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(s[i])); + d->data[i] = byteSwap(static_cast(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 + } //////////////////////////////////////////////////////////////////////////////// -- 2.40.0