2
* Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4
* This file is part of libass.
6
* libass 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
* libass 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 along
17
* with libass; if not, write to the Free Software Foundation, Inc.,
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29
#include "ass_library.h"
31
#include "ass_utils.h"
33
int mystrtoi(char **p, int *res)
37
temp_res = strtod(*p, p);
38
*res = (int) (temp_res + (temp_res > 0 ? 0.5 : -0.5));
45
int mystrtoll(char **p, long long *res)
49
temp_res = strtod(*p, p);
50
*res = (int) (temp_res + (temp_res > 0 ? 0.5 : -0.5));
57
int mystrtou32(char **p, int base, uint32_t *res)
60
*res = strtoll(*p, p, base);
67
int mystrtod(char **p, double *res)
77
int strtocolor(ass_library_t *library, char **q, uint32_t *res)
86
ass_msg(library, MSGL_DBG2, "suspicious color format: \"%s\"\n", p);
88
if (*p == 'H' || *p == 'h') {
90
result = mystrtou32(&p, 16, &color);
92
result = mystrtou32(&p, 0, &color);
96
unsigned char *tmp = (unsigned char *) (&color);
113
// Return a boolean value for a string
114
char parse_bool(char *str)
116
while (*str == ' ' || *str == '\t')
118
if (!strncasecmp(str, "yes", 3))
120
else if (strtol(str, NULL, 10) > 0)
125
void ass_msg(ass_library_t *priv, int lvl, char *fmt, ...)
129
priv->msg_callback(lvl, fmt, va, priv->msg_callback_data);
133
unsigned ass_utf8_get_char(char **str)
135
uint8_t *strp = (uint8_t *) * str;
136
unsigned c = *strp++;
137
unsigned mask = 0x80;
143
if (len <= 0 || len > 4)
146
while ((*strp & 0xc0) == 0x80) {
149
c = (c << 6) | (*strp++ & 0x3f);
153
*str = (char *) strp;
157
strp = (uint8_t *) * str;
159
*str = (char *) strp;
164
void *ass_guess_buffer_cp(ass_library_t *library, unsigned char *buffer,
165
int buflen, char *preferred_language,
168
const char **languages;
170
EncaAnalyser analyser;
171
EncaEncoding encoding;
172
char *detected_sub_cp = NULL;
175
languages = enca_get_languages(&langcnt);
176
ass_msg(library, MSGL_V, "ENCA supported languages");
177
for (i = 0; i < langcnt; i++) {
178
ass_msg(library, MSGL_V, "lang %s", languages[i]);
181
for (i = 0; i < langcnt; i++) {
184
if (strcasecmp(languages[i], preferred_language) != 0)
186
analyser = enca_analyser_alloc(languages[i]);
187
encoding = enca_analyse_const(analyser, buffer, buflen);
188
tmp = enca_charset_name(encoding.charset, ENCA_NAME_STYLE_ICONV);
189
if (tmp && encoding.charset != ENCA_CS_UNKNOWN) {
190
detected_sub_cp = strdup(tmp);
191
ass_msg(library, MSGL_INFO, "ENCA detected charset: %s", tmp);
193
enca_analyser_free(analyser);
198
if (!detected_sub_cp) {
199
detected_sub_cp = strdup(fallback);
200
ass_msg(library, MSGL_INFO,
201
"ENCA detection failed: fallback to %s", fallback);
204
return detected_sub_cp;