Tuesday, 15 January 2008

Folding with vim, a cheat sheet

v|V, select zf or any other command ( fold in visual mode )
:20,101 fo[ld]: fold selected lines
zf/string: fold until the first match of "string" .
:mkview : save folds.
:loadview : load folds.
zf/string creates a fold from the cursor to string . Try within an "#ifdef" and "#endif".

zf#j creates a fold from the cursor down # lines.
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero -- all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.
]z move to end of open fold.

Monday, 7 January 2008

A C pointer FAQ ( kind of ) [ as of now for me ]


Of course, pointers are not limited to ints. It's quite common to use pointers to other types, especially char. Here is the innards of the mystrcmp function we saw in a previous chapter, rewritten to use pointers. (mystrcmp, you may recall, compares two strings, character by character.)

// p1, p2 point to the address of the first character in str1, str2 respectively
char *p1 = &str1[0], *p2 = &str2[0];

while(1)
{
// compare the values of str1[x], str[x] at the position x. Not equal
if(*p1 != *p2)
return *p1 - *p2;
// we reached the end of both str1, str2 -> they are equal.
if(*p1 == '\0' || *p2 == '\0')
return 0;
// p1++ and p2++ do the increment of p1 and p2.
// i.e., the next characters in str1 ( str[x + 1] and str2 ( str2[ x + 1]
p1++;
p2++;
}


As another example, here is the strcpy (string copy) loop from a previous chapter, rewritten to use pointers:

char *dp = &dest[0], *sp = &src[0];
while(*sp != '\0')
// *dp++ means *(dp++), i.e., the value of the pointer next to dp.
// to access the address of the next pointer of dp, use (*dp)++
*dp++ = *sp++;
*dp = '\0';

(One question that comes up is whether the expression *dp++ increments p or what it points to. The answer is that it increments p. To increment what p points to, you can use (*dp)++.)

ref: http://www.eskimo.com/~scs/cclass/notes/sx10b.html

Os type detection with uname


[vuhung@test_make]$make vh
SunOs
[vuhung@test_make]$cat Makefile
uname=$(shell uname)

ifeq ($(uname), Linux)
APP_OSTYPE = Linux
endif

ifeq ($(uname), SunOS)
APP_OSTYPE = SunOs
endif

vh:
@echo $(APP_OSTYPE)