Tuesday, 30 October 2012

Smoothly Scroll Element Inside div With Scrollbar Into View

We can scroll to an element which is inside a div which has scrollbar using jQuery.animate() and for scrollTop pass in $(elemId).parent().scrollTop() + $(elemId).offset().top - $(elemId).parent().offset().top

The scrollTop() method sets or returns the vertical position of the scrollbar for the selected elements.
The offset() method allows us to retrieve the current position of an element relative to the document.

The idea is add the current offset of the scrollbar with the current offset of the element, and subtract the top offset of the parent element, i.e., the area which is fixed below which the scroll area beings. Offset on an element relative to the document can be positive or negative, depending on whether the element is yet to be scrolled or the element has passed the scrolling.

Sample code:
<!-- html -->
    .content {
        height: 300px;
        overflow-y: scroll;
        <!-- link to navigation to sections -->
    <article class="content">
        <!-- more sections here -->
// js
    scrollTop: $(elemId).parent().scrollTop() + $(elemId).offset().top - $(elemId).parent().offset().top
}, {
    duration: 1000,
    specialEasing: {
        width: 'linear',
        height: 'easeOutBounce'
    complete: function (e) {
        console.log("animation completed");
Live Demo | View Source

Unable to find Mach task port for process-id

When we try to build and run any project under Xcode and if we get an error like "Unable to find Mach task port for process-id 274: (os/kern) failure (0x5)", then it is mainly due to some issues in validating cryptographic certificates. As written in the Building GDB for Darwin article the Darwin kernel refuses to allow GDB to debug another process if the user don't have special rights. Kernel allows a root user to debug. The latest method to allow GDB to control another process is to sign it with any system-trusted code signing authority. I got this error because of wrong system date. If system date is wrong, then verifying cryptographic signature fails.

Monday, 29 October 2012

PDCurses for Windows

ncurses is a C API that helps us in creating text based user interfaces in a terminal-independent manner. It is a free software emulation of curses in System V.
PDCurses is a public domain implementation of the curses library for DOS, Win32, OS/2, X11 platforms. Installation procedure is given below.
1. Install latest GCC (>=v4.7.2). Download it from equation.com and follow the installation instructions. Let's say GCC is installed into D:\gcc4.7.2 directory.
2. Download pdc34dllw.zip or newer from sourceforge.net. For direct link click here.
3. Extract pdcurses.dll to "D:\gcc4.7.2\libexec\gcc\i686-pc-mingw32\4.7.2" directory.
4. Extract pdcurses.lib to "D:\gcc4.7.2\i686-pc-mingw32\lib" directory.
5. Extract curses.h and panel.h to "D:\gcc4.7.2\i686-pc-mingw32\include" directory.
6. Ensure that proper environment variables and path variables are set. The path must be added to the front of the other path variables so that it takes precedence.
7. Run the sample program to see if it prints "Hello World from PDCurses!" in the command prompt.
#include <curses.h>

int main() { 
    initscr(); // Start curses mode
    printw("Hello World from PDCurses!");  // Print Hello World
    refresh(); // Print it on to the real screen
    getch(); // Wait for user input
    endwin(); // End curses mode
    return 0;
/* ---------------------------------------
> gcc -Wall -g -lpdcurses -o hello hello.c
> hello.exe
8. To run the executable the pdcurses.dll library is required. So if we are to bundle the app, ensure that the pdcurses.dll file is also included along with the executable.
9. Awesome! Now we have a working curses library.

To know more about ncurses see ncurses programming howto, Programmer's Guide to NCurses by Dan Gookin.

getchar() not waiting for input

When scanf() is used to get input from stdin and then using getchar() will mostly make the program exit without waiting for the input from stdin. The reason is that scanf() reads the input till new line from stdin which is a buffered stream. So the new line is still present in the buffered input stream. So getchar() sees the buffer as non-empty and reads from it which is the new line and returns. Instead, we can use fgets() to get the input as string and if we are looking for an integer input, then use strtol() to get the integer (long) value from the string.
// read_integer_input_before_getchar.c
// Monday 29 October 2012

#include <stdio.h>
#include <stdlib.h>

enum {
    MAX_RANGE = 4    //maximum number of input characters, max two digit with '\n' and '\0'

int main(int argc, char **argv) {
    long num;        //input value
    char *in;        //input buffer
    char *end_ptr;   //used by strtol, a result param, used for checking error conditions
    int base = 10;   //used by strtol to determine set of recognized digits
    int c;           //will contain a character
    puts("Enter a number");
    in = malloc(MAX_RANGE);
    if (in == NULL) {
        fprintf(stderr, "\nError allocating memory");
    fgets(in, MAX_RANGE, stdin);
    // @link: http://www.kernel.org/doc/man-pages/online/pages/man3/strtol.3.html
    num = strtol(in, &end_ptr, base);
    // handle strtol error conditions
    if (end_ptr == in) {
        fprintf(stderr, "\nNo digits found");
    free(in);       //free the allocated memory
    in = NULL;
    printf("Entered number is %ld\n", num);
    puts("\nEnter a single character");
    c = getchar();
    printf("\nEntered character is %c", (char) c);
    return 0;

/* ------------------------
// input - output

$> Enter a number
$ Entered number is 1

$ Enter a single character

$ Entered character is a
------------------------ */

Sunday, 28 October 2012

Git for Beginners

Common commands in the day-to-day usage of git.

Configure global email
git config --global user.email "user@example.com"
Configure global name
git config --global user.name "username"
Generate SSH key
ssh-keygen -t rsa -C "user@example.com"
View SSH key fingerprint
ssh-keygen -l -f ~/.ssh/id_rsa.pub
Add a remote repository
git remote add origin repo_url_here
Remove a remote repository
git remote rm origin
Push 'master' branch to remote repository
git push origin master
Rename a local branch
git branch -m old_branch_name new_branch_name
Delete remote branch
git push origin :old_branch
Exclude files but include a specific file in .gitignore
*.rar  #excludes all files ending in .rar
!myfile.rar #includes myfile.rar if present
Tagging a commit (give the first 7 chars of the commit)
git tag v1.0.0 dee70ed
git push origin --tags