[57358] users/toby/objcports

toby at macports.org toby at macports.org
Wed Sep 9 23:26:15 PDT 2009


Revision: 57358
          http://trac.macports.org/changeset/57358
Author:   toby at macports.org
Date:     2009-09-09 23:26:12 -0700 (Wed, 09 Sep 2009)
Log Message:
-----------
begin converting to C

Modified Paths:
--------------
    users/toby/objcports/MPArrayAdditions.h
    users/toby/objcports/MPArrayAdditions.m
    users/toby/objcports/MPDictionaryAdditions.h
    users/toby/objcports/MPDictionaryAdditions.m
    users/toby/objcports/MPIndex.h
    users/toby/objcports/MPStringAdditions.h
    users/toby/objcports/MPStringAdditions.m
    users/toby/objcports/objcports.xcodeproj/project.pbxproj
    users/toby/objcports/port.m

Added Paths:
-----------
    users/toby/objcports/MPIndex.c
    users/toby/objcports/internal.c
    users/toby/objcports/internal.h

Removed Paths:
-------------
    users/toby/objcports/MPIndex.m

Modified: users/toby/objcports/MPArrayAdditions.h
===================================================================
--- users/toby/objcports/MPArrayAdditions.h	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/MPArrayAdditions.h	2009-09-10 06:26:12 UTC (rev 57358)
@@ -1,3 +1,7 @@
+CFArrayRef CFArrayCreateWithTclObjects(CFAllocatorRef allocator, Tcl_Obj **objects, CFIndex count);
+
+#ifdef __OBJC__
 @interface NSArray (MPArrayAdditions)
 - (id)initWithTclObjects:(Tcl_Obj * const *)objects count:(int)count;
 @end
+#endif

Modified: users/toby/objcports/MPArrayAdditions.m
===================================================================
--- users/toby/objcports/MPArrayAdditions.m	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/MPArrayAdditions.m	2009-09-10 06:26:12 UTC (rev 57358)
@@ -4,6 +4,23 @@
 #include "MPArrayAdditions.h"
 #include "MPStringAdditions.h"
 
+CFArrayRef
+CFArrayCreateWithTclObjects(CFAllocatorRef allocator, Tcl_Obj **objects, CFIndex count)
+{
+	CFIndex i;
+	CFStringRef array[count];
+	CFArrayRef result;
+
+	for (i = 0; i < count; i++) {
+		array[i] = CFStringCreateWithTclObject(allocator, objects[i]);
+	}
+	result = CFArrayCreate(allocator, (const void **)array, count, &kCFTypeArrayCallBacks);
+	for (i = 0; i < count; i++) {
+		CFRelease(array[i]);
+	}
+	return result;
+}
+
 @implementation NSArray (MPArrayAdditions)
 
 - (id)initWithTclObjects:(Tcl_Obj * const *)objects count:(int)count

Modified: users/toby/objcports/MPDictionaryAdditions.h
===================================================================
--- users/toby/objcports/MPDictionaryAdditions.h	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/MPDictionaryAdditions.h	2009-09-10 06:26:12 UTC (rev 57358)
@@ -1,3 +1 @@
- at interface NSDictionary (MPDictionaryAdditions)
-- (id)initWithTclObjects:(Tcl_Obj * const *)objects count:(int)count;
- at end
+CFDictionaryRef CFDictionaryCreateWithTclObjects(CFAllocatorRef allocator, Tcl_Obj **objects, CFIndex count);

Modified: users/toby/objcports/MPDictionaryAdditions.m
===================================================================
--- users/toby/objcports/MPDictionaryAdditions.m	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/MPDictionaryAdditions.m	2009-09-10 06:26:12 UTC (rev 57358)
@@ -4,31 +4,28 @@
 #include "MPDictionaryAdditions.h"
 #include "MPStringAdditions.h"
 
