options: Make dynamic dup hack work with new options
[mplayer.git] / libass / ass_library.c
blobcda928704577745fb6946a79fbc1277cf8a2cbab
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*-
2 // vim:ts=8:sw=8:noet:ai:
3 /*
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "ass.h"
27 #include "ass_library.h"
30 ass_library_t* ass_library_init(void)
32 return calloc(1, sizeof(ass_library_t));
35 void ass_library_done(ass_library_t* priv)
37 if (priv) {
38 ass_set_fonts_dir(priv, NULL);
39 ass_set_style_overrides(priv, NULL);
40 ass_clear_fonts(priv);
41 free(priv);
45 void ass_set_fonts_dir(ass_library_t* priv, const char* fonts_dir)
47 if (priv->fonts_dir)
48 free(priv->fonts_dir);
50 priv->fonts_dir = fonts_dir ? strdup(fonts_dir) : 0;
53 void ass_set_extract_fonts(ass_library_t* priv, int extract)
55 priv->extract_fonts = !!extract;
58 void ass_set_style_overrides(ass_library_t* priv, char** list)
60 char** p;
61 char** q;
62 int cnt;
64 if (priv->style_overrides) {
65 for (p = priv->style_overrides; *p; ++p)
66 free(*p);
67 free(priv->style_overrides);
70 if (!list) return;
72 for (p = list, cnt = 0; *p; ++p, ++cnt) {}
74 priv->style_overrides = malloc((cnt + 1) * sizeof(char*));
75 for (p = list, q = priv->style_overrides; *p; ++p, ++q)
76 *q = strdup(*p);
77 priv->style_overrides[cnt] = NULL;
80 static void grow_array(void **array, int nelem, size_t elsize)
82 if (!(nelem & 31))
83 *array = realloc(*array, (nelem + 32) * elsize);
86 void ass_add_font(ass_library_t* priv, char* name, char* data, int size)
88 int idx = priv->num_fontdata;
89 if (!name || !data || !size)
90 return;
91 grow_array((void**)&priv->fontdata, priv->num_fontdata, sizeof(*priv->fontdata));
93 priv->fontdata[idx].name = strdup(name);
95 priv->fontdata[idx].data = malloc(size);
96 memcpy(priv->fontdata[idx].data, data, size);
98 priv->fontdata[idx].size = size;
100 priv->num_fontdata ++;
103 void ass_clear_fonts(ass_library_t* priv)
105 int i;
106 for (i = 0; i < priv->num_fontdata; ++i) {
107 free(priv->fontdata[i].name);
108 free(priv->fontdata[i].data);
110 free(priv->fontdata);
111 priv->fontdata = NULL;
112 priv->num_fontdata = 0;