2 * FryingPan - Amiga CD/DVD Recording Software (User Interface and supporting Libraries only)
3 * Copyright (C) 2001-2011 Tomasz Wiszkowski Tomasz.Wiszkowski at gmail.com
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "FileReader.h"
21 #include <libclass/dos.h>
22 #include <Optical/IOptItem.h>
23 #include <LibC/LibC.h>
24 #include <libclass/utility.h>
26 FileReader::FileReader(const char* sFName
, EDtError
&rc
)
29 _D(Lvl_Info
, "%s: Setting up defaults", (int)__PRETTY_FUNCTION__
);
39 _D(Lvl_Info
, "%s: accessing file", (int)__PRETTY_FUNCTION__
);
40 fh
= DOS
->Open(const_cast<char*>(getFileName()), MODE_OLDFILE
);
44 _D(Lvl_Debug
, "File opened OK");
45 FileInfoBlock
*fFib
= new FileInfoBlock
;
46 if (DOS
->ExamineFH(fh
, fFib
))
48 // problems with 64 bit (the size is signed -> problems with 32bit values)
49 uint32 tmp
= fFib
->fib_Size
;
50 size
= (unsigned long long)tmp
;
51 _D(Lvl_Info
, "File analysed, estimated size: %lu bytes (%08lx:%08lx)", size
, (size
>> 32)&0xffffffff, size
& 0xffffffff);
56 rc
= DT_UnableToOpenFile
;
62 _D(Lvl_Debug
, "File could not be opened");
63 rc
= DT_UnableToOpenFile
;
67 FileReader::~FileReader()
69 _D(Lvl_Info
, "%s: Killing item", (int)__PRETTY_FUNCTION__
);
77 BPTR
FileReader::getFile()
79 _D(Lvl_Info
, "%s: asked for a file handle (%08lx)", (int)__PRETTY_FUNCTION__
, (int)fh
);
83 const char* FileReader::getFileName()
85 return sFileName
.Data();
88 bool FileReader::readData(const IOptItem
*, void* buffer
, int len
)
93 short *b
= (short*)buffer
;
105 k
= udiv6432(size
, sectorSize
) - position
; // see how much left do we have
107 _D(Lvl_Info
, "%s: current file: %lx, buffer: %lx, length: %lds, position: %lds, total: %lds",
108 (int)__PRETTY_FUNCTION__
,
113 udiv6432(size
, sectorSize
));
115 if (k
>=(unsigned)len
) // if still more than requested
116 k
= len
; // read only as much as they need
118 memset(buffer
, 0, len
*sectorSize
); // clear memory
119 // and read as much as we have
126 k
= DOS
->Read(fh
, buffer
, k
);
131 if (k
< (unsigned)(len
* sectorSize
))
132 memset(&((char*)buffer
)[k
>>1], 0, (len
*sectorSize
) - k
);
145 *b
= ((*b
&0xff00)>>8)|((*b
&0xff)<<8);
153 void FileReader::setBlockSize(unsigned short size
)
156 _D(Lvl_Info
, "%s: New sector size: %ldb",
157 (int)__PRETTY_FUNCTION__
,
162 void FileReader::setLittleEndian(bool isLittle
)
164 _D(Lvl_Info
, "%s: module %s byte swappning",
165 (int)__PRETTY_FUNCTION__
,
166 isLittle
? (int)"does not require": (int)"requires");
170 bool FileReader::setUp()
174 DOS
->Seek(fh
, offset
, OFFSET_BEGINNING
);
175 _D(Lvl_Info
, "%s: set up complete. data offset: %ld bytes",
176 (int)__PRETTY_FUNCTION__
,
182 void FileReader::cleanUp()
184 _D(Lvl_Info
, "%s: cleanup complete", (int)__PRETTY_FUNCTION__
);
187 bool FileReader::checkFile(const char* sFileName
, EDtError
&rc
)
191 rc
= DT_UnableToOpenFile
;
192 fh
= DOS
->Open(const_cast<char*>(sFileName
), MODE_OLDFILE
);
202 void FileReader::setType(EDataType aType
)
204 _D(Lvl_Info
, "%s: Setting new data type to %ld", (int)__PRETTY_FUNCTION__
, aType
);
208 bool FileReader::fillOptItem(IOptItem
*item
)
210 _D(Lvl_Info
, "%s: Filling up disc item", (int)__PRETTY_FUNCTION__
);
211 item
->setDataType(dataType
);
212 item
->setDataBlockCount(getBlockCount());
215 // FIXME: malo tu wypelniamy
218 void FileReader::setDataOffset(uint64 lOffset
)
220 _D(Lvl_Info
, "%s: new data offset: %ld",
221 (int)__PRETTY_FUNCTION__
,
226 void FileReader::setDataSize(uint64 lSize
)
228 _D(Lvl_Info
, "%s: new data size: %lu (%08lx:%08lx)",
229 (int)__PRETTY_FUNCTION__
,
236 const char *FileReader::getTrackName()
238 _D(Lvl_Info
, "%s: Asking for track name (%s)", (int)__PRETTY_FUNCTION__
, (int)getFileName());
239 return getFileName();
242 void FileReader::dispose()
244 _D(Lvl_Info
, "%s: closing current track.", (int)__PRETTY_FUNCTION__
);
248 unsigned long FileReader::getBlockCount()
250 ASSERT(getBlockSize() != 0);
251 _D(Lvl_Info
, "%s: assuming size=%lub, sector=%ldb -> block count=%ld",
252 (int)__PRETTY_FUNCTION__
,
255 udiv6432(size
, getBlockSize()));
256 return udiv6432(size
, getBlockSize());
259 unsigned short FileReader::getBlockSize()
261 _D(Lvl_Info
, "%s: Reading block size (%ld)", (int)__PRETTY_FUNCTION__
, sectorSize
);
265 bool FileReader::isAudio()
267 _D(Lvl_Info
, "%s: Is Audio ? %ld", (int)__PRETTY_FUNCTION__
, dataType
== Data_Audio
);
268 if (dataType
== Data_Audio
)
273 bool FileReader::isData()
275 _D(Lvl_Info
, "%s: Is Data ? %ld", (int)__PRETTY_FUNCTION__
, dataType
!= Data_Audio
);
276 if (dataType
!= Data_Audio
)