Support multi-disk images for user-provided disk images placed in the Documents directory. Multi disk images are recognized by having disk1 and having a common filename prefix

This commit is contained in:
Yoshi Sugawara 2016-04-03 13:06:06 -04:00
parent 00a7bf4a5f
commit 953c76e076

View File

@ -61,6 +61,13 @@ void xmltostring(simplexml* _this,MyString& _output)
}
@interface DiskImageInfo : NSObject
@property (strong, nonatomic) NSString *name;
@property (nonatomic) NSUInteger slotNumber;
@end
@implementation DiskImageInfo
@end
static UIImage* defaultImage2GS = nil;
static UIImage* defaultImageII = nil;
@ -101,8 +108,7 @@ static UIImage* defaultImageII = nil;
}
-(void)retrieveDocumentList:(MyString&) tempXML withBaseURL:(MyString&) _baseUrl;
{
-(void) createXmlUsingStringRef:(MyString&)tempXML withBaseURLRef:(MyString&)_baseUrl {
tempXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<list version=\"1\">";
tempXML += "<source>My 2GS</source>";
@ -189,7 +195,58 @@ static UIImage* defaultImageII = nil;
fclose(f);
}
// parse les autres fichiers
// support multi-disk images by checking for the presence of "disk1" and create multiple disk image elements
// setup multi disk detection
NSString *currentMultiDiskPrefix = nil;
NSMutableArray *currentMultiDiskFilenames = [NSMutableArray array];
// block to write out xml for individual programs
void (^generateXmlForDiskImages)(NSArray *) = ^(NSArray *diskImages) {
DiskImageInfo *firstImage = [diskImages firstObject];
const char* cFilename = [firstImage.name UTF8String];
int slot = (int) firstImage.slotNumber;
tempXML += "<config version=\"2\">";
tempXML += "<name>";
tempXML += getfilenoext(cFilename);
tempXML += "</name>";
if (slot==6)
tempXML += "<format>APPLE 2</format>";
else
tempXML += "<format>2GS</format>";
tempXML += "<pic type=\"thumbnail\">";
// tempXML += utf8docdir;
// tempXML += "/";
tempXML += getfilenoext(cFilename);
tempXML += ".png</pic>";
MyString slotstr;
slotstr.Format("%d",slot);
BOOL isIIGSMultiMoreThanTwoDisks = slot != 6 && diskImages.count > 2;
[diskImages enumerateObjectsUsingBlock:^(DiskImageInfo *diskImage, NSUInteger idx, BOOL * _Nonnull stop) {
unsigned long diskIndex = idx + 1;
if ( slot == 6 || isIIGSMultiMoreThanTwoDisks ) {
// for Apple II disks, assume 1 disk drive
// for Apple IIGS that have more than 2 disks, use 1 drive
diskIndex = 1;
}
tempXML += "<image slot=\"";
tempXML += slotstr;
tempXML += [[NSString stringWithFormat:@"\" disk=\"%lu\">",diskIndex] UTF8String];
tempXML += [diskImage.name UTF8String];
tempXML += "</image>";
}];
tempXML += "<bootslot>";
tempXML += slotstr;
tempXML += "</bootslot>";
tempXML += "</config>";
};
for (NSString *s in fileList)
{
const char* fn = [s UTF8String];
@ -211,22 +268,16 @@ static UIImage* defaultImageII = nil;
if (ignoreList.Find(pat)!=-1)
continue;
/*
MyString fullpathname = utf8docdir;
fullpathname += "/";
fullpathname += fn;
*/
// check if file exists
MyString path;
MyString shortname;
// CDownload dl;
// dl.bNotifyDownloadFailure = true;
dl.retrieveFile(fn,path,shortname);
FILE* f= fopen(path.c_str(),"rb");
if (!f) continue;
fseek(f,0,SEEK_END);
int si = ftell(f);
// determine slot based on disk size
fseek(f,0,SEEK_END);
int si = (int) ftell(f);
int slot;
if (si < 800*1024)
slot = 6;
@ -235,37 +286,52 @@ static UIImage* defaultImageII = nil;
slot = 5;
else
slot = 7;
tempXML += "<config version=\"2\">";
tempXML += "<name>";
tempXML += getfilenoext(fn);
tempXML += "</name>";
if (slot==6)
tempXML += "<format>APPLE 2</format>";
else
tempXML += "<format>2GS</format>";
tempXML += "<pic type=\"thumbnail\">";
// tempXML += utf8docdir;
// tempXML += "/";
tempXML += getfilenoext(fn);
tempXML += ".png</pic>";
tempXML += "<image slot=\"";
MyString slotstr;
slotstr.Format("%d",slot);
tempXML += slotstr;
tempXML += "\" disk=\"1\">";
tempXML += fn;
tempXML += "</image>";
tempXML += "<bootslot>";
tempXML += slotstr;
tempXML += "</bootslot>";
tempXML += "</config>";
fclose(f);
// strip out the spaces in the filename to help in comparison
NSString *comparisonFilename = [[s stringByReplacingOccurrencesOfString:@" " withString:@""] lowercaseString];
if ( currentMultiDiskPrefix != nil && [comparisonFilename containsString:currentMultiDiskPrefix] ) {
// in the middle of multidisk images, add this and continue
DiskImageInfo *info = [[DiskImageInfo alloc] init];
info.name = s;
info.slotNumber = slot;
[currentMultiDiskFilenames addObject:info];
continue;
}
// finish up creating the last multidisk images, if necessary
if ( [currentMultiDiskFilenames count] > 0 ) {
generateXmlForDiskImages(currentMultiDiskFilenames);
[currentMultiDiskFilenames removeAllObjects];
currentMultiDiskPrefix = nil;
}
// this is a new disk image set
// check if multi disk
NSRange rangeOfMultiDiskIndicator = [comparisonFilename rangeOfString:@"disk1"];
if ( rangeOfMultiDiskIndicator.location != NSNotFound ) {
[currentMultiDiskFilenames removeAllObjects];
DiskImageInfo *info = [[DiskImageInfo alloc] init];
info.name = s;
info.slotNumber = slot;
[currentMultiDiskFilenames addObject:info];
currentMultiDiskPrefix = [[comparisonFilename substringWithRange:NSMakeRange(0, rangeOfMultiDiskIndicator.location)] lowercaseString];
continue;
}
// single disk
DiskImageInfo *diskImage = [[DiskImageInfo alloc] init];
diskImage.name = s;
diskImage.slotNumber = slot;
generateXmlForDiskImages(@[diskImage]);
}
// deal with any left over multidisk images
if ( [currentMultiDiskFilenames count] > 0 ) {
generateXmlForDiskImages(currentMultiDiskFilenames);
[currentMultiDiskFilenames removeAllObjects];
currentMultiDiskPrefix = nil;
}
tempXML+="</list>";
@ -293,7 +359,8 @@ static UIImage* defaultImageII = nil;
else
{
MyString baseURL;
[self retrieveDocumentList:xmlString withBaseURL:baseURL];
[self createXmlUsingStringRef:xmlString withBaseURLRef:baseURL];
// [self retrieveDocumentList:xmlString withBaseURL:baseURL];
list.pathName = baseURL;
list.processString(xmlString.c_str());