HTML Encoder in Pasteboard

Before posting more, I needed an html encoder in Pasteboard to change <,>,&,… into <,>&… before pasting into in <pre>code-here</pre>.

I copy text/code into Pasteboard by cmd+c, run the program and cmd+v to paste the encoded string.

AppDelegate.m

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSPasteboard*  pasteBoard  = [NSPasteboard generalPasteboard];
    // get string from the pasteboard
    NSString* copiedString = [pasteBoard stringForType:NSPasteboardTypeString];
    // html encode
    NSString* encodedString = [NSString HtmlEncode:copiedString];
    // copy it into the pasteboard
    [pasteBoard declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil] owner:nil];
    [pasteBoard setString:encodedString forType:NSStringPboardType];
    
    // quit the app
    [[NSApplication sharedApplication] terminate:self];
}

NSString+HTML.h/.m

#import <Foundation/Foundation.h>

@interface NSString (HTML)

+ (NSString*)HtmlEncode:(NSString*)htmlString;

@end

#import "NSString+HTML.h"

@implementation NSString (HTML)

+ (NSString*)HtmlEncode:(NSString*)htmlString {
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"&"  withString:@"&amp;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"<"  withString:@"&lt;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@">"  withString:@"&gt;"];
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"""" withString:@"&quot;"];    
    htmlString = [htmlString stringByReplacingOccurrencesOfString:@"'"  withString:@"&#039;"];
    return htmlString;
}

@end

strong/weak ptr in ARC and C++11

__strong / __weak in ARC vs shared_ptr / weak_ptr in C++11.

result

*** ARC ***
I'm CocoaPitcher, ball = <CocoaBall: 0x7f8809d00570>
I'm CocoaPitcher, ball = (null)
*** C++11 ***
I'm CppPitcher, ball = 7802300.
I'm CppPitcher, ball is null.

main.mm

#import <Foundation/Foundation.h>
#import "CocoaPitcher.h"
#import "CocoaBall.h"
#include "CppPitcher.h"
#include "CppBall.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSLog(@"*** ARC ***");
        CocoaPitcher *pitcher = [[CocoaPitcher alloc] init];

        @autoreleasepool {
            CocoaBall *ball = [[CocoaBall alloc] init];
            pitcher.ball = ball;
            [pitcher print];                
        }
        [pitcher print];
        
        NSLog(@"*** C++11 ***");
        std::shared_ptr<CppPitcher> cppPitcher(new CppPitcher);
        {
            std::shared_ptr<CppBall> ball(new CppBall);
            cppPitcher->ball = ball;
            cppPitcher->print();
        }
        cppPitcher->print();
    }
    return 0;
}

CocoaPitcher.h/m

#import <Foundation/Foundation.h>

@class CocoaBall;

@interface CocoaPitcher : NSObject

- (void)print;

@property (weak) CocoaBall *ball;

@end

#import "CocoaPitcher.h"
#import "CocoaBall.h"


@implementation CocoaPitcher
@synthesize ball;

- (void)print {
    NSLog(@"I'm %@, ball = %@", [self class], ball);
}
@end

CppPitcher.h/cpp

#include <memory>

class CppBall;

class CppPitcher {
public:
    void print();
    std::weak_ptr<CppBall> ball;
};

#include "CppPitcher.h"
#include "CppBall.h"

void CppPitcher::print(){
    printf("I'm CppPitcher, ");
    if (auto b = ball.lock()) {
        printf("ball = %x.\n", b.get());
    } else {
        printf("ball is null.\n");
    }
}

Ramdisk backup & restore on MacOS X Lion

rd-backup.sh:

#!/bin/sh

mount |grep /Volumes/RamDisk > /dev/null
if [ $? -eq 0 ]; then
    echo saving /Volumes/RamDisk in /var/spool/rdimage
    sudo rsync -av --delete /Volumes/RamDisk/ /var/spool/rdimage/
    sudo diskutil unmount /Volumes/RamDisk
fi

rd-restore.sh:

#!/bin/sh

mount |grep /Volumes/RamDisk > /dev/null
if [ $? -ne 0 ]; then
    # 2GB
    sudo diskutil erasevolume HFS+ "RamDisk" `hdiutil attach -owners on -nomount ram://4194304`
    sudo diskutil enableOwnership /Volumes/RamDisk

    #restore ramdisk image from disk
    sudo rsync -a /var/spool/rdimage/ /Volumes/RamDisk/
fi

And use it as a temporary area in Xcode etc.

Automatic Reference Counting

Tested __strong, __weak and __unsafe_unretained.
__weak reference becomes nil when the target is dealloced, __unsafe_unretained becomes a dangling pointer.

Test Result:
1. Alloc -> Print

2012-06-14 23:49:08.286 ArcSpike[1561:403] strong: <NSObject: 0x7f8813b225c0>
2012-06-14 23:49:08.287 ArcSpike[1561:403] weak:   <NSObject: 0x7f8813b225c0>
2012-06-14 23:49:08.287 ArcSpike[1561:403] unsafe: <NSObject: 0x7f8813b225c0>

2. Alloc -> Free -> Print

2012-06-14 23:49:09.253 ArcSpike[1561:403] strong: (null)
2012-06-14 23:49:09.254 ArcSpike[1561:403] weak:   (null)
2012-06-14 23:49:09.255 ArcSpike[1561:403] unsafe: <NSObject: 0x7f8813b225c0>

or, causes access violation when referencing unsafeReference in print.

Test Code:

@interface WindowController : NSWindowController
{
id __strong strongReference;
id __weak weakReference;
id __unsafe_unretained unsafeReference;
}

- (IBAction)testAlloc:(id)sender;
- (IBAction)testFree:(id)sender;
- (IBAction)print:(id)sender;

@end

@implementation WindowController
...

- (IBAction)testAlloc:(id)sender
{
strongReference = [[NSObject alloc] init];
weakReference = strongReference;
unsafeReference = strongReference;
}

- (IBAction)testFree:(id)sender
{
strongReference = nil;
}

- (IBAction)print:(id)sender
{
NSLog(@"strong: %@", strongReference);
NSLog(@"weak: %@", weakReference);
NSLog(@"unsafe: %@", unsafeReference);
}
@end