Enable asyncio.library.
[AROS-Contrib.git] / FryingPan / DTLib / FileReader.cpp
bloba2737af8f0b988f8f6012e3ec0909f75a882aaea
1 /*
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
4 *
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.
9 *
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)
28 _ND("Reader");
29 _D(Lvl_Info, "%s: Setting up defaults", (int)__PRETTY_FUNCTION__);
30 sFileName = sFName;
31 fh = 0;
32 position = 0;
33 size = 0;
34 offset = 0;
35 isLE = true;
36 sectorSize = 2352;
37 rc = DT_OK;
39 _D(Lvl_Info, "%s: accessing file", (int)__PRETTY_FUNCTION__);
40 fh = DOS->Open(const_cast<char*>(getFileName()), MODE_OLDFILE);
41 ASSERT(fh != 0);
42 if (0 != fh)
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);
53 else
55 size = 0;
56 rc = DT_UnableToOpenFile;
58 delete fFib;
60 else
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__);
70 _ED();
71 if (0 != fh)
73 DOS->Close(fh);
77 BPTR FileReader::getFile()
79 _D(Lvl_Info, "%s: asked for a file handle (%08lx)", (int)__PRETTY_FUNCTION__, (int)fh);
80 return fh;
83 const char* FileReader::getFileName()
85 return sFileName.Data();
88 bool FileReader::readData(const IOptItem*, void* buffer, int len)
90 ASSERT(buffer != 0);
91 ASSERT(fh != 0);
93 short *b = (short*)buffer;
94 uint64 k;
97 if (0 >= len)
98 return true;
100 if (0 == buffer)
102 return false;
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__,
109 (int)fh,
110 (int)buffer,
111 len,
112 position,
113 udiv6432(size, sectorSize));
115 if (k>=(unsigned)len) // if still more than requested
116 k = len; // read only as much as they need
117 else // otherwise
118 memset(buffer, 0, len*sectorSize); // clear memory
119 // and read as much as we have
121 k *= sectorSize;
123 if (0 != k)
125 if (fh)
126 k = DOS->Read(fh, buffer, k);
127 else
128 k = 0;
130 k &= ~1;
131 if (k < (unsigned)(len * sectorSize))
132 memset(&((char*)buffer)[k>>1], 0, (len*sectorSize) - k);
134 position += len;
136 if (k <= 0)
137 return true;
139 ASSERT(b == buffer);
141 if (false == isLE)
143 while (k > 0)
145 *b = ((*b&0xff00)>>8)|((*b&0xff)<<8);
146 k-=2;
147 b++;
150 return true;
153 void FileReader::setBlockSize(unsigned short size)
155 ASSERT(size != 0);
156 _D(Lvl_Info, "%s: New sector size: %ldb",
157 (int)__PRETTY_FUNCTION__,
158 size);
159 sectorSize = size;
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");
167 isLE = isLittle;
170 bool FileReader::setUp()
172 ASSERT(fh != 0);
174 DOS->Seek(fh, offset, OFFSET_BEGINNING);
175 _D(Lvl_Info, "%s: set up complete. data offset: %ld bytes",
176 (int)__PRETTY_FUNCTION__,
177 offset);
178 position = 0;
179 return true;
182 void FileReader::cleanUp()
184 _D(Lvl_Info, "%s: cleanup complete", (int)__PRETTY_FUNCTION__);
187 bool FileReader::checkFile(const char* sFileName, EDtError &rc)
189 BPTR fh;
191 rc = DT_UnableToOpenFile;
192 fh = DOS->Open(const_cast<char*>(sFileName), MODE_OLDFILE);
193 if (0 != fh)
195 DOS->Close(fh);
196 rc = DT_OK;
197 return true;
199 return false;
202 void FileReader::setType(EDataType aType)
204 _D(Lvl_Info, "%s: Setting new data type to %ld", (int)__PRETTY_FUNCTION__, aType);
205 dataType = 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());
214 return true;
215 // FIXME: malo tu wypelniamy
218 void FileReader::setDataOffset(uint64 lOffset)
220 _D(Lvl_Info, "%s: new data offset: %ld",
221 (int)__PRETTY_FUNCTION__,
222 lOffset);
223 offset = lOffset;
226 void FileReader::setDataSize(uint64 lSize)
228 _D(Lvl_Info, "%s: new data size: %lu (%08lx:%08lx)",
229 (int)__PRETTY_FUNCTION__,
230 lSize,
231 lSize >> 32,
232 lSize & 0xffffffff);
233 size = lSize;
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__);
245 delete this;
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__,
253 size,
254 getBlockSize(),
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);
262 return sectorSize;
265 bool FileReader::isAudio()
267 _D(Lvl_Info, "%s: Is Audio ? %ld", (int)__PRETTY_FUNCTION__, dataType == Data_Audio);
268 if (dataType == Data_Audio)
269 return true;
270 return false;
273 bool FileReader::isData()
275 _D(Lvl_Info, "%s: Is Data ? %ld", (int)__PRETTY_FUNCTION__, dataType != Data_Audio);
276 if (dataType != Data_Audio)
277 return true;
278 return false;
281 // vim: ts=3 et