Plugin utilities: Moved encoding guesser to header

Moved the string encoding guesser/converter to the Plugin.h header, so
it may be accessible from any plugin. I may make it a global member of
something eventually, but a static inline for such a simple function
should be fine for now.

This function facilitates converting arbitrary 8 bit encoded strings to
Unicode NSString objects. It should be used anywhere that UTF-8 is
expected, but not necessarily guaranteed, and where other 8-bit
encodings may also be supplied by a user's files.

Not using this setup for string inputs has already led to failed UTF-8
decoding resulting in nil NSStrings being passed to the inline array or
dictionary initializers, which results in crashes due to uncaught
exceptions.

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
swiftingly
Christopher Snowhill 2022-05-24 01:05:43 -07:00
parent 4b2cf22800
commit f7dc6beda1
2 changed files with 7 additions and 7 deletions

View File

@ -98,3 +98,10 @@
- (int)putMetadataInURL:(NSURL *)url;
@end
static NSString *guess_encoding_of_string(const char *input) {
NSString *ret = @"";
NSData *stringData = [NSData dataWithBytes:input length:strlen(input)];
[NSString stringEncodingForData:stringData encodingOptions:nil convertedString:&ret usedLossyConversion:nil];
return ret;
}

View File

@ -59,13 +59,6 @@ static size_t http_curl_write_wrapper(HTTPSource *fp, void *ptr, size_t size) {
return size - avail;
}
static NSString *guess_encoding_of_string(const char *input) {
NSString *ret = @"";
NSData *stringData = [NSData dataWithBytes:input length:strlen(input)];
[NSString stringEncodingForData:stringData encodingOptions:nil convertedString:&ret usedLossyConversion:nil];
return ret;
}
static int http_parse_shoutcast_meta(HTTPSource *fp, const char *meta, size_t size) {
// DLog (@"reading %d bytes of metadata\n", size);
DLog(@"%s", meta);