Trimming C++ strings

Posted in Uncategorized by tux4life on July 26, 2009

The c++ string class is one of the most complete implementation of the string library and has functions for every possible string operation one can think of.  But one of the most dearly missed function is the trim() function (to remove the leading and trailing spaces/tabs).

Fortunately, it is very easy to implement an efficient version  the trim function using other member functions of the string class.  This is illustrated in the code snippet shown below.

void trim(std::string& str)
{
std::string trim_chars = ” \t”;
std::string::size_type pos = str.find_last_not_of(trim_chars);
if(pos != std::string::npos) {
str.erase(pos + 1);
pos = str.find_first_not_of(trim_chars);
if(pos != std::string::npos) str.erase(0, pos);
}
else
str.erase(str.begin(), str.end());
}

The logic is pretty simple.  Find the first/last character that is not a space/tab, then remove everything that precedes/follows it.  The else clause handles the case where the string only has spaces or tabs and erases everything.

As a small optimisation, the “trim_chars” variable can be made static so that it is not created and deleted every time the function is called.  This would be particularly helpful if the function called multiple times.

void trim(std::string& str)

{

std::string trim_chars = ” \t”;

std::string::size_type pos = str.find_last_not_of(trim_chars);

if(pos != std::string::npos) {

str.erase(pos + 1);

pos = str.find_first_not_of(trim_chars);

if(pos != std::string::npos) str.erase(0, pos);

}

else

str.erase(str.begin(), str.end());

}

Advertisements

Born Brave

Posted in Uncategorized by tux4life on March 18, 2009

This is one of the most visually descriptive and at the same time factually true pictures that I stumbled upon.. Guess almost everyone would agree 😉

Born Brave

Born Brave

Download Albums From Picasa Web

Posted in Ubuntu HowTo's, Uncategorized by tux4life on February 8, 2009

Recently, one of my friends shared a picasa web album to me.  The photos were good and I wanted to download all the photos in the album to my hard drive.

There is an option in the picasa web interface to download a photo.  But it allows you to download only one photo at a time and so downloading the entire album can be a real pain.

But there is a much easier way of doing this.  You will need Firefox and an addon called DownThemAll.

Here is how to do it.

Install the firefox addon DownThemAll from here.

Go the picasa web albu that you want to download and click on the “RSS” link.  (You will find this link on the right hand side.)

From the page that comes up, right click and select DownThemAll.

Select the “Images” checkbox, choose a download location for the photos and click “Start!”

DownThemAll !!

DownThemAll takes care of the rest.

For all those tech geeks, you also use the Google APIs to achieve the samething.  I am working on developing a perl script for the same.

I am also working on developing a script to download Youtube favourites as MP3 files.  Will post about those once I complete them.