예제 #1
0
void opentv_read_titles (unsigned char *data, unsigned int length, bool huffman_debug)
{
	epgdb_title_t *title;
	unsigned short int channel_id	= (data[3] << 8) | data[4];
	unsigned short int mjd_time		= (data[8] << 8) | data[9];
	
	if ((channel_id > 0) && (mjd_time > 0))
	{
		unsigned int offset = 10;
		
		while ((offset + 11) < length)
		{
			unsigned short int	event_id;
			unsigned char		description_length;
			unsigned short int	packet_length = ((data[offset + 2] & 0x0f) << 8) | data[offset + 3];
			
			if ((data[offset + 4] != 0xb5) || ((packet_length + offset) > length)) break;
			
			event_id = (data[offset] << 8) | data[offset + 1];
			offset += 4;
			description_length = data[offset + 1] - 7;
			
			if ((offset + 9 + description_length) > length) break;
			
			if (channels[channel_id] != NULL)
			{
				char tmp[256];
				
				/* prepare struct */
				title = _malloc (sizeof (epgdb_title_t));
				title->event_id = event_id;
				title->start_time = ((mjd_time - 40587) * 86400) + ((data[offset + 2] << 9) | (data[offset + 3] << 1));
				title->mjd = mjd_time;
				title->length = ((data[offset + 4] << 9) | (data[offset + 5] << 1));
				title->genre_id = data[offset + 6];
				title->flags = 0;
				//title->genre_sub_id = 0;
				title->iso_639_1 = 'e';		// TODO: load language from provider configuration
				title->iso_639_2 = 'n';
				title->iso_639_3 = 'g';
				title = epgdb_titles_add (channels[channel_id], title);
				
				if (!huffman_decode (data + offset + 9, description_length, tmp, 256, huffman_debug))
					tmp[0] = '\0';
				
				if (huffman_debug)
				{
					char mtime[20];
					struct tm *loctime = localtime ((time_t*)&title->start_time);
					printf ("Nid: %x Tsid: %x Sid: %x\n", channels[channel_id]->nid, channels[channel_id]->tsid, channels[channel_id]->sid);
					strftime (mtime, 20, "%d/%m/%Y %H:%M", loctime);
					printf ("Start time: %s\n", mtime);
				}
				
				epgdb_titles_set_description (title, tmp);
				tit_count++;
			}

			offset += packet_length;
		}
	}
}
bool dbmerge_merge (FILE *fd_h, FILE *fd_d, void(*progress_callback)(int, int))
{
	char tmp[256];
	unsigned char revision;
	int channels_count, i, j, aliases_groups_count, indexes_count;
	time_t now = time (NULL);

	/* read headers */
	fread (tmp, strlen (MAGIC_HEADERS), 1, fd_h);
	if (memcmp (tmp, MAGIC_HEADERS, strlen (MAGIC_HEADERS)) != 0)
	{
		log_add ("Bad magic header");
		return false;
	}
	fread (&revision, sizeof (unsigned char), 1, fd_h);
	if (revision != DB_REVISION)
	{
		log_add ("Bad db revision");
		return false;
	}

	fseek (fd_h, 22, SEEK_SET);

	fread (&channels_count, sizeof (int), 1, fd_h);
	for (i=0; i<channels_count; i++)
	{
		int titles_count;
		epgdb_channel_t *tmp;
		epgdb_channel_t *channel, *channel_tmp;

		channel_tmp = _malloc (sizeof (epgdb_channel_t));
		fread (channel_tmp, sizeof (epgdb_channel_header_t), 1, fd_h);
		channel = epgdb_channels_add (channel_tmp->nid, channel_tmp->tsid, channel_tmp->sid);
		_free (channel_tmp);

		fread (&titles_count, sizeof (int), 1, fd_h);

		for (j=0; j<titles_count; j++)
		{
			char *desc, *ldesc;
			epgdb_title_t *title = _malloc (sizeof (epgdb_title_t));
			fread (title, sizeof (epgdb_title_header_t), 1, fd_h);

			desc = dbmerge_read_description (fd_d, title);
			ldesc = dbmerge_read_long_description (fd_d, title);

			title = epgdb_titles_add (channel, title);

			epgdb_titles_set_description(title, desc);
			epgdb_titles_set_long_description(title, ldesc);

			_free (desc);
			_free (ldesc);
		}

		if (progress_callback)
			progress_callback (i, channels_count);
	}


	return true;
}