Converting from Mac to Unix line endings

Posted: March 10th, 2010 | Author: Rob Searles | Filed under: Linux, Tutorials | Comments

This is more a post for me than anyone else, but I thought I’d share anyway.

In our team we have a mixture of Linux and Mac users, and we are constantly editing the same files. One of the most irritating things is when a file is saved with Mac line endings instead of Unix line endings (displaying as ^M in Emacs). This happens very rarely, but on the occasion it does I can quickly convert back using the following commands on my Linux box:

$ tr '\r' '\n' < file-in-question.txt > tmp.file
$ mv tmp.file file-in-question.txt

Works perfectly

If you want to find more about the tr (Translate) program, visit the man page.


Site was Cracked

Posted: March 1st, 2010 | Author: Rob Searles | Filed under: General | Comments

Some of you may have noticed I’ve been gone for a few months. More on that later. However, whilst I was away I found out that this site was cracked!

My apologies to any of you who were affected by this. I have taken steps to ensure this won’t happen again.

I’ll be back soon with more writing on PHP, JavaScript, Ubuntu and other Linux Distros and the fantastic Awesome Windows manager, I promise!


Understanding Bitwise Operators (hopefully)

Posted: December 4th, 2009 | Author: Rob Searles | Filed under: PHP, Tutorials | Comments

After the trouble I had with bitwise operators yesterday I found some time to really sit down and get my head properly around them. Let’s dive straight in.

We need to initially define our flags:

define('BASE', 0); // binary 00000000
define('F1', 1);   // binary 00000001
define('F2', 2);   // binary 00000010
define('F3', 4);   // binary 00000100

To start with we have no flags set, so if we set F1 using the following:

$f1_set = BASE + F1; // $f1_set = 1
echo "F1 set = $f1_set\n";

All is well and good, $f1_set = 1 as expected.

However, what if we set F1 again?

$f1_set_twice = $f1_set + F1; // $f1_set_twice = 2 !!! wrong !!!
echo "F1 set twice = $f1_set_twice\n";

As you can see, if we set F1 twice, it effectively “unsets” F1 and sets F2. Not what we were after.

So why is this? Well, it’s kind of obvious and I was being a bit of a muppet for not spotting it yesterday. The reason is pretty simple: 1 + 1 = 2. (I told you it was obvious!)

Clearly this is not what we want, but how can we solve this? By using the bitwise OR. If we change the statements slightly as follows:

$f1_or_set = BASE | F1; // $f1_or_set = 1
echo "F1 OR set = $f1_or_set\n";
 
$f1_or_set_twice = $f1_or_set | F1; // $f1_or_set_twice = 1 - huzzah!
echo "F1 OR set twice = $f1_or_set_twice\n";

As far as “unsetting” the flags if we use my original method we fall (again) into trouble.

$f1_and_f3 = BASE | F1 | F3; // $f1_and_f3 = 5;
$unset_f3 = $f1_and_f3 - F3; // $unset_f3 = 1
echo "Unset F3 = $unset_f3\n";
 
$unset_f1 = $f1_and_f3 - F1; // $unset_f1 = 4;
echo "Unset F1 = $unset_f1\n";

Now, if we try to “unset” F1 twice, we arrive at the problem.

$unset_f1 = $f1_and_f3 - F1; // $unset_f1 = 3;
echo "Unset F1 = $unset_f1\n";

Unsetting F1 twice here effectively turns off F3 and sets F1 and F2 – completely wrong!

Instead, if we use the &~ binary operator mentioned in Jesper’s comment all works as expected. (note: I can’t find mention of this operator in the PHP docs, please someone help me out)

$f1_and_f3 = BASE | F1 | F3; // $f1_and_f3 = 5;
$unset_f3 = $f1_and_f3 &amp;~ F3; // $unset_f3 = 1
echo "Unset F3 = $unset_f3\n";
 
$unset_f1 = $f1_and_f3 &amp;~ F1; // $unset_f1 = 4;
echo "Unset F1 = $unset_f1\n";

