File: XADSplitFileParser.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (68 lines) | stat: -rw-r--r-- 2,126 bytes parent folder | download
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#import "XADSplitFileParser.h"
#import "XADRegex.h"

@implementation XADSplitFileParser

+(int)requiredHeaderSize { return 0; }

+(BOOL)recognizeFileWithHandle:(CSHandle *)handle firstBytes:(NSData *)data name:(NSString *)name
{
	if(!name) return NO;

	// Check if filename is of the form .001
	NSArray *matches=[name substringsCapturedByPattern:@"^(.*)\\.([0-9]{3})$" options:REG_ICASE];
	if(!matches) return NO;

	// Find another filename in the series. Pick .001 if the given file is not already that,
	// and .002 otherwise.
	NSString *otherext;
	if([[matches objectAtIndex:2] isEqual:@"001"]) otherext=@"002";
	else otherext=@"001";

	// Check if this other file exists, too.
	NSString *othername=[NSString stringWithFormat:@"%@.%@",[matches objectAtIndex:1],otherext];
	return [[NSFileManager defaultManager] fileExistsAtPath:othername];
}

+(NSArray *)volumesForHandle:(CSHandle *)handle firstBytes:(NSData *)data name:(NSString *)name
{
	NSArray *matches=[name substringsCapturedByPattern:@"^(.*)\\.[0-9]{3}$" options:REG_ICASE];
	if(matches)
	{
		return [self scanForVolumesWithFilename:name
		regex:[XADRegex regexWithPattern:[NSString stringWithFormat:@"^%@\\.[0-9]{3}$",
			[[matches objectAtIndex:1] escapedPattern]] options:REG_ICASE]
		firstFileExtension:nil];
	}

	return nil;
}

-(void)parse
{
	NSString *basename=[[self name] stringByDeletingPathExtension];
	CSHandle *handle=[self handle];

	NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithObjectsAndKeys:
		[self XADPathWithUnseparatedString:basename],XADFileNameKey,
		[NSNumber numberWithLongLong:[handle fileSize]],XADFileSizeKey,
		[NSNumber numberWithLongLong:[handle fileSize]],XADCompressedSizeKey,
	nil];

	NSString *ext=[basename pathExtension];
	if([ext caseInsensitiveCompare:@"zip"]==0)
	[dict setObject:[NSNumber numberWithBool:YES] forKey:XADIsArchiveKey];

	[self addEntryWithDictionary:dict];
}

-(CSHandle *)handleForEntryWithDictionary:(NSDictionary *)dict wantChecksum:(BOOL)checksum
{
	CSHandle *handle=[self handle];
	[handle seekToFileOffset:0];
	return handle;
}

-(NSString *)formatName { return @"Split file"; }

@end