Tuesday 28 July 2009

HOWTO: get stdin from shell in Gedit External Tool

The following code can get the input from Gedit External Tool as you assigned for 'input'.

#!/bin/bash
search=`xargs`
echo $search
The trick is the command of xargs.

Also available Environment Variables in Gedit External Tool are:
=====================
GEDIT_CURRENT_DOCUMENT_URI
GEDIT_CURRENT_DOCUMENT_NAME
GEDIT_CURRENT_DOCUMENT_SCHEME
GEDIT_CURRENT_DOCUMENT_PATH
GEDIT_CURRENT_DOCUMENT_DIR
GEDIT_DOCUMENTS_URI
GEDIT_DOCUMENTS_PATH

An example: compile java file and run it
=========================
javac $GEDIT_CURRENT_DOCUMENT_NAME && java -cp . `echo $GEDIT_CURRENT_DOCUMENT_NAME | sed s/.java//`

Wednesday 22 July 2009

16-bit binary data interpreting difference between Java and Matlab

The 16-bit data will be interpreted as different values in Java and Matlab. WHY??

Here are some experiments: save number 15 to 8-bit and 16-bit binary files, and another 259 in 16-bit binary files (refer to : this post).
======================

In Matlab (fread(fid1, 'uint8');): 15

16 bit bin file containing 15:
In WinHex: 0F 00
In java (DataInputStream.readShort();): [3840, 0]
=> 3840/256=15;
=> so, it's a kind of [15 00]
In Matlab (fread(fid1, 'uint16');): 15

16 bit bin file containing 259:
In WinHex: 03 01
In java (DataInputStream.readShort();): [769, 0] //Note, for every byte, java can

contain a value larger than 256, while other programming language will increment.
=> 769%256 = 1; 769/256=3.0039;
=> so, it's a kind of [03 01]
In Matlab (fread(fid1, 'uint16');): 259

Therefore:
========================
Therefore, a integer value in the range of 16-bit, for example 41837. In Matlab,

it will be intepreted as 41837, mod(41837,256)=109, 41837/256=163.4. So 41837 can

be displayed as [163 109] = 163*256+109; while java intepret [163 109] as

163+109*256 = 28067;


Conclusion:
========================
This seems wrong inside Java, but Java know how to process such value and export

it correctly to another bin file, which one can be understood by Matlab, WinHex or

other programming languages or editor.

Tuesday 21 July 2009

Simplest procedure to TRIPLE BOOT on Macbook

The must:

  • Only ONE partition for Linux; Only ONE partition for Windows. (to satisfy Windows)
  • Windows must locates at the last partition. (to satisfy Macbook)
  • Before installing, ensure the partitions are ready and good! (Disk Utility normally can not do this, or do half of this task. Because it will create several 'spare spaces' between each partitions made by it, these partitions always make the Windows installation fail due to more than 4 primary partitions existing. But Disk Utility can create one more partition from the original one whole partition, then split it using other ways, which includes 'diskutil' in Mac, 'gparted' from Linux Livecd or even the Windows installer during Windows setup). Any partitions modification will make Windows can not boot up even synced by rEfit.

  • - If Windows has been installed by Boot Camp. Try to change boot.ini in my windows partition (disk util create a new partition between mac and windows and messed up my boot.ini, the partition it tried to boot (third) is now linux Hd instead of windows (become 4th. This might be your problem. It gave me BSoD right after splash screen)

So the procedure:

  1. Partitioning (DO NOT USE BOOT CAMP ASSISTANT)
  2. sync mbr/gpt using rEfit. (maybe can be ignored)
  3. Install Winxp on the 4th (last) partition.
  4. sync mbr/gpt using rEfit.(maybe can be ignored)
  5. Install Linux.

Java/Matlab read and write 16-bit data file

  • Read 16 bit binary file:
    File file = new File("FILENAME.bin");
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream( bis );

    short[] myarray = new short[256000];
    int cnt;
    for (cnt = 0; cnt < 256000; cnt++) {

    myarray[cnt] = dis.readShort();

    }
    fis.close();

    Alternatively:
    RandomAccessFile dis = new RandomAccessFile(new File("FILENAME.bin"), "r");

    Note:
    1) bufferedInputStream can be ignored, but it will improve the performance 90% for large file operation.
    2) RandomAccessFile works like a 'pointer', similar to fread in Matlab, offset or skip can be assign when data retrieving while not really load all the file. This class has both read and write methods, it is a convenience class, like filereader and filewriter, while the former method is kinda low level methods.

  • Write 16 bit binary file:
    FileOutputStream fos = new FileOutputStream(new File("OUTPUT.bin"));
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    DataOutputStream dos = new DataOutputStream(bos);

    for (cnt = 0; cnt < 256000; cnt++) {

    myarray[cnt] = dis.readShort();

    }

    bos.flush();

    fos.close();

    Alternatively:
    RandomAccessFile dos = new RandomAccessFile(new File("OUTPUT.bin"), "rw");
    Note: The following step is essential!!!! Otherwise, there will be some data lost.
    bos.flush();
  • Matlab:

    fid1 = fopen('filename.bin', 'r');
    A = fread(fid1, 100, 'uint16=>float32');
    fclose(fid1);

    fid2 = fopen('output.bin', 'w');
    fwrite(fid2, A, 'uint16');
    fclose(fid2);