Even if we try to “unset” a flag twice, it still has the same results:

$unset_f1 = $f1_and_f3 &amp;~ F1; // $unset_f1 = 4;
echo "Unset F1 = $unset_f1\n";
 
$unset_f1_twice = $unset_f1 &amp;~ F1; // $unset_f1_twice = 4;
echo "Unset F1 twice = $unset_f1_twice\n";

Also in Jesper’s comment and original post was the use of the left shift operator: <<. After playing around with this it seems very simple to use, as follows:

$f1 = 1;    // 00000001
$f2 = 1<<1; // 00000010
$f3 = 1<<2; // 00000100
$f4 = 1<<3; // 00001000

Or to put it another way:

$f1 = 1;      // 00000001
$f2 = $f1<<1; // 00000010
$f3 = $f2<<1; // 00000100
$f4 = $f3<<1; // 00001000

After all this I think I am a little closer to understanding Bitwise operations, hopefully! Tomorrow I’ll have a crack at testing to see if a flag is turned on or not. Until then…


Bitwise Operators used for Flagging Items

Posted: December 2nd, 2009 | Author: Rob Searles | Filed under: PHP, Tutorials | Comments

Update: Thanks to Jesper Noehr of BitBucket fame for pointing out gaping flaws in my post below (see his comment). I strongly advise you disregard all I have said below, because it will get you into a mess, in much the same way it has me. I’m going to sit down when I have a spare 1/2 hour and try to work out exactly what is going on! Many thanks and big kudos to Jesper, I really appreciate the time you took to correct me.


I have always wondered what the point of Bitwise Operators were,to me they seem to belong to a distant past. However, after reading a couple of great blog posts I have at last an understanding of how they can be put to use, and have started playing around with them a bit (ba dum!).

Jesper Noehr has written about using bitwise operators for a flexible permissions scheme within Python  and Jonathan Snook has taken the bitwise concept further creating a great calendar app in Javascript. After reading these I thought I better dive in, and an opportunity came along yesterday when I had to code a flagging system within PHP.

Read the rest of this entry »


First Steps with Node.js: exciting stuff

Posted: November 29th, 2009 | Author: Rob Searles | Filed under: JavaScript, Opinion | Comments

During my Saturday morning reading yesterday I fell over something called Node.js.  According to the website

Node’s goal is to provide an easy way to build scalable network programs.

which is kind of exciting, but not mind blowing, however, Ryan Dahl’s GitHub page describes it as

evented I/O for v8 javascript

Which is slightly more exciting. However, it is when you start to play around with it that things begin to get very exciting indeed.

Read the rest of this entry »


3 days without the Interwebs

Posted: November 28th, 2009 | Author: Rob Searles | Filed under: General | Comments

We had a bit of a nightmare over at ibrow Towers recently. Some bright spark at the building site down the road managed to cut the pipe supplying our interweb!

Disaster!

Fortunately the culprit managed to refrain from slicing all the way through, stopping just at the point to give us the drip feed equivalent of roughly a modem circa 1997. i.e. r e a l l y slow.

Did we really ever live with that? How did we cope?

But now it is the weekend, meaning that I have some spare time at home to explore the internet again. So here are a few links that I’m going to spend my morning reading.

Read the rest of this entry »


RobSearles: 1 Year Old!

Posted: November 20th, 2009 | Author: Rob Searles | Filed under: General | Comments

This is just a quick post to say Happy Birthday this blog. Yes, I began my little journey into the Blogosphere one year ago today. Looking back on that first article, now is a good time to see if I managed to stay focused on the topics that I laid out in the beginning, as well as looking at some stats for the year and why I’m doing this.

Read the rest of this entry »


Sony PRS-300 Pocket eReader Review

Posted: November 18th, 2009 | Author: Rob Searles | Filed under: Opinion, Reviews | Comments

