2009-10-28

Sort all installed package by Size


dpkg-query -W --showformat='${Installed-Size;10}\t${Package}\n' | sort -k1,1n



dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' | sort -k 2 -n


source:

Adding a new disk to a VMWare Virtual Machine in Linux

Nice tutorial with snapshots:
Adding a new disk to a VMWare Virtual Machine in Linux

Svn Access through a Proxy

The Subversion client can go through a proxy, if you configure it to do so.

First, edit your servers configuration file to indicate which proxy to use. The file's location depends on your operating system. On Linux or Unix it is located in the directory ~/.subversion. On Windows it is in %APPDATA%\Subversion (try echo %APPDATA%, note this is a hidden directory).
Example: Edit the servers file and add something like at global session:
[global]
http-proxy-host = your.proxy.name
http-proxy-port = 3128
Check your internet browser setting, if you have only a proxy configuration, for example http://proxyconf.some.name. You can simply open this link using your browser and download the configuration script, I believe you can find the proxy server and the port.

Play OpenEmbedded

OpenEmbedded is
".. a framework from the first download of meta framework to the final system installation.. "

A comprehensive tutorial is OpenEmbedded Guide by Example. But in the time, OpenEmbedded uses git not monotone to download stuffs. But you'd better also read an updated version Setting up the toolchain and doing a build .

For users of VMplaer, which at default allocated only 8 G, you should notice that OpenEmbedded needs more than 10G space. Use VMware workstation, instead!

References:

2009-10-27

2009-10-26

Don't Forget Firewall for VMware Bridged Network

Max has a windows host and debian vm guest. In host-only and NAT vm network, everything fine. But if the vmware has a bridged network, host has no access the guest anymore. The basic differences between the three network configurations is briefly explain here. For example, putty. Don't forget add an exception to host firewall, in which your vmware workstation should not be blocked.

VMWare Network Modes

VMware has three kinds of network configurations:
  • Host-Only Networking: Principally, the virtual guest can only talk with host and has no access to outside. But, with help of proxy server, it is possible.
  • Network Address Translation (NAT) The guest can connect to a TCP/IP network using a Token Ring adapter on the host computer. The virtual machine does not have its own IP address on the external network. Instead, a separate private network is set up on the host computer.
  • Bridged Networking: Bridged mode lets the virtual machine share the host's Ethernet connection, while appearing as a separate machine with its own MAC and TCP/IP address.



References:
  1. VMware Networks, Bridged vs. Nat vs. Host
  2. Network address translation

2009-10-19

A Bach Script Adding File Description

Now Max has a small problem. He just read the code guideline of project and found that he forgot to add a pre-defined doxgen description for his source files. He has implemented more than 20 c++ classes in 5 directories. Never mind. The following bash scripts can help him. It includes
  • header.in
  • add-header.sh
  • add-header-dir.sh
Usage cannot be simpler.
  • add-header-dir.sh ${codedir}
The followings are the scripts:

header.in(header info template)
/****************************************************************************
**
** file: [filename]
** author: [your name]
** date: [date goes here]
** summary: 
** Copyright (C) 2010 xxx GmbH and/or its subsidiary(-ies).
** All rights reserved.
**
****************************************************************************/

