2007-03-02 01:36:52 +00:00
|
|
|
//
|
|
|
|
// FileSource.m
|
|
|
|
// FileSource
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 3/1/07.
|
|
|
|
// Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "FileSource.h"
|
|
|
|
|
|
|
|
@implementation FileSource
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (void)initialize {
|
|
|
|
fex_init();
|
2013-10-04 23:32:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)init {
|
|
|
|
self = [super init];
|
|
|
|
if(self) {
|
|
|
|
fex = NULL;
|
|
|
|
_fd = NULL;
|
|
|
|
}
|
|
|
|
return self;
|
2013-10-04 23:32:58 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)open:(NSURL *)url {
|
2009-03-01 06:03:51 +00:00
|
|
|
[self setURL:url];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
NSString *path = [url path];
|
|
|
|
|
|
|
|
fex_type_t type;
|
|
|
|
fex_err_t error = fex_identify_file(&type, [path UTF8String]);
|
|
|
|
|
|
|
|
if(!error && type && strcmp(fex_type_name(type), "file")) {
|
|
|
|
error = fex_open_type(&fex, [path UTF8String], type);
|
|
|
|
if(!error) {
|
|
|
|
while(!fex_done(fex)) {
|
|
|
|
NSString *name = [[NSString stringWithUTF8String:fex_name(fex)] lowercaseString];
|
|
|
|
if([name hasSuffix:@".diz"] || [name hasSuffix:@".txt"] || [name hasSuffix:@".nfo"]) {
|
|
|
|
error = fex_next(fex);
|
|
|
|
if(error)
|
|
|
|
return NO;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(fex_done(fex))
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
error = fex_data(fex, &data);
|
|
|
|
if(error)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
size = fex_size(fex);
|
|
|
|
offset = 0;
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
} else
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2018-06-28 10:59:59 +00:00
|
|
|
_fd = fopen([path UTF8String], "r");
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
return (_fd != NULL);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)seekable {
|
2007-03-04 00:18:28 +00:00
|
|
|
return YES;
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)seek:(long)position whence:(int)whence {
|
|
|
|
if(fex) {
|
|
|
|
switch(whence) {
|
|
|
|
case SEEK_CUR:
|
|
|
|
position += offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEEK_END:
|
|
|
|
position += size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = position;
|
|
|
|
|
|
|
|
return (position >= 0) && (position < size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_fd == NULL) {
|
2015-01-15 06:19:17 +00:00
|
|
|
offset = 0;
|
|
|
|
size = 0;
|
|
|
|
return NO;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
return (fseek(_fd, position, whence) == 0);
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (long)tell {
|
|
|
|
if(fex)
|
|
|
|
return offset;
|
|
|
|
else
|
|
|
|
return ftell(_fd);
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (long)read:(void *)buffer amount:(long)amount {
|
|
|
|
if(fex) {
|
|
|
|
if(amount + offset > size)
|
|
|
|
amount = size - offset;
|
|
|
|
memcpy(buffer, (const char *)data + offset, amount);
|
|
|
|
offset += amount;
|
|
|
|
return amount;
|
|
|
|
} else {
|
|
|
|
return fread(buffer, 1, amount, _fd);
|
|
|
|
}
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)close {
|
|
|
|
if(_fd) {
|
2007-10-15 03:29:30 +00:00
|
|
|
fclose(_fd);
|
|
|
|
_fd = NULL;
|
|
|
|
}
|
2013-10-04 23:32:58 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(fex) {
|
|
|
|
fex_close(fex);
|
|
|
|
fex = NULL;
|
|
|
|
}
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSURL *)url {
|
2007-03-04 04:36:10 +00:00
|
|
|
return _url;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSString *)mimeType {
|
2007-10-14 18:12:15 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setURL:(NSURL *)url {
|
2007-03-04 04:36:10 +00:00
|
|
|
_url = url;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)schemes {
|
2022-01-19 02:12:57 +00:00
|
|
|
return @[@"file"];
|
2007-03-02 01:36:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-13 07:09:46 +00:00
|
|
|
- (void)dealloc {
|
2007-10-15 03:29:30 +00:00
|
|
|
[self close];
|
2007-10-13 07:09:46 +00:00
|
|
|
}
|
|
|
|
|
2007-03-02 01:36:52 +00:00
|
|
|
@end
|