Skip to content

Commit

Permalink
applied patch of macnow@hoax.pl for subtitle types of MicroDVD, MPL2 …
Browse files Browse the repository at this point in the history
…and TMPlayer.

git-svn-id: http://movist.googlecode.com/svn/trunk@305 9988c26d-9134-0410-b5bb-5778289bb252
  • Loading branch information
cocoable committed Nov 29, 2009
1 parent a93624c commit 88a7c8c
Show file tree
Hide file tree
Showing 5 changed files with 626 additions and 2 deletions.
16 changes: 14 additions & 2 deletions AppController_Open.m
Expand Up @@ -29,6 +29,7 @@
#import "MMovie_QuickTime.h"
#import "MSubtitleParser_SMI.h"
#import "MSubtitleParser_SRT.h"
#import "MSubtitleParser_TXT.h"
#import "MSubtitleParser_SSA.h"
#import "MSubtitleParser_MKV.h"
#import "MSubtitleParser_SUB.h"
Expand Down Expand Up @@ -113,8 +114,8 @@ - (NSArray*)subtitleFromURL:(NSURL*)subtitleURL
NSString* ext = [[path pathExtension] lowercaseString];
Class parserClass = ([ext isEqualToString:@"smi"] ||
[ext isEqualToString:@"sami"])? [MSubtitleParser_SMI class] :
([ext isEqualToString:@"srt"] ||
[ext isEqualToString:@"txt"]) ? [MSubtitleParser_SRT class] :
([ext isEqualToString:@"srt"]) ? [MSubtitleParser_SRT class] :
([ext isEqualToString:@"txt"]) ? [MSubtitleParser_TXT class] :
([ext isEqualToString:@"ssa"] ||
[ext isEqualToString:@"ass"]) ? [MSubtitleParser_SSA class] :
([ext isEqualToString:@"mkv"] ||
Expand Down Expand Up @@ -148,6 +149,17 @@ - (NSArray*)subtitleFromURL:(NSURL*)subtitleURL
stringEncoding, MSubtitleParserOptionKey_stringEncoding,
nil];
}
else if (parserClass == [MSubtitleParser_TXT class]) {
MMovieInfo movieInfo;
NSURL* movieURL = [_movie url];
[MMovie getMovieInfo:&movieInfo forMovieURL:movieURL error:nil];
NSString* movieFps = [NSString stringWithFormat: @"%f", movieInfo.fps];
NSNumber* stringEncoding = [NSNumber numberWithInt:cfEncoding];
options = [NSDictionary dictionaryWithObjectsAndKeys:
stringEncoding, MSubtitleParserOptionKey_stringEncoding,
movieFps, @"movieFps",
nil];
}
else if (parserClass == [MSubtitleParser_SUB class]) {
// no options for SUB
}
Expand Down
44 changes: 44 additions & 0 deletions CSRegex.h
@@ -0,0 +1,44 @@
//
// CSRegex.h
//
// http://www.cocoadev.com/index.pl?CSRegex
//

#import <Foundation/Foundation.h>
#import <regex.h>

@interface CSRegex : NSObject
{
regex_t preg;
}

-(id)initWithPattern:(NSString *)pattern options:(int)options;
-(void)dealloc;

-(BOOL)matchesString:(NSString *)string;
-(NSString *)matchedSubstringOfString:(NSString *)string;
-(NSArray *)capturedSubstringsOfString:(NSString *)string;

+(CSRegex *)regexWithPattern:(NSString *)pattern options:(int)options;
+(CSRegex *)regexWithPattern:(NSString *)pattern;

+(NSString *)null;

+(void)initialize;

@end

@interface NSString (CSRegex)

-(BOOL)matchedByPattern:(NSString *)pattern options:(int)options;
-(BOOL)matchedByPattern:(NSString *)pattern;

-(NSString *)substringMatchedByPattern:(NSString *)pattern options:(int)options;
-(NSString *)substringMatchedByPattern:(NSString *)pattern;

-(NSArray *)substringsCapturedByPattern:(NSString *)pattern options:(int)options;
-(NSArray *)substringsCapturedByPattern:(NSString *)pattern;

-(NSString *)escapedPattern;

@end
160 changes: 160 additions & 0 deletions CSRegex.m
@@ -0,0 +1,160 @@
//
// CSRegex.m
//
// http://www.cocoadev.com/index.pl?CSRegex
//

#import "CSRegex.h"

static NSString *nullstring=nil;

@implementation CSRegex

- (id) initWithPattern:(NSString *)pattern options:(int)options
{
if (self=[super init]) {
int err=regcomp(&preg,[pattern UTF8String],options|REG_EXTENDED);
if (err) {
char errbuf[256];
regerror(err,&preg,errbuf,sizeof(errbuf));
[NSException raise:@"CSRegexException"
format:@"Could not compile regex \"%@\": %s",pattern,errbuf];
}
}
return self;
}

