Use [NSString stringEncodingForData:encodingOptions:convertedString:usedLossyConversion:] to try to load text files with unknown encoding.
This commit is contained in:
4 files changed
+50
-61
No files matched your search
@@ -100,7 +100,7 @@
|
||||
MP42SSAConverter *converter = [[MP42SSAConverter alloc] initWithParser:_parser];
|
||||
|
||||
MP42SubSerializer *ss = [[MP42SubSerializer alloc] init];
|
||||
[ss setSSA:YES];
|
||||
ss.ssa = YES;
|
||||
|
||||
for (MP42SSALine *line in _parser.lines) {
|
||||
NSString *text = [converter convertLine:line];
|
||||
|
||||
+19
-22
@@ -11,6 +11,8 @@
|
||||
#import "MP42TextSample.h"
|
||||
#import "mp4v2.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MP42SubLine : NSObject
|
||||
{
|
||||
@public
|
||||
@@ -20,31 +22,23 @@
|
||||
unsigned top;
|
||||
unsigned forced;
|
||||
}
|
||||
-(instancetype)initWithLine:(NSString*)l start:(unsigned)s end:(unsigned)e;
|
||||
-(instancetype)initWithLine:(NSString*)l start:(unsigned)s end:(unsigned)e top_pos:(unsigned)p forced:(unsigned)f;
|
||||
- (instancetype)initWithLine:(NSString *)l start:(unsigned)s end:(unsigned)e;
|
||||
- (instancetype)initWithLine:(NSString *)l start:(unsigned)s end:(unsigned)e top_pos:(unsigned)p forced:(unsigned)f;
|
||||
@end
|
||||
|
||||
@interface MP42SubSerializer : NSObject
|
||||
{
|
||||
// input lines, sorted by 1. beginning time 2. original insertion order
|
||||
NSMutableArray<MP42SubLine *> *lines;
|
||||
BOOL finished;
|
||||
|
||||
BOOL position_information, forced;
|
||||
unsigned last_begin_time, last_end_time;
|
||||
unsigned linesInput;
|
||||
- (void)addLine:(MP42SubLine *)sline;
|
||||
|
||||
BOOL ssa;
|
||||
}
|
||||
-(void)addLine:(MP42SubLine *)sline;
|
||||
-(void)setFinished:(BOOL)finished;
|
||||
-(MP42SubLine *)getSerializedPacket;
|
||||
-(BOOL)isEmpty;
|
||||
-(BOOL)positionInformation;
|
||||
-(void)setPositionInformation:(BOOL)info;
|
||||
-(BOOL)forced;
|
||||
-(void)setForced:(BOOL)info;
|
||||
-(void)setSSA:(BOOL)ssa;
|
||||
@property (nonatomic) BOOL finished;
|
||||
|
||||
- (nullable MP42SubLine *)getSerializedPacket;
|
||||
|
||||
@property (nonatomic, readonly) BOOL isEmpty;
|
||||
|
||||
@property (nonatomic) BOOL positionInformation;
|
||||
@property (nonatomic) BOOL forced;
|
||||
@property (nonatomic) BOOL ssa;
|
||||
|
||||
@end
|
||||
|
||||
@@ -68,6 +62,9 @@ typedef struct {
|
||||
UInt32 duration;
|
||||
} PacketControlData;
|
||||
|
||||
int ExtractVobSubPacket(UInt8 *dest, UInt8 *framedSrc, int srcSize, int *usedSrcBytes, int index);
|
||||
ComponentResult ReadPacketControls(UInt8 *packet, UInt32 palette[16], PacketControlData *controlDataOut,BOOL *forced);
|
||||
int ExtractVobSubPacket(UInt8 *dest, UInt8 *framedSrc, int srcSize, int * _Nullable usedSrcBytes, int index);
|
||||
ComponentResult ReadPacketControls(UInt8 *packet, UInt32 palette[_Nonnull 16], PacketControlData *controlDataOut, BOOL *forced);
|
||||
Boolean ReadPacketTimes(uint8_t *packet, uint32_t length, uint16_t *startTime, uint16_t *endTime, uint8_t *forced);
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
||||
+29
-37
@@ -11,12 +11,19 @@
|
||||
#import "MP42HtmlParser.h"
|
||||
|
||||
@implementation MP42SubSerializer
|
||||
{
|
||||
// input lines, sorted by 1. beginning time 2. original insertion order
|
||||
NSMutableArray<MP42SubLine *> *lines;
|
||||
|
||||
unsigned last_begin_time, last_end_time;
|
||||
unsigned linesInput;
|
||||
}
|
||||
|
||||
-(instancetype)init
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
lines = [[NSMutableArray alloc] init];
|
||||
finished = NO;
|
||||
_finished = NO;
|
||||
last_begin_time = last_end_time = 0;
|
||||
linesInput = 0;
|
||||
}
|
||||
@@ -68,7 +75,7 @@ static CFComparisonResult CompareLinesByBeginTime(const void *a, const void *b,
|
||||
NSMutableString *str;
|
||||
int i;
|
||||
|
||||
if (!finished) {
|
||||
if (!_finished) {
|
||||
if (nlines > 1) {
|
||||
unsigned maxEndTime = first->end_time;
|
||||
|
||||
@@ -105,7 +112,7 @@ canOutput:
|
||||
if (l->begin_time <= begin_time) {
|
||||
// Try to be a bit smart and avoid duplicated lines
|
||||
// from ssa.
|
||||
if (!ssa || [str rangeOfString:l->line].location == NSNotFound) {
|
||||
if (!_ssa || [str rangeOfString:l->line].location == NSNotFound) {
|
||||
[str appendString:l->line];
|
||||
}
|
||||
}
|
||||
@@ -145,44 +152,15 @@ canOutput:
|
||||
return ret;
|
||||
}
|
||||
|
||||
-(void)setFinished:(BOOL)_finished
|
||||
{
|
||||
finished = _finished;
|
||||
}
|
||||
|
||||
-(BOOL)isEmpty
|
||||
{
|
||||
return [lines count] == 0;
|
||||
}
|
||||
|
||||
-(BOOL)positionInformation
|
||||
{
|
||||
return position_information;
|
||||
}
|
||||
|
||||
-(void)setPositionInformation:(BOOL)info
|
||||
{
|
||||
position_information = info;
|
||||
}
|
||||
|
||||
-(BOOL)forced
|
||||
{
|
||||
return forced;
|
||||
}
|
||||
|
||||
-(void)setForced:(BOOL)info
|
||||
{
|
||||
forced = info;
|
||||
}
|
||||
|
||||
-(void)setSSA:(BOOL)isSSA
|
||||
{
|
||||
ssa = isSSA;
|
||||
}
|
||||
|
||||
-(NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"lines left: %lu finished inputting: %d",(unsigned long)[lines count],finished];
|
||||
return [NSString stringWithFormat:@"lines left: %lu finished inputting: %d",(unsigned long)[lines count],_finished];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -297,12 +275,25 @@ extern NSString *STLoadFileWithUnknownEncoding(NSURL *url)
|
||||
{
|
||||
NSData *data = [NSData dataWithContentsOfURL:url];
|
||||
|
||||
if (!data)
|
||||
if (!data) {
|
||||
return nil;
|
||||
}
|
||||
|
||||
UniversalDetector *ud = [[UniversalDetector alloc] init];
|
||||
NSString *res = nil;
|
||||
NSStringEncoding enc;
|
||||
|
||||
BOOL lossy = NO;
|
||||
enc = [NSString stringEncodingForData:data
|
||||
encodingOptions:nil
|
||||
convertedString:&res
|
||||
usedLossyConversion:&lossy];
|
||||
|
||||
if (res && lossy == NO) {
|
||||
NSLog(@"Encoding %lu failed, retrying.\n", (unsigned long)enc);
|
||||
return res;
|
||||
}
|
||||
|
||||
float conf;
|
||||
NSString *enc_str;
|
||||
BOOL latin2;
|
||||
@@ -329,12 +320,13 @@ extern NSString *STLoadFileWithUnknownEncoding(NSURL *url)
|
||||
|
||||
if (!res) {
|
||||
if (latin2) {
|
||||
NSLog(@"Encoding %s failed, retrying.\n",[enc_str UTF8String]);
|
||||
NSLog(@"Encoding %s failed, retrying.\n", enc_str.UTF8String);
|
||||
enc = (enc == NSWindowsCP1252StringEncoding) ? NSWindowsCP1250StringEncoding : NSWindowsCP1252StringEncoding;
|
||||
res = [[NSString alloc] initWithData:data encoding:enc];
|
||||
if (!res) NSLog(@"Both of latin1/2 failed.\n");
|
||||
} else {
|
||||
res = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
||||
}
|
||||
if (!res) {
|
||||
res = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
if (!res) { NSLog(@"ASCII failed."); NSLog(@"Failed to load file as guessed encoding %@.",enc_str); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +102,7 @@
|
||||
_parser = [[MP42SSAParser alloc] initWithMKVHeader:cookieString];
|
||||
_converter = [[MP42SSAConverter alloc] initWithParser:_parser];
|
||||
|
||||
[_ss setSSA:YES];
|
||||
_ss.ssa = YES;
|
||||
}
|
||||
|
||||
_trackID = track.sourceId;
|
||||
|
||||
Reference in new issue
Block a user