Reading data files in C++

December 3, 2007

Text processing in Python is so easy that I don’t feel like doing such kind of programming in C++ at all. However, sometimes I don’t have the choice. For example now I just had to implement a CLAM Processing that loads a big table of float data from a text file. Specifically, this is done in the processing ConcreteConfigure method.

What annoys me more of C++ file streams is the time I spend understanding its API. Let alone remembering it! So since I’ve just implemented a quite generic solution that loads a table of floats let’s blog about it and keep it at hand.

The input file. Actually, the file format is very flexible in terms of separators and the number of columns per row. The only requirement is that everything can be read as a float.


0 51.0 0.00164916 -0.00770348 73.0007 40.7776 214.276 76.1719
1 51.0 0.00164916 -0.00770348 73.0007 40.7777 214.276 76.1719
2 51.0 0.00164916 -0.00770348 73.0007 40.7782 214.276 76.1719
3 51.0 0.00164916 -0.00770348 73.0007 40.7795 214.275 76.1719
...

And this is the C++ code that reads the file in a data structure and then uses it:

#include ;
#include ;
#include ;
#include ;

// Data type where to load the table
typedef std::vector Row;
std::vector table;

// Load table from file
std::ifstream file(“/tmp/data”);
while (file)
{
std::string line;
std::getline(file, line);
std::istringstream is(line);
Row row;
while (is)
{
float data;
is >> data;
row.push_back(data);
}
table.push_back(row);
}

// Now let’s use the table
for (unsigned i=0; i

This is a quick post (or should I say nanoblog?) to share my new home page. It’s at parumi.org (I did a big update on Des 2nd). After using so many web2.0-social networks I felt the need to have a boring, simple and static home page written in html. Actually, I happily failed at writing directly in html just because I had wiko at hand.

Wiko stands for wiki compiler, and is the project name we gave to some python scripts David and me have been lately developing for personal use. It basically convert wiki text files to html, LaTeX and blogs; as is easy to imagine it combines very well with your favorite version control system to create an actual wiki (it’s collaborative but only through committing to the version control system). Visit the wiko home page to learn more. And maybe use it.

I’ve been a long time longing for a N700 or N800 so this short message that just popped into my inbox made my day ;)

Assumpte: N810 maemo submission accepted
Data: Fri, 9 Nov 2007 18:17:31 +0200 (EET) (17:17 CET)

Congratulations! You have been accepted to the N810 maemo
device program. We will send your discount and instructions
as soon as the device is available in your selected shop (soon).

maemo team – http://maemo.org

The N810 maemo device program aims to offering a low price for the new Nokia N810 Internet Tablet (99€) to the active contributors of the maemo community, open source programmers, designers, bloggers and the like.
I’m eager to have it into my hands. We’ll see how hard it is to port Clam and other Linux audio apps to it.

Since the change of SVN for CVS in Clam (a year ago aprox) we do not explicitly tag the releases and that’s fine because SVN revision numbers are global. On the other hand, SVN do can create tags but it is dangerous because a tag is exactly the same thing as a branch. So SVN doesn’t prevent to commit to a tag.

Our tagging approach is very simple and proved useful: just write the revision number of each release in the CHANGES files. This simplifies the release process and also the way to make diffs –since you always use the same syntax.

Now an example: Let’s say we want to see changes in SMSTranspose files from last stable release (1.1.0):

1) Look for the svn revision corresponding to a stable version in the CHANGES file


NetworkEditor$ head CHANGES

2007-??-?? NetworkEditor 1.1.1 SVN $Revision: 10220 $
''
*

2007-06-08 NetworkEditor 1.1.0
'More eye-candy, please'
* SVN Revision: 10216
* Using CLAM 1.1.0
* New examples
* genderChange: fully working now and with an interface

So version 1.1.0 is revision 10216.
By the way, you maybe are curious on this $Revision: 10220 $ part of the first line. This is a SVN variable (or pattern). Each time you commit this CHANGE file the number gets updated to the current revision. That means that we actually never write the revision numbers in CHANGES files, we only have to remove the “$” when we decide to tag the release.

2) Now diff the files of interest on that version and head


NetworkEditor$ svn diff -r 10216:HEAD src/processing/SMSTranspose.*xx
Index: src/processing/SMSTranspose.cxx
===================================================================
--- src/processing/SMSTranspose.cxx (revision 10216)
+++ src/processing/SMSTranspose.cxx (revision 10281)
@@ -20,11 +20,15 @@
*/
#include "SMSTranspose.hxx"
-#include
+#include
#include
...

Last, a quick tip for the gvim users: pipe the diff result to gvim using -R – options:
$ svn diff | gvim -R -
And take advantage of vim syntax highlighting and quick navigation.

I just discovered this article on Linux kernel management style written by Linus Torvalds. It’s amusing and ironic in the best Linus style. For example, his first recommendation:

First off, I’d suggest buying “Seven Habits of Highly Successful
People”, and NOT read it. Burn it, it’s a great symbolic gesture.

I think these paragraphs summarize quite well the core of the philosophy:

So the name of the game is to avoid decisions, at least the big and painful ones. Making small and non-consequential decisions is fine, and makes you look like you know what you’re doing, so what a kernel manager needs to do is to turn the big and painful ones into small things where nobody really cares.

It helps to realize that the key difference between a big decision and a small one is whether you can fix your decision afterwards. Any decision can be made small by just always making sure that if you were wrong (and you will be wrong), you can always undo the damage later by backtracking. Suddenly, you get to be doubly managerial for making two inconsequential decisions – the wrong one and the right one.

Later he talks about how to put blame and criticize. I do strongly agree on how sense humor plays an important role here.

Some humor can help pad both the bluntness and the moralizing. Going overboard to the point of being ridiculous can drive a point home without making it painful to the recipient, who just thinks you’re being silly. It can thus help get through the personal mental block we all have about criticism.

Dreamhost

February 9, 2007

I’ve lately become a Dreamhost client. I host my personal web, my band’s web and, soon, the org.lliure.cat web. The last is an organization that campaigns and promotes free-software in the Catalan administration. Well, so far I’m very happy with it and I highly recommend it. They are very cheap and have grate customer support and community. The site is entirely run by free software. Debian is the operating system, you have Apache, of course and many great web publishing systems: WordPress, Mediawiki, Gallery. Other goodies include php5, mailing-lists, etc.

They have an interesting discount schema. Enter my referral code: PAUARUMI when you sign up and you will save 60eur ($77). In return I get 15eur ($20) referral discount. I used this system when I joined and it makes the first year only 33eur ($42). Absurdly cheap! All clients can create referral codes, when you create one you have to decide how to split $97 between the you and the referee.

Guillamino band on tv

December 29, 2006

Pau Guillamet just discovered this short video from a local tv. We are the guillamino band playing at L’Espai theater in Barcelona. It is the more formal gig we have done so far (I mean: people seated, serious sound system…). Yes, I’m the guy at bass and synth. The PG interview is in catalan and most of the music is from the record, not live.

The amazing thing is that I didn’t notice anyone with a camera on that gig!

(I don’t know how if planet accept embedded youtube html. In this case just follow this link)