Tuesday 26 October 2010

tar with excluding files

Sometimes you may want to create a tar ball of a software project, but you don't want to include everything to the archive you are creating. On Google anwers I found the answer. With a caveat: there is a difference between tar-version you are using (GNU or not GNU). The example given below is for GNU tar.

As I am using bazaar for version management, the directory to be archived usually contains a sub-directory called .bzr. This is obviously a directory to be excluded.

In my Makefile I first define a variable for my project's directory:
sourcedir=$(PWD)

This is done for portability.

Then the actual rule to make the tar:

tar: veryclean
(cd ../; tar -cvzf $(PROJECT)-`date +'%b-%d-%y' | tr A-Z a-z`.tgz \
--exclude "$(sourcedir)/.bzr" $(sourcedir))

veryclean is a rule to remove files generated during compilation (like log files), and is called first.

Thursday 21 October 2010

C programming - reading line from file

Suppose you want to 'feed' a program a file with data. In this case it is often convenient when you add some comments to the data, so you will understand it in the future. So, the file may look like

data data
comments comments
data
more comments

In the program reading this data file, you may want to 'skip' the lines containing comments. One way of doing this is reading the entire line, until end-of-line character.

This can easily be done using
fscanf(fs, "\n%[^\n]", dummy);

where fs is file handlers, and dummy a (large enough) string to hold the line.

Idea was found on programmers heaven. As it took me quite some searching for a solution, henceforth this post.

Wednesday 20 October 2010

creating push branches

To manage our software we use bazaar as version control system, which worked fine. As a certain point, some changes were needed with respect to the location of the software repository. Until know the repository was located under the home directory of several users (i.e., /net/home/johndoe/sw_repos/OurProject). This however is a bit tedious and not very nice. So a fake 'user' was created that would hold all the software projects, and related distributions, repositories, etc.

Next question was how do we transfer all our present versioned code to the new user, without destroying the lineage and links. In the following bazaar specific terminology is used.

Suppose we have a working branch of our project
/net/home/wdrenth/sw_dev/Project
which is bound (i.e., is a checkout of) /net/home/johndoe/sw_repos/Project
Let the new fake 'user' be called 'omnium'.

First create a repository and branch:
bzr init-repos /net/home/omnium/sw_repos/project
and
bzr init /net/home/omnium/sw_repos/project/Project

then, from user wdrenth
cd /net/home/wdrenth/sw_dev/Project
bzr push /net/home/omnium/sw_repos/project/Project
this populates the branch under 'omnium'
then
bzr bind /net/home/omnium/sw_repos/project/Project
which binds the wdrenth checkout to the new branch under omnium.

and ready!

Wednesday 13 October 2010

Extracting pages from pdf files

Found somewhere on the www while searching for a tool to extract pages from a pdf document using the command-line. Since gs (ghostscript) is installed almost on all unix type system, this routine is portable:


#!/bin/bash
if [ $# -lt 3 -o $# -gt 4 ]; then
echo $usage
else
if [ $# == 3 ]; then
outfile=${3%.pdf}_p${1}-p${2}.pdf
else
outfile=${4}
fi
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \
-dFirstPage=${1} \
-dLastPage=${2} \
-sOutputFile=${outfile} \
${3}
fi


On the command-line simple type:
pdfpextr firstpage lastpage inputfile [ouputfile]

Tuesday 12 October 2010

Customizing java's JFormattedTextField

In GUIs, text fields are a very common widget. With this text field, a user can enter all sorts of text (data) that is to be used by the application. This can for example be a simple descriptive text, but also more complex data specifying files to be read when the application is executed.

Anyway, when making GUIs with Java the JFormattedTextField is a very useful feature. However, in case of a textual input, for example for a descriptive piece of text, one might want two additional features not present in the default JFormattedTextField.

First is the default overwrite setting. This is not wanted in this case. This can be avoided by changing the formatter of the widget.
Use

AbstractFormatterFactory abstractFormatter = new AbstractFormatterFactory() {

@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
InternationalFormatter format = new InternationalFormatter();
return format;
}
};

and
myFormattedTextField.setFormatterFactory(abstractFormatter);

to get a text field that can be edited without overwriting existing text.

Secondly, when focusing a text field, one normally want the caret to be on the position where the mouse pointer was clicked. Default behavior results in the caret being positioned on the left.
Use, as found here:

MouseListener ml = new MouseAdapter() {
@Override
public void mousePressed(final MouseEvent e) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
JTextField tf = (JTextField) e.getSource();
int offset = tf.viewToModel(e.getPoint());
tf.setCaretPosition(offset);
}
});
}
};
in combination with
myFormattedTextField.addMouseListener(ml);

to get the wanted behavior.

First post

The intention of this blog is to collect bits and pieces of code I found useful. This may be pieces of Java, bits of Fortran, or just a bash script.

As such, it may be seen as an online note book.