2013/11/20

"%.*s" in printf()

Background

Currently, I am dealing with regex example in C.  There is a abnormal printf command.
......
printf ("'%.*s' (bytes %d:%d)\n", (finish - start),  to_match + start, start, finish);
......
Ouput:
$& is '1 is nice 2' (bytes 5:16)
I am wondering why there are 3 "%s" format options but there are 4 parameters to map those options.

Dig deeper...

To make the story short, "%.*s" is where the amazing happen. This option take 2 arguments: length of the string and the starting point of a string. (XXX: so ?) OK. Take a look the example code below.

#include <stdio.h>

int main(int argc, const char *argv[])
{
        char a[] = "0123456789";

        int len, offset;
        len = 4;

        for (offset = 0; offset < 3; offset++) {
                printf("%.*s\n", len, a + offset);
        }

        return 0;
}

Output:
0123
1234
2345

In other word, "%.*s" allows you to print a subset of a string without doing tedious jobs like following.

char *b = (char *) malloc(4+1);
memcpy(b, a+offset, len);
b[len+1] = '\0';
printf("%s\n",b);



2013/11/17

Introduction to enyoJS

Background

Diverged from xtuple paragraph, enyo is one of xtuple dependencies. Or, in other words, xtuple use this framework to build their client side UI.

Introduction

enyo js
http://enyojs.com/about/
http://enyojs.com/get-enyo/#Bootplate
Difference between enyo and jQuery
jQuery focus on manupliating HTML elements while enyo focus on encapsulating HTMLs as a module and reuse them

Dig deeper

1. Cloning their "blootplate" and play around

The 1st question I would like to ask is what blootplate is.
After reading a while from their reference, I understand several things.
a) You can produce a web page with a minify source easily if you can make use of their shell script and directory structure.
b) If you have not interested on building an APP they claimed like me(well... it means a HTML5 webpage), you can simply read source/App.js. This file control what "debug.html" look like.

2. Diving back to their developer guide

Well... their developer guide make more sense than then point 1 I just pass-through.
I understand 2 more keywords in enyo namespace:
kind: A kind is a JavaScript constructor for an object that's been defined using the enyo.kind method. (That's why you can write `new App()`)
enyo.Control: A kind provided by enyo's core. A factory for users like us to create enyo.instance with user-defined properties.
enyo.Control() VS enyo.kind:
var t = enyo.Control(.....);
t.renderInto(document.body);
enyo.kind({name:'test',kind:enyo.Control...});
var k = new test();
k.renderInto(document.body);


In other word, enyo.kind allows you to publish your enyo.instance to a global namespace as a constructor while enyo.Control return a pointer of your enyo.instance.

3. Walk through their example

Here is their tutorial and here is my github repository, which folk from enyo.bootplate, to play with the provided example.
Notes:
1. "published":
properties insides "published" will be given a setter and getter method.
2. "event handler":
There are 2 parameters, object that's the source of the event and the event object, will be passed into the event handler.
3. "jsonp example":
Since Twitter have updated their APIs, the original example does not work. I have coded another apps in order to try their Ajax wrapping. Not Bad :D

4. Summery

It is fun for me to code with enjo JS. However, there are no too much online sources for you. If you get problems with enjo JS, you better be able to solve it yourself........
PS: Currently, # of jquery tag on stackoverflow is 389,821 while enyo is 120....

5. Useful links

2013/11/16

Introduction to OAuthn

Background

While I am dealing with Guildwars2 (umm it is nice game :D) web API, I found that they are going to evolve their API with OAuth. So, I am really wondering what is OAuth and what it can do. 

So... what is OAuth?

According to their page definition, it is a new protocol aims on providing an efficient way to control the web-application access rights. In other word, web-applications can ONLY access what you have granted them.

Conceptually, they have defined 3 entities. Users (You and me), Consumer (web-applications) and Service Provider (eg Twitter).
Overview of Oauth

The diagram in left hand side is what I got after digesting the data in OAuth webpage. The ultimate goal of consumers is getting protected resources which manged by Service Provider.

Before, web-application may get users credential in order to archive the above goal.

This is not a good approach since web-applications can do whatever they want after getting your credentials.

Therefore, that's why OAuth introduced. By the help of the token concepts, all accesses must be granted by Service Providers and Users without letting Consumers know Users credentials.

Flow chart from OAuthn to show how it works




2013/11/12

Install phpunit on ubuntu 12.04

Background
OS: ubuntu 12.04

Steps
sudo apt-get install phpunit
sudo pear upgrade pear
sudo pear channel-discover pear.symfony.com
sudo pear channel-discover pear.phpunit.de
sudo pear channel-discover components.ez.no
sudo pear channel-discover pear.symfony.com
sudo pear install --alldeps phpunit/PHPUnit

Testing
phpunit --version
PHPUnit 3.5.5 by Sebastian Bergmann.

Reference
http://www.giocc.com/installing-phpunit-on-ubuntu-11-04-natty-narwhal.html

2013/11/10

Library for drawing Graph on web: jsPlumb.js

Background
jsPlumb is a open-source javascript library for drawing graph on web platform. FYI, please go in their official website http://jsplumbtoolkit.com/home/jquery.html. There are many demos and relevant documentations for you to start your development.

My opinions
To be honest, I have not read all of the documentations. However, in beginner point of view, their documentation is not programmer friendly and examples are quite hard to follow.

After working for few hours, I have written a test website to play around with this library. Here is the link: https://github.com/mondwan/jsplumb_test

Two things I would like to drop down after building up the above test page
1. No idea for why css position:relative is required for jsPlumb.draggable() function properly. Actually, I have reported it as a bug to jsplumb on github. However, they claim that this is not a bug.

2. jsPlumb.draggable() is not a pure wrapper which means jsplumb element cannot be updated correctly if you are using 3rd party dragging library. (I have struggle a while...)

Install nvidia driver on Ubuntu

Background
OS: ubuntu 12.10
Graphic card: GTX 560

Goal
Install the driver from nvidia official website.

Steps
0. Install required package
`sudo apt-get update`
`sudo apt-get dist-upgrade`
sudo apt-get install build-essential linux-source linux-headers-`uname -r`
1. Make sure your graphic card module and OS system
`lspci -v | less`
`uname -a` # 32bit or 64bit
2. http://www.geforce.com.tw/drivers get your device installation file
3. Reboot your machine and enter recovery mode
4. Enable network mode (In order to get the write access on grub)
5. Enter root shell on grub and modify file
/etc/default/grub
GRUB_CMDLINE_LINUX_DEFAULT="nomodeset"
6. Go to the directory where you download the *.run file
`chmod 755 *.run`
`./*.run`
7. Follow the installation shell.
PS. Press yes and yes and yes....
8. Reboot after installation finished
9. Enjoy :D

CAUTION
You may fail to boot ubuntu again if you have already installed nvidia-* provided by apt-get.
`sudo apt-get remove --purge nvidia-*`
In other word, make use you pick either one way for installing nvidia driver instead of both.

2013/11/04

SCMBUG: branch name validation

BACKGROUND
Assuming that your svn and bugzillia have linked up via scmbug already.

GOAL
Modify the validation rules for branch name on scmbug.

STEPS
1. Modify <Your_SVN_Dir>/hooks/etc/scmbug/glue.conf
2. Read lines under Policy block
3. Done