- at implementation NSDictionary (MPDictionaryAdditions)
-
-- (id)initWithTclObjects:(Tcl_Obj * const *)objects count:(int)count
+CFDictionaryRef
+CFDictionaryCreateWithTclObjects(CFAllocatorRef allocator, Tcl_Obj **objects, CFIndex count)
 {
-	int count2, i;
-	NSDictionary *result;
+	CFIndex count2, i;
+	CFDictionaryRef result;
 
 	if ((count % 2) != 0) {
 		return nil;
 	}
 
 	count2 = count / 2;
-
-	NSString *keys[count2];
-	NSString *objs[count2];
+	
+	const void *keys[count2];
+	const void *values[count2];
 	for (i = 0; i < count2; i++) {
-		keys[i] = [[NSString alloc] initWithTclObject:objects[i * 2]];
-		objs[i] = [[NSString alloc] initWithTclObject:objects[i * 2 + 1]];
+		keys[i] = CFStringCreateWithTclObject(allocator, objects[i * 2]);
+		values[i] = CFStringCreateWithTclObject(allocator, objects[i * 2 + 1]);
 	}
-	result = [self initWithObjects:objs forKeys:keys count:count2];
+	result = CFDictionaryCreate(allocator, keys, values, count2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
 	for (i = 0; i < count2; i++) {
-		[keys[i] release];
-		[objs[i] release];
+		CFRelease(keys[i]);
+		CFRelease(values[i]);
 	}
 	return result;
 }
-
- at end

Copied: users/toby/objcports/MPIndex.c (from rev 57175, users/toby/objcports/MPIndex.m)
===================================================================
--- users/toby/objcports/MPIndex.c	                        (rev 0)
+++ users/toby/objcports/MPIndex.c	2009-09-10 06:26:12 UTC (rev 57358)
@@ -0,0 +1,68 @@
+#include <CoreFoundation/CoreFoundation.h>
+#include <tcl.h>
+
+#include "MPIndex.h"
+#include "MPArrayAdditions.h"
+#include "MPDictionaryAdditions.h"
+#include "MPStringAdditions.h"
+#include "internal.h"
+
+CFDictionaryRef
+MPCopyPortIndex(CFStringRef filename)
+{
+	CFMutableDictionaryRef result = NULL;
+	Tcl_Interp *interp;
+	char *fn;
+	Tcl_Channel chan;
+
+	result = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+
+	interp = Tcl_CreateInterp();
+	assert(Tcl_SetSystemEncoding(interp, "utf-8") == TCL_OK);
+	fn = strdup_cf(filename);
+	chan = Tcl_OpenFileChannel(interp, fn, "r", 0);
+	free(fn);
+	Tcl_RegisterChannel(interp, chan);
+
+	while (1) {
+		int objc;
+		Tcl_Obj **objv;
+		const void *key, *value;
+		CFArrayRef info;
+		Tcl_Obj *line;
+		int len;
+
+		line = Tcl_NewObj();
+		Tcl_IncrRefCount(line);
+
+		/* Read info line. */
+		if (Tcl_GetsObj(chan, line) < 0) {
+			Tcl_DecrRefCount(line);
+			break;
+		}
+		Tcl_ListObjGetElements(interp, line, &objc, &objv);
+		info = CFArrayCreateWithTclObjects(NULL, objv, objc);
+		assert(CFArrayGetCount(info) == 2);
+		key = CFRetain(CFArrayGetValueAtIndex(info, 0));
+		len = CFStringGetIntValue(CFArrayGetValueAtIndex(info, 1));
+		CFRelease(info);
+
+		/* Read dictionary. */
+		Tcl_ReadChars(chan, line, len, 0);
+		Tcl_ListObjGetElements(interp, line, &objc, &objv);
+		value = CFDictionaryCreateWithTclObjects(NULL, objv, objc);
+		assert(value != nil);
+
+		/* Store data. */
+		CFDictionarySetValue(result, key, value);
+		CFRelease(key);
+		CFRelease(value);
+
+		Tcl_DecrRefCount(line);
+	}
+
+	Tcl_UnregisterChannel(interp, chan);
+	Tcl_DeleteInterp(interp);
+
+	return result;
+}

Modified: users/toby/objcports/MPIndex.h
===================================================================
--- users/toby/objcports/MPIndex.h	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/MPIndex.h	2009-09-10 06:26:12 UTC (rev 57358)
@@ -1,10 +1 @@
- at interface MPIndex : NSObject
-{
-	NSMutableDictionary *_storage;
-}
-
-- (id)initWithPortindex:(NSString *)portindex;
-
-- (NSDictionary *)fullIndex;
-
- at end
+CFDictionaryRef MPCopyPortIndex(CFStringRef filename);

Deleted: users/toby/objcports/MPIndex.m
===================================================================
--- users/toby/objcports/MPIndex.m	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/MPIndex.m	2009-09-10 06:26:12 UTC (rev 57358)
@@ -1,117 +0,0 @@
-#include <Foundation/Foundation.h>
-#include <tcl.h>
-
-#include "MPIndex.h"
-#include "MPArrayAdditions.h"
-#include "MPDictionaryAdditions.h"
-#include "MPStringAdditions.h"
-
-static NSDictionary *
-CopyIndexEntryFromTclList(Tcl_Interp *interp, Tcl_Obj **objects, int count)
-{
-	int count2, i;
-	NSDictionary *result = nil;
-
-	if ((count % 2) != 0) {
-		return nil;
-	}
-
-	count2 = count / 2;
-
-	NSString *keys[count2];
-	id objs[count2];
-	for (i = 0; i < count2; i++) {
-		keys[i] = [[NSString alloc] initWithTclObject:objects[i * 2]];
-		if ([keys[i] isEqualToString:@"variant_desc"]) {
-			int objc;
-			Tcl_Obj **objv;
-			Tcl_ListObjGetElements(interp, objects[i * 2 + 1], &objc, &objv);
-			objs[i] = [[NSDictionary alloc] initWithTclObjects:objv count:objc];
-		} else if ([keys[i] hasPrefix:@"depends_"]) {
-			int objc;
-			Tcl_Obj **objv;
-			Tcl_ListObjGetElements(interp, objects[i * 2 + 1], &objc, &objv);
-			objs[i] = [[NSArray alloc] initWithTclObjects:objv count:objc];
-		} else {
-			objs[i] = [[NSString alloc] initWithTclObject:objects[i * 2 + 1]];
-		}
-	}
-	result = [[NSDictionary alloc] initWithObjects:objs forKeys:keys count:count2];
-	for (i = 0; i < count2; i++) {
-		[keys[i] release];
-		[objs[i] release];
-	}
-	return result;
-}
-
- at implementation MPIndex
-
-- (id)initWithPortindex:(NSString *)portindex
-{
-	Tcl_Interp *interp;
-	Tcl_Channel chan;
-
-	self = [super init];
-
-	_storage = [[NSMutableDictionary alloc] initWithCapacity:0];
-
-	interp = Tcl_CreateInterp();
-	assert(Tcl_SetSystemEncoding(interp, "utf-8") == TCL_OK);
-	chan = Tcl_OpenFileChannel(interp, [portindex UTF8String], "r", 0);
-	Tcl_RegisterChannel(interp, chan);
-
-	while (1) {
-		int objc;
-		Tcl_Obj **objv;
-		id key, object;
-		NSArray *info;
-		Tcl_Obj *line;
-		int len;
-
-		line = Tcl_NewObj();
-		Tcl_IncrRefCount(line);
-
-		/* Read info line. */
-		if (Tcl_GetsObj(chan, line) < 0) {
-			Tcl_DecrRefCount(line);
-			break;
-		}
-		Tcl_ListObjGetElements(interp, line, &objc, &objv);
-		info = [[NSArray alloc] initWithTclObjects:objv count:objc];
-		assert([info count] == 2);
-		key = [[info objectAtIndex:0] retain];
-		len = [[info objectAtIndex:1] intValue];
-		[info release];
-
-		/* Read dictionary. */
-		Tcl_ReadChars(chan, line, len, 0);
-		Tcl_ListObjGetElements(interp, line, &objc, &objv);
-		object = CopyIndexEntryFromTclList(interp, objv, objc);
-		assert(object != nil);
-
-		/* Store data. */
-		[_storage setObject:object forKey:key];
-		[object release];
-		[key release];
-
-		Tcl_DecrRefCount(line);
-	}
-
-	Tcl_UnregisterChannel(interp, chan);
-	Tcl_DeleteInterp(interp);
-
-	return self;
-}
-
-- (void)dealloc
-{
-	[_storage release];
-	[super dealloc];
-}
-
-- (NSDictionary *)fullIndex
-{
-	return _storage;
-}
-
- at end

Modified: users/toby/objcports/MPStringAdditions.h
===================================================================
--- users/toby/objcports/MPStringAdditions.h	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/MPStringAdditions.h	2009-09-10 06:26:12 UTC (rev 57358)
@@ -1,3 +1,7 @@
+CFStringRef CFStringCreateWithTclObject(CFAllocatorRef allocator, Tcl_Obj *object);
+
+#ifdef __OBJC__
 @interface NSString (MPStringAdditions)
 - (id)initWithTclObject:(Tcl_Obj *)object;
 @end
+#endif

Modified: users/toby/objcports/MPStringAdditions.m
===================================================================
--- users/toby/objcports/MPStringAdditions.m	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/MPStringAdditions.m	2009-09-10 06:26:12 UTC (rev 57358)
@@ -3,6 +3,12 @@
 
 #include "MPStringAdditions.h"
 
+CFStringRef
+CFStringCreateWithTclObject(CFAllocatorRef allocator, Tcl_Obj *object)
+{
+	return CFStringCreateWithCString(allocator, Tcl_GetString(object), kCFStringEncodingUTF8);
+}
+
 @implementation NSString (MPStringAdditions)
 
 - (id)initWithTclObject:(Tcl_Obj *)object

Added: users/toby/objcports/internal.c
===================================================================
--- users/toby/objcports/internal.c	                        (rev 0)
+++ users/toby/objcports/internal.c	2009-09-10 06:26:12 UTC (rev 57358)
@@ -0,0 +1,21 @@
+#include <CoreFoundation/CoreFoundation.h>
+
+#include "internal.h"
+
+char *
+strdup_cf(CFStringRef str)
+{
+	CFIndex length, size;
+	char *result;
+
+	length = CFStringGetLength(str);
+	size = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
+	result = calloc(size, sizeof(char));
+	if (result) {
+		if (!CFStringGetCString(str, result, size, kCFStringEncodingUTF8)) {
+			free(result);
+			result = NULL;
+		}
+	}
+	return result;
+}

Added: users/toby/objcports/internal.h
===================================================================
--- users/toby/objcports/internal.h	                        (rev 0)
+++ users/toby/objcports/internal.h	2009-09-10 06:26:12 UTC (rev 57358)
@@ -0,0 +1 @@
+char *strdup_cf(CFStringRef str);
\ No newline at end of file

Modified: users/toby/objcports/objcports.xcodeproj/project.pbxproj
===================================================================
--- users/toby/objcports/objcports.xcodeproj/project.pbxproj	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/objcports.xcodeproj/project.pbxproj	2009-09-10 06:26:12 UTC (rev 57358)
@@ -9,7 +9,8 @@
 /* Begin PBXBuildFile section */
 		8DD76F9C0486AA7600D96B5E /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 08FB779EFE84155DC02AAC07 /* Foundation.framework */; };
 		DA13887B101AED7000F73A82 /* MPConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = DA13887A101AED7000F73A82 /* MPConfig.m */; };
