Example #1
0
FileData *File::read(int64 size)
{
	bool isopen = isOpen();

	if (!isopen && !open(MODE_READ))
		throw love::Exception("Could not read file %s.", getFilename().c_str());

	int64 max = getSize();
	int64 cur = tell();
	size = (size == ALL) ? max : size;

	if (size < 0)
		throw love::Exception("Invalid read size.");

	// Clamping because the file offset may be in a weird position.
	if (cur < 0)
		cur = 0;
	else if (cur > max)
		cur = max;

	if (cur + size > max)
		size = max - cur;

	FileData *fileData = new FileData(size, getFilename());
	int64 bytesRead = read(fileData->getData(), size);

	if (bytesRead < 0 || (bytesRead == 0 && bytesRead != size))
	{
		delete fileData;
		throw love::Exception("Could not read from file.");
	}

	if (bytesRead < size)
	{
		FileData *tmpFileData = new FileData(bytesRead, getFilename());
		memcpy(tmpFileData->getData(), fileData->getData(), (size_t) bytesRead);
		fileData->release();
		fileData = tmpFileData;
	}

	if (!isopen)
		close();

	return fileData;
}
Example #2
0
	Data * File::read(int size)
	{
		bool isOpen = (file != 0);

		if(!isOpen && !open(READ))
			throw love::Exception("Could not read file %s.", filename.c_str());

		int max = (int)PHYSFS_fileLength(file);
		size = (size == ALL) ? max : size;
		size = (size > max) ? max : size;

		FileData * fileData = new FileData(size, getFilename());

		read(fileData->getData(), size);

		if(!isOpen)
			close();

		return fileData;
	}