<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jon&#039;s Blog &#187; Cocoa</title>
	<atom:link href="http://jonshier.com/category/programming/cocoa/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonshier.com</link>
	<description>From my mind to your eyes.</description>
	<lastBuildDate>Sat, 16 Feb 2013 08:38:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>CoRD, Pixen, and the passage of time&#8230;</title>
		<link>http://jonshier.com/2013/02/16/cord-pixen-and-the-passage-of-time/</link>
		<comments>http://jonshier.com/2013/02/16/cord-pixen-and-the-passage-of-time/#comments</comments>
		<pubDate>Sat, 16 Feb 2013 08:33:08 +0000</pubDate>
		<dc:creator>jshier</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[CoRD]]></category>
		<category><![CDATA[Pixen]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jonshier.com/?p=64</guid>
		<description><![CDATA[It has been nearly two years since I last updated this blog and a lot has happened. First, my Pixen fork is dead. I&#8217;m sorry but I found it rather difficult to spend time on a project I for which &#8230; <a href="http://jonshier.com/2013/02/16/cord-pixen-and-the-passage-of-time/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>It has been nearly two years since I last updated this blog and a lot has happened.</p>
<p>First, my Pixen fork is dead. I&#8217;m sorry but I found it rather difficult to spend time on a project I for which had no personal use. Luckily Pixen&#8217;s original creators have started updating <a title="Pixen @ GitHub" href="https://github.com/Pixen/Pixen" target="_blank">the project</a> once again. They released version <a title="Download Pixen 3.5.10b1" href="http://mattrajca.com/assets/files/Pixen_3_5_10b1.zip" target="_blank">3.5.10b1</a> just a few weeks ago. It also looks like they&#8217;re taking the project commercial on the <a title="Pixen in the app store" href="http://itunes.apple.com/us/app/pixen/id525180431?mt=12" target="_blank">app store</a>. Anyone looking for a good pixel editor should give it a try.</p>
<p>Second, I&#8217;ve recently started hacking on<a title="CoRD @ GitHub" href="https://github.com/dorianj/CoRD" target="_blank"> CoRD</a>, an RDP client for OS X. I&#8217;m currently a windows server admin for my university and I needed a good RDP client. Microsoft&#8217;s official client just wasn&#8217;t cutting it for the number of servers I connect to on a daily basis and its stability was rather poor. So far I&#8217;ve modernized the code a bit, moved the minimum requirements to 64-bit on 10.6, and added one new feature, the ability to set a default username, password, and domain for new connections. I&#8217;ve also begun refactoring some of the more egregious metafiles (1000+ line AppController). Anyone interested can track my fork <a title="My CoRD fork @ GitHub" href="https://github.com/jshier/CoRD" target="_blank">here</a> and download my latest build <a title="My Latest CoRD Build" href="http://jonshier.com/wp-content/uploads/2013/02/CoRD.zip">here</a>. I&#8217;m very interested in getting CoRD switched from rdesktop to FreeRDP but have hit a few obstacles along the way. But I have finally gotten FreeRDP playing nice with Xcode so I&#8217;ll be working on it more over my spring break next week.</p>
<p>Finally, I&#8217;ll be graduating with a degree in computer science at the end of April. In the mean time I&#8217;ll continue hacking away on CoRD and hope to make some sort of contribution by the time I find a job. Speaking of which, I should probably put my resume up here somewhere.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonshier.com/2013/02/16/cord-pixen-and-the-passage-of-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pixen: Out with old, in with the nu</title>
		<link>http://jonshier.com/2011/05/16/pixen-out-with-old-in-with-the-nu/</link>
		<comments>http://jonshier.com/2011/05/16/pixen-out-with-old-in-with-the-nu/#comments</comments>
		<pubDate>Mon, 16 May 2011 04:16:36 +0000</pubDate>
		<dc:creator>jshier</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Jon's Life]]></category>
		<category><![CDATA[Pixen]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jonshier.com/?p=57</guid>
		<description><![CDATA[I&#8217;d been planning a Pixen update explaining my continued lack of progress for quite a while. That I was busy in my new role as an iOS developer for the company I&#8217;d been working at for over year. How I &#8230; <a href="http://jonshier.com/2011/05/16/pixen-out-with-old-in-with-the-nu/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;d been planning a Pixen update explaining my continued lack of progress for quite a while. That I was busy in my new role as an iOS developer for the company I&#8217;d been working at for over year. How I would surely make progress on Pixen by leveraging the skills I developed on the job. But then they decided to downsize their iOS team from 3 developers to 2 and I suddenly found myself unemployed. Luckily my night classes at the local community college have paid off and I&#8217;ve been admitted to the local university&#8217;s computer science program. So now instead of being to busy working to work on Pixen I&#8217;ll be too busy studying to work on Pixen. But fear not! I&#8217;ve found a solution: <strong>rewrite it</strong>. Yes, I know rewrites are usually a foolish waste of time and money, but fortunately being unpaid, all I&#8217;ll waste is time! Turns out that it&#8217;s much easier, at least for me, to create a pixel editor from scratch than it is to shoehorn Pixen&#8217;s current code into modern APIs. I call it nuPixen. It&#8217;s still in the prototype phase but it does enough to be worthy of a screenshot.<br />
<div id="attachment_58" class="wp-caption aligncenter" style="width: 1013px"><a href="http://jonshier.com/wp-content/uploads/2011/05/nuPixenPrototype.png"><img src="http://jonshier.com/wp-content/uploads/2011/05/nuPixenPrototype.png" alt="A bit simplistic?" title="nuPixenPrototype" width="1003" height="852" class="size-full wp-image-58" /></a><p class="wp-caption-text">It can edit pixels, I swear!</p></div><br />
You may have noticed by the window controls that I am, in fact, developing on 10.7. This will eliminate any version specific code I need to write and allow me to take advantage of all of the Lion APIs without worrying about compatibility. nuPixen will be open source on my GitHub page but will not be available until sometime after Lion&#8217;s release. In the mean time I&#8217;ll be posting updates on my progress and questions for users of Pixen and Mac developers in general about how I should move forward. Like I said, I&#8217;m still in the prototype phase, so that means I&#8217;m still trying to grok the OS X imaging APIs and how to fundamentally write a pixel editor. As you can see from the screenshot though, I do have basic image input, editing, and output working, along with the ability to set your fill color and switch the pixel grid on and off.</p>
<p>This is where I&#8217;ve hit my first issue: my pixel grid is uneven. Here&#8217;s my current code:</p><pre class="crayon-plain-tag">- (void)drawRect:(NSRect)dirtyRect
{
    context = [[NSGraphicsContext currentContext] graphicsPort];
    CGContextAddRect(context, NSRectToCGRect(self.bounds));
	CGContextSetRGBStrokeColor(context, 1.0f, 0.0f, 0.0f, 1.0f);
	CGContextStrokePath(context);
    CGContextSetInterpolationQuality(context, kCGInterpolationNone);
    CGContextSetShouldAntialias(context, NO);
    
    if (image)
    {
        
        NSRect imageRect = NSZeroRect;
        imageRect.size = CGImageGetSize([image CGImage]);
        drawRect = [self bounds];
        NSRect viewRect = drawRect;
        CGFloat aspectRatio = imageRect.size.width / imageRect.size.height;
        if (viewRect.size.width / viewRect.size.height &lt;= aspectRatio)
        {
            drawRect.size.width = viewRect.size.width;
            drawRect.size.height = imageRect.size.height * (viewRect.size.width / imageRect.size.width);
        }
        else
        {
            drawRect.size.height = viewRect.size.height;
            drawRect.size.width = imageRect.size.width * (viewRect.size.height / imageRect.size.height);
        }
        
        drawRect.origin.x += (viewRect.size.width - drawRect.size.width) / 2.0;
        drawRect.origin.y += (viewRect.size.height - drawRect.size.height) / 2.0;
        
        CGContextDrawImage(context, drawRect, [image CGImage]);
        
        if (showPixelGrid)
        {
            //Draw grid by creating start and end points for vertical and horizontal lines.
            //FIXME: Grid is uneven, especially at smaller sizes. 
            CGContextSetStrokeColorWithColor(context, CGColorGetConstantColor(kCGColorBlack));
            CGContextAddRect(context, drawRect);
            CGContextStrokePath(context);
            NSUInteger numXPoints = (NSUInteger)imageRect.size.width * 2;
            NSUInteger numYPoints = (NSUInteger)imageRect.size.height * 2;
            CGPoint xPoints[numXPoints];
            CGPoint yPoints[numYPoints];
            CGPoint startPoint;
            CGPoint endPoint;
            CGFloat widthRatio = drawRect.size.width / imageRect.size.width;
            CGFloat heightRatio = drawRect.size.height / imageRect.size.height;
            startPoint.x  = drawRect.origin.x;
            startPoint.y = drawRect.origin.y;
            endPoint.x = drawRect.origin.x;
            endPoint.y = drawRect.size.height + drawRect.origin.y;
            for (NSUInteger i = 0; i &lt; numXPoints; i += 2)
            {
                startPoint.x += widthRatio;
                endPoint.x += widthRatio;
                xPoints[i] = startPoint;
                xPoints[i + 1] = endPoint;
            }
            startPoint.x  = drawRect.origin.x;
            startPoint.y = drawRect.origin.y;
            endPoint.x = drawRect.size.width + drawRect.origin.x;
            endPoint.y = drawRect.origin.y;
            for (NSUInteger i = 0; i &lt; numYPoints; i += 2)
            {
                startPoint.y += heightRatio;
                endPoint.y += heightRatio;
                yPoints[i] = startPoint;
                yPoints[i + 1] = endPoint;
            }
            CGContextStrokeLineSegments(context, xPoints, numXPoints);
            CGContextStrokeLineSegments(context, yPoints, numYPoints);
        }
    }
       
}</pre><p>This method both resizes the image (currently stored as an NSBitmapImageRep) while maintaining the aspect ratio and draws the pixel grid when when it&#8217;s turned on. As you may be able to tell from the screenshot, the pixel grid is uneven, or at least appears to be. So my question is this, Mac developers: what would be a better way to draw the grid? As well, I currently access the image data by using NSBitmapImageReps&#8217; setColor:atX:y: and it seems to work fine, but is it the best way? I ask you so I can code and not worry that what I&#8217;m writing is stupidly wrong. I look forward to your input.</p>
<p>P.S. Does anyone know a good, free, code-friendly WordPress theme? Carrington is just too damn narrow.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonshier.com/2011/05/16/pixen-out-with-old-in-with-the-nu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Secret Lion Feature Revealed!</title>
		<link>http://jonshier.com/2010/11/19/secret-lion-feature-revealed/</link>
		<comments>http://jonshier.com/2010/11/19/secret-lion-feature-revealed/#comments</comments>
		<pubDate>Sat, 20 Nov 2010 04:41:21 +0000</pubDate>
		<dc:creator>jshier</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jonshier.com/?p=42</guid>
		<description><![CDATA[For whatever reason, I&#8217;m subscribed to the WebKit project commit feed. So I can see whenever a new change in pushed into the project. Sometimes those commits reveal things. Like one from yesterday. Commit 72356 simply states &#8220;Enable compaction support.&#8221; &#8230; <a href="http://jonshier.com/2010/11/19/secret-lion-feature-revealed/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>For whatever reason, I&#8217;m subscribed to the WebKit project commit feed. So I can see whenever a new change in pushed into the project. Sometimes those commits reveal things. Like one from yesterday. Commit<a href="http://trac.webkit.org/changeset/72356" target="_blank"> 72356</a> simply states &#8220;Enable compaction support.&#8221; And it sets a single linker flag for the Mac parts of the project.</p><pre class="crayon-plain-tag">OTHER_LDFLAGS_macosx_1070 = -Xlinker -objc_gc_compaction;</pre><p>Stunning, isn&#8217;t it? It may seem insignificant, but the addition of heap compaction to the Objective-C garbage collector is likely a prerequisite for its addition to the iOS SDK. You read about heap compaction in a great answer to this <a href="http://stackoverflow.com/questions/2663292/how-does-heap-compaction-work-quickly" target="_blank">StackOverflow question</a>. Why would it lead to garbage collection on iOS? Given that all iOS devices are vastly more memory constrained than their Mac OS X counterparts and lack on-disk virtual memory, anything can optimize memory use, especially for &#8220;free&#8221; from a developer&#8217;s perspective, is a big win. Reducing memory fragmentation, and more importantly, being able to reuse the same memory addresses during the common alloc/dealloc cycle that most objects go through will likely allow the Objective-C garbage collector to have the performance needed for Apple to enable it in iOS. As well, it&#8217;s continued evidence of Apple&#8217;s investment in Objective-C as the Mac OS X and iOS language of choice. It&#8217;s likely more details of this feature will be uncovered in the open source Objective-C runtime Apple produces, as well as the clang compiler project. A small but important feature we&#8217;ll see in Lion next year.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonshier.com/2010/11/19/secret-lion-feature-revealed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pixen: Where am I?</title>
		<link>http://jonshier.com/2010/08/21/pixen-where-am-i/</link>
		<comments>http://jonshier.com/2010/08/21/pixen-where-am-i/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 23:43:06 +0000</pubDate>
		<dc:creator>jshier</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Pixen]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jonshier.com/?p=39</guid>
		<description><![CDATA[Well, it has been almost a year since I posted about my adoption of Pixen. While I haven&#8217;t made the progress I had hoped in that time, there is some to report. My current GitHub repository builds on 10.6 only &#8230; <a href="http://jonshier.com/2010/08/21/pixen-where-am-i/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Well, it has been almost a year since I posted about my adoption of Pixen. While I haven&#8217;t made the progress I had hoped in that time, there is some to report. My current <a href="http://github.com/jshier/Pixen" target="_blank">GitHub repository</a> builds on 10.6 only and only with the 32-bit Intel architecture. I&#8217;ve gotten the build down to 3 warnings with all of the options I&#8217;ve enabled but the clang static analyzer still finds over a hundred issues with the code. There are also several bugs still present that I&#8217;ve been trying to narrow down. And, unfortunately, several major parts of Pixen must be rewritten nearly from scratch to build against the 64-bit APIs. Overall, this has turned out to be a much more complicated project that I&#8217;d hoped. Pixen consists of over 25,000 lines of custom code, and that doesn&#8217;t count 3rd party libraries, of which there are several. There are an assortment of custom classes used throughout the program, including custom UI elements and even the basic image type is custom. Combine this with very little in the way of documentation or comments and my new job, and I just haven&#8217;t had the time (or expertise) to delve as far into the code as necessary. So I apologize to anyone hoping for some Pixen progress.</p>
<p>There is still hope however. Pixen: we can rebuild it, we have the technology. Over the coming weeks, time allowing, I&#8217;ll be adding a list of several major issues that need to be addressed for Pixen to move into 2010. These will be posted on my GitHub page, so feel free to comment with any suggestions. I always welcome additional insight into the Pixen code. Even if you don&#8217;t implement features or fix bugs, pointing me in the right direction so I can try to do so myself would be great. I&#8217;ll be focusing on any bugs that pop up (I already have at least one) and the transition to the 64-bit APIs. One of the biggest changes I&#8217;d like to see, even if I don&#8217;t do much with the foundation of the program, is a refreshed UI. I am not a designer by any stretch of the imagination, so if anyone is interested in submitting UI ideas or artwork, especially the UI icons, I would greatly appreciate it.</p>
<p>Thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonshier.com/2010/08/21/pixen-where-am-i/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>I&#8217;ve adopted a Pixen!</title>
		<link>http://jonshier.com/2009/09/15/ive-adopted-a-pixen/</link>
		<comments>http://jonshier.com/2009/09/15/ive-adopted-a-pixen/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 03:20:55 +0000</pubDate>
		<dc:creator>jshier</dc:creator>
				<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Pixen]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jonshier.com/?p=31</guid>
		<description><![CDATA[Just over a week ago, Andy Matuschak, of Sparkle fame, asked for volunteers to adopt his Pixen project. It&#8217;s a pixel-based image editor for Mac OS X, and Andy just doesn&#8217;t have the time to maintain it or add features &#8230; <a href="http://jonshier.com/2009/09/15/ive-adopted-a-pixen/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Just over a week ago, <a title="Andy Matsuchak" href="http://andymatuschak.org/" target="_blank">Andy Matuschak</a>, of <a title="Sparkle" href="http://sparkle.andymatuschak.org/" target="_blank">Sparkle</a> fame, asked for volunteers to <a title="adopt his Pixen project" href="http://blog.andymatuschak.org/post/179455697/adopt-a-pixen" target="_blank">adopt his Pixen project</a>. It&#8217;s a pixel-based image editor for Mac OS X, and Andy just doesn&#8217;t have the time to maintain it or add features for the upcoming 4.0 release. So I offered my services. I figured I may be more successful working on an already established project rather than creating my own from scratch. You can follow <a title="my progress at GitHub" href="http://github.com/jshier/Pixen/" target="_blank">my progress at GitHub</a> and some of my code has already been integrated into the <a title="main Pixen repository" href="http://github.com/philippec/Pixen" target="_blank">main Pixen repository</a> there.</p>
<p>So far my efforts have been limited to project cleanup, modernization, and house keeping. I expect this to be a great learning experience for me going forward and I&#8217;ll be posting code updates as I understand more of the Pixen code. Right now my priorities are fixing all of the warnings I created with new compiler options, updating the Obj-C syntax and Cocoa API&#8217;s used, and taking a look at the performance. Plus we&#8217;ll all be discussing exactly what new features to add for version 4.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonshier.com/2009/09/15/ive-adopted-a-pixen/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>BitMaster &#8211; Yet another Mac torrent client&#8230;</title>
		<link>http://jonshier.com/2009/01/27/bitmaster-yet-another-mac-torrent-client/</link>
		<comments>http://jonshier.com/2009/01/27/bitmaster-yet-another-mac-torrent-client/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 19:59:31 +0000</pubDate>
		<dc:creator>jshier</dc:creator>
				<category><![CDATA[BitMaster]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://jonshier.com/?p=18</guid>
		<description><![CDATA[Over the last few years I&#8217;ve been teaching myself how to program. Little programs like ultrawar were my first complete programs, but I&#8217;ve read and learned a lot since then. I&#8217;ve been working on a BitTorrent client for the last &#8230; <a href="http://jonshier.com/2009/01/27/bitmaster-yet-another-mac-torrent-client/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Over the last few years I&#8217;ve been teaching myself how to program. Little programs like ultrawar were my first complete programs, but I&#8217;ve read and learned a lot since then. I&#8217;ve been working on a BitTorrent client for the last few years, mostly as a learning exercise but also with the goal of a real client at the end. I know Mac OS X doesn&#8217;t really need another torrent client, since Transmission rules and even the mighty uTorrent has a Mac version now. But I wondered what a client that focused solely on Mac technologies would look like, whether it could be any better than those that strive to live across platforms. So I&#8217;ve created BitMaster. Eventually it will be open source from this site, but it&#8217;s still in a very prototype stage (I can only read and parse torrent files, hash downloads, and create torrent files), so I&#8217;ll be posting code excerpts for now. It&#8217;s my hope that having public eyes on the project will help me learn to code and to actually release something, rather than playing with it whenever I&#8217;m in the mood. So here&#8217;s my first bit of code for public scrutiny: my torrent file parsing code.</p>
<p>Obviously, when I first sat down to develop BitMaster I had to determine what functionality I needed first. I chose torrent file parsing, since it seemed like a simple and ubiquitous part of torrent client development. You can see my first attempt a this code in my previous entry &#8220;Learning through doing&#8230;&#8221; which is quite rough. After feedback from various people online, it now looks like this:</p><pre class="crayon-plain-tag">//
//  JSbdecoder.m
//  BitMaster
//
//  Created by Jon Shier on 4/9/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import &quot;JSbdecoder.h&quot;

@implementation JSbdecoder

- (NSDictionary *) decodeTorrentFile: (NSString *) pathToTorrentFile {
    torrentData = [NSData dataWithContentsOfFile: pathToTorrentFile options: NSUncachedRead error: nil];
    rawTorrentData = [torrentData bytes];
    position = 0;

    return [self convertObjectDataToStrings:[[self bdecodeData] objectAtIndex: 0]];
}

- (NSArray *) bdecodeData {
    NSMutableArray *methodArray = [NSMutableArray array];
    do {
        if(rawTorrentData[position] == 'i') {
            position++;
            [methodArray addObject:[self decodeInt]];
        } else if(rawTorrentData[position] == 'l') {
            position++;
            [methodArray addObject:[self decodeList]];
        } else if(rawTorrentData[position] == 'd') {
            position++;
            [methodArray addObject:[self decodeDictionary]];
        } else if(isdigit(rawTorrentData[position])) {
            [methodArray addObject:[self decodeBytes]];
        } else if(rawTorrentData[position] == 'e') {
            position++;
            //Should only hit this when at the end of lists, dictionaries, or integers.
            break;
        } else {
            NSLog(@&quot;Error decoding torrent. Unexpected character '%c' at position %d.&quot;, rawTorrentData[position], position);
            return methodArray;
        }
    } while (position &lt; [torrentData length]);

    return methodArray;
}

- (NSNumber *) decodeInt {
    NSUInteger methodPosition = position;
    while(rawTorrentData[position] != 'e') {
       position++;
    }
    position++;
    return [NSNumber numberWithInteger: [[NSString stringWithUTF8String: [[torrentData subdataWithRange: NSMakeRange(methodPosition, (position - methodPosition))] bytes]] integerValue]];
}

- (NSData *) decodeBytes {
    NSUInteger methodPosition = position;
    while(rawTorrentData[position] != ':') {
        position++;
    }
    NSUInteger bytesLength = [[NSString stringWithUTF8String: [[torrentData subdataWithRange: NSMakeRange(methodPosition, (position - methodPosition))] bytes]] integerValue];
    position++;
    methodPosition = position;
    position += bytesLength;

    return [torrentData subdataWithRange: NSMakeRange(methodPosition, bytesLength)];
}

- (NSArray *) decodeList {
    return [self bdecodeData];
}

- (NSDictionary *) decodeDictionary {
    NSMutableDictionary *methodDictionary = [NSMutableDictionary dictionary];
    NSArray *methodArray = [NSArray arrayWithArray:[self bdecodeData]];
    for(NSUInteger i = 0; i &lt; [methodArray count]; i += 2) {
        [methodDictionary setObject: [methodArray objectAtIndex: i + 1] forKey: [methodArray objectAtIndex: i]];
    }

    return methodDictionary;
}

- (id) convertObjectDataToStrings: (id) object {
    if ([object conformsToProtocol: @protocol(NSFastEnumeration)]) {
        NSMutableArray *valueArray = [NSMutableArray array];
        if ([object isKindOfClass: [NSArray class]]) {
            for (id enumeratedObject in object) {
                [valueArray addObject: [self convertObjectDataToStrings: enumeratedObject]];
            }
            return valueArray;
        } else if ([object isKindOfClass: [NSDictionary class]]) {
            valueArray = [[object allValues] mutableCopy];
            NSMutableArray *keyArray = [NSMutableArray arrayWithArray: [object allKeys]];
            for (NSUInteger i = 0; i &lt; [keyArray count]; i++) {
                [keyArray replaceObjectAtIndex: i withObject: [self convertObjectDataToStrings: [keyArray objectAtIndex: i]]];
            }
            for (NSUInteger i = 0; i &lt; [valueArray count]; i++) {
                if (![[keyArray objectAtIndex: i] isEqualToString: @&quot;pieces&quot;]) {
                    [valueArray replaceObjectAtIndex: i withObject: [self convertObjectDataToStrings: [valueArray objectAtIndex: i]]];
                }
            }
            return [NSDictionary dictionaryWithObjects: valueArray forKeys: keyArray];
        }
    } else if ([object isKindOfClass: [NSNumber class]]) {
        return object;
    } else if ([object isKindOfClass: [NSData class]]) {
        return [[NSString alloc] initWithBytes: [object bytes] length: [object length] encoding: NSUTF8StringEncoding];
    } else {
        NSLog(@&quot;Unexpected object %@ of class %@&quot;, object, [object class]);
    }
    return object;
}

@end</pre><p>This code is much faster, cleaner, and accurate than the previous attempt. But it&#8217;s still inelegant in a lot of ways. For example, I store the torrent file data within both an NSData object (for the subdata* methods) and a char array (so I can access individual characters to iterate over). Surely there must be a better way, I just haven&#8217;t found it. For a few of my methods, there must be a better way of converting to an NSNumber object than getting a subdata, making it a string, and making the string a number. Seems quite obtuse compared to the conversions I&#8217;ve seen in other languages.</p>
<p>Well, that&#8217;s all for now. I&#8217;ll try to get this post out and around so I can get feed back, so feel free to leave comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://jonshier.com/2009/01/27/bitmaster-yet-another-mac-torrent-client/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.463 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-18 06:49:20 -->