Sunday 19 July 2009

BUG, HOWTO: how java read 8-bit and 16-bit

you have to use twice as much memory, but there really is no other solution:
=======================

use a short to hold an unsigned byte, use a long to hold an unsigned int. (And use a char to hold an unsigned short.)

This example create a 16-bit file, which only contain a value, 15 in 16-bit (000f), This file can be viewed by Gedit. From Matlab, it can retrieve and display correctly as 15, but in Java, using DataInputStream.readShort(), it's 3840, which equals to 15*256.



Example 1: Using Matlab to generate a 16-bit data file.
================
clear;clc;%% create binA=15;fid = fopen('output.bin', 'w'); fwrite(fid, A, 'uint16');fclose(fid);%% read binfid1 = fopen('output.bin', 'r'); B = fread(fid1, 1, 'uint16')fclose(fid1);

Example 2: Using Java to retrive the 16-bit data from the bin file.
===========
import java.io.*;public class convbin { public static void main(String args[]) throws IOException { File file = new File("onev.bin"); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); DataInputStream dis = new DataInputStream( bis );// RandomAccessFile dis = new RandomAccessFile(new File("fotech1.bin"), "r"); short[] myarray = new short[256000]; int cnt; for (cnt = 0; cnt < 1; cnt++) { myarray[cnt] = dis.readShort(); } fis.close(); System.out.println("Done."); FileOutputStream fos = new FileOutputStream(new File("o22.bin")); BufferedOutputStream bos = new BufferedOutputStream(fos); DataOutputStream dos = new DataOutputStream(bos);// RandomAccessFile dos = new RandomAccessFile(new File("o22.bin"), "rw"); for (cnt = 0; cnt < 1; cnt++) { dos.writeShort(myarray[cnt]); } System.out.println("Finish."); bos.flush(); fos.close(); }}

More details, except the example: http://www.darksleep.com/player/JavaAndUnsignedTypes.html

Friday 17 July 2009

HOWTO: run remote X11 applications

The server is running on Linux. To run the remote server applications on my own machine:

  • On Windows:
    =======================
    1. Install Xming as the X11 server.
    2. Run Putty, as ssh client, with the X11 forwarding arguments: 'localhost:0' or ':0', or even with blank only enable forwarding.
  • On Mac OSX
    =======================
    1. run: ssh -X -l username 192.168.1.XXX
    * using Putty: with the X11 forwarding arguments: ':0'!!!!!!!!
  • On Linux
    =======================
    1. run: ssh -X -l username 192.168.1.XXX
    * using Putty: with the X11 forwarding arguments: ':0' or blank.

Sunday 12 July 2009

HOWTO: install Adobe Air on Feodra 11

Just guest Redhat developers want to improve the boot speed, they make the fedora 11 without some 'useful' packages by default. This makes some program can not installed properly, such as Adobe Air.

The fix:
================

yum -y install xterm gtk2-devel gnome-keyring libxml2-devel libxslt rpm-devel nss
From: yum -y install xterm gtk2-devel gnome-keyring libxml2-devel libxslt rpm-devel nss

放弃在Mac 10.5.7 上安装GNUstep 0.23

经过大半个星期的下班时间调试,尝试在Mac osx 10.5.7 (darwin 9.7)上安装GNUstep 0.23,最终没有成功,还把Mac弄残一次,我猜可能是由于compiling的时候出现的segmentation fault致使Kernel Panic。


总结失败教训如下:
  1. make的时候,常常是需要 $LDFLAG 比用到 $DYLD_LIBRARY_PATH 的时候多。后者貌似更像是运行程序的时候使用,而不是configure和make时候用。
  2. Mac作为操作系统,其界面无可争议,但是底层系统方面,我个人觉得还不如Linux。在最近的console下编译程序的时候,出现两次致使Mac不断重启的情况,而解决方法都是不得不重装系统后,第二次更离谱,连cmd+s的single user mode都无法进入。Mac啊,现在给我的感觉是典型的外强中干。
  3. GNUstep需要有gnu的objc runtime,如果用默认的设置,必然使得其调用mac自带的gcc和objc的库,这样肯定会带来些兼容性的问题。 我猜我弄出的两次mac无法开机不断重启,与此两种不兼容并冲突的objc有关系。因为GNUstep在ubuntu 9.04和fedora 11下编译运行的很好。如果想在Mac上安装,我觉得还得是要等macports提供,macports跟进的还算不错,对gnustep,和官方新版本放出的时间也就差2个星期左右。可是,macports下的objc目前还不能安装,等到这个出现后,可以再试编译gnustep源文件。

以下是我用到的env设置:
#export LDFLAGS='-L/usr/local/lib -L/usr/lib -L/opt/local/lib -L/sw/lib'
export LDFLAGS='-L/opt/local/lib -L/sw/lib -L/usr/GNUstep/Local/Library/Libraries'
#export DYLD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/opt/local/lib:/sw/lib

export CPPFLAGS='-I/opt/local/include -I/sw/include'
#export CFLAGS='-I/usr/local/include -I/usr/include -I/opt/local/include -I/sw/include'

export PATH=/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sb:/usr/bin:/bin:/usr/sbin:/sbin

#. /usr/GNUstep/System/Library/Makefiles/GNUstep.sh

注:个人认为,在编译gnu的程序时,最好避免用到mac自带的一些命令。而且,最好在编译之前,把/usr/bin/gcc改成是 /opt/local/bin/gcc-mp-4.2这样的,以防止避免gnu和mac的程序冲突。

Thursday 9 July 2009

HOWTO: remove files older than N days

Command Syntax

find /path/to/files* -mtime +5 -exec rm {} \;

Note that there are spaces between rm, {}, and \;

Usage:
======================
Extremely useful for Miro, because it doesn't delete files unwatched. So with this command added in /usr/bin/miro, the Miro library will keep only latest videos no matter you watch it or not.

Explanation
=======================
The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.

The third argument, -exec, allows you to pass in a command such as rm. The {} \; at the end is required to end the command.


Command from: http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/

Tuesday 7 July 2009

HOWTO: compile source code on MAC OS X

Conditions:

======================
Macport (recommended, can be found on apple.com, updated and organized well) and Fink are installed in default locations, /opt/local and /sw.

Note: Fink is buggy, normally reinstallation on the previous version will not work, issues like path setup maybe occur, either bin files or libraries are not accessible.

To compile:
======================
  1. For library invoking:
    export LDFLAGS='-L/usr/local/lib -L/usr/lib -L/opt/local/lib -L/sw/lib'
    - '-L/dir' is the convention to setup gcc switch, while -L for make is --check-symlink-times, useless for me :)
    - Official way for invoking library is to setup $DYLD_LIBRARY_PATH (export DYLD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/opt/local/lib:/sw/lib), which in Linux is $LD_LIBRARY_PATH by default.
    - Normally lib path don't need specified, because the bin files (in /usr/bin or /opt/local/bin' will know where to get appropriate libraries, no matter bin files from Macports or Fink.

  2. For header files invoking:
    export CPPFLAGS='-I/usr/local/include -I/usr/include -I/opt/local/include -I/sw/include'
    - This is the way to setup both 'make' and 'gcc' switch, which is 'make -I/usr/local/include'.

