Wednesday, December 1, 2010

Get Locale in Client Side

Here is a function to detect user's locale in client side:

function getLocale() {
if ( navigator ) {
if ( navigator.language ) {
return navigator.language;
else if ( navigator.browserLanguage ) {
return navigator.browserLanguage;
else if ( navigator.systemLanguage ) {
return navigator.systemLanguage;
else if ( navigator.userLanguage ) {
return navigator.userLanguage;


Wednesday, May 5, 2010

Apache Ant Presentation

This afternoon, May 5, 2010, I gave a presentation on Apache Ant in our regular informal Tech Talks held in our office. I tried to make it concise yet complete enough for beginners.
Apache Ant

Install new fonts in Ubuntu 9.04

If you have downloaded fonts from the web, purchased them, or acquired them from other sources and want to install these fonts in your Ubuntu 9.04 Jaunty Jackalope box, then there is an easy manual process to do this.

There are various locations in GNU/Linux in which fonts can be kept. These locations are defined in /etc/fonts/fonts.conf . Check your fonts.conf file and most probably you will find these type of entries there:


The last entry means .fonts direcotry in the user's home directory.

If you want to make the new fonts available for only one user, then copy those new fonts to the .fonts directory of that user. If .fonts directory is not there, create one first.

If you want to make the new fonts available for all users, then the best place to copy those fonts is to ...say..../usr/share/fonts

Now open a console and run this:
sudo fc-cache -f -v
That's it. There are other ways to install new fonts. But this one is quite good to live with :)

Thursday, April 22, 2010

Recover MySQL root Password

You can recover MySQL database server's root password with the following five steps.
  1. Stop the MySQL server process: $ /etc/init.d/mysql stop
  2. Start the MySQL (mysqld) server/daemon process with the --skip-grant-tables option so that it will not prompt for password: $ mysqld_safe --skip-grant-tables &
  3. Connect to mysql server as the root user: $ mysql -u root
  4. Setup new mysql root account password and quit: mysql> use mysql;
    mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
    mysql> flush privileges;
    mysql> quit
  5. Restart the MySQL server: $ /etc/init.d/mysql stop

Monday, April 5, 2010

Make data or tmp web-writable

In a web application, we need to make data or tmp directory writable by the web server. I saw many people achieve this by changing the permission level of that directory recursively to 777 in Unix/Linux:

$ chmod -R 777 tmp


$ chmod -R 777 data

This is really unnecessary. You can find out on behalf of which user, your web server is running by running this single line of php code:

echo `whoami`;

In my case, i got 'daemon' as the user on behalf of which the webserver is running.
Now we need to change the ownership of the data or tmp directory and make 'daemon' as the owner.

$chown -R daemon tmp

Wednesday, January 20, 2010

Tricky error during std::find function applied on std::string

An tricky problem is described here that is worth mentioning. Look at this code chunk:

int count(std::string& s, char c) {
std::string::const_iterator i = std::find(s.begin(), s.end(), c);
int n = 0;
while (i != s.end()) {
i = std::find(i+1, s.end(), c);
return n;

If you try to compile this function (offcourse adding main function with required headers), you will find that the first call to find doesn't produce any error. But the second call within the loop generates compilation error of this form:

In function ‘int count(std::string&, char)’:
error: no matching function for call to ‘find(__gnu_cxx::__normal_iterator, std::allocator > >, __gnu_cxx::__normal_iterator, std::allocator > >, char&)’

At first look, it seems to be weird. But it's not :)

The general find function is declared as:

InputIterator find(
InputIterator _First,
InputIterator _Last,
const Type& _Val

You can see that the type of the first and second parameters of the function template correspond to the same type. The compiler cannot deduce that it is this function that you want to call if your call has a combination of string::const_iterator and string::iterator as arguments.

During first call, i.e. std::find(s.begin(), s.end(), c), the first and second arguments are of string::iterator types and the return value is also of type string::iterator. But this return value is being casted to string::const_iterator.

During second call, i.e. std::find(i+1, s.end(), c), the first argument is of type string::const_iterator (due to that earlier casting) and the second argument is of type string::iterator. So, the types of first and second arguments don't match. That's why , the compiler is generating the error.

Here goes two different types of fixes.

Fix 1:

int count(std::string& s, char c) {
std::string::iterator i = std::find(s.begin(), s.end(), c);
int n = 0;
while (i != s.end()) {
i = std::find(i+1, s.end(), c);
return n;

Fix 2:

int count(const std::string& s, char c) {
std::string::const_iterator i = std::find(s.begin(), s.end(), c);
int n = 0;
while (i != s.end()) {
i = std::find(i+1, s.end(), c);
return n;

Tricky error..isn't it? ;)

Tuesday, January 19, 2010

Two-stage name lookup issues with template code

The C++ standard prescribes that all names that are not dependent on template parameters are bound to their present definitions when parsing a template function or class. Only names that are dependent are looked up at the point of instantiation. This distinction between lookup of dependent and non-dependent names is called two-stage (or dependent) name lookup. G++ implements it since version 3.4.

Two-stage name lookup sometimes leads to situations with behavior different from non-template codes. The most common is probably this:



template class Vec : public std::vector {
Vec() : std::vector () {}
Vec(int s) : std::vector (s) {}
T& operator[] (int i) { return at(i);}
const T&operator[] (int i) const { return at(i); }

the call to at() is not dependent on template arguments (there are no arguments that depend on the type T, and it is also not otherwise specified that the call should be in a dependent context). Thus a global declaration of such a function must be available, since the one in the base class is not visible until instantiation time. The compiler will consequently produce the following error message:

vec.h: In member function ‘T& Vec::operator[](int)’:
vec.h:7: error: there are no arguments to ‘at’ that depend on a template parameter, so a declaration of ‘at’ must be available
vec.h:7: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
vec.h: In member function ‘const T& Vec::operator[](int) const’:
vec.h:8: error: there are no arguments to ‘at’ that depend on a template parameter, so a declaration of ‘at’ must be available

To make the code valid either use this->at(i), or vector::at(i). Using the -fpermissive flag will also let the compiler accept the code, by marking all function calls for which no declaration is visible at the time of definition of the template for later lookup at instantiation time, as if it were a dependent call. Using -fpermissive to work around invalid code is not recommended however, and it will also only catch cases where functions in base classes are called, not where variables in base classes are used (as in the example above).

Some compilers (including G++ versions prior to 3.4) get these examples wrong and accept above code without an error. Those compilers do not implement two-stage name lookup correctly.