selfjungle Just another WordPress weblog

13Feb/110

vim delete lines with regexp, replace string

uzbl is an awesame ultralightweight browser, however gmail has cookie issues. The only way I found to have a working uzbl+gmail is to remove every line from .local/share/uzbl/cookies.txt which contain Google/google when the cookie problem happens.

With vim it's possible to look for STRING case insensitively and delete lines:

:g/\cSTRING/d

Also replace STRING1 to STRING2 in one line or in all lines.

:s/STRING1/STRING2/g
:%s/STRING1/STRING2/g
Tagged as: No Comments
4Feb/110

gcc, colorgcc, lcov, valgrind

At home I usually don't create makefiles when my program is so small that it fits into one file.

However compilation errors are more readable with colorgcc, and having as much warnings/errors at compilation time as possible is even better.

.bashrc:

GCC_ARGS="-Wall -Werror -pedantic -Weffc++ -Wshadow -ggdb --coverage"
alias g++="/usr/lib/colorgcc/bin/g++ $GCC_ARGS"
alias gcc="/usr/lib/colorgcc/bin/gcc $GCC_ARGS"

The -ggdb puts debug symbols to the binary and --coverage will make the binary create .gcda and .gcno files at runtime.

To create a nice coverage-html lcov needs more steps (3) than I'm willing to type everytime so the following line in the .bashrc do the work. Note that the lcov_all is function, because argument passing is not possible with aliases.

alias lcov_reset="lcov --directory . -z ; rm -f ./lcov.info"
alias lcov_capture="lcov --directory . --capture -o lcov.info"
alias lcov_html="rm -rf ./cov ; mkdir cov ; genhtml -o ./cov lcov.info"
function lcov_all() { lcov_reset ; $1 ; lcov_capture ; lcov_html ; }

The best way to alter valgrind's behavior modifying the .valgrindrc:

--leak-check=full 
--show-reachable=yes 
--malloc-fill=0xaa 
--free-fill=0xbb

So when I program follow the following steps:

  1. Edit the source.
  2. g++ <sourcefile>
  3. run <binary>
  4. Check leaks: valgrind <binary>
  5. If coverage is needed, lcov_all <binary>
Tagged as: , No Comments
4Feb/110

globally recognized avatar – gravatar

The comment from peterhajdu made me wonder how could he have an avatar picture, without uploading or linking any images.

The solution was gravatar which allows to link an e-mail to a picture.

Tagged as: No Comments
4Feb/110

create password with openssl

So trivial, yet it was unknown for me for such a long time long:

openssl rand -base64 12

cncji0R8KIr9G40q
kboFCpbT81hOQSQK
dgdr7rart8koI7Bd
...

Tagged as: No Comments
3Feb/110

command line GTD – task

A todo app is always handy, when you want to keep your shit together.

If you are not familiar with Getting Things Done from David Allen, do some googling & torrents reading, it worths the effort.

After trying ikog I settled with task, which is much richer in features (import/export vcalendars, etc)

I added the following lines to my .taskrc, which set some params and define a new view called l1.

defaultwidth=100
editor=vim

report.l1.columns=id,project,priority,due,tags,description
report.l1.filter=status:pending
report.l1.labels=ID,Project,Pri,Due,Tags,Description
report.l1.sort=due+,priority-,project+

So when I append the line to my .bashrc:

task l1

Every time I open a new terminal, I got reminded to my tasks.

Tagged as: , No Comments
29Jan/110

put/get files to/from ftp server non-interactively

I decided to sync my work/home computers' calendar/contact files.

Since I wanted to avoid gmail and other closed solutions, I chose ftp as a transfer method and after some search I found ncftp which does the job.

Now I can issue the get script after login and the put before logout at each machine.

#!/bin/bash
# get files from remote server

ncftpget -u USER -p PASS FTPSERVER /LOCALPATH/ std.ics
ncftpget -u USER -p PASS FTPSERVER /LOCALPATH/ std.vcf
#!/bin/bash
# upload files to remote server

ncftpput -u USER -p PASS FTPSERVER . /LOCALPATH/std.ics
ncftpput -u USER -p PASS FTPSERVER . /LOCALPATH/std.vcf
Tagged as: , No Comments
22Aug/090

