summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDamiano Galassi <[email protected]>2016-02-26 12:06:37 +0100
committerDamiano Galassi <[email protected]>2016-02-26 12:06:37 +0100
commit5764ff3816214014c822ddf3c8099ec3e5f69b61 (patch)
tree16369dca977e61ec8173efd62fc8b4a25f8625d4
parentc3c1f6b8f4464c98369a2a127f771399d86c25ef (diff)
MacGui: add a NSDictionary extension to convert from and to hb_dict_t.
-rw-r--r--macosx/HandBrake.xcodeproj/project.pbxproj6
-rw-r--r--macosx/NSDictionary+HBAdditions.h19
-rw-r--r--macosx/NSDictionary+HBAdditions.m191
3 files changed, 216 insertions, 0 deletions
diff --git a/macosx/HandBrake.xcodeproj/project.pbxproj b/macosx/HandBrake.xcodeproj/project.pbxproj
index cfd33e7da..119b7eedb 100644
--- a/macosx/HandBrake.xcodeproj/project.pbxproj
+++ b/macosx/HandBrake.xcodeproj/project.pbxproj
@@ -112,6 +112,7 @@
A932E26F198833920047D13E /* HBAudioDefaultsController.m in Sources */ = {isa = PBXBuildFile; fileRef = A932E26E198833920047D13E /* HBAudioDefaultsController.m */; };
A932E273198834130047D13E /* HBAudioDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = A932E272198834130047D13E /* HBAudioDefaults.m */; };
A937EECB1C6C7C0300EEAE6D /* dsa_pub.pem in Resources */ = {isa = PBXBuildFile; fileRef = A937EECA1C6C7C0300EEAE6D /* dsa_pub.pem */; };
+ A93B0DF81C804CF50051A3FA /* NSDictionary+HBAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = A93B0DF71C804CF50051A3FA /* NSDictionary+HBAdditions.m */; };
A93E0ED31972957000FD67FB /* HBVideoController.m in Sources */ = {isa = PBXBuildFile; fileRef = A93E0ED11972957000FD67FB /* HBVideoController.m */; };
A93E0ED71972958C00FD67FB /* Video.xib in Resources */ = {isa = PBXBuildFile; fileRef = A93E0ED51972958C00FD67FB /* Video.xib */; };
A93FD4751A62ABE800A6AC43 /* HBAudio.m in Sources */ = {isa = PBXBuildFile; fileRef = A93FD4741A62ABE800A6AC43 /* HBAudio.m */; };
@@ -346,6 +347,8 @@
A932E271198834130047D13E /* HBAudioDefaults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBAudioDefaults.h; sourceTree = "<group>"; };
A932E272198834130047D13E /* HBAudioDefaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBAudioDefaults.m; sourceTree = "<group>"; };
A937EECA1C6C7C0300EEAE6D /* dsa_pub.pem */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = dsa_pub.pem; sourceTree = "<group>"; };
+ A93B0DF61C804CF50051A3FA /* NSDictionary+HBAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+HBAdditions.h"; sourceTree = "<group>"; };
+ A93B0DF71C804CF50051A3FA /* NSDictionary+HBAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDictionary+HBAdditions.m"; sourceTree = "<group>"; };
A93E0ED01972957000FD67FB /* HBVideoController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HBVideoController.h; sourceTree = "<group>"; };
A93E0ED11972957000FD67FB /* HBVideoController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HBVideoController.m; sourceTree = "<group>"; };
A93E0ED61972958C00FD67FB /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = Video.xib; sourceTree = "<group>"; };
@@ -819,6 +822,8 @@
A997D8EB1A4ABB0900E19B6F /* HBPresetCoding.h */,
A975C08C1AE8C5270061870D /* HBStateFormatter.h */,
A975C08D1AE8C5270061870D /* HBStateFormatter.m */,
+ A93B0DF61C804CF50051A3FA /* NSDictionary+HBAdditions.h */,
+ A93B0DF71C804CF50051A3FA /* NSDictionary+HBAdditions.m */,
);
name = Others;
sourceTree = "<group>";
@@ -1277,6 +1282,7 @@
A9C9F88919A733FE00DC8923 /* HBHUDView.m in Sources */,
A932E26F198833920047D13E /* HBAudioDefaultsController.m in Sources */,
A91AFD0F1A949472009BECED /* HBJobOutputFileWriter.m in Sources */,
+ A93B0DF81C804CF50051A3FA /* NSDictionary+HBAdditions.m in Sources */,
46AB433515F98A2B009C0961 /* DockTextField.m in Sources */,
A9DEC87F1A23DF6F00C79B48 /* HBJob.m in Sources */,
A9E2FD271A21BC4A000E8D3F /* HBAddPresetController.m in Sources */,
diff --git a/macosx/NSDictionary+HBAdditions.h b/macosx/NSDictionary+HBAdditions.h
new file mode 100644
index 000000000..974739b9f
--- /dev/null
+++ b/macosx/NSDictionary+HBAdditions.h
@@ -0,0 +1,19 @@
+/* NSDictionary+HBAdditions.h $
+
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr/>.
+ It may be used under the terms of the GNU General Public License. */
+
+#import <Foundation/Foundation.h>
+#include "hb_dict.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface NSDictionary (HBAddtions)
+
+- (instancetype)initWithHBDict:(const hb_dict_t *)dict;
+- (hb_dict_t *)hb_dictValue;
+
+@end
+
+NS_ASSUME_NONNULL_END
diff --git a/macosx/NSDictionary+HBAdditions.m b/macosx/NSDictionary+HBAdditions.m
new file mode 100644
index 000000000..5e3ef5982
--- /dev/null
+++ b/macosx/NSDictionary+HBAdditions.m
@@ -0,0 +1,191 @@
+/* NSDictionary+HBAdditions.m $
+
+ This file is part of the HandBrake source code.
+ Homepage: <http://handbrake.fr/>.
+ It may be used under the terms of the GNU General Public License. */
+
+#import "NSDictionary+HBAdditions.h"
+
+@implementation NSDictionary (HBAddtions)
+
+- (instancetype)initWithHBDict:(const hb_dict_t *)dict
+{
+ self = [self init];
+ if (self)
+ {
+ self = convertDictToObjcType(dict);
+ }
+ return self;
+}
+
+- (hb_dict_t *)hb_dictValue
+{
+ return convertObjcDictToHBValue(self);
+}
+
+#pragma mark - hb_dict_t to NSDictionary
+
+static id valueToObjcValue(const hb_value_t *val)
+{
+ hb_value_type_t val_type = hb_value_type(val);
+ id result = nil;
+
+ switch (val_type)
+ {
+ case HB_VALUE_TYPE_INT:
+ result = @(hb_value_get_int(val));
+ break;
+
+ case HB_VALUE_TYPE_DOUBLE:
+ result = @(hb_value_get_double(val));
+ break;
+
+ case HB_VALUE_TYPE_BOOL:
+ result = @((BOOL)hb_value_get_bool(val));
+ break;
+
+ case HB_VALUE_TYPE_STRING:
+ result = @(hb_value_get_string(val));
+ break;
+
+ case HB_VALUE_TYPE_DICT:
+ result = convertDictToObjcType(val);
+ break;
+
+ case HB_VALUE_TYPE_ARRAY:
+ result = convertArrayToObjcType(val);
+ break;
+
+ case HB_VALUE_TYPE_NULL:
+ result = [NSNull null];
+ break;
+
+ default:
+ break;
+ }
+
+ return result;
+}
+
+static NSDictionary * convertDictToObjcType(const hb_dict_t *dict)
+{
+ NSMutableDictionary *result = [NSMutableDictionary dictionary];
+ hb_dict_iter_t iter;
+
+ for (iter = hb_dict_iter_init(dict);
+ iter != HB_DICT_ITER_DONE;
+ iter = hb_dict_iter_next(dict, iter))
+ {
+ const NSString *key = @(hb_dict_iter_key(iter));
+ const hb_value_t *val = hb_dict_iter_value(iter);
+
+ id objcType = valueToObjcValue(val);
+
+ if (objcType)
+ {
+ result[key] = objcType;
+ }
+ }
+
+ return [result copy];
+}
+
+static NSArray * convertArrayToObjcType(const hb_value_array_t *array)
+{
+ size_t count = hb_value_array_len(array);
+ NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
+
+ for (int ii = 0; ii < count; ii++)
+ {
+ const hb_value_t *val = hb_value_array_get(array, ii);
+ id objcType = valueToObjcValue(val);
+
+ if (objcType)
+ {
+ [result addObject:objcType];
+ }
+ }
+
+ return [result copy];
+}
+
+#pragma mark - NSDictionary to hb_dict_t
+
+static hb_value_t * objcValueToHBValue(id val)
+{
+ hb_value_t *result = NULL;
+
+ if ([val isKindOfClass:[NSNumber class]])
+ {
+ NSNumber *number = val;
+ const char *objCType = number.objCType;
+
+ if((strcmp(objCType, @encode(float))) == 0 ||
+ (strcmp(objCType, @encode(double))) == 0)
+ {
+ result = hb_value_double(number.doubleValue);
+ }
+ else if((strcmp(objCType, @encode(BOOL))) == 0)
+ {
+ result = hb_value_bool(number.boolValue);
+ }
+ else
+ {
+ result = hb_value_int(number.integerValue);
+ }
+
+ }
+ else if ([val isKindOfClass:[NSString class]])
+ {
+ NSString *string = val;
+ result = hb_value_string(string.UTF8String);
+
+ }
+ else if ([val isKindOfClass:[NSDictionary class]])
+ {
+ result = convertObjcDictToHBValue(val);
+ }
+ else if ([val isKindOfClass:[NSArray class]])
+ {
+ result = convertObjcArrayToHBValue(val);
+ }
+
+ return result;
+}
+
+static hb_dict_t * convertObjcDictToHBValue(NSDictionary *dict)
+{
+ hb_dict_t *result = hb_dict_init();
+
+ [dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop)
+ {
+ if ([key isKindOfClass:[NSString class]])
+ {
+ hb_value_t *val = objcValueToHBValue(obj);
+ if (val)
+ {
+ hb_dict_set(result, [key UTF8String], objcValueToHBValue(obj));
+ }
+ }
+ }];
+
+ return result;
+}
+
+static hb_value_array_t * convertObjcArrayToHBValue(NSArray *array)
+{
+ hb_value_array_t *result = hb_value_array_init();
+
+ for (id obj in array)
+ {
+ hb_value_t *val = objcValueToHBValue(obj);
+ if (val)
+ {
+ hb_value_array_append(result, objcValueToHBValue(obj));
+ }
+ }
+
+ return result;
+}
+
+@end