Ha! My Kung-Fu is the best.
Here is a list of idioms I use for various purposes. I do not
claim to be the first one to discover any of them, but I haven't seen
any of them being used in programs written by other people. If you have
used any of the following before I did, please send an email so I can give
you credit. I would also love to learn new idioms. The address is
cinarus at yahoo dot com .
So, here goes.
int read_input(FILE *stream, /* could be stdin for example */
char **buf,
int *len) {
char tbuf[2000]; /* temporary buffer */
int clen; /* current length */
int R; /* bytes read at this point */
clen= *len;
R= fread(tbuf,sizeof(char),2000,stream);
if (R<0) /* error in read */
return 0;
*len+= R;
if (R<2000) { /* end of input reached */
*buf= malloc(sizeof(char)*(*len + 1));
/* plus 1 for the terminating \0 */
(*buf)[*len]= 0;
} else
if (!read_input(stream, buf,len))
return 0;
memcpy(*buf + clen, tbuf, R);
return 1;
}
By using this method you can:
Makefile) :
help:
grep '^#1' Makefile
#1
#1 Compiling foo
#1
#1 Set the variables at the top of the Makefile first.
#1 Then type 'make foo'
#1
#!/bin/bash count=1 outf=$0.bak inf=$0 if [ "$1" == 'i' ]; then echo '#!/bin/bash' > $outf let count=$count+1 echo $count echo count=$count >> $outf tail -n 16 $inf >> $outf chmod 755 $outf mv -f $outf $inf else echo $count fiHere is how you use it:
count for this example).count i in order to increment the value and print the result. count in order to just print the current value.
Some languages with an advanced(!) syntax do not have single-character block markers such as { and } . Instead, they have the more readable(!) begin, end, startexecutingthefollowingcodeimmediately, iteratetheloop and similar. So what I do is, I just put a little comment with a { or } in it after each such keyword. Then I just use the mex command { or } in order to go to the beginning or end of the current block. Of course, I do this only when the nesting gets too deep, or a block spans multiple screens. Here is a snippet (in the ultimate 4GL language from PROKRESS):
foreach id,name in employee do: /* { */
display id , name.
end /* } */
Where does the no-lock go, again?