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(uint size, char value = 0);
00065 
00069     ByteVector(const ByteVector &v);
00070 
00074     ByteVector(const ByteVector &v, uint offset, uint length);
00075 
00079     ByteVector(char c);
00080 
00084     ByteVector(const char *data, uint length);
00085 
00093     ByteVector(const char *data);
00094 
00098     virtual ~ByteVector();
00099 
00103     ByteVector &setData(const char *data, uint length);
00104 
00109     ByteVector &setData(const char *data);
00110 
00118     char *data();
00119 
00123     const char *data() const;
00124 
00130     ByteVector mid(uint index, uint length = 0xffffffff) const;
00131 
00136     char at(uint index) const;
00137 
00144     int find(const ByteVector &pattern, uint offset = 0, int byteAlign = 1) const;
00145 
00152     int find(char c, uint offset = 0, int byteAlign = 1) const;
00153 
00160     int rfind(const ByteVector &pattern, uint offset = 0, int byteAlign = 1) const;
00161 
00169     bool containsAt(const ByteVector &pattern, uint offset, uint patternOffset = 0, uint patternLength = 0xffffffff) const;
00170 
00174     bool startsWith(const ByteVector &pattern) const;
00175 
00179     bool endsWith(const ByteVector &pattern) const;
00180 
00185     ByteVector &replace(const ByteVector &pattern, const ByteVector &with);
00186 
00197     int endsWithPartialMatch(const ByteVector &pattern) const;
00198 
00202     ByteVector &append(const ByteVector &v);
00203 
00207     ByteVector &clear();
00208 
00212     uint size() const;
00213 
00219     ByteVector &resize(uint size, char padding = 0);
00220 
00224     Iterator begin();
00225 
00229     ConstIterator begin() const;
00230 
00234     Iterator end();
00235 
00239     ConstIterator end() const;
00240 
00244     ReverseIterator rbegin();
00245 
00249     ConstReverseIterator rbegin() const;
00250 
00254     ReverseIterator rend();
00255 
00259     ConstReverseIterator rend() const;
00260 
00267     bool isNull() const;
00268 
00275     bool isEmpty() const;
00276 
00282     // BIC: Remove or make generic.
00283     uint checksum() const;
00284 
00295     uint toUInt(bool mostSignificantByteFirst = true) const;
00296 
00307     uint toUInt(uint offset, bool mostSignificantByteFirst = true) const;
00308 
00320     uint toUInt(uint offset, uint length, bool mostSignificantByteFirst = true) const;
00321 
00331     short toShort(bool mostSignificantByteFirst = true) const;
00332 
00342     short toShort(uint offset, bool mostSignificantByteFirst = true) const;
00343 
00353     unsigned short toUShort(bool mostSignificantByteFirst = true) const;
00354 
00364     unsigned short toUShort(uint offset, bool mostSignificantByteFirst = true) const;
00365 
00376     long long toLongLong(bool mostSignificantByteFirst = true) const;
00377 
00388     long long toLongLong(uint offset, bool mostSignificantByteFirst = true) const;
00389 
00390     /*
00391      * Converts the 4 bytes at \a offset of the vector to a float as an IEEE754
00392      * 32-bit little-endian floating point number.
00393      */
00394     float toFloat32LE(size_t offset) const;
00395 
00396     /*
00397      * Converts the 4 bytes at \a offset of the vector to a float as an IEEE754
00398      * 32-bit big-endian floating point number.
00399      */
00400     float toFloat32BE(size_t offset) const;
00401 
00402     /*
00403      * Converts the 8 bytes at \a offset of the vector to a double as an IEEE754
00404      * 64-bit little-endian floating point number.
00405      */
00406     double toFloat64LE(size_t offset) const;
00407 
00408     /*
00409      * Converts the 8 bytes at \a offset of the vector to a double as an IEEE754
00410      * 64-bit big-endian floating point number.
00411      */
00412     double toFloat64BE(size_t offset) const;
00413 
00414     /*
00415     * Converts the 10 bytes at \a offset of the vector to a long double as an
00416     * IEEE754 80-bit little-endian floating point number.
00417     *
00418     * \note This may compromise the precision depends on the size of long double.
00419     */
00420     long double toFloat80LE(size_t offset) const;
00421 
00422     /*
00423      * Converts the 10 bytes at \a offset of the vector to a long double as an
00424      * IEEE754 80-bit big-endian floating point number.
00425      *
00426      * \note This may compromise the precision depends on the size of long double.
00427      */
00428     long double toFloat80BE(size_t offset) const;
00429 
00439     static ByteVector fromUInt(uint value, bool mostSignificantByteFirst = true);
00440 
00449     static ByteVector fromShort(short value, bool mostSignificantByteFirst = true);
00450 
00460     static ByteVector fromLongLong(long long value, bool mostSignificantByteFirst = true);
00461 
00468     static ByteVector fromFloat32LE(float value);
00469 
00476     static ByteVector fromFloat32BE(float value);
00477 
00484     static ByteVector fromFloat64LE(double value);
00485 
00492     static ByteVector fromFloat64BE(double value);
00493 
00497     static ByteVector fromCString(const char *s, uint length = 0xffffffff);
00498 
00502     const char &operator[](int index) const;
00503 
00507     char &operator[](int index);
00508 
00512     bool operator==(const ByteVector &v) const;
00513 
00517     bool operator!=(const ByteVector &v) const;
00518 
00523     bool operator==(const char *s) const;
00524 
00529     bool operator!=(const char *s) const;
00530 
00536     bool operator<(const ByteVector &v) const;
00537 
00541     bool operator>(const ByteVector &v) const;
00542 
00546     ByteVector operator+(const ByteVector &v) const;
00547 
00551     ByteVector &operator=(const ByteVector &v);
00552 
00556     ByteVector &operator=(char c);
00557 
00563     ByteVector &operator=(const char *data);
00564 
00569     static ByteVector null;
00570 
00574     ByteVector toHex() const;
00575 
00576   protected:
00577     /*
00578      * If this ByteVector is being shared via implicit sharing, do a deep copy
00579      * of the data and separate from the shared members.  This should be called
00580      * by all non-const subclass members.
00581      */
00582     void detach();
00583 
00584   private:
00585     class ByteVectorPrivate;
00586     ByteVectorPrivate *d;
00587   };
00588 }
00589 
00594 TAGLIB_EXPORT std::ostream &operator<<(std::ostream &s, const TagLib::ByteVector &v);
00595 
00596 #endif