Sony Pocket Reader PRS-300You may have noticed I haven’t posted in a while. One of the reasons is because I have been immersed in the world of digital books. In my previous post I mentioned that I purchased myself a new toy, namely the Sony PRS-300 Pocket eReader. Now I’ve had it almost 3 weeks so I feel I’m in a position to write a fairly accurate review and not one based solely on first impressions.

I’ll start with all the fluff that don’t relate to actually reading a book, then I’ll move on to what it is actually like to read.

I bought my Pocket Reader online from WHSmiths for a very reasonable £159. I had weighed up getting the more expensive PRS-600, but decided against it as I don’t need my book to play me music, and whilst it can hold 8000 books compared to “only” 350 of the PRS-300 I’ll be impressed if I read 350 books a year!

Read the rest of this entry »


Two New Toys: First Impressions

Posted: October 31st, 2009 | Author: Rob Searles | Filed under: General | Comments

Microsoft Natural Ergonomic 4000 KeyboardThis week I purchased myself two new toys. I always do something like this around this time of year because it’s my birthday! This year I decided to get an ergonomic keyboard and an e-reader.

The ergonomic keyboard was the easier decision and I went with the Microsoft Natural Ergonomic 4000.One of the main reasons for this was the Jeff Atwood blog post.

The e-reader was much harder to choose. After much deliberation, googling, reading of reviews etc I eventually landed myself a Sony Pocket Reader PRS-300. I chose this over other e-readers for a number of reasons. Firstly the price was right, I didn’t really want to spend over £200 on what is effectively a fancy book. Secondly I only wanted it for reading, I didn’t care about taking notes, listening to music that kind of stuff. Finally I wanted to to be portable – obviously all readers are portable, but I wanted something that I could easily keep in a coat pocket for when I’m on a bus etc. or generally waiting around for something to happen.

First Impressions

The keyboard was huge! Much more so then I expected, but fortunately I have a big desk, so there was no problem there. I’ve never used an ergonomic keyboard before and I was suprised how comfortable it felt. I really thought I’d be able to enjoy typing. However, the space bar is a nightmare. Really sticky, hard to press down and very “clacky” Others have experienced this problem, but most suggested it gets “broken in” within a few days or week, so I’m not overly worried (yet).

Sony Pocket Reader PRS-300The Sony e-reader is just beautiful. The text is clear, navigation simple, weight nothing to speak of and the size is perfect. I am in love with it. One problem was I said I had to use Windows to connect to it, so I fired up Vista for the first time in about half a year, and after about 20 minutes of installing it crashed. It did this again a couple of times, until I found out a fix – something so do with VB script and premissions. Rather stupidly I forgot to save the link  that helped me fix the problem otherwise I would have posted it here. Sorry about that. However, it turns out I can connect to it within Ubuntu, and transfer books onto it – so that is good news.

I’m going to write a more substantial review for both after a couple of weeks usage, so stay tuned – sign up to my RSS feed.


Emacs CSS-Mode Fix (adding to the chain!)

Posted: October 29th, 2009 | Author: Rob Searles | Filed under: Open Source, Tutorials | Comments

After my week with Emacs I’m still struggling along. One of my main annoyances is the default way some of the modes indent the code. I think the worst offender of this is the default CSS-mode. After frustration when, yet again, I hit the tab key and the cursor rockets about 80 places to the right I decided to Google for a fix.

The internet is great! Clearly it wasn’t just me having a problem, Guido Stevens was also suffering and posted a fix, which he himself found on another blog post by Chris Miller, which Chris in turn found on StokeBloke.com which had been found originally on a now dead post.

Not bad! Four people all having problems with the same thing and posting a fix for others to share. So I thought I’d join the chain, and write this blog post.

Oh yes, and the fix itself:

;; fix css mode
(require 'css-mode)
(setq cssm-indent-level 2)
(setq cssm-newline-before-closing-bracket t)
(setq cssm-indent-function #'cssm-c-style-indenter)
(setq cssm-mirror-mode t)