tstring.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_STRING_H
00027 #define TAGLIB_STRING_H
00028 
00029 #include "taglib_export.h"
00030 #include "taglib.h"
00031 #include "tbytevector.h"
00032 
00033 #include <string>
00034 #include <iostream>
00035 
00045 #if defined(QT_VERSION) && (QT_VERSION >= 0x040000)
00046 #define QStringToTString(s) TagLib::String(s.toUtf8().data(), TagLib::String::UTF8)
00047 #else
00048 #define QStringToTString(s) TagLib::String(s.utf8().data(), TagLib::String::UTF8)
00049 #endif
00050 
00061 #define TStringToQString(s) QString::fromUtf8(s.toCString(true))
00062 
00063 namespace TagLib {
00064 
00065   class StringList;
00066 
00068 
00084   class TAGLIB_EXPORT String
00085   {
00086   public:
00087 
00088 #ifndef DO_NOT_DOCUMENT
00089     typedef std::basic_string<wchar>::iterator Iterator;
00090     typedef std::basic_string<wchar>::const_iterator ConstIterator;
00091 #endif
00092 
00097     enum Type {
00101       Latin1 = 0,
00105       UTF16 = 1,
00110       UTF16BE = 2,
00114       UTF8 = 3,
00118       UTF16LE = 4
00119     };
00120 
00124     String();
00125 
00131     String(const String &s);
00132 
00139     String(const std::string &s, Type t = Latin1);
00140 
00148     String(const wstring &s, Type t = UTF16BE);
00149 
00157     String(const wchar_t *s, Type t = UTF16BE);
00158 
00165     String(char c, Type t = Latin1);
00166 
00170     String(wchar_t c, Type t = Latin1);
00171 
00178     String(const char *s, Type t = Latin1);
00179 
00183     String(const ByteVector &v, Type t = Latin1);
00184 
00188     virtual ~String();
00189 
00196     std::string to8Bit(bool unicode = false) const;
00197 
00205     wstring toWString() const;
00206 
00225     const char *toCString(bool unicode = false) const;
00226 
00243     const wchar_t *toCWString() const;
00244 
00248     Iterator begin();
00249 
00253     ConstIterator begin() const;
00254 
00259     Iterator end();
00260 
00265     ConstIterator end() const;
00266 
00271     int find(const String &s, int offset = 0) const;
00272 
00278     int rfind(const String &s, int offset = -1) const;
00279 
00283     StringList split(const String &separator = " ") const;
00284 
00288     bool startsWith(const String &s) const;
00289 
00294     String substr(uint position, uint n = 0xffffffff) const;
00295 
00300     String &append(const String &s);
00301 
00307     String upper() const;
00308 
00312     uint size() const;
00313 
00317     uint length() const;
00318 
00324     bool isEmpty() const;
00325 
00333     bool isNull() const;
00334 
00345     ByteVector data(Type t) const;
00346 
00353     // BIC: merge with the method below
00354     int toInt() const;
00355 
00363     int toInt(bool *ok) const;
00364 
00368     String stripWhiteSpace() const;
00369 
00373     bool isLatin1() const;
00374 
00378     bool isAscii() const;
00379 
00383     static String number(int n);
00384 
00388     wchar &operator[](int i);
00389 
00393     const wchar &operator[](int i) const;
00394 
00399     bool operator==(const String &s) const;
00400 
00405     bool operator!=(const String &s) const;
00406 
00411     bool operator==(const char *s) const;
00412 
00417     bool operator!=(const char *s) const;
00418 
00423     bool operator==(const wchar_t *s) const;
00424 
00429     bool operator!=(const wchar_t *s) const;
00430 
00434     String &operator+=(const String &s);
00435 
00439     String &operator+=(const wchar_t* s);
00440 
00444     String &operator+=(const char* s);
00445 
00449     String &operator+=(wchar_t c);
00450 
00454     String &operator+=(char c);
00455 
00460     String &operator=(const String &s);
00461 
00465     String &operator=(const std::string &s);
00466 
00470     String &operator=(const wstring &s);
00471 
00475     String &operator=(const wchar_t *s);
00476 
00480     String &operator=(char c);
00481 
00485     String &operator=(wchar_t c);
00486 
00490     String &operator=(const char *s);
00491 
00495     String &operator=(const ByteVector &v);
00496 
00502     bool operator<(const String &s) const;
00503 
00507     static String null;
00508 
00509   protected:
00515     void detach();
00516 
00517   private:
00522     void copyFromLatin1(const char *s, size_t length);
00523 
00528     void copyFromUTF8(const char *s, size_t length);
00529 
00534     void copyFromUTF16(const wchar_t *s, size_t length, Type t);
00535 
00540     void copyFromUTF16(const char *s, size_t length, Type t);
00541 
00547     static const Type WCharByteOrder;
00548 
00549     class StringPrivate;
00550     StringPrivate *d;
00551   };
00552 }
00553 
00559 TAGLIB_EXPORT const TagLib::String operator+(const TagLib::String &s1, const TagLib::String &s2);
00560 
00566 TAGLIB_EXPORT const TagLib::String operator+(const char *s1, const TagLib::String &s2);
00567 
00573 TAGLIB_EXPORT const TagLib::String operator+(const TagLib::String &s1, const char *s2);
00574 
00575 
00581 TAGLIB_EXPORT std::ostream &operator<<(std::ostream &s, const TagLib::String &str);
00582 
00583 #endif