File: top_title.c

package info (click to toggle)
zapping 0.10~cvs6-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 9,856 kB
  • ctags: 11,841
  • sloc: ansic: 111,154; asm: 11,770; sh: 9,812; xml: 2,742; makefile: 1,283; perl: 488
file content (410 lines) | stat: -rw-r--r-- 8,088 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
 *  libzvbi - Tables Of Pages
 *
 *  Copyright (C) 2004 Michael H. Schimek
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/* $Id: top_title.c,v 1.3 2005/06/28 00:59:18 mschimek Exp $ */

#include <stdlib.h>		/* malloc(), qsort() */
#include "conv.h"		/* _vbi3_strdup_locale_teletext() */
#include "lang.h"		/* vbi3_character_set_code */
#include "cache-priv.h"


#ifndef TOP_TITLE_LOG
#define TOP_TITLE_LOG 0
#endif

#define log(templ, args...)						\
do {									\
	if (TOP_TITLE_LOG)						\
		fprintf (stderr, templ , ##args);			\
} while (0)

/**
 * Frees all resources associate with this vbi3_top_title
 * except the structure itself.
 */
void
vbi3_top_title_destroy		(vbi3_top_title *	tt)
{
	assert (NULL != tt);

	vbi3_free (tt->title);

	CLEAR (*tt);
}

/**
 * @param dst A copy of @a src will be stored here.
 * @param src vbi3_top_title to be copied. If @c NULL this function
 *   has the same effect as vbi3_top_title_init().
 *
 * Creates a deep copy of @a src in @a dst, overwriting the data
 * previously stored at @a dst.
 *
 * @returns
 * @c FALSE on failure (out of memory).
 */
vbi3_bool
vbi3_top_title_copy		(vbi3_top_title *	dst,
				 const vbi3_top_title *	src)
{
	if (dst == src)
		return TRUE;

	assert (NULL != dst);

	if (src) {
		char *title;

		if (!(title = strdup (src->title)))
			return FALSE;

		*dst = *src;
		dst->title = title;
	} else {
		CLEAR (*dst);
	}

	return TRUE;
}

/**
 * @param tt vbi3_top_title to be initialized.
 *
 * Initializes (clears) a vbi3_top_title structure.
 */
void
vbi3_top_title_init		(vbi3_top_title *	tt)
{
	assert (NULL != tt);

	CLEAR (*tt);
}

/**
 * @param tt Array of vbi3_top_title structures, can be @c NULL.
 * @param n_elements Number of structures in the array.
 *
 * Deletes an array of vbi3_top_title structures and all resources
 * associated with it.
 */
void
vbi3_top_title_array_delete	(vbi3_top_title *	tt,
				 unsigned int		n_elements)
{
	unsigned int i;

	if (NULL == tt || 0 == n_elements)
		return;

	for (i = 0; i < n_elements; ++i)
		vbi3_top_title_destroy (tt + i);

	vbi3_free (tt);
}

/**
 * @internal
 */
const ait_title *
cache_network_get_ait_title	(cache_network *	cn,
				 cache_page **		ait_cp,
				 vbi3_pgno		pgno,
				 vbi3_subno		subno)
{
	unsigned int i;

	for (i = 0; i < 8; ++i) {
		cache_page *cp;
		const ait_title *ait;
		unsigned int j;

		if (cn->btt_link[i].function != PAGE_FUNCTION_AIT)
			continue;

		cp = _vbi3_cache_get_page (cn->cache, cn,
					  cn->btt_link[i].pgno,
					  cn->btt_link[i].subno, 0x3f7f);
		if (!cp) {
			log ("...top ait page %x not cached\n",
			     cn->btt_link[i].pgno);
			continue;
		} else if (cp->function != PAGE_FUNCTION_AIT) {
			log ("...no ait page %x\n", cp->pgno);
			cache_page_unref (cp);
			continue;
		}

		ait = cp->data.ait.title;

		for (j = 0; j < N_ELEMENTS (cp->data.ait.title); ++j) {
			if (ait->page.pgno == pgno
			    && (VBI3_ANY_SUBNO == subno
				|| ait->page.subno == subno)) {
				*ait_cp = cp;
				return ait;
			}

			++ait;
		}

		cache_page_unref (cp);
	}

	*ait_cp = NULL;

	return NULL;
}

static vbi3_bool
_vbi3_top_title_from_ait_title	
				(vbi3_top_title *	tt,
				 const cache_network *	cn,
				 const ait_title *	ait,
				 const vbi3_character_set *cs)
{
	const page_stat *ps;
	char *title;

	title = _vbi3_strdup_locale_teletext
		(ait->text, N_ELEMENTS (ait->text), cs);

	if (!title) {
		/* Make vbi3_top_title_destroy() safe. */
		vbi3_top_title_init (tt);
		return FALSE;
	}

	tt->title = title;

	tt->pgno = ait->page.pgno;
	tt->subno = ait->page.subno;

	ps = cache_network_const_page_stat (cn, ait->page.pgno);

	tt->group = (VBI3_TOP_GROUP == ps->page_type);

	return TRUE;
}

/**
 * @internal
 */
vbi3_bool
cache_network_get_top_title	(cache_network *	cn,
				 vbi3_top_title *	tt,
				 vbi3_pgno		pgno,
				 vbi3_subno		subno)
{
	cache_page *ait_cp;
	const ait_title *ait;
	const vbi3_character_set *char_set[2];
	vbi3_bool r;

	assert (NULL != cn);
	assert (NULL != tt);

	if (!(ait = cache_network_get_ait_title (cn, &ait_cp, pgno, subno))) {
		/* Make vbi3_top_title_destroy() safe. */
		vbi3_top_title_init (tt);
		return FALSE;
	}

	if (NO_PAGE (ait->page.pgno)) {
		cache_page_unref (ait_cp);
		vbi3_top_title_init (tt);
		return FALSE;
	}

	_vbi3_character_set_init (char_set,
				 /* default en */ 0,
				 /* default en */ 0,
				 /* extension */ NULL,
				 ait_cp);

	r = _vbi3_top_title_from_ait_title (tt, cn, ait, char_set[0]);

	cache_page_unref (ait_cp);

	return r;
}

/**
 */
vbi3_bool
vbi3_cache_get_top_title		(vbi3_cache *		ca,
				 vbi3_top_title *	tt,
				 const vbi3_network *	nk,
				 vbi3_pgno		pgno,
				 vbi3_subno		subno)
{
	cache_network *cn;
	vbi3_bool r;

	assert (NULL != ca);
	assert (NULL != tt);
	assert (NULL != nk);

	if (!(cn = _vbi3_cache_get_network (ca, nk))) {
		/* Make vbi3_top_title_destroy() safe. */
		vbi3_top_title_init (tt);
		return FALSE;
	}

	r = cache_network_get_top_title (cn, tt, pgno, subno);

	cache_network_unref (cn);
	
	return r;
}

static int
top_title_cmp			(const void *		p1,
				 const void *		p2)
{
	const vbi3_top_title *tt1 = p1;
	const vbi3_top_title *tt2 = p2;

	if (tt1->pgno == tt2->pgno)
		return tt2->pgno - tt1->pgno;
	else
		return tt2->subno - tt2->subno;
}

/**
 * @internal
 */
vbi3_top_title *
cache_network_get_top_titles	(cache_network *	cn,
				 unsigned int *		n_elements)
{
	vbi3_top_title *tt;
	unsigned int size;
	unsigned int capacity;
	unsigned int i;

	assert (NULL != cn);
	assert (NULL != n_elements);

	capacity = 64;
	size = 0;

	if (!(tt = vbi3_malloc (capacity * sizeof (*tt))))
		return NULL;

	for (i = 0; i < 8; ++i) {
		cache_page *cp;
		const ait_title *ait;
		const vbi3_character_set *char_set[2];
		unsigned int j;

		if (cn->btt_link[i].function != PAGE_FUNCTION_AIT)
			continue;

		cp = _vbi3_cache_get_page (cn->cache, cn,
					  cn->btt_link[i].pgno,
					  cn->btt_link[i].subno, 0x3f7f);
		if (!cp) {
			log ("...top ait page %x not cached\n",
			     cn->btt_link[i].pgno);
			continue;
		} else if (cp->function != PAGE_FUNCTION_AIT) {
			log ("...no ait page %x\n", cp->pgno);
			cache_page_unref (cp);
			continue;
		}

		_vbi3_character_set_init (char_set,
					 /* default en */ 0,
					 /* default en */ 0,
					 /* extension */ NULL,
					 cp);

		ait = cp->data.ait.title;

		for (j = 0; j < N_ELEMENTS (cp->data.ait.title); ++j) {
			if (NO_PAGE (ait->page.pgno)) {
				++ait;
				continue;
			}

			if (size + 1 >= capacity) {
				vbi3_top_title *tt1;
				unsigned long n;

				n = sizeof (*tt) * 2 * capacity;
				if (!(tt1 = vbi3_realloc (tt, n))) {
					vbi3_top_title_array_delete (tt, size);
					cache_page_unref (cp);
					return NULL;
				}

				tt = tt1;
				capacity *= 2;
			}

			if (_vbi3_top_title_from_ait_title
			    (tt + size, cn, ait, char_set[0])) {
				++size;
			}

			++ait;
		}

		cache_page_unref (cp);
	}

	/* Last element empty. */
	vbi3_top_title_init (tt + size);

	if (0) {
		/* Make sure we're sorted by pgno. */
		qsort (tt, size, sizeof (*tt), top_title_cmp);
	}

	*n_elements = size;

	return tt;
}

/**
 */
vbi3_top_title *
vbi3_cache_get_top_titles	(vbi3_cache *		ca,
				 const vbi3_network *	nk,
				 unsigned int *		n_elements)
{
	cache_network *cn;
	vbi3_top_title *tt;

	assert (NULL != ca);
	assert (NULL != nk);
	assert (NULL != n_elements);

	*n_elements = 0;

	if (!(cn = _vbi3_cache_get_network (ca, nk)))
		return NULL;

	tt = cache_network_get_top_titles (cn, n_elements);

	cache_network_unref (cn);

	return tt;
}