-		DA96BED00F7C9C2500362779 /* MPIndex.m in Sources */ = {isa = PBXBuildFile; fileRef = DA96BECF0F7C9C2500362779 /* MPIndex.m */; };
+		DA7AF1BC1058D1E200CF2187 /* internal.c in Sources */ = {isa = PBXBuildFile; fileRef = DA7AF1BB1058D1E200CF2187 /* internal.c */; };
+		DA96BED00F7C9C2500362779 /* MPIndex.c in Sources */ = {isa = PBXBuildFile; fileRef = DA96BECF0F7C9C2500362779 /* MPIndex.c */; };
 		DA96BF4C0F7CA1A700362779 /* MPDictionaryAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = DA96BF4B0F7CA1A700362779 /* MPDictionaryAdditions.m */; };
 		DAD371710F0280EF0064AFF4 /* port.m in Sources */ = {isa = PBXBuildFile; fileRef = DAD371680F0280EF0064AFF4 /* port.m */; };
 		DAD371720F0280EF0064AFF4 /* MPArrayAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = DAD3716A0F0280EF0064AFF4 /* MPArrayAdditions.m */; };
@@ -35,8 +36,10 @@
 		8DD76FA10486AA7600D96B5E /* port */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = port; sourceTree = BUILT_PRODUCTS_DIR; };
 		DA138879101AED7000F73A82 /* MPConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPConfig.h; sourceTree = "<group>"; };
 		DA13887A101AED7000F73A82 /* MPConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPConfig.m; sourceTree = "<group>"; };
+		DA7AF1BA1058D1E200CF2187 /* internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = internal.h; sourceTree = "<group>"; };
+		DA7AF1BB1058D1E200CF2187 /* internal.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = internal.c; sourceTree = "<group>"; };
 		DA96BECE0F7C9C2500362779 /* MPIndex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPIndex.h; sourceTree = "<group>"; };
-		DA96BECF0F7C9C2500362779 /* MPIndex.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPIndex.m; sourceTree = "<group>"; };
+		DA96BECF0F7C9C2500362779 /* MPIndex.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = MPIndex.c; sourceTree = "<group>"; };
 		DA96BF4A0F7CA1A700362779 /* MPDictionaryAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPDictionaryAdditions.h; sourceTree = "<group>"; };
 		DA96BF4B0F7CA1A700362779 /* MPDictionaryAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPDictionaryAdditions.m; sourceTree = "<group>"; };
 		DAD371680F0280EF0064AFF4 /* port.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = port.m; sourceTree = "<group>"; };
@@ -83,14 +86,16 @@
 				DA96BF4A0F7CA1A700362779 /* MPDictionaryAdditions.h */,
 				DA96BF4B0F7CA1A700362779 /* MPDictionaryAdditions.m */,
 				DA96BECE0F7C9C2500362779 /* MPIndex.h */,
