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
= ass_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
= ass_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
)
70 *res
= ass_strtod(*p
, p
);
77 int strtocolor(ASS_Library
*library
, char **q
, uint32_t *res
, int hex
)
82 int base
= hex
? 16 : 10;
87 ass_msg(library
, MSGL_DBG2
, "suspicious color format: \"%s\"\n", p
);
89 if (*p
== 'H' || *p
== 'h') {
91 result
= mystrtou32(&p
, 16, &color
);
93 result
= mystrtou32(&p
, base
, &color
);
97 unsigned char *tmp
= (unsigned char *) (&color
);
114 // Return a boolean value for a string
115 char parse_bool(char *str
)
117 while (*str
== ' ' || *str
== '\t')
119 if (!strncasecmp(str
, "yes", 3))
121 else if (strtol(str
, NULL
, 10) > 0)
126 void ass_msg(ASS_Library
*priv
, int lvl
, char *fmt
, ...)
130 priv
->msg_callback(lvl
, fmt
, va
, priv
->msg_callback_data
);
134 unsigned ass_utf8_get_char(char **str
)
136 uint8_t *strp
= (uint8_t *) * str
;
137 unsigned c
= *strp
++;
138 unsigned mask
= 0x80;
144 if (len
<= 0 || len
> 4)
147 while ((*strp
& 0xc0) == 0x80) {
150 c
= (c
<< 6) | (*strp
++ & 0x3f);
154 *str
= (char *) strp
;
158 strp
= (uint8_t *) * str
;
160 *str
= (char *) strp
;
165 void *ass_guess_buffer_cp(ASS_Library
*library
, unsigned char *buffer
,
166 int buflen
, char *preferred_language
,
169 const char **languages
;
171 EncaAnalyser analyser
;
172 EncaEncoding encoding
;
173 char *detected_sub_cp
= NULL
;
176 languages
= enca_get_languages(&langcnt
);
177 ass_msg(library
, MSGL_V
, "ENCA supported languages");
178 for (i
= 0; i
< langcnt
; i
++) {
179 ass_msg(library
, MSGL_V
, "lang %s", languages
[i
]);
182 for (i
= 0; i
< langcnt
; i
++) {
185 if (strcasecmp(languages
[i
], preferred_language
) != 0)
187 analyser
= enca_analyser_alloc(languages
[i
]);
188 encoding
= enca_analyse_const(analyser
, buffer
, buflen
);
189 tmp
= enca_charset_name(encoding
.charset
, ENCA_NAME_STYLE_ICONV
);
190 if (tmp
&& encoding
.charset
!= ENCA_CS_UNKNOWN
) {
191 detected_sub_cp
= strdup(tmp
);
192 ass_msg(library
, MSGL_INFO
, "ENCA detected charset: %s", tmp
);
194 enca_analyser_free(analyser
);
199 if (!detected_sub_cp
) {
200 detected_sub_cp
= strdup(fallback
);
201 ass_msg(library
, MSGL_INFO
,
202 "ENCA detection failed: fallback to %s", fallback
);
205 return detected_sub_cp
;