bool CExpressivePixelsStorage::SequenceRead()

in Firmware/ExpressivePixelsCore/EPXApp_CStorage.cpp [429:552]


bool CExpressivePixelsStorage::SequenceRead(const char *pszGUID, EXPRESSIVEPIXEL_SEQUENCE *pSequence, bool playFromFile)
{
	bool success = false;
	uint8_t skipLen = 0, guidLen = 0;
	uint32_t fileSize;
	
	if (m_bForceDirectFromFile)
		playFromFile = true;
	memset(pSequence, 0x00, sizeof(EXPRESSIVEPIXEL_SEQUENCE));
	
	void *pFile = SequenceOpen(pszGUID, (int *) &fileSize);
	if (pFile != NULL)
	{
		uint8_t tokenType;
		size_t bytesRead = 0;

		uint32_t originalFileSize = fileSize;
		while (fileSize > 0)
		{
			if (!ReadBytes(pFile, &fileSize, &tokenType, sizeof(tokenType)))
				break;

			switch (tokenType)
			{
			case SEQUENCETOKEN_NAMELEN:
				if (!ReadBytes(pFile, &fileSize, &pSequence->nameLen, sizeof(pSequence->nameLen)))
					goto error;
				break;
						
			case SEQUENCETOKEN_NAME:					
				pSequence->pszName = (char *)TMALLOC(pSequence->nameLen + 1);
				if (pSequence->pszName == NULL)
					goto error;
				if (!ReadBytes(pFile, &fileSize, (uint8_t *) pSequence->pszName, pSequence->nameLen))
					goto error;
				pSequence->pszName[pSequence->nameLen] = 0x00;
				break;

			case SEQUENCETOKEN_GUIDLEN:
				if (!ReadBytes(pFile, &fileSize, &guidLen, sizeof(guidLen)))
					goto error;
				break;
				
			case SEQUENCETOKEN_GUID:
				if (!ReadBytes(pFile, &fileSize, (uint8_t *)pSequence->szID, guidLen))
					goto error;
				pSequence->szID[guidLen] = 0x00;
				break;
														
			case SEQUENCETOKEN_PALLETESIZE:
				if (!ReadBytes(pFile, &fileSize, (uint8_t *)&pSequence->Meta.cbPallete, sizeof(pSequence->Meta.cbPallete)))
					goto error;
				break;

			case SEQUENCETOKEN_PALLETEBYTES:
				pSequence->pRAMPalette = (PALETTE_ENTRY *)TMALLOC(pSequence->Meta.cbPallete * sizeof(PALETTE_ENTRY));
				if (pSequence->pRAMPalette == NULL)
					goto error;				
				if (!ReadBytes(pFile, &fileSize, (uint8_t *) pSequence->pRAMPalette, pSequence->Meta.cbPallete * sizeof(PALETTE_ENTRY)))
					goto error;
				break;

			case SEQUENCETOKEN_FRAMESBYTELEN:
				if (!ReadBytes(pFile, &fileSize, (uint8_t *) &pSequence->Meta.cbFrames, sizeof(pSequence->Meta.cbFrames)))
					goto error;
				break;

			case SEQUENCETOKEN_FRAMESBYTES:
				if (playFromFile)
				{
					pSequence->frameBytesStartOffset = CStorage::Position(pFile);
					fileSize -= pSequence->Meta.cbFrames;
				}
				else
				{
					pSequence->pRAMFrames = (uint8_t *)TMALLOC(pSequence->Meta.cbFrames);
					if (pSequence->pRAMFrames == NULL)
						goto error;
					if (!ReadBytes(pFile, &fileSize, (uint8_t *) pSequence->pRAMFrames, pSequence->Meta.cbFrames))
						goto error;
				}
				break;

			case SEQUENCETOKEN_FRAMECOUNT:
				if (!ReadBytes(pFile, &fileSize, (uint8_t *)&pSequence->Meta.frameCount, sizeof(pSequence->Meta.frameCount)))
					goto error;
				break;

			case SEQUENCETOKEN_FRAMERATE:
				if (!ReadBytes(pFile, &fileSize, (uint8_t *)&pSequence->Meta.frameRate, sizeof(pSequence->Meta.frameRate)))
					goto error;
				break;

			case SEQUENCETOKEN_LOOPCOUNT:
				if (!ReadBytes(pFile, &fileSize, (uint8_t *)&pSequence->Meta.loopCount, sizeof(pSequence->Meta.loopCount)))
					goto error;
				break;
					
			case SEQUENCETOKEN_UTCTIMESTAMP:
				if (!ReadBytes(pFile, &fileSize, (uint8_t *)&pSequence->utcTimeStamp, sizeof(pSequence->utcTimeStamp)))
					goto error;
				break;
				
			default:
				if (skipLen > 0)
				{
					int currentPos = originalFileSize - fileSize;					
					currentPos += skipLen;
					CStorage::Seek(pFile, currentPos);						
					fileSize -= skipLen;					
					skipLen = 0;
				}
				break;
			}
		}
		success = true;
error:
		if (playFromFile)
			pSequence->pFile = pFile;
		else
			CStorage::Close(pFile);
	}
	return success;
}