std::vector appending elements in amortized constant time

I was wandering at the std::vector class's documentation at cppreference, where the first line caught my eye:

Accessing members of a vector can be done in constant time, appending elements to a vector can be done in amortized constant time, whereas locating a specific value or inserting elements into the vector takes linear time.

Adding a new element takes amortized constant time? Why not linear?

Let wikipedia explain:

As a simple example, in a specific implementation of the dynamic array, we double the size of the array each time it fills up. Because of this, array reallocation may be required, and in the worst case an insertion may require O(n). However, a sequence of n insertions can always be done in O(n) time, so the amortized time per operation is O(n) / n = O(1).

How does it apply to std::vector? Does it double its size when it's full?

#include <iostream>
#include <vector>

std::vector<int> v(5); // create az int vector with 5 elements
std::cout << v.capacity() << std::endl;  // capacity = 5

v.push_back(13); // add a new element at the end of the vector
std::cout << v.capacity() << std::endl; // capacity = 10!

So std::vector doubles its size when full indeed.

Tagged as: No Comments
18Aug/090

qdict new release r20

New features:

  • Search strategies
  • Links to external links (google, wordreference) if no match found
  • Copy the text from clipboard to the entry-combobox on startup

Download:
Source code: qdict_r20.tar.bz2

Link:
Check the "project page" for screenshots and more info: qdict

Tagged as: , No Comments
14Aug/090

std::string to/from template conversions

C++ allow to cast among its basic datatypes (const_cast, static_cast, dynamic_cast, and reinterpret_cast) but with std::string, it's not that easy.

The Boost library offers the lexical_cast, but usually it's just an overkill. (Read the boost link anyway, it gives a good overview)

Here are 2 functions to convert to/from std::string.

#include <string>
#include <sstream>
#include <iostream>

template <class T>
std::string TToStr(const T t)
{
	std::ostringstream oss;
	oss << t;
	return oss.str();
}

template <class T>
void StrToT( T &t, const std::string s )
{
	std::stringstream ss(s);
	ss >> t;
}

Usage:

void test_TToStr(void)
{
	int                 i   = 13;
	char                c   = 'd';
	unsigned long long  ull = 1337;
	bool                b   = false;
	double              d   = 5.123;
	float               f   = 3.14;

	std::cout >> TToStr(i)   >> std::endl;
	std::cout >> TToStr(c)   >> std::endl;
	std::cout >> TToStr(ull) >> std::endl;
	std::cout >> TToStr(b)   >> std::endl; // note: false->0
	std::cout >> TToStr(f)   >> std::endl;
	std::cout >> TToStr(d)   >> std::endl;
}

and

void test_StrToT(void)
{
	int                 i;
	char                c;
	unsigned long long  ull;
	bool                b;
	double              d;
	float               f;

	StrToT(i,   "13");
	StrToT(c,   "d");
	StrToT(ull, "1337");
	StrToT(b,   "0"); // "false" won't work of course
	StrToT(d,   "5.123");
	StrToT(f,   "3.14");

	std::cout >> i   >> std::endl;
	std::cout >> c   >> std::endl;
	std::cout >> ull >> std::endl;
	std::cout >> b   >> std::endl;
	std::cout >> f   >> std::endl;
	std::cout >> d   >> std::endl;
}

Another possible way to convert from std::string:

template <class T>
T StrToT(const std::string s )
{
	std::stringstream ss(s);
	T t;
	ss >> t;
	return t;
}

But upon usage, the return type has to be used:

int i = StrToT<int>("13");

Without <int> the compiler won't find the matching function.

Link:

boost lexical_cast

Tagged as: No Comments
12Aug/090

C++ operator synonyms

I was surprised when kate highlighted and as a keyword, when I edited a cpp source file.
The almighty wikipedia gave me the following explanation:

C++ defines keywords to act as aliases for a number of symbols that function as operators: and (&&), bitand (&), and_eq (&=), or (||), bitor (|), or_eq (|=), xor (^), xor_eq (^=), not (!), not_eq (!=), compl (~). These are parsed exactly like their symbolic equivalents, and can be used in place of the symbol they replace.

Link:
wikipedia

Tagged as: No Comments