Tuesday, June 5, 2007

PHP code Snippets, Algorithms, Array minus

There are hundreds of php snippets under the \lib\snippets\ directory of the Zend Studio Installation directory. I document them, write test cases and put them here.
The copyright of these code belongs to the original author. I publish them only for distributation purpose.
* In this simple function you send two arrays
* and it gives you an array with the operation A-B,
* elements on A that are not included on B.
* @param array $vectorA
* @param
array $vectorB
* @return
function RestaDeArrays($vectorA,$vectorB)
return $nuevo_array;

Test Code:
$vectorA = array(1,2,3,4,5,6,7,8);
$vectorB = array(2,4,6,8,10);

output Result:
[0] => 1
[1] => 3
[2] => 5
[3] => 7

Wednesday, May 30, 2007

More about simple cross-platform C++ library

Last day, I posted that I will publish Chenhua's simple cross-platform C++ library here. Not only copy it character by character, but also modify, extend, test and make some usecases about it.
Now, I Compile most of the library successfully. and a list of library is as follows. In the compilation process, I encounter several problems, such as not correct compile command line, precompile header file error etc.

BaseHtmlParser.cpp Parse HTML Content, Get absolute URL.
BaseHTTP.cpp A utility class that used to POST and GET data.
BaseSock.cpp Encapsulate common socket network operations.
Convert.cpp A utility class for
cryptography classes. including some functions like Base64 encoding and decoding.
Des.cpp Data Encryption Standard encrypt algorithm implementation.
MD5.cpp Message Digest hash utility class.
MMutex.cpp Mutex and semaphore operations's cross-platform
Mthread.cpp Multi-thread ulitity functions that can be use both in Windows and POSIX systems.
XML.cpp Implement some simple XML read and write behaviors.

The following is a Class picture that snapped from Visual Studio.NET IDE( In the Visual C++.NET Perspective and Class View).

SQLite as virtueware

SQLite author's blessing

The author of SQLite disclaims copyright to the source code. he doesn't place a legal notice at the header of each source file but a blessing as follows:
  • May you do good and not evil.
  • May you find forgiveness for yourself and forgive others.
  • May you share freely, never taking more than you give.
How Kindhearted! Some certain humourous and being worth of respect. If every programmer follow these. There may be no cracker again.

Short introduction about SQLite

SQLite is a smart file engine database like Access. Light weight, Fast accessing and many other features.

Search Engine results' comparison

By the way, I saw the sentence "
May you do good and not evil" at a first glance, and then think it may be an idiom from some famous people, even Bible. So I search it in Google, But get a bad result until I add " before and after the sentence. and also baidu.com,
But result from iAsk.com is much better than I expected.

Tuesday, May 29, 2007

Simple but usefull cross-platform C++ library

Chenhua, the co-founder of kooxoo.com, On the 26th, May, 2005, published his cross-platform C++ Library on his blog, http://dawnli.spaces.live.com. At that time, kooxoo.com has been not found yet(kooxoo.com was started from late 2005, Now it becomes the most famous lifeday search engine in China).
The library consists of some simple but usefull C++ classes. including socket, multi-thread, cryptography and etc. "Use these classes ,you can develop C++ programs as easy as Java ones", Said Chen.

Thanks for his great work. Now I copy most of his code snippets, test and build it into some test programs. Then post here for everyone. The original copyright of the code belongs to Chen.
Thanks again.

Saturday, May 26, 2007

Fix the SEMAPHORE GDB debug bug

When we use gdb to debug posix thread program that use SEMAPHORE machanism. If at the time we debug, there are some threads stop at sem_wait, when we quit GDB, the condition of sem_wait will become invalid and got some uncertain errors even get the program core dump. As shown in the following sample program:

#include <STDIO.H>

#include <ERRNO.H>

#include <PTHREAD.H>


sem_t empty;

sem_t full;

int value[1];

void * child1(void *arg)


int i = 0;

int ret = 0;

pthread_t tid=pthread_self();