Example:
=====================
The following are what I typed in terminal with user root to compile GNUstep-0.22.

export CPPFLAGS='-I/usr/local/include -I/opt/local/include'
export LDFLAGS='-L/usr/local/lib -L/usr/lib -L/opt/local/lib -L/sw/lib'
export DYLD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/opt/local/lib:/sw/lib:/usr/GNUstep/Local/Library/Libraries
. /usr/GNUstep/System/Library/Makefiles/GNUstep.sh

Sunday 5 July 2009

MAC OS X: change the environment PATH

To add a new directory to the path, simply add it to the existing PATH line in .profile being careful to separate it from other directories there with colons and careful not to introduce unwanted spaces (everything after the space will be ignored). For example, to add the directory /mightyq/bin to the PATH shown above, the line could become any of the following examples:

export PATH=/mightyq/bin:/opt/local/bin:/opt/local/sbin:$PATH
export PATH=/opt/local/bin:/mightyq/bin:/opt/local/sbin:$PATH
export PATH=/opt/local/bin:/opt/local/sbin:$PATH:/mightyq/bin

Note that in the third example the new directory is added to the end of the PATH. You have the ability to optimize the searches your shell will do on your behalf each time you run a command by organizing your PATH logically. Putting less frequently used or really massive directories later in the path may give you a little performance boost (although these days things are pretty fast, so you have to be a little anal to really enjoy this).