add-header.sh (add the header to single file)
if [ $# -ne 1 ]; then
echo "Usage: add-header.sh filename"
exit 1
fi

file=$1
# check if the header exists
firstline=`head -1 ${file}`

if [ "${firstline:0:2}" = "/*" ]; then
echo "header exists. do nothing."
else
# add header.in at the begin of the file
# and copy to a temp file
( cat header.in; cat ${file} ) > ${file}.new

# replace the [filename] by current file name
filename=`echo ${file}|sed "s,.*/\(.*\),\1,"`
sed -i "s#\[filename\]#$filename#g" ${file}.new

# replace the author name
author='Jingfeng Han'
sed -i "s#\[your name]#$author#g" ${file}.new

# replace the date
date=`date +"%y/%m/%d/"`
sed -i "s#\[date goes here]#$date#g" ${file}.new

# replace old file with the temp file
rm ${file}
mv ${file}.new ${file}
fi

exit 0

add-header-dir.sh (recursively add the header to all source in the fold)
find $1 -type f -iname "*.h" -o -iname "*.cpp" -o -iname "*.qml" | while read file; do
# display info
echo ${file} ...

# add header to the file
./add-header.sh ${file}
done
exit 0

Sed Tips

In sed, if a pattern can be included inside of single quotes, no variable will be extented. Exmple 1 and 2. In order to use value of a variable, the pattern need to be included by double quotes and use "#" instead of "/" for seperator. See example 3 and 4.
  1. sed -i 's/\[filename\]/myfile/g' ${file}.new------>succ.
  2. sed -i 's/\[filename\]/$file/g' ${file}.new------->variable is not extended.
  3. sed -i "s/\[filename\]/$file/g" ${file}.new------->fails( Unknown option to 's')
  4. sed -i "s#\[filename\]#$file#g" ${file}.new-------->succ.

2009-10-17

Reset SQL Statement in sqlite3

Reset all bindings on a prepared statement, reset all host parameters to NULL.

int sqlite3_clear_bindings(sqlite3_stmt*);

Reset a prepared statement object back to its initial state, ready to be re-executed.

int sqlite3_reset(sqlite3_stmt *pStmt);

2009-10-16

On Redrawing a Qt Widget

As a GUI deverloper, Max is always looking for a elegant way to dynamically determine the appearance of widgets.

For example, now we have a wonderful recipe software, which can allow user input the name and display its ingredient. I want to open a dialog to show the ingredients of a recipe, let's say IngredientDialog. According to the recipe name, program retrieves the associated ingredients from database in the background. Assume that each ingredients are displayed in a QLabel. Obviously, we don't know the number of ingredients until the moment to open the dialog.

Solution 1: Everytime create a completely new IngredientDialog. Actually, it isn't a bad idea. Clear, easy but may be slow, because you need to remove everytime the old IngredientDialog and create a new one, in which a number of QLabel objects are created according to current recipe.

Solution 2: IngredientDialog is created once. Everytime it is open, the IngredientDialog cleans up the old QLabels and creates a new collection of QLabel, the others widgets keep unchanged. Looks very efficient. But frequent delete and new operations may lead to a memory leak, especially if the widgets are complicated or are implemented from the third parts.

Solution 3: Let's return back to the old style of C: allocate all the required variables and memory at the beginning of function and avoid to reallocate memory afterwards. For this example, IngredientDialog create enough QLabels in its constructor, but dynamically determine it is shown or hiden when the dialog is opened.
Seldomly, if a speical recipe has even more ingredients, the more QLabels will be created, but it is really seldom.

Tips: for a frequrently redrawn widget, it is a good idea to perserve the pointers of every created widgets and layout. In such way, the widgets can be easily altered, relocated or deleted among the layouts.

2009-10-14

A Simple Qt SAX Parser

Qt Xml framework implements two kinds of xml parser: "SAX2" and "DOM level 2". The difference between these two parsers are summarized in this blob artical. A nice code example of Qt SAX2 is discussed in the "tagreader walkthrough".

The most important characteristic of SAX parser is that it traverses the entire xml tree only once, from top to bottom. There is no intermediate memory for the element nodes, as in DOM parser. Therefore, SAX parser is memory efficient but cannot traverse backwards.

How the SAX parser works? By serveral events and their event handlers:
  • startElement
  • endElement
  • characters



Here is an example of a Qt SAX parser, which is a subclass of QXmlDefaultHandler.


=========================================

<Items>

<Item>
<Material>all purpose flour, stir before measuring</Material>
<Mount Unit="cups">3</Mount>
</Item>

<Item>
<Material>1 1/2 teaspoons baking soda</Material>
</Item>

<Item>
<Material>1 1/2 teaspoons ground cinnamon</Material>
</Item>

<Item>
<Material>8 ounces unsalted butter</Material>
</Item>

</Items>


=========================================


#include <QtXml/qxml.h>
#include <QString>

static const char TAG_ITEM[] = "Item";
static const char TAG_MATERIAL[]= "Material";
static const char TAG_MOUNT[] = "Mount";

class MyParser : public QXmlDefaultHandler
{
public:
bool startElement( const QString & namespaceURI,
const QString & localName,
const QString & qName,
const QXmlAttributes & atts )
{

if( localName == TAG_ITEM) {}
else if( localName == TAG_MATERIAL ) {}
else if( localName == TAG_MOUNT ){}
else{return false;}

currElement_ = localName;
return true;
}
bool characters ( const QString& ch_in)
{
if( currElement_ == TAG_ITEM) {}
else if( currElement_ == TAG_MATERIAL ) {}
else if( currElement_ == TAG_MOUNT ){}
else{return false;}

return true;

}
bool endElement( const QString&, const QString& localName, const QString& )
{
if( localName == TAG_ITEM) {}
else if( localName == TAG_MATERIAL ) {}
else if( localName == TAG_MOUNT ){}
else{return false;}

currElement_ = localName;
return true;
}

private:
QString currElement_;

};

2009-10-12

Difference between SAX and DOM

SAX: Was developed to run the java programs especially.
DOM: Was developed by w3c consortium so that it is an open standard.

SAX: It uses a memory resident model.
DOM: The xml file is arranged as a tree.

SAX: Top to bottom traversing
DOM: Traverse in any direction.


SAX: We cant insert or delete a node
DOM: We can insert or delete nodes


SAX: Doesnt store the XML in memory.
DOM: Occupies more memory

reference:
http://www.geekinterview.com/question_details/12797
SAX examples

Replace and Find String Cross Multiple Files

> find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'
> find . | xargs grep 'string'
> find . -name "*.cpp" -o -name "*.h" -print

reference:
Rushi's blog

2009-10-10

sqlite3. Binding Values To Prepared Statements

The basic use of sqlite3 c++ interface can be found here:
A simple but nice example can be found here.

Another very important and useful function is sqlite3_bind. Why do we need it?

Usually, though, it is not useful to evaluate exactly the same SQL statement more than once. More often, one wants to evalute similar statements. sqlite3_prepare_v2() can not only parse the state SQL statement, but also possible a SQL statement template, in which some parameters can be given later. The literals parameters are bound by the function sqlite3_bind. It is very flexible and efficient, because the SQL statement is need to be prepared only once and be used forever. In many SQL statements, the time needed to run sqlite3_prepare() equals or exceeds the time needed by sqlite3_step().

The following code is c++ class do a select sql operation, retrieving "File" according given "ID".

"select File from tb_image where ID=?1"

The parametrized SQL statement is prepared in constructor once using

SqlHandler::prepare_sql(const char* sql_in, sqlite3_stmt*& pStmt_in)

The image object can be retrieved with different id.

bool SqlHandler::getRecipeHeaderImage( const int& imageId_in, QImage& image_out)

//-----------------------Example Code---------------------


static const char SQL_SELECT_HEADERIMAGE[]= "select File from tb_image where ID=?1";

SqlHandler::SqlHandler()
:bIsDBAvailable_(false)
,db_(0)
,pStmtHeaderImage_(0)
{
// Open the database
sqlite3_open(COKI_DB_FILE, &db_);
if( SQLITE_OK!=sqlite3_errcode(db_) )
{
qCritical("DB error: fail to open db file.");
sqlite3_close(db_);
return;
}

//prepare the SQL statement for select header info
if(!prepare_sql(SQL_SELECT_HEADERINFO, pStmtHeaderInfo_))
{
return;
}
SqlHandler::~SqlHandler()
{
sqlite3_finalize(pStmtHeaderImage_);
sqlite3_close(db_);
}
bool SqlHandler::isDBAvailable()
{
return bIsDBAvailable_;
}
bool SqlHandler::getRecipeHeaderImage( const int& imageId_in,
QImage& image_out)
{
if(!isDBAvailable())
{
return false;
}

// add id into sql statement
int iReturnCode = sqlite3_bind_int( pStmtHeaderImage_, 1, imageId_in);
if(iReturnCode != SQLITE_OK)
{
qCritical("Error: sqlite3_bind_in fails for ImageId = %d",imageId_in);
return false;
}

QString strFileName = COKI_IMAGE_DIR;
//execute the statement
iReturnCode = sqlite3_step( pStmtHeaderImage_ );
switch (iReturnCode )
{
case SQLITE_DONE:
return false;
break;

case SQLITE_ROW:
//load image
strFileName.append ((const char*)sqlite3_column_text( pStmtHeaderImage_, 0));
image_out.load(strFileName);
return true;
break;

default:
return false;

}


}
bool SqlHandler::prepare_sql(const char* sql_in, sqlite3_stmt*& pStmt_in)
{
if(sqlite3_prepare_v2(db_, sql_in, -1, &pStmt_in, 0))
{
sqlite3_finalize(pStmt_in);
return false;
}
return true;

}

2009-10-07

An example to retreive colume from a sqlite3 db

The following example shows how to retrieve the colume info from sqlite3 by its c interface. Some basic information

  • DB: my.db
  • table: tb
  • COLUMNs: Name, Owner, Category, Duration, ID

Given record id. now I want to retrieve the other column info.

bool MyClass::initialize(int id_in)
{

sqlite3* db;
sqlite3_stmt * pSTmt;
int iReturnCode = 0;
bool bRet = true;

// Open the database
sqlite3_open("my.db", &db);
if( SQLITE_OK!=sqlite3_errcode(db) )
{
qWarning("DB error: fail to open db file.");
sqlite3_close(db);
return false;
}

//prepare the SQL statement
char sSql[1000];
sprintf(sSql, "select Name, Owner, Category, Duration from tb where ID=%d", id_in);
if(sqlite3_prepare_v2(db, sSql, -1, &pSTmt, 0))
{
qWarning("DB error: fail to prepare the sql statement: [%s]", sSql);
sqlite3_finalize(pSTmt);
sqlite3_close(db);
return false;
}

//execute the statement
iReturnCode = sqlite3_step(pSTmt);
switch (iReturnCode )
{
case SQLITE_DONE:
qWarning("Warning: No record is found!");
bRet = false;
break;

case SQLITE_ROW:

sName_ = (const char*)sqlite3_column_text(pSTmt, 0);
sCategory_ = (const char*)sqlite3_column_text(pSTmt, 1);
sOwner_ = (const char*)sqlite3_column_text(pSTmt, 2);
sDuration_ = (const char*)sqlite3_column_text(pSTmt, 3);

bRet = true;
break;

default:

qWarning("DB error: fail to step the sql statement: [%s]", sSql);
qWarning("DB error code:");
bRet = false;
}

//finialize the state statement
sqlite3_finalize(pSTmt);
sqlite3_close(db);
return bRet;
}

2009-10-05

Convert Number to QString

Useful API of OString, converting number to QString

QString number ( long n, int base = 10 )
QString number ( ulong n, int base = 10 )
QString number ( int n, int base = 10 )
QString number ( uint n, int base = 10 )
QString number ( qlonglong n, int base = 10 )
QString number ( qulonglong n, int base = 10 )
QString number ( double n, char format = 'g', int precision = 6 )

2009-10-04

Create a Dialog with Scrollbar


ListDialog::ListDialog()
{
QScrollArea *scrollArea = new QScrollArea(this);
QGroupBox *groupBox = new QGroupBox(scrollArea);
QVBoxLayout *vbox = new QVBoxLayout;

for (int i=0; i< 20; i++)
{
vbox->addWidget(new QRadioButton("item xxx"));
}

groupBox->setLayout(vbox);
scrollArea->setWidget(groupBox);

QHBoxLayout* layout = new QHBoxLayout();
layout->addWidget(scrollArea);
setLayout(layout);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
}

2009-10-02

Useful sqlite3 Commands

>sqlite3 $DB //create a db
>sqlite3 $DB < $sql_script //create a db using a sql script
>sqlite3 //enter sqlite3 sql shell
>.exit //leave the sqlite3 sql shell
>
.header ON //Column title is printed out by select

2009-10-01

protected and private constructor in C++/C# class

1) There exists only one reason to make constructor protected: The class will not be instantiated, but its subclass will be.
Bingo! Abstract class. See the C# example below:


//abstract class, never be created because abstract
//functions is not yet implemented
public abstract class MarkerBase
{
protected MarkerBase( string req_in ) {}
protected abstract int getMarkerID();
}
//subclass implement the abstract function and
//its constructor reuses the protected
//constructor of its abstract parent.

public class MyMarker : MarkerBase
{
public MyMarker(string req_in) : base(req_in){}
protected override int getMarkerID(){return 0;}
}


2) It is really confused that the class constructor is private. It means
that the class never be instantiated. If the class will never be created,
why do we need it?

