asfutils.h

Go to the documentation of this file.
00001 /***************************************************************************
00002     copyright            : (C) 2015 by Tsuda Kageyu
00003     email                : tsuda.kageyu@gmail.com
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_ASFUTILS_H
00027 #define TAGLIB_ASFUTILS_H
00028 
00029 // THIS FILE IS NOT A PART OF THE TAGLIB API
00030 
00031 #ifndef DO_NOT_DOCUMENT  // tell Doxygen not to document this header
00032 
00033 namespace TagLib
00034 {
00035   namespace ASF
00036   {
00037     namespace
00038     {
00039 
00040       inline unsigned short readWORD(File *file, bool *ok = 0)
00041       {
00042         const ByteVector v = file->readBlock(2);
00043         if(v.size() != 2) {
00044           if(ok) *ok = false;
00045           return 0;
00046         }
00047         if(ok) *ok = true;
00048         return v.toUShort(false);
00049       }
00050 
00051       inline unsigned int readDWORD(File *file, bool *ok = 0)
00052       {
00053         const ByteVector v = file->readBlock(4);
00054         if(v.size() != 4) {
00055           if(ok) *ok = false;
00056           return 0;
00057         }
00058         if(ok) *ok = true;
00059         return v.toUInt(false);
00060       }
00061 
00062       inline long long readQWORD(File *file, bool *ok = 0)
00063       {
00064         const ByteVector v = file->readBlock(8);
00065         if(v.size() != 8) {
00066           if(ok) *ok = false;
00067           return 0;
00068         }
00069         if(ok) *ok = true;
00070         return v.toLongLong(false);
00071       }
00072 
00073       inline String readString(File *file, int length)
00074       {
00075         ByteVector data = file->readBlock(length);
00076         unsigned int size = data.size();
00077         while (size >= 2) {
00078           if(data[size - 1] != '\0' || data[size - 2] != '\0') {
00079             break;
00080           }
00081           size -= 2;
00082         }
00083         if(size != data.size()) {
00084           data.resize(size);
00085         }
00086         return String(data, String::UTF16LE);
00087       }
00088 
00089       inline ByteVector renderString(const String &str, bool includeLength = false)
00090       {
00091         ByteVector data = str.data(String::UTF16LE) + ByteVector::fromShort(0, false);
00092         if(includeLength) {
00093           data = ByteVector::fromShort(data.size(), false) + data;
00094         }
00095         return data;
00096       }
00097 
00098     }
00099   }
00100 }
00101 
00102 #endif
00103 
00104 #endif