]> granicus.if.org Git - taglib/commitdiff
Changed my mind on the last one -- use explicit types rather than trying
authorScott Wheeler <wheeler@kde.org>
Tue, 27 Apr 2004 01:09:31 +0000 (01:09 +0000)
committerScott Wheeler <wheeler@kde.org>
Tue, 27 Apr 2004 01:09:31 +0000 (01:09 +0000)
to figure things out.  This also allows the number conversion to be
templatized.

git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@306699 283d02a7-25f6-0310-bc7c-ecb5cbfe19da

toolkit/tbytevector.cpp
toolkit/tbytevector.h

index 59d1648ed5b399195266a596b03861a22efce5c5..3ef1b6bca4319a73b9577d42a2f656f76db47b09 100644 (file)
@@ -184,6 +184,19 @@ namespace TagLib {
   private:
     const ByteVector v;
   };
+
+  template <class T>
+  T toNumber(const std::vector<char> &data, bool mostSignificantByteFirst)
+  {
+    T sum = 0;
+    uint size = sizeof(T);
+    uint last = data.size() > size ? size - 1 : data.size() - 1;
+
+    for(uint i = 0; i <= last; i++)
+      sum |= (T) uchar(data[i]) << ((mostSignificantByteFirst ? last - i : i) * 8);
+
+    return sum;
+  }
 }
 
 using namespace TagLib;
@@ -474,47 +487,17 @@ TagLib::uint ByteVector::checksum() const
 
 TagLib::uint ByteVector::toUInt(bool mostSignificantByteFirst) const
 {
-  uint sum = 0;
-  int last = d->data.size() > 4 ? 3 : d->data.size() - 1;
-
-  for(int i = 0; i <= last; i++)
-    sum |= uchar(d->data[i]) << ((mostSignificantByteFirst ? last - i : i) * 8);
-
-  return sum;
+  return toNumber<uint>(d->data, mostSignificantByteFirst);
 }
 
-int ByteVector::toInt(bool mostSignificantByteFirst) const
+short ByteVector::toShort(bool mostSignificantByteFirst) const
 {
-  int sum = 0;
-  int last = d->data.size() > 4 ? 3 : d->data.size() - 1;
-
-  bool negative = uchar(d->data[mostSignificantByteFirst ? 0 : last]) & 0x80;
-
-  if(negative) {
-    for(int i = 0; i <= last; i++)
-      sum |= uchar(d->data[i] ^ 0xff) << ((mostSignificantByteFirst ? last - i : i) * 8);
-    sum = (sum + 1) * -1;
-  }
-  else {
-    for(int i = 0; i <= last; i++)
-      sum |= uchar(d->data[i]) << ((mostSignificantByteFirst ? last - i : i) * 8);
-  }
-
-  return sum;
+  return toNumber<unsigned short>(d->data, mostSignificantByteFirst);
 }
 
 long long ByteVector::toLongLong(bool mostSignificantByteFirst) const
 {
-  // Just do all of the bit operations on the unsigned value and use an implicit
-  // cast on the way out.
-
-  unsigned long long sum = 0;
-  int last = d->data.size() > 8 ? 7 : d->data.size() - 1;
-
-  for(int i = 0; i <= last; i++)
-    sum |= (unsigned long long) uchar(d->data[i]) << ((mostSignificantByteFirst ? last - i : i) * 8);
-
-  return sum;
+  return toNumber<unsigned long long>(d->data, mostSignificantByteFirst);
 }
 
 const char &ByteVector::operator[](int index) const
index afe3c162db52815e6a89aa77aadcf35798147551..f31d5786e33ecdfab639982ff5e6734f3203341f 100644 (file)
@@ -244,20 +244,16 @@ namespace TagLib {
     uint toUInt(bool mostSignificantByteFirst = true) const;
 
     /*!
-     * Converts the first 4 bytes of the vector to an integer.
+     * Converts the first 2 bytes of the vector to a short.
      *
      * If \a mostSignificantByteFirst is true this will operate left to right
      * evaluating the integer.  For example if \a mostSignificantByteFirst is
      * true then $00 $00 $00 $01 == 0x00000001 == 1, if false, $01 00 00 00 ==
      * 0x01000000 == 1.
      *
-     * This will correctly adjust for the bit indicated signedness being the most
-     * significant bit of the most significant byte.
-     *
-     * \see fromUInt()
+     * \see fromShort()
      */
-    int toInt(bool mostSignificantByteFirst = true) const;
-
+    short toShort(bool mostSignificantByteFirst = true) const;
 
     /*!
      * Converts the first 8 bytes of the vector to a (signed) long long.