ios: Create an objective-c wrapper around config_file_t, use it to simplify code.

This commit is contained in:
meancoot 2013-02-21 22:16:18 -05:00
parent b52aff0b8f
commit be3aa0a063
13 changed files with 172 additions and 133 deletions

View File

@ -83,6 +83,7 @@
96C19C2216D2F3BA00FE8D5A /* RADirectoryFilterList.m in Sources */ = {isa = PBXBuildFile; fileRef = 96C19C2116D2F3BA00FE8D5A /* RADirectoryFilterList.m */; };
96C19C2416D453BA00FE8D5A /* RADirectoryGrid.m in Sources */ = {isa = PBXBuildFile; fileRef = 96C19C2316D453BA00FE8D5A /* RADirectoryGrid.m */; };
96C19C2916D5A56500FE8D5A /* browser.m in Sources */ = {isa = PBXBuildFile; fileRef = 96C19C2816D5A56400FE8D5A /* browser.m */; };
96C19C3016D7045700FE8D5A /* RAConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 96C19C2F16D7045700FE8D5A /* RAConfig.m */; };
96CF015016C2C0B700ABF9C9 /* overlay.c in Sources */ = {isa = PBXBuildFile; fileRef = 96AFAFCE16C1FBC0009DE44C /* overlay.c */; };
96CF015C16C2F72900ABF9C9 /* ios_input.c in Sources */ = {isa = PBXBuildFile; fileRef = 96CF015B16C2F72900ABF9C9 /* ios_input.c */; };
/* End PBXBuildFile section */
@ -231,6 +232,8 @@
96C19C2516D455BE00FE8D5A /* browser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = browser.h; sourceTree = "<group>"; };
96C19C2616D455BE00FE8D5A /* rarch_wrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rarch_wrapper.h; sourceTree = "<group>"; };
96C19C2816D5A56400FE8D5A /* browser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = browser.m; sourceTree = "<group>"; };
96C19C2E16D7045600FE8D5A /* RAConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RAConfig.h; sourceTree = "<group>"; };
96C19C2F16D7045700FE8D5A /* RAConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RAConfig.m; sourceTree = "<group>"; };
96CF015B16C2F72900ABF9C9 /* ios_input.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = ios_input.c; sourceTree = "<group>"; };
/* End PBXFileReference section */
@ -310,6 +313,8 @@
96366C6F16CAF62200D64A22 /* settings */,
96AFAE3416C1D4EA009DE44C /* Supporting Files */,
96CF015B16C2F72900ABF9C9 /* ios_input.c */,
96C19C2E16D7045600FE8D5A /* RAConfig.h */,
96C19C2F16D7045700FE8D5A /* RAConfig.m */,
963F5AC516CC523B009BBD19 /* RAGameView.m */,
96096DD716D1ABAF00BF4499 /* RAModuleInfoList.m */,
963F5AC616CC523B009BBD19 /* RAModuleList.m */,
@ -691,6 +696,7 @@
96C19C2216D2F3BA00FE8D5A /* RADirectoryFilterList.m in Sources */,
96C19C2416D453BA00FE8D5A /* RADirectoryGrid.m in Sources */,
96C19C2916D5A56500FE8D5A /* browser.m in Sources */,
96C19C3016D7045700FE8D5A /* RAConfig.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

26
ios/RetroArch/RAConfig.h Normal file
View File

@ -0,0 +1,26 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2013 - Jason Fetters
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
@interface RAConfig : NSObject
- (id)initWithPath:(NSString*)path;
- (void)writeToFile:(NSString*)path;
- (int)getIntNamed:(NSString*)name withDefault:(int)def;
- (unsigned)getUintNamed:(NSString*)name withDefault:(unsigned)def;
- (NSString*)getStringNamed:(NSString*)name withDefault:(NSString*)def;
- (void)putIntNamed:(NSString*)name value:(int)value;
- (void)putStringNamed:(NSString*)name value:(NSString*)value;
@end

89
ios/RetroArch/RAConfig.m Normal file
View File

@ -0,0 +1,89 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2013 - Jason Fetters
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#import "RAConfig.h"
#include "conf/config_file.h"
@implementation RAConfig
{
config_file_t* _config;
}
- (id)initWithPath:(NSString*)path
{
_config = config_file_new([path UTF8String]);
return self;
}
- (void)dealloc
{
if (_config)
config_file_free(_config);
}
- (void)writeToFile:(NSString*)path
{
if (_config)
config_file_write(_config, [path UTF8String]);
}
- (int)getIntNamed:(NSString*)name withDefault:(int)def
{
int result = def;
if (_config)
config_get_int(_config, [name UTF8String], &result);
return result;
}
- (unsigned)getUintNamed:(NSString*)name withDefault:(unsigned)def
{
unsigned result = def;
if (_config)
config_get_uint(_config, [name UTF8String], &result);
return result;
}
- (NSString*)getStringNamed:(NSString*)name withDefault:(NSString*)def
{
NSString* result = def;
if (_config)
{
char* data = 0;
if (config_get_string(_config, [name UTF8String], &data))
result = [NSString stringWithUTF8String:data];
free(data);
}
return result;
}
- (void)putIntNamed:(NSString*)name value:(int)value
{
if (_config)
config_set_int(_config, [name UTF8String], value);
}
- (void)putStringNamed:(NSString*)name value:(NSString*)value
{
if (_config)
config_set_string(_config, [name UTF8String], [value UTF8String]);
}
@end

View File

@ -13,13 +13,14 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#import "RAConfig.h"
#import "browser.h"
@implementation RADirectoryFilterList
{
NSString* _path;
config_file_t* _filterList;
RAConfig* _filterList;
unsigned _filterCount;
}
@ -30,92 +31,60 @@
if (path && ra_ios_is_file([path stringByAppendingPathComponent:@".rafilter"]))
{
config_file_t* configFile = config_file_new([[path stringByAppendingPathComponent:@".rafilter"] UTF8String]);
RAConfig* configFile = [[RAConfig alloc] initWithPath:[path stringByAppendingPathComponent:@".rafilter"]];
unsigned filterCount = [configFile getUintNamed:@"filter_count" withDefault:0];
if (configFile)
if (filterCount > 1)
return [[RADirectoryFilterList alloc] initWithPath:path config:configFile];
if (regex && filterCount == 1)
{
unsigned filterCount = 0;
if (configFile && config_get_uint(configFile, "filter_count", &filterCount) && filterCount > 1)
return [[RADirectoryFilterList alloc] initWithPath:path config:configFile];
char* regexValue = 0;
if (regex && filterCount == 1 && config_get_string(configFile, "filter_1_regex", &regexValue))
{
*regex = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithUTF8String:regexValue] options:0 error:nil];
free(regexValue);
}
config_file_free(configFile);
NSString* expr = [configFile getStringNamed:@"filter_1_regex" withDefault:@".*"];
*regex = [NSRegularExpression regularExpressionWithPattern:expr options:0 error:nil];
}
}
return nil;
}
- (id)initWithPath:(NSString*)path config:(config_file_t*)config
- (id)initWithPath:(NSString*)path config:(RAConfig*)config
{
self = [super initWithStyle:UITableViewStylePlain];
_path = path;
_filterList = config;
_filterCount = [_filterList getUintNamed:@"filter_count" withDefault:0];
if (!_filterList || !config_get_uint(_filterList, "filter_count", &_filterCount) || _filterCount == 0)
{
if (_filterCount == 0)
[RetroArch_iOS displayErrorMessage:@"No valid filters were found."];
}
[self setTitle: [path lastPathComponent]];
return self;
}
- (id)initWithPath:(NSString*)path
{
return [self initWithPath:path config:config_file_new([[path stringByAppendingPathComponent:@".rafilter"] UTF8String])];
}
- (void)dealloc
{
if (_filterList)
config_file_free(_filterList);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_filterList)
{
NSString* regexKey = [NSString stringWithFormat:@"filter_%d_regex", indexPath.row + 1];
char* regex = 0;
if (config_get_string(_filterList, [regexKey UTF8String], &regex))
{
NSRegularExpression* expr = [NSRegularExpression regularExpressionWithPattern:[NSString stringWithUTF8String:regex] options:0 error:nil];
free(regex);
[[RetroArch_iOS get] pushViewController:[RADirectoryList directoryListWithPath:_path filter:expr]];
}
}
NSString* regex = [NSString stringWithFormat:@"filter_%d_regex", indexPath.row + 1];
regex = [_filterList getStringNamed:regex withDefault:@".*"];
NSRegularExpression* expr = [NSRegularExpression regularExpressionWithPattern:regex options:0 error:nil];
[[RetroArch_iOS get] pushViewController:[RADirectoryList directoryListWithPath:_path filter:expr]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _filterCount;
return _filterCount ? _filterCount : 1;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* nameKey = [NSString stringWithFormat:@"filter_%d_name", indexPath.row + 1];
char* nameString = 0;
if (_filterList && config_get_string(_filterList, [nameKey UTF8String], &nameString))
{
nameKey = [NSString stringWithUTF8String:nameString];
free(nameString);
}
NSString* name = [NSString stringWithFormat:@"filter_%d_name", indexPath.row + 1];
name = [_filterList getStringNamed:name withDefault:@"BAD NAME"];
UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:@"filter"];
cell = (cell != nil) ? cell : [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"filter"];
cell.textLabel.text = nameKey;
cell.textLabel.text = name;
return cell;
}

View File

@ -13,33 +13,24 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#import "RAConfig.h"
#import "browser.h"
@implementation RADirectoryGrid
{
NSString* _path;
NSArray* _list;
UIImage* _templateImage;
}
- (id)initWithPath:(NSString*)path filter:(NSRegularExpression*)regex
{
_path = path ? path : ra_ios_get_browser_root();
// Load template image
NSString* templateName = [NSString stringWithFormat:@"%@/.coverart/template.png", _path];
_templateImage = [UIImage imageWithContentsOfFile:templateName];
if (!_templateImage)
{
[RetroArch_iOS displayErrorMessage:@"Coverart template.png is missing."];
_templateImage = [RetroArch_iOS get].file_icon;
}
//
RAConfig* config = [[RAConfig alloc] initWithPath:[NSString stringWithFormat:@"%@/.coverart/.config", _path]];
UICollectionViewFlowLayout* layout = [UICollectionViewFlowLayout new];
layout.itemSize = _templateImage.size;
layout.itemSize = CGSizeMake([config getUintNamed:@"item_width" withDefault:100], [config getUintNamed:@"item_height" withDefault:100]);
self = [super initWithCollectionViewLayout:layout];
_list = ra_ios_list_directory(_path, regex);

View File

@ -49,7 +49,7 @@ static NSString* check_path(NSString* path)
if ([UICollectionViewController instancesRespondToSelector:@selector(initWithCollectionViewLayout:)])
{
NSString* coverDir = path ? [path stringByAppendingPathComponent:@".coverart"] : nil;
if (coverDir && ra_ios_is_directory(coverDir) && ra_ios_is_file([coverDir stringByAppendingPathComponent:@"template.png"]))
if (coverDir && ra_ios_is_directory(coverDir))
return [[RADirectoryGrid alloc] initWithPath:path filter:regex];
}

View File

@ -14,7 +14,7 @@
*/
@implementation RAModuleInfo
+ (RAModuleInfo*)moduleWithPath:(NSString*)thePath data:(config_file_t*)theData
+ (RAModuleInfo*)moduleWithPath:(NSString*)thePath data:(RAConfig*)theData
{
RAModuleInfo* new = [RAModuleInfo new];
@ -22,22 +22,12 @@
new.data = theData;
return new;
}
- (void)dealloc
{
if (self.data)
{
config_file_free(self.data);
}
}
@end
static NSString* const labels[3] = {@"Emulator Name", @"Manufacturer", @"Name"};
static const char* const keys[3] = {"emuname", "manufacturer", "systemname"};
static const uint32_t sectionSizes[2] = {1, 2};
static NSString* const keys[3] = {@"emuname", @"manufacturer", @"systemname"};
static NSString* const sectionNames[2] = {@"Emulator", @"Hardware"};
static const uint32_t sectionSizes[2] = {1, 2};
@implementation RAModuleInfoList
{
@ -79,14 +69,7 @@ static NSString* const sectionNames[2] = {@"Emulator", @"Hardware"};
}
cell.textLabel.text = labels[sectionBase + indexPath.row];
char* text = 0;
if (config_get_string(_data.data, keys[sectionBase + indexPath.row], &text))
cell.detailTextLabel.text = [NSString stringWithUTF8String:text];
else
cell.detailTextLabel.text = @"Unspecified";
free(text);
cell.detailTextLabel.text = [_data.data getStringNamed:keys[sectionBase + indexPath.row] withDefault:@"Unspecified"];
return cell;
}

