1 /***************************************************************************
2     copyright           : (C) 2011 by Mathias Panzenböck
3     email               : grosser.meister.morti@gmx.net
4  ***************************************************************************/
5 
6 /***************************************************************************
7  *   This library is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU Lesser General Public License version   *
9  *   2.1 as published by the Free Software Foundation.                     *
10  *                                                                         *
11  *   This library is distributed in the hope that it will be useful, but   *
12  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
14  *   Lesser General Public License for more details.                       *
15  *                                                                         *
16  *   You should have received a copy of the GNU Lesser General Public      *
17  *   License along with this library; if not, write to the Free Software   *
18  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
19  *   02110-1301  USA                                                       *
20  *                                                                         *
21  *   Alternatively, this file is available under the Mozilla Public        *
22  *   License Version 1.1.  You may obtain a copy of the License at         *
23  *   http://www.mozilla.org/MPL/                                           *
24  ***************************************************************************/
25 
26 
27 #include "tdebug.h"
28 #include "modfilebase.h"
29 
30 using namespace TagLib;
31 using namespace Mod;
32 
FileBase(FileName file)33 Mod::FileBase::FileBase(FileName file) : TagLib::File(file)
34 {
35 }
36 
FileBase(IOStream * stream)37 Mod::FileBase::FileBase(IOStream *stream) : TagLib::File(stream)
38 {
39 }
40 
writeString(const String & s,unsigned long size,char padding)41 void Mod::FileBase::writeString(const String &s, unsigned long size, char padding)
42 {
43   ByteVector data(s.data(String::Latin1));
44   data.resize(size, padding);
45   writeBlock(data);
46 }
47 
readString(String & s,unsigned long size)48 bool Mod::FileBase::readString(String &s, unsigned long size)
49 {
50   ByteVector data(readBlock(size));
51   if(data.size() < size) return false;
52   int index = data.find((char) 0);
53   if(index > -1)
54   {
55     data.resize(index);
56   }
57   data.replace('\xff', ' ');
58 
59   s = data;
60   return true;
61 }
62 
writeByte(unsigned char byte)63 void Mod::FileBase::writeByte(unsigned char byte)
64 {
65   ByteVector data(1, byte);
66   writeBlock(data);
67 }
68 
writeU16L(unsigned short number)69 void Mod::FileBase::writeU16L(unsigned short number)
70 {
71   writeBlock(ByteVector::fromShort(number, false));
72 }
73 
writeU32L(unsigned long number)74 void Mod::FileBase::writeU32L(unsigned long number)
75 {
76   writeBlock(ByteVector::fromUInt(number, false));
77 }
78 
writeU16B(unsigned short number)79 void Mod::FileBase::writeU16B(unsigned short number)
80 {
81   writeBlock(ByteVector::fromShort(number, true));
82 }
83 
writeU32B(unsigned long number)84 void Mod::FileBase::writeU32B(unsigned long number)
85 {
86   writeBlock(ByteVector::fromUInt(number, true));
87 }
88 
readByte(unsigned char & byte)89 bool Mod::FileBase::readByte(unsigned char &byte)
90 {
91   ByteVector data(readBlock(1));
92   if(data.size() < 1) return false;
93   byte = data[0];
94   return true;
95 }
96 
readU16L(unsigned short & number)97 bool Mod::FileBase::readU16L(unsigned short &number)
98 {
99   ByteVector data(readBlock(2));
100   if(data.size() < 2) return false;
101   number = data.toUShort(false);
102   return true;
103 }
104 
readU32L(unsigned long & number)105 bool Mod::FileBase::readU32L(unsigned long &number) {
106   ByteVector data(readBlock(4));
107   if(data.size() < 4) return false;
108   number = data.toUInt(false);
109   return true;
110 }
111 
readU16B(unsigned short & number)112 bool Mod::FileBase::readU16B(unsigned short &number)
113 {
114   ByteVector data(readBlock(2));
115   if(data.size() < 2) return false;
116   number = data.toUShort(true);
117   return true;
118 }
119 
readU32B(unsigned long & number)120 bool Mod::FileBase::readU32B(unsigned long &number) {
121   ByteVector data(readBlock(4));
122   if(data.size() < 4) return false;
123   number = data.toUInt(true);
124   return true;
125 }
126