Beispiel #1
0
inline bool FileLoader::readByte(int32_t &value)
{
	if(m_use_cache)
	{
		if(m_cache_index == NO_VALID_CACHE)
		{
			m_lastError = ERROR_CACHE_ERROR;
			return false;
		}

		if(m_cache_offset >= m_cached_data[m_cache_index].size)
		{
			int32_t pos = m_cache_offset + m_cached_data[m_cache_index].base;
			int32_t tmp = getCacheBlock(pos);
			if(tmp < 0)
				return false;

			m_cache_index = tmp;
			m_cache_offset = pos - m_cached_data[m_cache_index].base;
			if(m_cache_offset >= m_cached_data[m_cache_index].size)
				return false;
		}

		value = m_cached_data[m_cache_index].data[m_cache_offset];
		m_cache_offset++;
		return true;
	}

	value = fgetc(m_file);
	if(value != EOF)
		return true;

	m_lastError = ERROR_EOF;
	return false;
}
Beispiel #2
0
inline bool FileLoader::readBytes(uint8_t* buffer, uint32_t size, int32_t pos)
{
	//seek at pos
	uint32_t remain = size, bufferPos = 0;
	do {
		//prepare cache
		uint32_t i = getCacheBlock(pos);
		if (i == NO_VALID_CACHE) {
			return false;
		}

		m_cache_index = i;
		m_cache_offset = pos - m_cached_data[i].base;

		//get maximum read block size and calculate remaining bytes
		uint32_t reading = std::min<int32_t>(remain, m_cached_data[i].size - m_cache_offset);
		remain -= reading;

		//read it
		memcpy(buffer + bufferPos, m_cached_data[m_cache_index].data + m_cache_offset, reading);

		//update variables
		m_cache_offset += reading;
		bufferPos += reading;
		pos += reading;
	} while (remain > 0);
	return true;
}
Beispiel #3
0
inline bool FileLoader::safeSeek(uint32_t pos)
{
	uint32_t i = getCacheBlock(pos);
	if (i == NO_VALID_CACHE) {
		return false;
	}

	m_cache_index = i;
	m_cache_offset = pos - m_cached_data[i].base;
	return true;
}
Beispiel #4
0
inline bool FileLoader::readBytes(unsigned char* buffer, int32_t size, int32_t pos)
{
	if(m_use_cache)
	{
		//seek at pos
		uint32_t reading, remain = size, bufferPos = 0;
		do
		{
			//prepare cache
			uint32_t i = getCacheBlock(pos);
			if(i == NO_VALID_CACHE)
				return false;

			m_cache_index = i;
			m_cache_offset = pos - m_cached_data[i].base;
			
			//get maximum read block size and calculate remaining bytes
			reading = std::min<uint32_t>(remain, m_cached_data[i].size - m_cache_offset);
			remain = remain - reading;

			//read it
			memcpy(buffer + bufferPos, m_cached_data[m_cache_index].data + m_cache_offset, reading);

			//update variables
			m_cache_offset = m_cache_offset + reading;
			bufferPos = bufferPos + reading;
			pos = pos + reading;
		}
		while(remain > 0);
		return true;
	}
	else
	{
		if(fseek(m_file, pos, SEEK_SET))
		{
			m_lastError = ERROR_SEEK_ERROR;
			return false;
		}

		int32_t value = fread(buffer, 1, size, m_file);
		if(value != size)
		{
			m_lastError = ERROR_EOF;
			return false;
		}
		else
			return true;
	}
}
Beispiel #5
0
inline bool FileLoader::safeSeek(unsigned long pos)
{
	if(m_use_cache){
		unsigned long i = getCacheBlock(pos);
		if(i == NO_VALID_CACHE)
			return false;

		m_cache_index = i;
		m_cache_offset = pos - m_cached_data[i].base;
	}
	else{
		if(fseek(m_file, pos, SEEK_SET)){
			m_lastError = ERROR_SEEK_ERROR;
			return false;
		}
	}
	return true;
}