View File

@ -46,7 +46,7 @@
NSString* modulePath = [moduleList objectAtIndex:i];
NSString* baseName = [[modulePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"info"];
[_modules addObject:[RAModuleInfo moduleWithPath:modulePath data:config_file_new([baseName UTF8String])]];
[_modules addObject:[RAModuleInfo moduleWithPath:modulePath data:[[RAConfig alloc] initWithPath:baseName]]];
}
[self setTitle:@"Choose Emulator"];

View File

@ -32,6 +32,4 @@ extern NSString* ra_ios_get_browser_root();
// Check path to see if a directory filter list is needed.
// If one is not needed useExpression will be set to a default expression to use.
+ (RADirectoryFilterList*) directoryFilterListAtPath:(NSString*)path useExpression:(NSRegularExpression**)regex;
- (id)initWithPath:(NSString*)path;
@end

View File

@ -15,31 +15,16 @@
#import <objc/runtime.h>
#import "settings.h"
#include "config_file.h"
@implementation RASettingData
@end
static NSString* get_value_from_config(config_file_t* config, NSString* name, NSString* defaultValue)
static NSString* get_value_from_config(RAConfig* config, NSString* name, NSString* defaultValue)
{
NSString* value = nil;
char* v = 0;
if (config && config_get_string(config, [name UTF8String], &v))
{
value = [[NSString alloc] initWithUTF8String:v];
free(v);
}
else
{
value = defaultValue;
}
return value;
return [config getStringNamed:name withDefault:defaultValue];
}
static RASettingData* boolean_setting(config_file_t* config, NSString* name, NSString* label, NSString* defaultValue)
static RASettingData* boolean_setting(RAConfig* config, NSString* name, NSString* label, NSString* defaultValue)
{
RASettingData* result = [[RASettingData alloc] init];
result.type = BooleanSetting;
@ -49,7 +34,7 @@ static RASettingData* boolean_setting(config_file_t* config, NSString* name, NSS
return result;
}
static RASettingData* button_setting(config_file_t* config, NSString* name, NSString* label, NSString* defaultValue)
static RASettingData* button_setting(RAConfig* config, NSString* name, NSString* label, NSString* defaultValue)
{
RASettingData* result = [[RASettingData alloc] init];
result.type = ButtonSetting;
@ -68,7 +53,7 @@ static RASettingData* group_setting(NSString* label, NSArray* settings)
return result;
}
static RASettingData* enumeration_setting(config_file_t* config, NSString* name, NSString* label, NSString* defaultValue, NSArray* values)
static RASettingData* enumeration_setting(RAConfig* config, NSString* name, NSString* label, NSString* defaultValue, NSArray* values)
{
RASettingData* result = [[RASettingData alloc] init];
result.type = EnumerationSetting;
@ -79,7 +64,7 @@ static RASettingData* enumeration_setting(config_file_t* config, NSString* name,
return result;
}
static RASettingData* subpath_setting(config_file_t* config, NSString* name, NSString* label, NSString* defaultValue, NSString* path, NSString* extension)
static RASettingData* subpath_setting(RAConfig* config, NSString* name, NSString* label, NSString* defaultValue, NSString* path, NSString* extension)
{
NSString* value = get_value_from_config(config, name, defaultValue);
value = [value stringByReplacingOccurrencesOfString:path withString:@""];
@ -100,7 +85,7 @@ static RASettingData* subpath_setting(config_file_t* config, NSString* name, NSS
@implementation RASettingsList
- (id)init
{
config_file_t* config = config_file_new([[RetroArch_iOS get].configFilePath UTF8String]);
RAConfig* config = [[RAConfig alloc] initWithPath:[RetroArch_iOS get].configFilePath];
NSString* overlay_path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/overlays/"];
NSString* shader_path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/shaders/"];
@ -168,9 +153,6 @@ static RASettingData* subpath_setting(config_file_t* config, NSString* name, NSS
nil],
nil
];
if (config)
config_file_free(config);
self = [super initWithSettings:settings title:@"RetroArch Settings"];
return self;
@ -188,15 +170,12 @@ static RASettingData* subpath_setting(config_file_t* config, NSString* name, NSS
- (void)writeToDisk
{
config_file_t* config = config_file_new([[RetroArch_iOS get].configFilePath UTF8String]);
config = config ? config : config_file_new(0);
config_set_string(config, "system_directory", [[RetroArch_iOS get].system_directory UTF8String]);
RAConfig* config = [[RAConfig alloc] initWithPath:[RetroArch_iOS get].configFilePath];
[config putStringNamed:@"system_directory" value:[RetroArch_iOS get].system_directory];
[self writeSettings:nil toConfig:config];
config_file_write(config, [[RetroArch_iOS get].configFilePath UTF8String]);
config_file_free(config);
[config writeToFile:[RetroArch_iOS get].configFilePath];
}
@end

View File

@ -32,7 +32,7 @@ static const char* const SETTINGID = "SETTING";
return self;
}
- (void)writeSettings:(NSArray*)settingList toConfig:(config_file_t *)config
- (void)writeSettings:(NSArray*)settingList toConfig:(RAConfig*)config
{
NSArray* list = settingList ? settingList : settings;
@ -52,13 +52,13 @@ static const char* const SETTINGID = "SETTING";
case FileListSetting:
if ([setting.value length] > 0)
config_set_string(config, [setting.name UTF8String], [[setting.path stringByAppendingPathComponent:setting.value] UTF8String]);
[config putStringNamed:setting.name value:[setting.path stringByAppendingPathComponent:setting.value]];
else
config_set_string(config, [setting.name UTF8String], "");
[config putStringNamed:setting.name value:@""];
break;
default:
config_set_string(config, [setting.name UTF8String], [setting.value UTF8String]);
[config putStringNamed:setting.name value:setting.value];
break;
}
}

View File

@ -13,8 +13,6 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "conf/config_file.h"
enum SettingTypes
{
BooleanSetting, ButtonSetting, EnumerationSetting, FileListSetting, GroupSetting

View File

@ -16,16 +16,16 @@
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#include "conf/config_file.h"
#import "RAConfig.h"
@interface RAGameView : UIViewController
@end
@interface RAModuleInfo : NSObject
@property (strong) NSString* path;
@property config_file_t* data;
@property (strong) RAConfig* data;
+ (RAModuleInfo*)moduleWithPath:(NSString*)thePath data:(config_file_t*)theData;
+ (RAModuleInfo*)moduleWithPath:(NSString*)thePath data:(RAConfig*)theData;
@end
@interface RAModuleInfoList : UITableViewController
@ -43,7 +43,7 @@
@interface RASettingsSubList : UITableViewController
- (id)initWithSettings:(NSArray*)values title:(NSString*)title;
- (void)writeSettings:(NSArray*)settingList toConfig:(config_file_t*)config;
- (void)writeSettings:(NSArray*)settingList toConfig:(RAConfig*)config;
@end
@interface RASettingsList : RASettingsSubList