If you don’t need a directory in your path, you can reverse the process by deleting the unwanted directory still taking care to preserve the no spaces, colon separation rules.

One last note, to test the change you made, you can use the echo command, but you need to make the shell reload the .profile first. Assuming you are in your home directory (if not, running ‘cd’ without any options will take you there), run these commands:

. ./.profile
echo $PATH

The first . is a shortcut to cause the shell to ’source’ or load the contents of the subsequent file as itself, in the manner that the shell uses when you login to a system or start a Terminal window. If you simply executed these commands like a shell script (bash .profile, for example) you would start a new shell, that shell would get the variable set, and at the end of running the .profile script, that new shell would cease to exist and the newly defined variables would be relegated to the missing sock universe.While . makes the variables generated from new shell ( child shell ) return to the current shell (parent shell).

Saturday 4 July 2009

BUG: GNUstep Project Center can not generate the conf for 'MAIN_MODEL_FILE'

GNUstep Project Center (0.5.0) can not generate the conf for 'MAIN_MODEL_FILE', therefore, to make the GUI (gorm style) work, put corresponding configuration in the 'GNUmakefile'. Like the following:

#
# Main application
#

PACKAGE_NAME=SimpleApp

APP_NAME=SimpleApp
SimpleApp_MAIN_MODEL_FILE=MainMenu.gorm

The missing part in Gorm Manual on GNUstep, about 'NewClass' controller

