| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 28 |
# include "config.h" |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
#include <vlc_common.h> |
|---|
| 32 |
#include <vlc_charset.h> |
|---|
| 33 |
#include "libvlc.h" |
|---|
| 34 |
|
|---|
| 35 |
#include <assert.h> |
|---|
| 36 |
|
|---|
| 37 |
#include <stdio.h> |
|---|
| 38 |
#include <stdarg.h> |
|---|
| 39 |
#include <stdlib.h> |
|---|
| 40 |
#include <errno.h> |
|---|
| 41 |
#include <sys/types.h> |
|---|
| 42 |
#ifdef HAVE_DIRENT_H |
|---|
| 43 |
# include <dirent.h> |
|---|
| 44 |
#endif |
|---|
| 45 |
#ifdef UNDER_CE |
|---|
| 46 |
# include <tchar.h> |
|---|
| 47 |
#endif |
|---|
| 48 |
#ifdef HAVE_SYS_STAT_H |
|---|
| 49 |
# include <sys/stat.h> |
|---|
| 50 |
#endif |
|---|
| 51 |
#ifdef HAVE_FCNTL_H |
|---|
| 52 |
# include <fcntl.h> |
|---|
| 53 |
#endif |
|---|
| 54 |
#ifdef WIN32 |
|---|
| 55 |
# include <io.h> |
|---|
| 56 |
#else |
|---|
| 57 |
# include <unistd.h> |
|---|
| 58 |
#endif |
|---|
| 59 |
|
|---|
| 60 |
#ifndef HAVE_LSTAT |
|---|
| 61 |
# define lstat( a, b ) stat(a, b) |
|---|
| 62 |
#endif |
|---|
| 63 |
|
|---|
| 64 |
#ifdef __APPLE__ |
|---|
| 65 |
|
|---|
| 66 |
# define ASSUME_UTF8 1 |
|---|
| 67 |
#endif |
|---|
| 68 |
|
|---|
| 69 |
#if defined (ASSUME_UTF8) |
|---|
| 70 |
|
|---|
| 71 |
#elif defined (WIN32) || defined (UNDER_CE) |
|---|
| 72 |
# define USE_MB2MB 1 |
|---|
| 73 |
#elif defined (HAVE_ICONV) |
|---|
| 74 |
# define USE_ICONV 1 |
|---|
| 75 |
#else |
|---|
| 76 |
# error No UTF8 charset conversion implemented on this platform! |
|---|
| 77 |
#endif |
|---|
| 78 |
|
|---|
| 79 |
#if defined (USE_ICONV) |
|---|
| 80 |
# include <langinfo.h> |
|---|
| 81 |
static char charset[sizeof ("CSISO11SWEDISHFORNAMES")] = ""; |
|---|
| 82 |
|
|---|
| 83 |
static void find_charset_once (void) |
|---|
| 84 |
{ |
|---|
| 85 |
strlcpy (charset, nl_langinfo (CODESET), sizeof (charset)); |
|---|
| 86 |
if (!strcasecmp (charset, "ASCII") |
|---|
| 87 |
|| !strcasecmp (charset, "ANSI_X3.4-1968")) |
|---|
| 88 |
strcpy (charset, "UTF-8"); |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
static int find_charset (void) |
|---|
| 92 |
{ |
|---|
| 93 |
static pthread_once_t once = PTHREAD_ONCE_INIT; |
|---|
| 94 |
pthread_once (&once, find_charset_once); |
|---|
| 95 |
return !strcasecmp (charset, "UTF-8"); |
|---|
| 96 |
} |
|---|
| 97 |
#endif |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
static char *locale_fast (const char *string, bool from) |
|---|
| 101 |
{ |
|---|
| 102 |
#if defined (USE_ICONV) |
|---|
| 103 |
if (find_charset ()) |
|---|
| 104 |
return (char *)string; |
|---|
| 105 |
|
|---|
| 106 |
vlc_iconv_t hd = vlc_iconv_open (from ? "UTF-8" : charset, |
|---|
| 107 |
from ? charset : "UTF-8"); |
|---|
| 108 |
if (hd == (vlc_iconv_t)(-1)) |
|---|
| 109 |
return NULL; |
|---|
| 110 |
|
|---|
| 111 |
const char *iptr = string; |
|---|
| 112 |
size_t inb = strlen (string); |
|---|
| 113 |
size_t outb = inb * 6 + 1; |
|---|
| 114 |
char output[outb], *optr = output; |
|---|
| 115 |
|
|---|
| 116 |
if (string == NULL) |
|---|
| 117 |
return NULL; |
|---|
| 118 |
|
|---|
| 119 |
while (vlc_iconv (hd, &iptr, &inb, &optr, &outb) == (size_t)(-1)) |
|---|
| 120 |
{ |
|---|
| 121 |
*optr++ = '?'; |
|---|
| 122 |
outb--; |
|---|
| 123 |
iptr++; |
|---|
| 124 |
inb--; |
|---|
| 125 |
vlc_iconv (hd, NULL, NULL, NULL, NULL); |
|---|
| 126 |
} |
|---|
| 127 |
*optr = '\0'; |
|---|
| 128 |
vlc_iconv_close (hd); |
|---|
| 129 |
|
|---|
| 130 |
assert (inb == 0); |
|---|
| 131 |
assert (*iptr == '\0'); |
|---|
| 132 |
assert (*optr == '\0'); |
|---|
| 133 |
assert (strlen (output) == (size_t)(optr - output)); |
|---|
| 134 |
return strdup (output); |
|---|
| 135 |
#elif defined (USE_MB2MB) |
|---|
| 136 |
char *out; |
|---|
| 137 |
int len; |
|---|
| 138 |
|
|---|
| 139 |
if (string == NULL) |
|---|
| 140 |
return NULL; |
|---|
| 141 |
|
|---|
| 142 |
len = 1 + MultiByteToWideChar (from ? CP_ACP : CP_UTF8, |
|---|
| 143 |
0, string, -1, NULL, 0); |
|---|
| 144 |
wchar_t wide[len]; |
|---|
| 145 |
|
|---|
| 146 |
MultiByteToWideChar (from ? CP_ACP : CP_UTF8, 0, string, -1, wide, len); |
|---|
| 147 |
len = 1 + WideCharToMultiByte (from ? CP_UTF8 : CP_ACP, 0, wide, -1, |
|---|
| 148 |
NULL, 0, NULL, NULL); |
|---|
| 149 |
out = malloc (len); |
|---|
| 150 |
if (out == NULL) |
|---|
| 151 |
return NULL; |
|---|
| 152 |
|
|---|
| 153 |
WideCharToMultiByte (from ? CP_UTF8 : CP_ACP, 0, wide, -1, out, len, |
|---|
| 154 |
NULL, NULL); |
|---|
| 155 |
return out; |
|---|
| 156 |
#else |
|---|
| 157 |
(void)from; |
|---|
| 158 |
return (char *)string; |
|---|
| 159 |
#endif |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
static inline char *locale_dup (const char *string, bool from) |
|---|
| 164 |
{ |
|---|
| 165 |
assert( string ); |
|---|
| 166 |
|
|---|
| 167 |
#if defined (USE_ICONV) |
|---|
| 168 |
if (find_charset ()) |
|---|
| 169 |
return strdup (string); |
|---|
| 170 |
return locale_fast (string, from); |
|---|
| 171 |
#elif defined (USE_MB2MB) |
|---|
| 172 |
return locale_fast (string, from); |
|---|
| 173 |
#else |
|---|
| 174 |
(void)from; |
|---|
| 175 |
return strdup (string); |
|---|
| 176 |
#endif |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
void LocaleFree (const char *str) |
|---|
| 184 |
{ |
|---|
| 185 |
#if defined (USE_ICONV) |
|---|
| 186 |
if (!find_charset ()) |
|---|
| 187 |
free ((char *)str); |
|---|
| 188 |
#elif defined (USE_MB2MB) |
|---|
| 189 |
free ((char *)str); |
|---|
| 190 |
#else |
|---|
| 191 |
(void)str; |
|---|
| 192 |
#endif |
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 205 |
char *FromLocale (const char *locale) |
|---|
| 206 |
{ |
|---|
| 207 |
return locale_fast (locale, true); |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
char *FromLocaleDup (const char *locale) |
|---|
| 220 |
{ |
|---|
| 221 |
return locale_dup (locale, true); |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
char *ToLocale (const char *utf8) |
|---|
| 235 |
{ |
|---|
| 236 |
return locale_fast (utf8, false); |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 |
char *ToLocaleDup (const char *utf8) |
|---|
| 250 |
{ |
|---|
| 251 |
return locale_dup (utf8, false); |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
int utf8_open (const char *filename, int flags, mode_t mode) |
|---|
| 264 |
{ |
|---|
| 265 |
#if defined (WIN32) || defined (UNDER_CE) |
|---|
| 266 |
if (GetVersion() < 0x80000000) |
|---|
| 267 |
{ |
|---|
| 268 |
|
|---|
| 269 |
wchar_t wpath[MAX_PATH + 1]; |
|---|
| 270 |
|
|---|
| 271 |
if (!MultiByteToWideChar (CP_UTF8, 0, filename, -1, wpath, MAX_PATH)) |
|---|
| 272 |
{ |
|---|
| 273 |
errno = ENOENT; |
|---|
| 274 |
return -1; |
|---|
| 275 |
} |
|---|
| 276 |
wpath[MAX_PATH] = L'\0'; |
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
return _wopen (wpath, flags, mode); |
|---|
| 283 |
} |
|---|
| 284 |
#endif |
|---|
| 285 |
const char *local_name = ToLocale (filename); |
|---|
| 286 |
|
|---|
| 287 |
if (local_name == NULL) |
|---|
| 288 |
{ |
|---|
| 289 |
errno = ENOENT; |
|---|
| 290 |
return -1; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
int fd = open (local_name, flags, mode); |
|---|
| 294 |
LocaleFree (local_name); |
|---|
| 295 |
return fd; |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
FILE *utf8_fopen (const char *filename, const char *mode) |
|---|
| 305 |
{ |
|---|
| 306 |
int rwflags = 0, oflags = 0; |
|---|
| 307 |
bool append = false; |
|---|
| 308 |
|
|---|
| 309 |
for (const char *ptr = mode; *ptr; ptr++) |
|---|
| 310 |
{ |
|---|
| 311 |
switch (*ptr) |
|---|
| 312 |
{ |
|---|
| 313 |
case 'r': |
|---|
| 314 |
rwflags = O_RDONLY; |
|---|
| 315 |
break; |
|---|
| 316 |
|
|---|
| 317 |
case 'a': |
|---|
| 318 |
rwflags = O_WRONLY; |
|---|
| 319 |
oflags |= O_CREAT; |
|---|
| 320 |
append = true; |
|---|
| 321 |
break; |
|---|
| 322 |
|
|---|
| 323 |
case 'w': |
|---|
| 324 |
rwflags = O_WRONLY; |
|---|
| 325 |
oflags |= O_CREAT | O_TRUNC; |
|---|
| 326 |
break; |
|---|
| 327 |
|
|---|
| 328 |
case '+': |
|---|
| 329 |
rwflags = O_RDWR; |
|---|
| 330 |
break; |
|---|
| 331 |
|
|---|
| 332 |
#ifdef O_TEXT |
|---|
| 333 |
case 't': |
|---|
| 334 |
oflags |= O_TEXT; |
|---|
| 335 |
break; |
|---|
| 336 |
#endif |
|---|
| 337 |
} |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
int fd = utf8_open (filename, rwflags | oflags, 0666); |
|---|
| 341 |
if (fd == -1) |
|---|
| 342 |
return NULL; |
|---|
| 343 |
|
|---|
| 344 |
if (append && (lseek (fd, 0, SEEK_END) == -1)) |
|---|
| 345 |
{ |
|---|
| 346 |
close (fd); |
|---|
| 347 |
return NULL; |
|---|
| 348 |
} |
|---|
| 349 |
|
|---|
| 350 |
FILE *stream = fdopen (fd, mode); |
|---|
| 351 |
if (stream == NULL) |
|---|
| 352 |
close (fd); |
|---|
| 353 |
|
|---|
| 354 |
return stream; |
|---|
| 355 |
} |
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
int utf8_mkdir( const char *dirname, mode_t mode ) |
|---|
| 366 |
{ |
|---|
| 367 |
#if defined (UNDER_CE) || defined (WIN32) |
|---|
| 368 |
VLC_UNUSED( mode ); |
|---|
| 369 |
|
|---|
| 370 |
wchar_t wname[MAX_PATH + 1]; |
|---|
| 371 |
char mod[MAX_PATH + 1]; |
|---|
| 372 |
int i; |
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
for( i = 0; *dirname; i++ ) |
|---|
| 376 |
{ |
|---|
| 377 |
if( i == MAX_PATH ) |
|---|
| 378 |
return -1; |
|---|
| 379 |
|
|---|
| 380 |
if( *dirname == '/' ) |
|---|
| 381 |
mod[i] = '\\'; |
|---|
| 382 |
else |
|---|
| 383 |
mod[i] = *dirname; |
|---|
| 384 |
dirname++; |
|---|
| 385 |
|
|---|
| 386 |
} |
|---|
| 387 |
mod[i] = 0; |
|---|
| 388 |
|
|---|
| 389 |
if( MultiByteToWideChar( CP_UTF8, 0, mod, -1, wname, MAX_PATH ) == 0 ) |
|---|
| 390 |
{ |
|---|
| 391 |
errno = ENOENT; |
|---|
| 392 |
return -1; |
|---|
| 393 |
} |
|---|
| 394 |
wname[MAX_PATH] = L'\0'; |
|---|
| 395 |
|
|---|
| 396 |
if( CreateDirectoryW( wname, NULL ) == 0 ) |
|---|
| 397 |
{ |
|---|
| 398 |
if( GetLastError( ) == ERROR_ALREADY_EXISTS ) |
|---|
| 399 |
errno = EEXIST; |
|---|
| 400 |
else |
|---|
| 401 |
errno = ENOENT; |
|---|
| 402 |
return -1; |
|---|
| 403 |
} |
|---|
| 404 |
return 0; |
|---|
| 405 |
#else |
|---|
| 406 |
char *locname = ToLocale( dirname ); |
|---|
| 407 |
int res; |
|---|
| 408 |
|
|---|
| 409 |
if( locname == NULL ) |
|---|
| 410 |
{ |
|---|
| 411 |
errno = ENOENT; |
|---|
| 412 |
return -1; |
|---|
| 413 |
} |
|---|
| 414 |
res = mkdir( locname, mode ); |
|---|
| 415 |
|
|---|
| 416 |
LocaleFree( locname ); |
|---|
| 417 |
return res; |
|---|
| 418 |
#endif |
|---|
| 419 |
} |
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
DIR *utf8_opendir( const char *dirname ) |
|---|
| 429 |
{ |
|---|
| 430 |
#ifdef WIN32 |
|---|
| 431 |
wchar_t wname[MAX_PATH + 1]; |
|---|
| 432 |
|
|---|
| 433 |
if (MultiByteToWideChar (CP_UTF8, 0, dirname, -1, wname, MAX_PATH)) |
|---|
| 434 |
{ |
|---|
| 435 |
wname[MAX_PATH] = L'\0'; |
|---|
| 436 |
return (DIR *)vlc_wopendir (wname); |
|---|
| 437 |
} |
|---|
| 438 |
#else |
|---|
| 439 |
const char *local_name = ToLocale( dirname ); |
|---|
| 440 |
|
|---|
| 441 |
if( local_name != NULL ) |
|---|
| 442 |
{ |
|---|
| 443 |
DIR *dir = opendir( local_name ); |
|---|
| 444 |
LocaleFree( local_name ); |
|---|
| 445 |
return dir; |
|---|
| 446 |
} |
|---|
| 447 |
#endif |
|---|
| 448 |
|
|---|
| 449 |
errno = ENOENT; |
|---|
| 450 |
return NULL; |
|---|
| 451 |
} |
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
char *utf8_readdir( DIR *dir ) |
|---|
| 462 |
{ |
|---|
| 463 |
#ifdef WIN32 |
|---|
| 464 |
struct _wdirent *ent = vlc_wreaddir (dir); |
|---|
| 465 |
if (ent == NULL) |
|---|
| 466 |
return NULL; |
|---|
| 467 |
|
|---|
| 468 |
return FromWide (ent->d_name); |
|---|
| 469 |
#else |
|---|
| 470 |
struct dirent *ent; |
|---|
| 471 |
|
|---|
| 472 |
ent = readdir( (DIR *)dir ); |
|---|
| 473 |
if( ent == NULL ) |
|---|
| 474 |
return NULL; |
|---|
| 475 |
|
|---|
| 476 |
return vlc_fix_readdir( ent->d_name ); |
|---|
| 477 |
#endif |
|---|
| 478 |
} |
|---|
| 479 |
|
|---|
| 480 |
static int dummy_select( const char *str ) |
|---|
| 481 |
{ |
|---|
| 482 |
(void)str; |
|---|
| 483 |
return 1; |
|---|
| 484 |
} |
|---|
| 485 |
|
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
int utf8_loaddir( DIR *dir, char ***namelist, |
|---|
| 491 |
int (*select)( const char * ), |
|---|
| 492 |
int (*compar)( const char **, const char ** ) ) |
|---|
| 493 |
{ |
|---|
| 494 |
if( select == NULL ) |
|---|
| 495 |
select = dummy_select; |
|---|
| 496 |
|
|---|
| 497 |
if( dir == NULL ) |
|---|
| 498 |
return -1; |
|---|
| 499 |
else |
|---|
| 500 |
{ |
|---|
| 501 |
char **tab = NULL; |
|---|
| 502 |
char *entry; |
|---|
| 503 |
unsigned num = 0; |
|---|
| 504 |
|
|---|
| 505 |
rewinddir( dir ); |
|---|
| 506 |
|
|---|
| 507 |
while( ( entry = utf8_readdir( dir ) ) != NULL ) |
|---|
| 508 |
{ |
|---|
| 509 |
char **newtab; |
|---|
| 510 |
|
|---|
| 511 |
if( !select( entry ) ) |
|---|
| 512 |
{ |
|---|
| 513 |
free( entry ); |
|---|
| 514 |
continue; |
|---|
| 515 |
} |
|---|
| 516 |
|
|---|
| 517 |
newtab = realloc( tab, sizeof( char * ) * (num + 1) ); |
|---|
| 518 |
if( newtab == NULL ) |
|---|
| 519 |
{ |
|---|
| 520 |
free( entry ); |
|---|
| 521 |
goto error; |
|---|
| 522 |
} |
|---|
| 523 |
tab = newtab; |
|---|
| 524 |
tab[num++] = entry; |
|---|
| 525 |
} |
|---|
| 526 |
|
|---|
| 527 |
if( compar != NULL ) |
|---|
| 528 |
qsort( tab, num, sizeof( tab[0] ), |
|---|
| 529 |
(int (*)( const void *, const void *))compar ); |
|---|
| 530 |
|
|---|
| 531 |
*namelist = tab; |
|---|
| 532 |
return num; |
|---|
| 533 |
|
|---|
| 534 |
error:{ |
|---|
| 535 |
unsigned i; |
|---|
| 536 |
|
|---|
| 537 |
for( i = 0; i < num; i++ ) |
|---|
| 538 |
free( tab[i] ); |
|---|
| 539 |
if( tab != NULL ) |
|---|
| 540 |
free( tab ); |
|---|
| 541 |
} |
|---|
| 542 |
} |
|---|
| 543 |
return -1; |
|---|
| 544 |
} |
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
|
|---|
| 557 |
|
|---|
| 558 |
int utf8_scandir( const char *dirname, char ***namelist, |
|---|
| 559 |
int (*select)( const char * ), |
|---|
| 560 |
int (*compar)( const char **, const char ** ) ) |
|---|
| 561 |
{ |
|---|
| 562 |
DIR *dir = utf8_opendir (dirname); |
|---|
| 563 |
int val = -1; |
|---|
| 564 |
|
|---|
| 565 |
if (dir != NULL) |
|---|
| 566 |
{ |
|---|
| 567 |
val = utf8_loaddir (dir, namelist, select, compar); |
|---|
| 568 |
closedir (dir); |
|---|
| 569 |
} |
|---|
| 570 |
return val; |
|---|
| 571 |
} |
|---|
| 572 |
|
|---|
| 573 |
static int utf8_statEx( const char *filename, struct stat *buf, |
|---|
| 574 |
bool deref ) |
|---|
| 575 |
{ |
|---|
| 576 |
#if defined (WIN32) || defined (UNDER_CE) |
|---|
| 577 |
|
|---|
| 578 |
if( GetVersion() < 0x80000000 ) |
|---|
| 579 |
{ |
|---|
| 580 |
|
|---|
| 581 |
wchar_t wpath[MAX_PATH + 1]; |
|---|
| 582 |
|
|---|
| 583 |
if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) ) |
|---|
| 584 |
{ |
|---|
| 585 |
errno = ENOENT; |
|---|
| 586 |
return -1; |
|---|
| 587 |
} |
|---|
| 588 |
wpath[MAX_PATH] = L'\0'; |
|---|
| 589 |
|
|---|
| 590 |
return _wstati64( wpath, buf ); |
|---|
| 591 |
} |
|---|
| 592 |
#endif |
|---|
| 593 |
#ifdef HAVE_SYS_STAT_H |
|---|
| 594 |
const char *local_name = ToLocale( filename ); |
|---|
| 595 |
|
|---|
| 596 |
if( local_name != NULL ) |
|---|
| 597 |
{ |
|---|
| 598 |
int res = deref ? stat( local_name, buf ) |
|---|
| 599 |
: lstat( local_name, buf ); |
|---|
| 600 |
LocaleFree( local_name ); |
|---|
| 601 |
return res; |
|---|
| 602 |
} |
|---|
| 603 |
errno = ENOENT; |
|---|
| 604 |
#endif |
|---|
| 605 |
return -1; |
|---|
| 606 |
} |
|---|
| 607 |
|
|---|
| 608 |
|
|---|
| 609 |
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
int utf8_stat( const char *filename, struct stat *buf) |
|---|
| 615 |
{ |
|---|
| 616 |
return utf8_statEx( filename, buf, true ); |
|---|
| 617 |
} |
|---|
| 618 |
|
|---|
| 619 |
|
|---|
| 620 |
|
|---|
| 621 |
|
|---|
| 622 |
|
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
int utf8_lstat( const char *filename, struct stat *buf) |
|---|
| 626 |
{ |
|---|
| 627 |
return utf8_statEx( filename, buf, false ); |
|---|
| 628 |
} |
|---|
| 629 |
|
|---|
| 630 |
|
|---|
| 631 |
|
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
int utf8_unlink( const char *filename ) |
|---|
| 638 |
{ |
|---|
| 639 |
#if defined (WIN32) || defined (UNDER_CE) |
|---|
| 640 |
if( GetVersion() < 0x80000000 ) |
|---|
| 641 |
{ |
|---|
| 642 |
|
|---|
| 643 |
wchar_t wpath[MAX_PATH + 1]; |
|---|
| 644 |
|
|---|
| 645 |
if( !MultiByteToWideChar( CP_UTF8, 0, filename, -1, wpath, MAX_PATH ) ) |
|---|
| 646 |
{ |
|---|
| 647 |
errno = ENOENT; |
|---|
| 648 |
return -1; |
|---|
| 649 |
} |
|---|
| 650 |
wpath[MAX_PATH] = L'\0'; |
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 |
return _wunlink( wpath ); |
|---|
| 657 |
} |
|---|
| 658 |
#endif |
|---|
| 659 |
const char *local_name = ToLocale( filename ); |
|---|
| 660 |
|
|---|
| 661 |
if( local_name == NULL ) |
|---|
| 662 |
{ |
|---|
| 663 |
errno = ENOENT; |
|---|
| 664 |
return -1; |
|---|
| 665 |
} |
|---|
| 666 |
|
|---|
| 667 |
int ret = unlink( local_name ); |
|---|
| 668 |
LocaleFree( local_name ); |
|---|
| 669 |
return ret; |
|---|
| 670 |
} |
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
static int utf8_vasprintf( char **str, const char *fmt, va_list ap ) |
|---|
| 679 |
{ |
|---|
| 680 |
char *utf8; |
|---|
| 681 |
int res = vasprintf( &utf8, fmt, ap ); |
|---|
| 682 |
if( res == -1 ) |
|---|
| 683 |
return -1; |
|---|
| 684 |
|
|---|
| 685 |
*str = ToLocaleDup( utf8 ); |
|---|
| 686 |
free( utf8 ); |
|---|
| 687 |
return res; |
|---|
| 688 |
} |
|---|
| 689 |
|
|---|
| 690 |
|
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
int utf8_vfprintf( FILE *stream, const char *fmt, va_list ap ) |
|---|
| 695 |
{ |
|---|
| 696 |
char *str; |
|---|
| 697 |
int res = utf8_vasprintf( &str, fmt, ap ); |
|---|
| 698 |
if( res == -1 ) |
|---|
| 699 |
return -1; |
|---|
| 700 |
|
|---|
| 701 |
fputs( str, stream ); |
|---|
| 702 |
free( str ); |
|---|
| 703 |
return res; |
|---|
| 704 |
} |
|---|
| 705 |
|
|---|
| 706 |
|
|---|
| 707 |
|
|---|
| 708 |
|
|---|
| 709 |
|
|---|
| 710 |
int utf8_fprintf( FILE *stream, const char *fmt, ... ) |
|---|
| 711 |
{ |
|---|
| 712 |
va_list ap; |
|---|
| 713 |
int res; |
|---|
| 714 |
|
|---|
| 715 |
va_start( ap, fmt ); |
|---|
| 716 |
res = utf8_vfprintf( stream, fmt, ap ); |
|---|
| 717 |
va_end( ap ); |
|---|
| 718 |
return res; |
|---|
| 719 |
} |
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
static char *CheckUTF8( char *str, char rep ) |
|---|
| 723 |
{ |
|---|
| 724 |
uint8_t *ptr = (uint8_t *)str; |
|---|
| 725 |
assert (str != NULL); |
|---|
| 726 |
|
|---|
| 727 |
for (;;) |
|---|
| 728 |
{ |
|---|
| 729 |
uint8_t c = ptr[0]; |
|---|
| 730 |
int charlen = -1; |
|---|
| 731 |
|
|---|
| 732 |
if (c == '\0') |
|---|
| 733 |
break; |
|---|
| 734 |
|
|---|
| 735 |
for (int i = 0; i < 7; i++) |
|---|
| 736 |
if ((c >> (7 - i)) == ((0xff >> (7 - i)) ^ 1)) |
|---|
| 737 |
{ |
|---|
| 738 |
charlen = i; |
|---|
| 739 |
break; |
|---|
| 740 |
} |
|---|
| 741 |
|
|---|
| 742 |
switch (charlen) |
|---|
| 743 |
{ |
|---|
| 744 |
case 0: |
|---|
| 745 |
ptr++; |
|---|
| 746 |
continue; |
|---|
| 747 |
|
|---|
| 748 |
case -1: |
|---|
| 749 |
case 1: |
|---|
| 750 |
goto error; |
|---|
| 751 |
} |
|---|
| 752 |
|
|---|
| 753 |
assert (charlen >= 2); |
|---|
| 754 |
|
|---|
| 755 |
uint32_t cp = c & ~((0xff >> (7 - charlen)) << (7 - charlen)); |
|---|
| 756 |
for (int i = 1; i < charlen; i++) |
|---|
| 757 |
{ |
|---|
| 758 |
assert (cp < (1 << 26)); |
|---|
| 759 |
c = ptr[i]; |
|---|
| 760 |
|
|---|
| 761 |
if ((c == '\0') |
|---|
| 762 |
|| ((c >> 6) != 2)) |
|---|
| 763 |
goto error; |
|---|
| 764 |
|
|---|
| 765 |
cp = (cp << 6) | (ptr[i] & 0x3f); |
|---|
| 766 |
} |
|---|
| 767 |
|
|---|
| 768 |
if (cp < 128) |
|---|
| 769 |
goto error; |
|---|
| 770 |
if (cp < (1u << (5 * charlen - 3))) |
|---|
| 771 |
goto error; |
|---|
| 772 |
|
|---|
| 773 |
ptr += charlen; |
|---|
| 774 |
continue; |
|---|
| 775 |
|
|---|
| 776 |
error: |
|---|
| 777 |
if (rep == 0) |
|---|
| 778 |
return NULL; |
|---|
| 779 |
*ptr++ = rep; |
|---|
| 780 |
str = NULL; |
|---|
| 781 |
} |
|---|
| 782 |
|
|---|
| 783 |
return str; |
|---|
| 784 |
} |
|---|
| 785 |
|
|---|
| 786 |
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
char *EnsureUTF8( char *str ) |
|---|
| 794 |
{ |
|---|
| 795 |
return CheckUTF8( str, '?' ); |
|---|
| 796 |
} |
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
|
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
const char *IsUTF8( const char *str ) |
|---|
| 807 |
{ |
|---|
| 808 |
return CheckUTF8( (char *)str, 0 ); |
|---|
| 809 |
} |
|---|