Thursday, December 09, 2004

gengetopt

http://www.gnu.org/software/gengetopt/gengetopt.html
If an option is given multiple times

If an option is specified as multiple, then it can be specified multiple times at command line. In this case, say the option is called foo, the generated foo_given field in the args structure contains the number of times it was specified and the generated field foo_arg is an array containing all the values that were specified for this option.

Notice that if a default value is specified for a multiple option, that value is assigned to the option only if no other value is specified on the command line (and the corresponding _given field will be set to 1), i.e., a default value IS NOT always part of the values of a multiple option.

for (i = 0; i < args_info.string_given; ++i)

printf ("passed string: %s\n", args_info.string_arg[i]);


Sunday, December 05, 2004

C++

http://www.stanford.edu/class/cs106b/winterhandouts/H06%20C++%20streams.pdf

fs.open(filename.c_str());

The above line attempts to open the file with the given filename (searching in the current directory). Note that open member function takes in a char* argument and not a C++-style string, so you must use c_str to convert it. (Sigh, this is just a weird artifact of the stream libraries being completed before the string library). Opening the file may or may not succeed (depending on whether the named file exists, what permissions it has, etc.), so before you proceed, you should check the state of the stream.

if (fs.fail()) return;

The call to fail checks if the state of the stream is unhappy, in this case, it will determine if the file was properly opened. This is essential before we start trying to output to the stream (in the same way that we check if a FILE* is NULL in C).

Thursday, December 02, 2004

Emacs

a few emacs shortcuts

M-/ autocompletion. It's based on the tokens in the currently open buffers.

ESC-u change a word into uppercase
ESC-c capitalise a word
C-x C-u upcase region

Sunday, November 28, 2004

GNU Build system

The top directory should atleast have Makefile.am and configure.in

Makefile.am in the top directory should have the subdirs where Makefiles need to be created.
e.g Makefile.am for toplevel
+++++
SUBDIRS = src
+++++

and in the directory "src" you can possibly have the following Makefile.am
++++++++++++++++
bin_PROGRAMS = hello
hello_SOURCES = hello.cpp

++++++++++++++++++

Next you need to have a configure.in in your toplevel directory
following is just a template , read and understand on your own , i have no explanations
++++++++++++++++++
AC_INIT(src/hello.cpp)
AM_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE(hello, 0.1)
AC_PROG_CXX
AC_PROG_INSTALL
AC_OUTPUT(Makefile src/Makefile)

++++++++++++++++
you are pretty much done with the files at this stage ,

$aclocal
$autoconf
$touch AUTHORS NEWS README ChangeLog
$autoheader
$automake --add-missing





Thursday, October 28, 2004

Installing perl modules

from http://www.brandonhutchinson.com/installing_perl_modules.html

perl -MCPAN -e shell (to get an interactive CPAN shell)
perl -MCPAN -e 'install Time::JulianDay' (if you know the name of the module, you can install it directly without interacting with the CPAN shell)

Within the CPAN shell:
i /expression/ will search for a Perl module containing expression, and
install module will install the module.

Example:
perl -MCPAN -e shell
i /JulianDay/
install Time::JulianDay

Note: if you are behind a firewall, you may wish to use passive FTP with Perl's Net::FTP module. Set the environment variable FTP_PASSIVE 1 (or any non-zero value) to use passive FTP when downloading Perl modules through CPAN.


To manually install a Perl module:

1. Download the Perl module from CPAN or other site.
2. Extract the tarball.
3. Run perl Makefile.PL
4. Run make
5. Run make test
6. Run make install

Note: you should use the same compiler to build Perl modules that you used to build Perl. For example, if you are building Perl modules with gcc and are using a version of Perl that was supplied with your distribution (ex. Solaris 8 includes Perl 5.005_03), you may run into errors.

Tuesday, October 05, 2004

fflush(stdin)

from http://www.eskimo.com/~scs/C-faq/q12.26.html

How can I flush pending input so that a user's typeahead isn't read at the next prompt? Will fflush(stdin) work?


fflush is defined only for output streams. Since its definition of ``flush'' is to complete the writing of buffered characters (not to discard them), discarding unread input would not be an analogous meaning for fflush on input streams.

There is no standard way to discard unread characters from a stdio input stream, nor would such a way be sufficient unread characters can also accumulate in other, OS-level input buffers.

References: ANSI Sec. 4.9.5.2
ISO Sec. 7.9.5.2
H&S Sec. 15.2



dired to open new directories in same buffer

