diff options
author | Eric Wong <normalperson@yhbt.net> | 2007-12-28 02:56:25 +0000 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2007-12-28 02:56:25 +0000 |
commit | b79f6b882a70526ec2e9fc231e0baeebdfef1e52 (patch) | |
tree | 719912135d059476070834c3c62ae940955e9e20 /src/utf8.c | |
parent | 0d26248a0d8cb2e5cf6be5a438a18b16793bbb63 (diff) | |
download | mpd-b79f6b882a70526ec2e9fc231e0baeebdfef1e52.tar.gz mpd-b79f6b882a70526ec2e9fc231e0baeebdfef1e52.tar.xz mpd-b79f6b882a70526ec2e9fc231e0baeebdfef1e52.zip |
Merge branches/ew r7104
thread-safety work in preparation for rewrite to use pthreads
Expect no regressions against trunk (r7078), possibly minor
performance improvements in update (due to fewer heap
allocations), but increased stack usage.
Applied the following patches:
* maxpath_str for reentrancy (temporary fix, reverted)
* path: start working on thread-safe variants of these methods
* Re-entrancy work on path/character-set conversions
* directory.c: exploreDirectory() use reentrant functions here
* directory/update: more use of reentrant functions + cleanups
* string_toupper: a strdup-less version of strDupToUpper
* get_song_url: a static-variable-free version of getSongUrl()
* Use reentrant/thread-safe get_song_url everywhere
* replace rmp2amp with the reentrant version, rmp2amp_r
* Get rid of the non-reentrant/non-thread-safe rpp2app, too.
* buffer2array: assert strdup() returns a usable value in unit tests
* replace utf8ToFsCharset and fsCharsetToUtf8 with thread-safe variants
* fix storing playlists w/o absolute paths
* parent_path(), a reentrant version of parentPath()
* parentPath => parent_path for reentrancy and thread-safety
* allow "make test" to automatically run embedded unit tests
* remove convStrDup() and maxpath_str()
* use MPD_PATH_MAX everywhere instead of MAXPATHLEN
* path: get rid of appendSlash, pfx_path and just use pfx_dir
* get_song_url: fix the ability to play songs in the top-level music_directory
git-svn-id: https://svn.musicpd.org/mpd/trunk@7106 09075e82-0dd4-0310-85a5-a0d7c8717e4f
Diffstat (limited to 'src/utf8.c')
-rw-r--r-- | src/utf8.c | 89 |
1 files changed, 49 insertions, 40 deletions
diff --git a/src/utf8.c b/src/utf8.c index 2061a78de..bb62a34f7 100644 --- a/src/utf8.c +++ b/src/utf8.c @@ -23,51 +23,42 @@ #include <string.h> #include <stdlib.h> -static char *latin1ToUtf8(char c) +char *latin1_to_utf8(char *dest, char *in_latin1) { - static unsigned char utf8[3]; - unsigned char uc = c; - - memset(utf8, 0, 3); - - if (uc < 128) - utf8[0] = uc; - else if (uc < 192) { - utf8[0] = 194; - utf8[1] = uc; - } else { - utf8[0] = 195; - utf8[1] = uc - 64; + unsigned char *cp = (unsigned char *)dest; + unsigned char *latin1 = (unsigned char *)in_latin1; + + while (*latin1) { + if (*latin1 < 128) + *(cp++) = *latin1; + else { + if (*latin1 < 192) { + *(cp++) = 194; + *(cp++) = *latin1; + } else { + *(cp++) = 195; + *(cp++) = (*latin1) - 64; + } + } + ++latin1; } - return (char *)utf8; + *cp = '\0'; + + return dest; } char *latin1StrToUtf8Dup(char *latin1) { /* utf8 should have at most two char's per latin1 char */ - int len = strlen(latin1) * 2 + 1; - char *ret = xmalloc(len); - char *cp = ret; - char *utf8; - - memset(ret, 0, len); + char *ret = xmalloc(strlen(latin1) * 2 + 1); - len = 0; + ret = latin1_to_utf8(ret, latin1); - while (*latin1) { - utf8 = latin1ToUtf8(*latin1); - while (*utf8) { - *(cp++) = *(utf8++); - len++; - } - latin1++; - } - - return xrealloc(ret, len + 1); + return ((ret) ? xrealloc(ret, strlen((char *)ret) + 1) : NULL); } -static char utf8ToLatin1(char *inUtf8) +static char utf8_to_latin1_char(char *inUtf8) { unsigned char c = 0; unsigned char *utf8 = (unsigned char *)inUtf8; @@ -124,14 +115,10 @@ int validUtf8String(char *string) char *utf8StrToLatin1Dup(char *utf8) { /* utf8 should have at most two char's per latin1 char */ - int len = strlen(utf8) + 1; - char *ret = xmalloc(len); + char *ret = xmalloc(strlen(utf8) + 1); char *cp = ret; int count; - - memset(ret, 0, len); - - len = 0; + size_t len = 0; while (*utf8) { count = validateUtf8Char(utf8); @@ -139,10 +126,32 @@ char *utf8StrToLatin1Dup(char *utf8) free(ret); return NULL; } - *(cp++) = utf8ToLatin1(utf8); + *(cp++) = utf8_to_latin1_char(utf8); utf8 += count; len++; } + *cp = '\0'; + return xrealloc(ret, len + 1); } + +char *utf8_to_latin1(char *dest, char *utf8) +{ + char *cp = dest; + int count; + size_t len = 0; + + while (*utf8) { + count = validateUtf8Char(utf8); + if (count) { + *(cp++) = utf8_to_latin1_char(utf8); + utf8 += count; + len++; + } else + return NULL; + } + + *cp = '\0'; + return dest; +} |