printf("thread %d set value : %d\n",tid, value[0]);
ret = sem_post(&full);
printf("thread %d sem_post full fail: %d \n", tid, ret);
if(0 != (ret = sem_wait(
printf ("Sem_wait returned %ld\n", (unsigned long)ret);
printf("sem_wait for handler failed"); exit(1);
void * child2(void *arg)
pthread_t tid=pthread_self();
int ret = 0;
if(0 != (ret = sem_wait(&full)))
printf ("Sem_wait returned %ld\n", (unsigned long)ret);
printf("sem_wait for handler failed");
printf("thread %d get value %d\n",tid, value[0]);
ret = sem_post(&empty

printf("thread %d sem_post empty fail:%d\n", tid, ret); sleep(1);
int main(void)
pthread_t tid1,tid2;
sem_init (
&empty), 0, 0);
sem_init (&full, 0, 0);
printf("main thread exit\n");
return 0;

In this sample program. if when we debug it use GDB, Thread 2 wait at After the GDB process terminated. sem_wait,sem_wait will return a non-zero value automatically. But it is not what we want to see, because it should wait.

Why? Can it be fixed? The answer is yes. the quit of GDB will got a interupt signal EINTR, you can check the global variable errno whether equals EINTR, if this is , we ignore the signal and wait again. if not, another error emerges. I exit the program explictly, you can also call assert(0) to get the program core dump and then debug it.

code patch of the program shows as following:

if(0 != (ret = sem_wait(&empty))))


printf ("Sem_wait returned %ld\n", (unsigned long)ret);

printf("sem_wait for handler failed"); exit(1);



while (0 != (ret = sem_wait(&empty))))


if (errno != EINTR)


printf ("Sem_wait returned %ld\n", (unsigned long)ret); printf("sem_wait for handler failed"); exit(1);



if(0 != (ret = sem_wait(&full)))


printf ("Sem_wait returned %ld\n", (unsigned long)ret);

printf("sem_wait for handler failed");




while (0 != (ret = sem_wait(&full)))


if (errno != EINTR)


printf ("Sem_wait returned %ld\n", (unsigned long)ret);

printf("sem_wait for handler failed");




Extracting Single Images from a CImageList object in MFC

Here is the function:

It takes 3 parameters, and returns nothing.

  • lstImages: A pointer to the CImageList object containing all of the images.
  • nImage: The index of the image that is going to be extracted.
  • destBitmap: A pointer to the CBitmap object that is going to contain the extracted image.

The function makes a copy of the image list, and moves the requested image to the front of that list.

It then draws the requested image into the destination bitmap.

void CMyWindowClass::GetImageFromList(CImageList *lstImages,
int nImage, CBitmap* destBitmap)
//First we want to create a temporary image list we can manipulate
CImageList tmpList;

//Then swap the requested image to the first spot in the list
tmpList.Copy( 0, nImage, ILCF_SWAP );

//Now we need to get som information about the image
IMAGEINFO lastImage;

//Heres where it gets fun
//Create a Compatible Device Context using
//the valid DC of your calling window
CDC dcMem; dcMem.CreateCompatibleDC (GetWindowDC());

//This rect simply stored the size of the image we need
CRect rect (lastImage.rcImage);

//Using the bitmap passed in, Create a bitmap
//compatible with the window DC
//We also know that the bitmap needs to be a certain size.
destBitmap->CreateCompatibleBitmap (this->GetWindowDC(),
rect.Width (), rect.Height ());

//Select the new destination bitmap into the DC we created above
CBitmap* pBmpOld = dcMem.SelectObject (destBitmap);

//This call apparently "draws" the bitmap from the list,
//onto the new destination bitmap
tmpList.DrawIndirect (&dcMem, 0, CPoint (0, 0),
CSize (rect.Width (), rect.Height ()), CPoint (0, 0));

//cleanup by reselecting the old bitmap object into the DC
dcMem.SelectObject (pBmpOld);

It looks big, but remove the comments and you have a mere 12 lines of code.

you can call the function above use two lines of code like following:

CBitmap bitMap;

GetImageFromList(&m_ImageListSrc, extractIndex, &amp;amp;bitMap);

Then you can do what you like with the variable bitMap;

The original URL of the solution is:http://www.codeproject.com/bitmap/getimagefromlist.asp

Thursday, April 26, 2007

Just for fun

I search some thing about "mtrace" in Google, I click the url below and then ....

Select "I am married"
A dialog opened, And Then I click "Chat"....

Chat Information Please wait for a site operator to respond.
Chat Information Hello, my name is Daniel. I am a USAFIS organization representative. I am here to help you to register you for the American Green Card program.
Daniel: Are you interested in living and working in USA?
you: yes.
Daniel: Now, before we can proceed i need to check your eligibility for the Green Card lottery, ok?
Daniel: Which country were you born in?
you: No problem.I just want to learn something about in America.I am born in China.
Daniel: Which country were your parents born in?
Daniel: Which country was your wife/husband born in?
you: They are all born in China.
Daniel: I am sorry, but you are not eligible for the Green Card Lottery because you were born in a non-eligible country. This is the rule of the U.S. State Department.
Chat InformationChat session has been terminated by the site operator