Paul - The Programmer

simple & stupid

Alter the word order in lines by VIM regular expression

Somehow, I am required to alter the order of word in lines in a text file.

For example, the input text file is like below.

hello world
hello paul
this  is
for   test

I want this file be updated was below.

world hello
paul hello
is  this
test   for

Thanks to regular expression, I do not need to update the lines one after another.

The vim regular expression is a little different than the Perl's. But most of syntax are similar.

\w  : word charaters, includes both alphabet and number characters.

\s   : space characters.

*   : match zero or more preceding character or meta character like \s, \w.

The patterns can be grouped by enclosing with \( and \). 

& :  the whole matched pattern

\0:   the whole matched pattern

\1:   the matched pattern in the first pair of \( and \)

\2:   the matched pattern in the second pair of \( and \)

So, to accomplish the above task, the substitution regular expression is :

:%s/\(\w*\)\(\s*\)\(\w*\)/\3\2\1/

 

The proper way to access to file in resource by Java

The below code demonstrates how to access to a resource file.

import java.io.*;

public class FileTest{
    public void test() throws IOException {
        String filePath = this.getClass().getResource("/test.txt").getPath();
        System.out.println( filePath );
        System.out.println( file.exists() );
    }

    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

Try to execute:

~/lab/test$ java FileTest 
/home/paul/lab/test/test.txt
true

It seems work perfectly. But this code is acturally not 100% right. When the class file is executed in a directory which name contains space character, the resource file will not be found.

~/lab/my test$ java FileTest 
/home/paul/lab/my%20test/test.txt
false

The space character in the file path was replaced by the '%20'. So, I update the path string before use it to create the File object.

import java.io.*;

public class FileTest{
    public void test() throws IOException {
        String filePath = this.getClass().getResource("/test.txt").getPath();
        System.out.println( filePath.replace("%20", " ") );
        File file = new File(filePath.replace("%20", " "));
        System.out.println( file.exists() );
    }
    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

Try this again:

~/lab/my test$ java FileTest 
/home/paul/lab/my test/test.txt
true

Looks like it's all right this time. Even the space charater in pah is supported now.

But this is still not the best way.

The above code acturally use the URL to access to the file. But the best way is use URI to access to file. Only the URI can directly support both the space and Chinese characters in file path.

import java.io.*;

public class FileTest{
    public void test() throws IOException, URISyntaxException {
        System.out.println( this.getClass().getResource("/test.txt").toURI( ).getPath());
        File file = new File(this.getClass().getResource("/test.txt").toURI());
        System.out.println( file.exists() );
    }
    public static void main(String [] args) throws Exception {
       FileTest test = new FileTest();
       test.test();
    }
}

The execution result:

~/lab/my test$ java FileTest
/home/paul/lab/my test/test.txt
true 

It works properly and the code is more simple with the URI.

The above code does not support to access to the files in another Jar file's resouce. But I'd like to talk about this topic in another post.  ;-)

Connect to wireless network with wireless tools

Wireless tools is a package of command line tools for configure wireless network.

With the help of wireless tools, it is no need to install the graphical network configuration tools, such as the wifi-radar.

The wireless tools consists with ifrename, iwconfig, iwgetid and iwlist.

To connect to a wireless network, we only need the help of iwlist.

iwlist can list all the nearby wireless nework access point and their addional information.

$ sudo iwlist ath0 scan 
ath0      Scan completed :
          Cell 01 - Address: 00:25:86:49:16:F4
                    ESSID:"TP-LINK_4916F4"
                    Mode:Master
                    Frequency:2.422 GHz (Channel 3)
                    Quality=21/70  Signal level=-74 dBm  Noise level=-95 dBm
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 6 Mb/s
                              12 Mb/s; 24 Mb/s; 36 Mb/s; 9 Mb/s; 18 Mb/s
                              48 Mb/s; 54 Mb/s
                    Extra:bcn_int=100
                    Extra:ath_ie=xxxxxxxxxxxxxx

Now, we get the available access point. To access to the wireless network, we need to configure the network interface.

For Debian, the network interface configuration file is /etc/network/interfaces.

To use the interface ath0 to access the acccess point 'TP-LINK_4916F4', the configuration show like below:

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug ath0
iface ath0 inet dhcp
	# wireless-* options are implemented by the wireless-tools package
	wireless-mode managed
	wireless-essid TP-LINK_4916F4
	wireless-key1 s:xxxxxxxx

Connect to the wireless network by restarting the networking init script.

# /etc/init.d/networking restart

 

Why Google Chrome GPU acceleration does not work for me

The latest Google Chrome browser comes with the GPU accelereation feature.

You may already know that to enable the GPU acceleration, we only need to enable the flag 'GPU Accelerated Compositing' and flag 'GPU Accelerated Canvas 2D' in the tab about:flags.

But it is not that kind of easy. On Linux, the GPU acceleration needs the OpenGL 2.0 support.

By checking with glxinfo.

$ glxinfo | grep -i opengl
OpenGL vendor string: DRI R300 Project
OpenGL renderer string: Mesa DRI R300 (RV380 5460) 20090101 x86/MMX/SSE2 TCL
OpenGL version string: 1.5 Mesa 7.7.1
OpenGL extensions:

My video card only support OpenGL 1.5 so far. This means even though the flag is enabled in the Chrome, still no hardware accelerated for page rendering at all.

The ATI Catalyst driver can give you up to OpenGL 4.0 and better 3D performance. But for my old X300, I can only choose the open source drive xf86-video-ati with OpenGL support by Mesa 3D Graphic Library.

The Mesa 3D Graphic Library can support OpenGL 2.0 by software-only rendering driver. I doubt update Mesa library can bring any help. 

So, I just give up the Chrome GPU acceleration. 

By the way, the flag 'Web Page Prerendering' is really helpful to imporve the page loading performance.

Change the blog fonts with Google Font API

I changed the default fonts with the Google Font API

I just add a few lines in the <head> and change the CSS accordingly. The 'Droid Sans' font is more eye-candy, isn't it? 

Didn't notice too much delay of page loading comparing to the time without Google Font.

I do not have much more to talk about them.

Just follow the simple instructions below and choose your favorite fonts. A piece of cake. :-)

http://code.google.com/apis/webfonts/docs/getting_started.html#Quick_Start

--- update ---

Just added the WebFontLoader Javascript in the <head>. The Google fonts are loaded asynchronosly. I believe you will notice the fonts change with a low speed connection.