My blog has moved! Redirecting…

You should be automatically redirected. If not, visit http://blog.stastnarodina.com/honza-en/ and update your bookmarks.

Tuesday, April 1, 2008

Moved

This blog has moved to http://blog.stastnarodina.com/honza-en so redirect your RSS readers and enjoy reading.

Sunday, January 20, 2008

C++ round

27th January Update: as somebody has pointed via email (thanks for that), the function I gave only worked for positive numbers. This is the solution he offered (altered a bit not to give warnings so that you can cut&paste ):

#include <cmath>
int function round(double num) {
  char sign = static_cast(num/fabs(num));
  return static_cast<int>(sign* (fabs(num)+ 0.5));
}

Actually, the solution I gave first (and then suddenly changed it to the one that only works for positive number...) worked perfectly with both positive and negative too. This is it:
#include <cmath>
int function round(double num) {
  return static_cast<int>(floor(num+0.5));
}

I was a bit shocked first, finding out there was no round function in C++.
It didn't fortunately take me long to google a nice substitute for it. It can be as simple as this (Warning: this works for positive numbers only. See above for better solution):

int round(double num) {
 return (int)(num+0.5);
}

I may be one of the more dumb and lazy programmers (isn't "laziness" in the flag of all programmers?), but I would expect a function like that to be a part of a modern language. Yes, it didn't take me long to google it (unlike some other C++ problems, which can take hours), but no googling is faster than short googling, isn't it?

I think it is worth seeing, what The Others offer

Labels:

Saturday, January 12, 2008

WD MyBook useful links

System upgrade

When things break

Labels:

Thursday, January 10, 2008

WD My Book Debian Lenny -- Samba

I have recently installed Debian Lenny to my Western Digital My Book. All thanks to great guide at Mario Pascucci's site (big thanks!) A commented version with some other details can be found at Hacking WD MyBook World Ed site

The system installed is full-fledged version of testing version (called lenny) of Debian, so one can use use apt-get to install packages the same way as at PC (providing you use a packages mirror, that serves arm architecture). Having successfully installed FTP (vsftpd), htop, gcc, and others, I also wanted to install Samba (as the most important part of such a device).

I was fighting with Samba at MyBook for more than two days (and nights). The problem was that it didn't want to list the shares at the server. The problem in the end was that there is a bug in Debian samba package version 3.0.28-1-- current testing (lenny) version, which has something to do with arm compiler -- if you download Samba source code and compile it yourself without additional parameter -O0, the result will be the same as with the samba Debian package.

The bug has already been fixed in 3.0.28-2. This version is not in testing (lenny) yet, but you can manually download the current versions from

To install it do as root:
cd /tmp
wget http://http.us.debian.org/debian/pool/main/s/samba/samba-common_3.0.28-2_arm.deb
wget http://http.us.debian.org/debian/pool/main/s/samba/samba_3.0.28-2_arm.deb
dpkg --install samba-common_3.0.28-2_arm.deb
dpkg --install samba_3.0.28-2_arm.deb
rm samba-common_3.0.28-2_arm.deb samba_3.0.28-2_arm.deb

Another way is to add sid deb sources to you /etc/apt/sources.list

I hope that this can make somebody's fight shorter than two days...

Labels:

Sunday, November 11, 2007

How to open Western Digital My Book World Edition

There are few guides on how to dismantle Western Digital My Book World Edition (500 GB) available online. (see here or here).

Just for those who have the new model (white with rubber around it):
  • There is no screw in the cover.
  • You need to remove the rubber before you try to slide the cover.

Labels:

Monday, September 24, 2007

Mysterious MySQL

Česká verze tohoto zápisku dostupná zde

MySQL can be sometimes pretty surprising.
I have recently encountered this thing:
I have this SQL query:
SELECT id, username, locked, c.*, (SELECT COUNT(*) FROM web_login_users WHERE user=id AND (NOW()-last_action)<600) AS login_count FROM web_users LEFT JOIN web_user_information AS c ON (id=user)
It works without any problems and the returned column set includes the column named login_count.
I wanted to get only the rows, where login_count is greater than 1. I intuitively tried this:
SELECT id, username, locked, c.*, (SELECT COUNT(*) FROM web_login_users WHERE user=id AND (NOW()-last_action)<600) as login_count FROM web_users LEFT JOIN web_user_information AS c ON (id=user) WHERE login_count > 1
This does't work. MySQL complains about not knowing the login_count column. To make it more interesting, this will work as expected:
SELECT id, username, locked, c.*, (SELECT COUNT(*) FROM web_login_users WHERE user=id AND (NOW()-last_action)<600) AS login_count FROM web_users LEFT JOIN web_user_information AS c ON (id=user) ORDER BY login_count
Some JOIN workaround will probably be needed.
Having made some more attempts and consulted the problem with a friend, I give up.
This is the JOIN version that I quite expected to work:
SELECT id, username, locked, c.*, COUNT(login_users.user) AS login_count FROM web_users LEFT JOIN web_user_information AS c ON (id=user) LEFT OUTER JOIN web_login_users AS login_users ON (user=id AND (NOW()-last_action)<600) WHERE login_count > 0
It returns the same error as the previous attempts (Unknown column 'login_count' in 'where clause').
I don't quite like this version, but it functions well:
SELECT id, username, locked, c.*, (SELECT COUNT(*) FROM web_login_users WHERE user=id AND (NOW()-last_action)<600) AS login_count FROM web_users LEFT JOIN web_user_information AS c ON (id=user) WHERE (SELECT COUNT(*) FROM web_login_users WHERE user=id AND (NOW()-last_action)<600) > 0

Labels: ,