-				DA96BECF0F7C9C2500362779 /* MPIndex.m */,
+				DA96BECF0F7C9C2500362779 /* MPIndex.c */,
 				DAD3716D0F0280EF0064AFF4 /* MPPort.h */,
 				DAD3716E0F0280EF0064AFF4 /* MPPort.m */,
 				DAD3716F0F0280EF0064AFF4 /* MPStringAdditions.h */,
 				DAD371700F0280EF0064AFF4 /* MPStringAdditions.m */,
-				FD1CD53B1018F5AD0071534F /* variables.plist */,
 				DA138879101AED7000F73A82 /* MPConfig.h */,
 				DA13887A101AED7000F73A82 /* MPConfig.m */,
+				DA7AF1BA1058D1E200CF2187 /* internal.h */,
+				DA7AF1BB1058D1E200CF2187 /* internal.c */,
+				FD1CD53B1018F5AD0071534F /* variables.plist */,
 			);
 			name = Source;
 			sourceTree = "<group>";
@@ -165,10 +170,11 @@
 				DAD371710F0280EF0064AFF4 /* port.m in Sources */,
 				DAD371720F0280EF0064AFF4 /* MPArrayAdditions.m in Sources */,
 				DAD371750F0280EF0064AFF4 /* MPStringAdditions.m in Sources */,