- (void) dealloc
{
regfree(&preg);
[super dealloc];
}

- (BOOL) matchesString:(NSString *)string
{
if (regexec(&preg,[string UTF8String],0,NULL,0) == 0)
return YES;

return NO;
}

- (NSString *) matchedSubstringOfString:(NSString *)string
{
const char *cstr=[string UTF8String];
regmatch_t match;
if (regexec(&preg,cstr,1,&match,0) == 0) {
return [[[NSString alloc] initWithBytes:cstr+match.rm_so
length:match.rm_eo-match.rm_so encoding:NSUTF8StringEncoding] autorelease];
}

return nil;
}

- (NSArray *) capturedSubstringsOfString:(NSString *)string
{
const char *cstr = [string UTF8String];
int num = preg.re_nsub+1;
regmatch_t *matches = calloc(sizeof(regmatch_t),num);

if (regexec(&preg,cstr,num,matches,0) == 0) {
NSMutableArray *array=[NSMutableArray arrayWithCapacity:num];

int i;
for (i=0;i<num;i++) {
NSString *str;

if(matches[i].rm_so == -1 && matches[i].rm_eo == -1)
str=nullstring;
else
str=[[[NSString alloc] initWithBytes:cstr+matches[i].rm_so
length:matches[i].rm_eo-matches[i].rm_so encoding:NSUTF8StringEncoding] autorelease];

[array addObject:str];
}

free(matches);

return [NSArray arrayWithArray:array];
}

free(matches);

return nil;
}

+ (CSRegex *) regexWithPattern:(NSString *)pattern options:(int)options
{
return [[[CSRegex alloc] initWithPattern:pattern options:options] autorelease];
}

+ (CSRegex *) regexWithPattern:(NSString *)pattern
{
return [[[CSRegex alloc] initWithPattern:pattern options:0] autorelease];
}

+ (NSString *) null
{
return nullstring;
}

+ (void) initialize
{
if (!nullstring)
nullstring=[[NSString alloc] initWithString:@""];
}

@end

@implementation NSString (CSRegex)

- (BOOL) matchedByPattern:(NSString *)pattern options:(int)options
{
CSRegex *re=[CSRegex regexWithPattern:pattern options:options|REG_NOSUB];
return [re matchesString:self];
}

- (BOOL) matchedByPattern:(NSString *)pattern
{
return [self matchedByPattern:pattern options:0];
}

- (NSString *) substringMatchedByPattern:(NSString *)pattern options:(int)options
{
CSRegex *re=[CSRegex regexWithPattern:pattern options:options];
return [re matchedSubstringOfString:self];
}

- (NSString *) substringMatchedByPattern:(NSString *)pattern
{
return [self substringMatchedByPattern:pattern options:0];
}

- (NSArray *) substringsCapturedByPattern:(NSString *)pattern options:(int)options
{
CSRegex *re=[CSRegex regexWithPattern:pattern options:options];
return [re capturedSubstringsOfString:self];
}

- (NSArray *) substringsCapturedByPattern:(NSString *)pattern
{
return [self substringsCapturedByPattern:pattern options:0];
}

- (NSString *) escapedPattern
{
int len=[self length];
NSMutableString *escaped=[NSMutableString stringWithCapacity:len];

int i;
for (i=0;i<len;i++) {
unichar c=[self characterAtIndex:i];
if(c=='^'||c=='.'||c=='['||c=='$'||c=='('||c==')'
||c=='|'||c=='*'||c=='+'||c=='?'||c=='{'||c=='\\')
[escaped appendFormat:@"\\%C",c];
else
[escaped appendFormat:@"%C",c];
}
return [NSString stringWithString:escaped];
}

@end
44 changes: 44 additions & 0 deletions MSubtitleParser_TXT.h
@@ -0,0 +1,44 @@
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim. All rights reserved.
// Yong-Hoe Kim <cocoable@gmail.com>
//
// This file is part of Movist.
//
// Movist is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Movist is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

// author: ...

#import "Movist.h"

#import "MSubtitleParser.h"

@interface MSubtitleParser_TXT : MSubtitleParser
{
NSString* _source;
NSRange _sourceRange;

NSCharacterSet* _fontDelimSet;

NSMutableArray* _subtitles;
}

- (NSArray*)parseString:(NSString*)string options:(NSDictionary*)options
error:(NSError**)error;

- (NSMutableAttributedString*)parseSubtitleString:(NSString*)string;

@end

0 comments on commit 88a7c8c

Please sign in to comment.