From http://list-archive.xemacs.org/xemacs-beta/200006/msg00277.html

Pasting the entire mail

Recently, there was some discussion in the comp.emacs newsgroup

regarding dired and the way `dired-find-file' handles directories.
`dired-find-file' will always create a new buffer for each directory.
If you work in many directories, these dired buffers can accumulate
and quickly become unwieldy.

The original post asked for a way to reuse the same dired buffer when
changing directories. Someone suggested using 'i',
`dired-maybe-insert-subdir', instead of 'RET'. This isn't ideal,
however, because it simply inserts the contents of the subdirectory
into the current dired buffer and doesn't remove the old directory's
contents. It also forces users to use two different keys to navigate
through directories and select files, 'i' and 'RET', instead of just
being able to use 'RET'.

I would really like to have the new directory contents replace the old
in a dired buffer. If my cursor is positioned over a file, however, I
want dired to do the normal thing: open the file in a new buffer.
This seems to me like the most intuitive behavior. I hacked out some
lisp code that does this:


(defun dired-follow-file ()
"In dired, visit the file or directory on this line.
If a directory is on the current line, replace the current
dired buffer with one containing the contents of the directory.
Otherwise, invoke `dired-find-file' on the file."
(interactive)
(let ((filename (dired-get-filename)))
;; if the file is a directory, replace the buffer with the
;; directory's contents
(if (file-directory-p filename)
(find-alternate-file filename)
;; otherwise simply perform a normal `dired-find-file'
(dired-find-file))))


Now, I simply need to remap the dired keys


(add-hook
'dired-mode-hook
(lambda ()
(local-set-key "\C-m" 'dired-follow-file)
(local-set-key "e" 'dired-follow-file)
(local-set-key "f" 'dired-follow-file)))


and (abracadabra) dired replaces the old directory with the new one in
the same buffer when I press 'RET'. (Note that it's always easy to
get back to the parent directory because you can just select '..' in
the new directory listing.)

I posted this lisp snippet to comp.emacs and was asked to also send it
to and . Here it
is. I hope this is helpful.

Regards,
Samuel Padgett

Friday, September 24, 2004

ssh

To setup OpenSSH's sshd, you must first set up the keys used:

cd /etc/ssh
ssh-keygen -b 1024 -f ssh_host_key -t rsa1 -N ''
ssh-keygen -b 1024 -f ssh_host_rsa_key -t rsa -N ''
ssh-keygen -b 1024 -f ssh_host_dsa_key -t dsa -N ''


Thursday, September 23, 2004

common perl mistakes -1

$var2 = chomp($var1);

isn't going to put the chomped $var1 in $var2

perl error

Reference found where even-sized list expected at ./test.pl line 39.

-- you get this error when you try to initialize a hash like below

%hash_name = {};

from http://linux.about.com/library/cmd/blcmdl1_perl5005delta.htm

Reference found where even-sized list expected
You gave a single reference where Perl was expecting a list with an
even number of elements (for assignment to a hash). This usually means
that you used the anon hash constructor when you meant to use parens.
In any case, a hash requires key/value pairs.

%hash = { one => 1, two => 2, }; # WRONG
%hash = [ qw/ an anon array / ]; # WRONG
%hash = ( one => 1, two => 2, ); # right
%hash = qw( one 1 two 2 ); # also fine


Curly brackets are used to construct anonymous hashes. Their result is
a reference to that hash. So with %f = {} you're trying to assign a
reference (one item) to a hash. However, hashes are initialized with
lists which must have an even number of elements. Hence the warning.
The proper way to initialize an empty hash is with an empty list, which
is why %f = () works.


Tuesday, September 21, 2004

scanf

From: http://webcourse.cs.technion.ac.il/234117/Spring2004/ar/faq_Homework.html

>>I'm having a problem using scanf. After I enter the username, the following scanf reads the "enter" I clicked. What should I do?

Instead of writing scanf("%c") you can write scanf(" %c") (with a space before %c), and it should work better. The space before the "%c" will read all white spaces (newline, tab and space) and the first non-white space will be read into the "%c". Using the scanf this way will prevent the scanf from reading the "ente

Sunday, September 19, 2004

find /tmp -type f -name "*.gz" -exec tar -xvzf '{}' ';'
find ./ -name "*.gz" -exec tar -xvzf {} \;

#### to exclude files in directory results
find . -name results -prune -o -type f -name "*.bc"

ls -lai | grep -v imp_file | xargs rm -f