Check out your svn copy through ssh without those annoying password prompts

This is an instruction I wrote to help people in our lab to access codebase on our server. I have removed all the specific information.

Since setting up something on Windows is always such a challenging work, help this one helps.

The background is that, on the server side, you are using ssh to protect your codebase. Instead of setting up another group of users and passwords for the svn repository separately, the server asks user for one’s Windows credential. So, on the client side, user are using commands in the form of “svn co svn+ssh://path/to/repository” to checkout a copy. Please note that the scheme for the url of repository is “svn+ssh” instead of “svn”. It tells the svn to connect to the server by using ssh then checkout the source code.

I may post a tutorial on how to setup the server ON WINDOWS in the future.

Please follow this instruction to check out a sample repository.

1. Install SVN Client “TortoiseSVN”
Open http://tortoisesvn.net/downloads.html in your web-browser, choose “TortoiseSVN 64-Bit”/”TortoiseSVN 32-Bit” to download the installation program. Open it and keep clicking “next” to finish the installation.

2. Have a try
Open your windows explorer, get in a folder such as “C:\Repo”, right click on anywhere blank, choose “SVN Checkout…”.
The URL of repository is “svn+ssh://path/to/your/repository”.
Click “Ok”.
You will be prompted for your username and password for THREE times then you should get the files.

3. I can’t stand this!
Yes it’s quite annoying to repeat filling your credential, remove the folder and we will try to get rid of this.

4. Utilities
If you are on an unix-based machine, google for “ssh key” you get A LOT OF tutorials on this topic, for example http://linuxproblem.org/art_9.html.
If you are on WINDOWS, open this PuTTY Download Page http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
You will need PuTTY.exe, Pageant.exe and PuTTYgen.exe

5. Generate Key Pairs
Launch PUTTY.exe, fill in the Host Name as “Your.Repository.Host.Name”, click “Open”.
Input your credential, note that when you type in your password there will be “no response”, it is ok, just go ahead.
You should meet our Hippo now.
Type in “ssh-keygen.exe”
Keep hitting the “Enter” button.
Type in “cd ~/.ssh”
Type in “mv id_rsa.pub authorized_keys”
Type in “ls” and you should see this:
“authorized_keys id_rsa”
id_rsa is your private key.
Type in “cat id_rsa”
Scroll up and highlight/select lines from “—–BEGIN RSA PRIVATE KEY—–” to “—–END RSA PRIVATE KEY—–“, including these two lines.
Hit “Ctrl+C” to copy them.

6. Make your private key file
Create a new text file in certain folder, e.g. “My Documents”, “Ctrl+V” to paste lines in this file, save it in a name e.g. “ssh_key.txt”.
Launch PUTTYgen.exe, choose “load”, in the dialog change the extension to “All Files” and find your “ssh_key.txt”, click “open”.
Click “Ok” when anything pop out.
Choose “Save Private Key”, get a name for it such as “ssh_key.ppk” and click “save”.

7. Come on, we are almost there
Launch Pageant.exe, nothing happen? Right, it hides in the task bar. Right click on its icon and choose “Add Key”.
Find your “ssh_key.ppk” and choose “Open”.

8. Have another try
Ok, Repeat Step 2, you should be able to get all the files without entering any credential.
Cheers!

[iOS]How to save and load a custom object?

Here we go.

How to write a custom object which can be archived to a text file?

Usually, we use NSKeyedArchiver to serialize an object and write it to a file.
Correspondingly, NSKeyedUnarchiver is used to get the object from the file.

The NSKeyedArchiver’s interface is like this:

+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;

NSKeyedArchiver is a NSCoder. The rootObject should conform to the protocol NSCoding.

@protocol NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;

@end

For example, now we have a customObject:

@interface CustomObject : NSObject  {
    NSString*   mStringValue;
    int         mIntValue;
    BOOL        mBOOLValue;
}

@property (non-atomic, retain) NSString *stringValue;
@property (nonatomic, assign) int       intValue;
@property (nonatomic, assign) BOOL      boolValue;

@end

It conforms to NSCoding, so we need to implement those two methods in the .m file

#define kEncodeKeyStringValue   @"kEncodeKeyStringValue" 
#define kEncodeKeyIntValue      @"kEncodeKeyIntValue" 
#define kEncodeKeyBOOLValue     @"kEncodeKeyBOOLValue" 

#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.stringValue   forKey:kEncodeKeyStringValue];
    [aCoder encodeInt:self.intValue         forKey:kEncodeKeyIntValue];
    [aCoder encodeBool:self.boolValue       forKey:kEncodeKeyBOOLValue];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super init]))
    {
        self.stringValue = [aDecoder decodeObjectForKey:kEncodeKeyStringValue];
        self.intValue    = [aDecoder decodeIntForKey:kEncodeKeyIntValue];
        self.boolValue   = [aDecoder decodeBoolForKey:kEncodeKeyBOOLValue];
    }
    return self;
}

Now, we can save and load a customObject like:

- (IBAction)saveObjectPlain:(id)sender {

    printf("==========================================\n");
    printf("saveObjectPlain===========================\n");
    printf("==========================================\n");

    //< Create and Save the Object
    {
        CustomObject *obj = [[[CustomObject alloc] init] autorelease];
        obj.stringValue = @"The String Value";
        obj.intValue    = 12345;
        obj.boolValue   = YES;
        [NSKeyedArchiver archiveRootObject:obj toFile:[self tempFilePath]];
        printf("Save: \n %s \n", [[obj description] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    
    //< Load and Print the Object
    {
        CustomObject *obj = [NSKeyedUnarchiver unarchiveObjectWithFile:[self tempFilePath]];
        printf("Load: \n %s \n", [[obj description] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    
    printf("==========================================\n");
}

Quite easy, right?

How about to save/load an array of our CustomObjects?
Since NSArray conforms to protocol NSCoding, it is quite straightforward.

- (IBAction)saveObjectsInArray:(id)sender {

    printf("==========================================\n");
    printf("saveObjectsInArray========================\n");
    printf("==========================================\n");
    
    //< Create Two Keys and Save the Object
    {
        CustomObject *obj1 = [[[CustomObject alloc] init] autorelease];
        obj1.stringValue = @"The String Value 1";
        obj1.intValue    = 12345;
        obj1.boolValue   = YES;
        
        CustomObject *obj2 = [[[CustomObject alloc] init] autorelease];
        obj2.stringValue = @"The String Value 2";
        obj2.intValue    = 54321;
        obj2.boolValue   = NO;
        
        NSArray *array = [NSArray arrayWithObjects:obj1, obj2, nil];
        [NSKeyedArchiver archiveRootObject:array toFile:[self tempFilePath]];

        printf("Save: \n %s \n ", [[array description] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    
    //< Load and Print the Object
    {
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:[self tempFilePath]];
        printf("Load: \n %s \n ", [[array description] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    
    printf("==========================================\n");
}

So, if there is a member variable of type NSArray in our CustomObject,
we need to make sure the array contains only objects conform to NSCoding.
Otherwise, an exception of unrecognized selector [.. encodeWithCoder:] will be raised.

One step further, how to save/load a NSDictionary has a CustomObject as the key and another CustomObject as the Object.

This is actually another problem. We need to make sure CustomObject can be used as a key in the NSDictionary.
According to the documentation, NSDictionary requires its key to conform to the protocol NSCopying.

@protocol NSCopying

- (id)copyWithZone:(NSZone *)zone;

@end

The key object needs to be copied to a specified piece of memory described by NSZone.
The implementation is like:

#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
    CustomObject *copy = [[CustomObject allocWithZone:zone] init];
    copy.stringValue = [[self.stringValue copy] autorelease];
    copy.intValue = self.intValue;
    copy.boolValue = self.boolValue;
    return copy;
}

We use allocWithZone to allocate a CustomObject on the indicated piece of memory.
Then copy member variables to the new instance.

The save/load procedure is like:

- (IBAction)saveObjectAsKey:(id)sender {

    printf("==========================================\n");
    printf("saveObjectAsKey===========================\n");
    printf("==========================================\n");
    
    //< Create Two Keys and Save the Object
    {
        CustomObject *keyObj = [[[CustomObject alloc] init] autorelease];
        keyObj.stringValue = @"The String Value Key";
        keyObj.intValue    = 12345;
        keyObj.boolValue   = YES;

        CustomObject *valObj = [[[CustomObject alloc] init] autorelease];
        valObj.stringValue = @"The String Value Value";
        valObj.intValue    = 54321;
        valObj.boolValue   = NO;
        
        NSDictionary *dict = [NSDictionary dictionaryWithObject:valObj forKey:keyObj];
        [NSKeyedArchiver archiveRootObject:dict toFile:[self tempFilePath]];

        printf("Save: \n %s \n", [[dict description] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    
    //< Load and Print the Object
    {
        NSDictionary *dict = [NSKeyedUnarchiver unarchiveObjectWithFile:[self tempFilePath]];
        printf("Load: \n %s \n", [[dict description] cStringUsingEncoding:NSUTF8StringEncoding]);
    }
    
    printf("==========================================\n");
}

The whole demo can be found here:
Demo CustomObject NSCoding

Hope it helps! :]

----------------------------------------------------
When we were learning new things, we searched for answers in Google, from Stackoverflow and followed well-written tutorials. When other one faces problems we met and solved, we are glad to answer their questions, post tutorial for the related topics. This is called the spirit of Community. I love it.

[Libsvm]libsvm for matlab compilation error on mac

Error:

>> make
/bin/bash: -c: line 0: unexpected EOF while looking for matching `"'
/bin/bash: -c: line 1: syntax error: unexpected end of file
??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> make at 5
mex -O -largeArrayDims -I..\ -c ..\svm.cpp

Fix:

  1. Under folder ./libsvm-3.1/matlab
  2. Open file make.m
  3. replace backslash(\) to slash(/)
  4. save and close file
  5. execute the make command inside matlab


Error:

>> make
i686-apple-darwin10-gcc-4.2.1: svm.obj: No such file or directory
i686-apple-darwin10-gcc-4.2.1: svm_model_matlab.obj: No such file or directory

    mex: link of ' "svmtrain.mexmaci64"' failed.

??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> make at 7
mex -O -largeArrayDims -I../ svmtrain.c svm.obj svm_model_matlab.obj

Fix:

  1. Open the make.m file again
  2. Replace all the occurrences of .obj to .o
  3. Save and close
  4. Try again!

Hope these help :]