-				DA96BED00F7C9C2500362779 /* MPIndex.m in Sources */,
+				DA96BED00F7C9C2500362779 /* MPIndex.c in Sources */,
 				DAD371740F0280EF0064AFF4 /* MPPort.m in Sources */,
 				DA96BF4C0F7CA1A700362779 /* MPDictionaryAdditions.m in Sources */,
 				DA13887B101AED7000F73A82 /* MPConfig.m in Sources */,
+				DA7AF1BC1058D1E200CF2187 /* internal.c in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

Modified: users/toby/objcports/port.m
===================================================================
--- users/toby/objcports/port.m	2009-09-10 06:22:21 UTC (rev 57357)
+++ users/toby/objcports/port.m	2009-09-10 06:26:12 UTC (rev 57358)
@@ -15,14 +15,12 @@
 
 	NSLog(@"%@", [MPConfig sharedConfig]);
 	
-#if 0
-	NSString *filename = [[NSString alloc] initWithUTF8String:argv[1]];
-	MPIndex *index = [[MPIndex alloc] initWithPortindex:filename];
-	[filename release];
-	// do stuff
-	[index release];
+	CFStringRef filename = CFStringCreateWithCString(NULL, argv[1], kCFStringEncodingUTF8);
+	CFDictionaryRef index = MPCopyPortIndex(filename);
+	CFShow(index);
+	CFRelease(index);
+	CFRelease(filename);
 	return 0;
-#endif
 
 	while (--argc) {
 		MPPort *port = [[MPPort alloc] initWithPath:[NSString stringWithUTF8String:*++argv] options:nil];
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macports-changes/attachments/20090909/b8a1ea2a/attachment-0001.html>


More information about the macports-changes mailing list