8 Creating an Application
If you have ProjectCenter, you need to open it and create an “Application” project.
Create it with the name “FirstApp”. From there you can open the MainMenu.gorm by
clicking on interfaces and selecting MainMenu.gorm. If Gorm.app is properly installed, you
Gorm should start up.
If you don’t have ProjectCenter, you can create the Gorm file by hand. First you need
to start Gorm. You can either do this by doing ‘gopen Gorm.app’ from a command line
prompt, or you can invoke it from the Dock or from the workspace’s file viewer.
You then need to select the ‘Document’ menu, and then ‘New Application’. This should
produce a new document window, with a menu and an empty window. This should be
the same as with the ProjectCenter gorm file since this is the basic starting point for an
application.
For the sections below... only do one or the other, not both.
8.1 Creating A Class In Gorm
There are two ways to do this next operation. I will take you through each step by step.
First click on the classes icon in the toolbar on the top of the Gorm document window.
You should see the view below change to an outline view containing a list of class names.
Once this happens we’re ready to create a class. Select the class you wish to subclass in
the outline view. For our example we will use the simplest: NSObject. Select it by clicking
on the class name once. Then go to the Classes menu in the main menu and select Create
Subclass (you can also type Alt-Shift-c, which will do this as well. The new class will be
created in the list with the name “NewClass”.
8.2 Using The Outline View
From here double click on the subclass name to make it editable. Type the name of the
class and hit enter. For our example, please use the class name MyController. When you
hit enter an alert panel will appear and warn you about breaking connections, simply select
OK and continue.
This method of inputting the classes was inspired by IB in OPENSTEP 4.2/Mach which
had functionality very similar to this. For users of that the transition to Gorm will be
seamless.
8.2.1 Adding Outlets In The Outline View
Too add an outlet, select the round icon with the two horizontal lines in it (it sort of looks
like a wall outlet. This should become depressed. Here you need to go to the Gorm Menu,
under Classes select “Add Outlet/Action”. Each time you press this menu item another
outlet will be added with a name similar to newOutlet, as you add more the number at the
end will increase. For now add only one outlet.
To rename the outlet simply double click it and change it’s name like you did the class
above to “value” for the sake of our example.
Chapter 8: Creating an Application 20
8.2.2 Adding Actions In the Outline View
The procedure to add on action is precisely the same as adding an outlet, except you
must click on the button which looks like a target (a circle with a + inside). Add an action
and name it “buttonPressed:” for the sake of our example.
8.3 Using The Class Edit Inspector
This way is much more inline with the “OPENSTEP/GNUstep” philosophy. For each
object there is an inspector, even for Class objects.
Once you have created the class as described in the previous section “Creating a Class
In Gorm”, you must skip to this section to use the inspector. In the Gorm main menu
select Tools and then select “Inspectors”. This will make certain that the inspectors win-
dow is being displayed. Once the inspectors window is up move the pulldown on the top
to “Attributes” and select the class you created which should, at this point, have the name
“NewClass”. You’ll notice that the “Class” field at the top which shows the name’s back-
ground color has turned white, instead of grey. This indicates that this class name is
editable. Erase “NewClass” from the text field and type “MyController”.
8.3.1 Adding Outlets In The Inspector
Adding outlets is very intuitive in the inspector. Simply select the “Outlets” tab in the
tab view and click “Add” to add more outlets, and “Remove” to remove them. For the sake
of our example, add one outlet and name it “value”.
8.3.2 Adding Actions In the Inspector
Very much like above only with the “Actions” tab, add an action called button pressed.
8.4 Instantiating The Class
In the Classes outline view select the new class you’ve created, now called MyController
and then go to the Gorm menu and select Classes, and then Instantiate. The document
window should shift from the classes view to the objects view. Amoung the set of objects
should be a new object called MyController.
8.5 Adding Controls from the Palette
Go to the Gorm menu and select Tools, then Palettes. This will bring the palette window
to the front. The second palette from the left is the “ControlsPalette”. Select that one and
find the button object (it should have the word “Button” in it). Drag that to the window
and drop it anywhere you like.
Repeat this operation with the text field. It’s the control with “Text” in it. We are now
ready to start making connections between different objects in the document.
Chapter 8: Creating an Application 21
8.5.1 Making Connections
The type of application we are creating is known as a “NSApplication delegate” this
means that the MyController object will be set as the delegate of NSApplication.
To make this connection click on NSOwner and hold down the Control button, keep it
pressed as you drag from the NSOwner object to the MyController object. The inspectors
window should change to the Connections inspector and should show two outlets “delegate”
and “menu”. Select the “delegate”, at this point you should see a green S and a purple T
on the NSOwner and MyController objects respectively, and press the “Connect” button
in the inspector. In the “Connections” section of the inspector you should see an entry
which looks similar to “delegate (MyController)” this indicates that the connection has
been made.
Now we need to make connections from the controller to the textfield and from the
controller to the button. Select the MyController object and Control-Drag (as before)
from the object to the text field, this will make an outlet connection. You should see the
connections inspector again, this time select the “value” outlet and hit Connect.
Next, control-drag from the button to the controller, this will make an action connec-
tion. The connections inspector should again appear. This time you need to select the
“target” outlet, to get the list of actions. The list should have only one entry, which is
“buttonPressed:” since this is the one we added earlier. Press Connect. You should see an
entry like “buttonPressed: (MyController” in the Connections section of the inspector.
It is also possible to make this connection to NSFirst, but to keep things simple, make
it directly to the object. If you make the connection to buttonPressed: on NSFirst the
functionality of the application will be unchanged, but the invocation will take the path
described above in the section which describes “The Responder Chain”.
8.6 Saving the gorm file
At this point you must save the .gorm file. Go to the Gorm menu and click Documents
and then select “Save”. If the document was opened from a pre-existing .gorm, it will save
to that same file name. If it is an UNTITLED .gorm file a file dialog will appear and you
will need to select the directory where you want to store the .gorm file and type the name
of the .gorm file.
8.7 Generating .h and .m files from the class.
This is different than saving, some people have gotten this confused with the idea of
Gorm generating the code for the gui. Gorm does nothing of the sort (grin).
Go to the Classes section in the Document window and select the MyController class yet
again. Now go to the Gorm menu and select Classes and the select “Create Class Files”.
This will bring up a file panel and it allow you to select the directory in which to put the
files. It will first create the MyController.m file and then the MyController.h file. Simply
select the directory in which your app will reside and hit okay for both. You can change
the names, but the default ones, which are based on the class name, should be sufficient.

When you look at the .m for this class, you should see the ‘buttonPressed:’ method with
the commecnt ‘/* insert your code here */’ in it. Delete this comment and add ‘[value
setStringValue: @‘‘Hello’’];’. The class should look like this after you’re done:
/* All Rights reserved */
#include
#include "MyController.h"
@implementation MyController
- (void) buttonPressed: (id)sender
{
[value setStringValue: @”Hello”];
}
@end
You recall, we connected the textfield to the “value” variable. The call above causes the
method setStringValue to be invoked on the textfield you added to the window.
Also, note that the name of the method is “buttonPressed:”. This is the action which is
bound to the button. When it is pressed the text in the textfield should change to “Hello”.
You now need to build the application either by copying in a GNUmakefile and making
the appropriate changes or by using ProjectCenter’s build capability, depending on if you
use it or not.
This app is available as “SimpleApp” in the Examples directory under the Documen-
tation directory distributed with Gorm. Hopefully this has helped to demonstrate, albeit
on a small scale, the capabilities of Gorm. In later chapters we will cover more advanced
application architectures and topics.

Friday 3 July 2009

HOWTO: add shared libraries to lib path in Linux

Permanent way:
  • Create a new file in /etc/ld.so.conf.d/ called .conf
  • Edit the file and add a line per directory of shared libraries (*.so files), it will look something like:
/usr/lib/APPLICATION/lib
Reload the list of system-wide library paths:
  • sudo ldconfig
====================================
Temporary way:
  • export LD_LIBRARY_PATH=/your/lib/path
On Mac, the dynamic library path is $DYLD_LIBRARY_PATH

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter