tbytevector.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     copyright            : (C) 2002 - 2008 by Scott Wheeler
00003     email                : wheeler@kde.org
00004  ***************************************************************************/
00005 
00006 /***************************************************************************
00007  *   This library is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU Lesser General Public License version   *
00009  *   2.1 as published by the Free Software Foundation.                     *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful, but   *
00012  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the Free Software   *
00018  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
00019  *   02110-1301  USA                                                       *
00020  *                                                                         *
00021  *   Alternatively, this file is available under the Mozilla Public        *
00022  *   License Version 1.1.  You may obtain a copy of the License at         *
00023  *   http://www.mozilla.org/MPL/                                           *
00024  ***************************************************************************/
00025 
00026 #ifndef TAGLIB_BYTEVECTOR_H
00027 #define TAGLIB_BYTEVECTOR_H
00028 
00029 #include "taglib.h"
00030 #include "taglib_export.h"
00031 
00032 #include <vector>
00033 #include <iostream>
00034 
00035 namespace TagLib {
00036 
00038 
00045   class TAGLIB_EXPORT ByteVector
00046   {
00047   public:
00048 #ifndef DO_NOT_DOCUMENT
00049     typedef std::vector<char>::iterator Iterator;
00050     typedef std::vector<char>::const_iterator ConstIterator;
00051     typedef std::vector<char>::reverse_iterator ReverseIterator;
00052     typedef std::vector<char>::const_reverse_iterator ConstReverseIterator;
00053 #endif
00054 
00058     ByteVector();
00059 
00064     ByteVector(unsigned int size, char value = 0);
00065 
00069     ByteVector(const ByteVector &v);
00070 
00074     ByteVector(const ByteVector &v, unsigned int offset, unsigned int length);
00075 
00079     ByteVector(char c);
00080 
00084     ByteVector(const char *data, unsigned int length);
00085 
00093     ByteVector(const char *data);
00094 
00098     virtual ~ByteVector();
00099 
00103     ByteVector &setData(const char *data, unsigned int length);
00104 
00109     ByteVector &setData(const char *data);
00110 
00118     char *data();
00119 
00123     const char *data() const;
00124 
00130     ByteVector mid(unsigned int index, unsigned int length = 0xffffffff) const;
00131 
00136     char at(unsigned int index) const;
00137 
00144     int find(const ByteVector &pattern, unsigned int offset = 0, int byteAlign = 1) const;
00145 
00152     int find(char c, unsigned int offset = 0, int byteAlign = 1) const;
00153 
00160     int rfind(const ByteVector &pattern, unsigned int offset = 0, int byteAlign = 1) const;
00161 
00169     bool containsAt(const ByteVector &pattern, unsigned int offset,
00170                     unsigned int patternOffset = 0, unsigned int patternLength = 0xffffffff) const;
00171 
00175     bool startsWith(const ByteVector &pattern) const;
00176 
00180     bool endsWith(const ByteVector &pattern) const;
00181 
00186     ByteVector &replace(char oldByte, char newByte);
00187 
00192     ByteVector &replace(const ByteVector &pattern, const ByteVector &with);
00193 
00204     int endsWithPartialMatch(const ByteVector &pattern) const;
00205 
00209     ByteVector &append(const ByteVector &v);
00210 
00214     ByteVector &append(char c);
00215 
00219     ByteVector &clear();
00220 
00224     unsigned int size() const;
00225 
00231     ByteVector &resize(unsigned int size, char padding = 0);
00232 
00236     Iterator begin();
00237 
00241     ConstIterator begin() const;
00242 
00246     Iterator end();
00247 
00251     ConstIterator end() const;
00252 
00256     ReverseIterator rbegin();
00257 
00261     ConstReverseIterator rbegin() const;
00262 
00266     ReverseIterator rend();
00267 
00271     ConstReverseIterator rend() const;
00272 
00283      // BIC: remove
00284     bool isNull() const;
00285 
00292     bool isEmpty() const;
00293 
00299     // BIC: Remove or make generic.
00300     unsigned int checksum() const;
00301 
00312     unsigned int toUInt(bool mostSignificantByteFirst = true) const;
00313 
00324     unsigned int toUInt(unsigned int offset, bool mostSignificantByteFirst = true) const;
00325 
00337     unsigned int toUInt(unsigned int offset, unsigned int length,
00338                         bool mostSignificantByteFirst = true) const;
00339 
00349     short toShort(bool mostSignificantByteFirst = true) const;
00350 
00360     short toShort(unsigned int offset, bool mostSignificantByteFirst = true) const;
00361 
00371     unsigned short toUShort(bool mostSignificantByteFirst = true) const;
00372 
00382     unsigned short toUShort(unsigned int offset, bool mostSignificantByteFirst = true) const;
00383 
00394     long long toLongLong(bool mostSignificantByteFirst = true) const;
00395 
00406     long long toLongLong(unsigned int offset, bool mostSignificantByteFirst = true) const;
00407 
00408     /*
00409      * Converts the 4 bytes at \a offset of the vector to a float as an IEEE754
00410      * 32-bit little-endian floating point number.
00411      */
00412     float toFloat32LE(size_t offset) const;
00413 
00414     /*
00415      * Converts the 4 bytes at \a offset of the vector to a float as an IEEE754
00416      * 32-bit big-endian floating point number.
00417      */
00418     float toFloat32BE(size_t offset) const;
00419 
00420     /*
00421      * Converts the 8 bytes at \a offset of the vector to a double as an IEEE754
00422      * 64-bit little-endian floating point number.
00423      */
00424     double toFloat64LE(size_t offset) const;
00425 
00426     /*
00427      * Converts the 8 bytes at \a offset of the vector to a double as an IEEE754
00428      * 64-bit big-endian floating point number.
00429      */
00430     double toFloat64BE(size_t offset) const;
00431 
00432     /*
00433     * Converts the 10 bytes at \a offset of the vector to a long double as an
00434     * IEEE754 80-bit little-endian floating point number.
00435     *
00436     * \note This may compromise the precision depends on the size of long double.
00437     */
00438     long double toFloat80LE(size_t offset) const;
00439 
00440     /*
00441      * Converts the 10 bytes at \a offset of the vector to a long double as an
00442      * IEEE754 80-bit big-endian floating point number.
00443      *
00444      * \note This may compromise the precision depends on the size of long double.
00445      */
00446     long double toFloat80BE(size_t offset) const;
00447 
00457     static ByteVector fromUInt(unsigned int value, bool mostSignificantByteFirst = true);
00458 
00467     static ByteVector fromShort(short value, bool mostSignificantByteFirst = true);
00468 
00478     static ByteVector fromLongLong(long long value, bool mostSignificantByteFirst = true);
00479 
00486     static ByteVector fromFloat32LE(float value);
00487 
00494     static ByteVector fromFloat32BE(float value);
00495 
00502     static ByteVector fromFloat64LE(double value);
00503 
00510     static ByteVector fromFloat64BE(double value);
00511 
00515     static ByteVector fromCString(const char *s, unsigned int length = 0xffffffff);
00516 
00520     const char &operator[](int index) const;
00521 
00525     char &operator[](int index);
00526 
00530     bool operator==(const ByteVector &v) const;
00531 
00535     bool operator!=(const ByteVector &v) const;
00536 
00541     bool operator==(const char *s) const;
00542 
00547     bool operator!=(const char *s) const;
00548 
00554     bool operator<(const ByteVector &v) const;
00555 
00559     bool operator>(const ByteVector &v) const;
00560 
00564     ByteVector operator+(const ByteVector &v) const;
00565 
00569     ByteVector &operator=(const ByteVector &v);
00570 
00574     ByteVector &operator=(char c);
00575 
00581     ByteVector &operator=(const char *data);
00582 
00586     void swap(ByteVector &v);
00587 
00597     // BIC: remove
00598     static ByteVector null;
00599 
00603     ByteVector toHex() const;
00604 
00608     ByteVector toBase64() const;
00609 
00613     static ByteVector fromBase64(const ByteVector &);
00614 
00615   protected:
00616     /*
00617      * If this ByteVector is being shared via implicit sharing, do a deep copy
00618      * of the data and separate from the shared members.  This should be called
00619      * by all non-const subclass members.
00620      */
00621     void detach();
00622 
00623   private:
00624     class ByteVectorPrivate;
00625     ByteVectorPrivate *d;
00626   };
00627 }
00628 
00633 TAGLIB_EXPORT std::ostream &operator<<(std::ostream &s, const TagLib::ByteVector &v);
00634 
00635 #endif