]> granicus.if.org Git - taglib/commitdiff
Move byteSwap to a (private) shared include file, rather than having the same code...
authorLukáš Lalinský <lalinsky@gmail.com>
Thu, 20 Jun 2013 13:22:06 +0000 (15:22 +0200)
committerLukáš Lalinský <lalinsky@gmail.com>
Thu, 20 Jun 2013 13:22:06 +0000 (15:22 +0200)
taglib/toolkit/tbytevector.cpp
taglib/toolkit/tstring.cpp
taglib/toolkit/tutils.h [new file with mode: 0644]

index 63bce4ccef88077c1699418b52211e8130c5a381..0ea7517c67d2a1ffa53b2443e3eb21c218486b0e 100644 (file)
 #include <cstdio>
 #include <cstring>
 
-#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
-
 #include <tstring.h>
 #include <tdebug.h>
 #include "trefcounter.h"
+#include "tutils.h"
 
 #include "tbytevector.h"
 
@@ -190,114 +181,6 @@ int findVector(
   return -1;
 }
 
-template <class T>
-T byteSwap(T x)
-{
-  // There should be all counterparts of to*() and from*() overloads for integral types.
-  debug("byteSwap<T>() -- Non specialized version should not be called");
-  return 0;
-}
-
-template <>
-ushort byteSwap<ushort>(ushort x)
-{
-#if defined(HAVE_GCC_BYTESWAP_16)
-
-  return __builtin_bswap16(x);
-
-#elif defined(HAVE_MSC_BYTESWAP)
-
-  return _byteswap_ushort(x);
-
-#elif defined(HAVE_GLIBC_BYTESWAP)
-
-  return __bswap_16(x);
-
-#elif defined(HAVE_MAC_BYTESWAP)
-
-  return OSSwapInt16(x);
-
-#elif defined(HAVE_OPENBSD_BYTESWAP)
-
-  return swap16(x);
-
-#else
-
-  return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
-
-#endif
-}
-
-template <>
-uint byteSwap<uint>(uint x)
-{
-#if defined(HAVE_GCC_BYTESWAP_32)
-
-  return __builtin_bswap32(x);
-
-#elif defined(HAVE_MSC_BYTESWAP)
-
-  return _byteswap_ulong(x);
-
-#elif defined(HAVE_GLIBC_BYTESWAP)
-
-  return __bswap_32(x);
-
-#elif defined(HAVE_MAC_BYTESWAP)
-
-  return OSSwapInt32(x);
-
-#elif defined(HAVE_OPENBSD_BYTESWAP)
-
-  return swap32(x);
-
-#else
-
-  return ((x & 0xff000000u) >> 24) 
-    | ((x & 0x00ff0000u) >>  8) 
-    | ((x & 0x0000ff00u) <<  8)
-    | ((x & 0x000000ffu) << 24);
-
-#endif
-}
-
-template <>
-ulonglong byteSwap<ulonglong>(ulonglong x)
-{
-#if defined(HAVE_GCC_BYTESWAP_64)
-
-  return __builtin_bswap64(x);
-
-#elif defined(HAVE_MSC_BYTESWAP)
-
-  return _byteswap_uint64(x);
-
-#elif defined(HAVE_GLIBC_BYTESWAP)
-
-  return __bswap_64(x);
-
-#elif defined(HAVE_MAC_BYTESWAP)
-
-  return OSSwapInt64(x);
-
-#elif defined(HAVE_OPENBSD_BYTESWAP)
-
-  return swap64(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>
 T toNumber(const ByteVector &v, size_t offset, size_t length, bool mostSignificantByteFirst)
 {
@@ -333,7 +216,7 @@ T toNumber(const ByteVector &v, size_t offset, bool mostSignificantByteFirst)
   const bool swap != mostSignificantByteFirst;
 #endif
   if(swap)
-    return byteSwap<T>(tmp);
+    return byteSwap(tmp);
   else
     return tmp;
 }
@@ -349,7 +232,7 @@ ByteVector fromNumber(T value, bool mostSignificantByteFirst)
   const bool swap != mostSignificantByteFirst;
 #endif
  if(swap)
-    value = byteSwap<T>(value);
+    value = byteSwap(value);
 
   return ByteVector(reinterpret_cast<const char *>(&value), size);
 }
index 8fcbb2debbed669bd01eb2579c3ef28330b485bb..d28855b8c0323f49aec06890c4bfa041f33b6bea 100644 (file)
 #include "tdebug.h"
 #include "tstringlist.h"
 #include "trefcounter.h"
+#include "tutils.h"
 
 #include <iostream>
 #include <cstdio>
 #include <cstring>
 
-#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
-
 #ifdef HAVE_STD_CODECVT
 # include <codecvt>
 #else
 
 namespace 
 {
-  inline TagLib::ushort byteSwap(TagLib::ushort x)
-  {
-#if defined(HAVE_GCC_BYTESWAP_16)
-
-    return __builtin_bswap16(x);
-
-#elif defined(HAVE_MSC_BYTESWAP)
-
-    return _byteswap_ushort(x);
-
-#elif defined(HAVE_GLIBC_BYTESWAP)
-
-    return __bswap_16(x);
-
-#elif defined(HAVE_MAC_BYTESWAP)
-
-    return OSSwapInt16(x);
-
-#elif defined(HAVE_OPENBSD_BYTESWAP)
-
-    return swap16(x);
-
-#else
-
-    return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
-
-#endif
-  }
 
   inline unsigned short combine(unsigned char c1, unsigned char c2)
   {
diff --git a/taglib/toolkit/tutils.h b/taglib/toolkit/tutils.h
new file mode 100644 (file)
index 0000000..73c3571
--- /dev/null
@@ -0,0 +1,151 @@
+/***************************************************************************
+    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_TUTILS_H
+#define TAGLIB_TUTILS_H
+
+// THIS FILE IS NOT A PART OF THE TAGLIB API
+
+#ifndef DO_NOT_DOCUMENT  // tell Doxygen not to document this header
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#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
+
+namespace TagLib
+{
+
+  inline ushort byteSwap(ushort x)
+  {
+#if defined(HAVE_GCC_BYTESWAP_16)
+
+    return __builtin_bswap16(x);
+
+#elif defined(HAVE_MSC_BYTESWAP)
+
+    return _byteswap_ushort(x);
+
+#elif defined(HAVE_GLIBC_BYTESWAP)
+
+    return __bswap_16(x);
+
+#elif defined(HAVE_MAC_BYTESWAP)
+
+    return OSSwapInt16(x);
+
+#elif defined(HAVE_OPENBSD_BYTESWAP)
+
+    return swap16(x);
+
+#else
+
+    return ((x >> 8) & 0xff) | ((x & 0xff) << 8);
+
+#endif
+  }
+
+  inline uint byteSwap(uint x)
+  {
+#if defined(HAVE_GCC_BYTESWAP_32)
+
+    return __builtin_bswap32(x);
+
+#elif defined(HAVE_MSC_BYTESWAP)
+
+    return _byteswap_ulong(x);
+
+#elif defined(HAVE_GLIBC_BYTESWAP)
+
+    return __bswap_32(x);
+
+#elif defined(HAVE_MAC_BYTESWAP)
+
+    return OSSwapInt32(x);
+
+#elif defined(HAVE_OPENBSD_BYTESWAP)
+
+    return swap32(x);
+
+#else
+
+    return ((x & 0xff000000u) >> 24)
+      | ((x & 0x00ff0000u) >>  8)
+      | ((x & 0x0000ff00u) <<  8)
+      | ((x & 0x000000ffu) << 24);
+
+#endif
+  }
+
+  inline ulonglong byteSwap(ulonglong x)
+  {
+#if defined(HAVE_GCC_BYTESWAP_64)
+
+    return __builtin_bswap64(x);
+
+#elif defined(HAVE_MSC_BYTESWAP)
+
+    return _byteswap_uint64(x);
+
+#elif defined(HAVE_GLIBC_BYTESWAP)
+
+    return __bswap_64(x);
+
+#elif defined(HAVE_MAC_BYTESWAP)
+
+    return OSSwapInt64(x);
+
+#elif defined(HAVE_OPENBSD_BYTESWAP)
+
+    return swap64(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
+  }
+
+};
+
+#endif
+
+#endif