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 
00038     inline ushort readWORD(File *file, bool *ok = 0)
00039     {
00040       const ByteVector v = file->readBlock(2);
00041       if(v.size() != 2) {
00042         if(ok) *ok = false;
00043         return 0;
00044       }
00045       if(ok) *ok = true;
00046       return v.toUShort(false);
00047     }
00048 
00049     inline uint readDWORD(File *file, bool *ok = 0)
00050     {
00051       const ByteVector v = file->readBlock(4);
00052       if(v.size() != 4) {
00053         if(ok) *ok = false;
00054         return 0;
00055       }
00056       if(ok) *ok = true;
00057       return v.toUInt(false);
00058     }
00059 
00060     inline long long readQWORD(File *file, bool *ok = 0)
00061     {
00062       const ByteVector v = file->readBlock(8);
00063       if(v.size() != 8) {
00064         if(ok) *ok = false;
00065         return 0;
00066       }
00067       if(ok) *ok = true;
00068       return v.toLongLong(false);
00069     }
00070 
00071     inline String readString(File *file, int length)
00072     {
00073       ByteVector data = file->readBlock(length);
00074       unsigned int size = data.size();
00075       while (size >= 2) {
00076         if(data[size - 1] != '\0' || data[size - 2] != '\0') {
00077           break;
00078         }
00079         size -= 2;
00080       }
00081       if(size != data.size()) {
00082         data.resize(size);
00083       }
00084       return String(data, String::UTF16LE);
00085     }
00086 
00087     inline ByteVector renderString(const String &str, bool includeLength = false)
00088     {
00089       ByteVector data = str.data(String::UTF16LE) + ByteVector::fromShort(0, false);
00090       if(includeLength) {
00091         data = ByteVector::fromShort(data.size(), false) + data;
00092       }
00093       return data;
00094     }
00095 
00096   }
00097 }
00098 
00099 #endif
00100 
00101 #endif