The answer is a little mysterious: The class can be created only once by itself,
so-called singlton design pattern.

In some resource-related applications, we want to have only single instance for some classes. For example, if we have implemented a class to access a specific database, we want certainly the application has only an unique instance of this class. Otherwise, if multipe instances may write and read the same database, it is hard to control the synchronization. It is the reason why a singlton class has a private construct, which can be called only by itself once and prevent the class being created for several times by the other classes.

See the c++ class below. The class class manages accessement of a sqlite3 database.

class DBReader
{
public:
void insertRecord(const char* sql_in);
void select(const char* sql_in);
private:
sqlite3 * db_;

//for singleton design pattern
public:
static DBReader* getInstance();
private:
static DBReader* pDBReader_;
DBReader();
void databaseError();
};
DBReader* DBReader::pDBReader_ = NULL;
DBReader* DBReader::getInstance()
{
if(pDBReader_ == NULL)
{
pDBReader_ = new DBReader();
}
return pDBReader_;
}
DBReader::DBReader()
:db_(NULL)
{
// Open the database
sqlite3_open("coki.db", &db_);
if( SQLITE_OK!=sqlite3_errcode(db_) )
{
databaseError();
return;
}
}
...

int main()
{
....
//DBReader::getInstance() is the only
//way to get access database.

DBReader::getInstance()-> insertRecoard(sSQL_CM1);
DBReader::getInstance()-> select(sSQL_CM2);
return 0;