Sunday, August 26, 2012

replace continuous and repeated pat into single replacement


replace continuous and repeated pattern into a single replacement


 //"Reeeep chineese";  trap if pat == rep
    public static String replaceConsecutiveRepeatedPattern(String str, String pat, String rep) {
        if (pat == null || pat.length() == 0) return str;

        int id = str.indexOf(pat);
        StringBuilder sb = new StringBuilder();

        while (id >= 0) {
            sb.append(str.substring(0, id));
            str = str.substring(id);
            while (str.startsWith(pat, pat.length())) {
               str = str.replaceFirst(pat, "");
            }
            str = str.replaceFirst(pat, rep);

            id = str.indexOf(pat, rep.length());
        }
        sb.append(str);

        return sb.toString();
    }


for example:
with
       str = "Aug  3 23:02:54  2012";
       pat = "&nbsp";
       rep = " ";
replaceConsecutiveRepeatedPattern will change str into
       "Aug 3 23:02:54 2012"

Thursday, July 26, 2012

a model

Circular Buffer


1 background
Circular buffer(CB) must be created to be able to save(receive) binary data from network or other thread(say writer thread), and another thread can read the received data.
One of the usage is accepting network video continuously,  a consumer thread read frames from CB 
and display it on screen.

2 Necessary data structure to represent the circular buffer
const int MAX_BUFFER_SIZE = 2<<24; //
const int MAX_READABLE_SIZE = MAX_BUFFER_SIZE-1;

precondition: a pre-allocated array of unsigned char
uchar *buffer = new uchar[MAX_BUFFER_SIZE];

Identifier
recv_pos:  the position from which the netwrok data is to be saved next time
read_pos:  the position from which the reader thread to read next time (include read_pos)

If CB is full, the writer thread cannot write data into it, must wait until there is available free space
If CB is empty, the reader thread cannot read data from it, must wait until there is avaiable data

full condition: recv_pos == read_pos (just one of the implementation)
empty condition: (recv_pos+1)%MAX_BUFFER_SIZE == read_pos

For example: let MAX_BUFFER_SIZE = 10

3 possible situations of the circular buffer:

In the following, green grids represent the effective data bytes can be read and white girds mean free spaces.

1  Empty buffer:
0             1              2              3               4              5               6             7             8               9
read_pos
recv_pos










0             1              2              3               4              5               6             7             8               9










               
                    
read_pos
recv_pos



0             1              2              3               4              5               6             7             8               9






read_pos
recv_pos







Green grids contain received bytes, White space grids can be used to received new data

2  Full buffer:
0             1              2              3              4               5             6               7              8              9
read_pos









recv_pos


0              1              2              3              4               5             6               7              8              9
                             
recv_pos
read_pos







0              1              2              3              4               5             6               7              8              9
recv_pos
read_pos
               











3 Non-full and non-empty buffer:
0             1              2              3              4               5             6               7              8              9
read_pos




recv_pos






0              1              2              3              4               5             6               7              8              9
              

               

recv_pos


read_pos




0             1               2               3              4               5             6               7              8              9
          


read_pos





recv_pos

When next time writer begin to write data into buffer,  recv_pos will also be used, so when the buffer is full, the effective size of data can be read is MAX_BUFFER_SIZE – 1.


4  Pseudo code

bool isBufferEmpty() {
        return recv_pos == nextReadPos;
}

bool isBufferFull() {
        //this also works when nextRecPos reaches the end of buffer
        return ( recv_pos+1)%MAX_BUFFER_SIZE == read_pos;
}

Implementation 1 
supposing the receiving thread can save more bytes to first position of the buffer(ie to position: recv_pos...N-1, 0,...X)

//get the size of effective data received
int effectiveSize() {
    int ef_size = 0;
    if (read_pos < recv_pos) { 
        ef_size = recv_pos – read_pos;
    }
    if (read_pos > recv_pos) {
        ef_size = (MAX_BUFFER_SIZE – read_pos) + recv_pos;
    }
    return ef_size;
}

//get the free space
int freeSpace() {
    int free_size = MAX_BUFFER_SIZE – 1;
    if (read_pos < recv_pos) {
        free_size = MAX_BUFFER_SIZE - recv_pos + read_pos - 1;
    }
    if (read_pos > recv_posv) {
        free_size = read_pos – recv_pos – 1;
    }
    return free_size;
}

//or receive, may wrap around
void write(uchar* src, int len) {
    while (free_space() < len) wait(); //wait until there is enough free space to save data
    
    if (len > MAX_B UFFER_SIZE – recv_pos) {
        memcpy(&buffer[recv_pos], src, MAX_BUFFER_SIZE – recv_posv);
        
        len -= (MAX_BUFFER_SIZE – recv_pos);
       
        memcpy(&buffer[0], &src[ MAX_BUFFER_SIZE – recv_pos], len);
       
        recv_pos = len;
    } else {
        memcpy(&buffer[recv_posv], src, len);
      
        recv_pos = (recv_pos + len) % MAX_BUFFER_SIZE;
    }
}

//read the specified len data, may block
int readFull(uchar* dst, int len) {
    if (len > MAX_READABLE_SIZE) return -1;
    while (effectiveSize() < len) wait();
    
    if (read_pos < recv_pos) {
        memcpy(dst, &buffer[read_pos], len);
        
        read_pos += len;
    } else if (read_pos > recv_pos) {
        if (MAX_BUFF_SIZE – read_pos < len) {
           
            memcpy(dst, &buffer[read_pos], MAX_BUFF_SIZE – read_pos);    
            
            len -= ( MAX_BUFF_SIZE – read_pos);
            
            memcpy(&dst[ MAX_BUFF_SIZE – read_pos], buffer, len);
           
            read_pos = len;
        } else {
            memcpy(dst, &buffer[read_pos], len);
            
            read_pos = (read_pos + len) % MAX_BUFF_SIZE);
        }
    }
    return len;
}

//read with, won't block.
int read(uchar* dst, int len) {
    if (len > MAX_READABLE_SIZE) return -1;
   
    if (len > effectiveSize()) len = effectiveSize();
   
    return readFull(dst, len);
}


Implementation 2 
supposing the receiving thread can only save data to the end of buffer each time

//get the size of effective data received
int effectiveSize() {
int ef_size = 0;
if (read_pos < recv_pos) {
ef_size = recv_pos – read_pos;
}
if (read_pos > recv_pos) {
ef_size = (MAX_BUFFER_SIZE – read_pos) + recv_pos;
}
return ef_size;
}

//get the free space
int freeSpace() {
int free_size = MAX_BUFFER_SIZE - recv_pos – 1;
if (read_pos > recv_pos) {
free_size = read_pos – recv_pos – 1;
} else {
if (read_pos > 0) {
free_size = MAX_BUFFER_SIZE – recv_pos;
}
}
return free_size;
}

//or receive, can not wrap around each time,
void writeNoWrap(uchar* src, int len) {
//while (isBufferFull() ) wait();
while (freeSpace() <= 0) wait();
if (len > freeSpace()) len = freeSpace();
memcpy(&buffer[recv_pos], src, len); //recv_pos the receive offset;
recv_pos = (recv_pos + len) % MAX_BUFFER_SIZE;
}

//read the specified len data, may block
int readFull(uchar* dst, int len) {
if (len > MAX_READABLE_SIZE) return -1;
while (effectiveSize() < len) wait();
if (read_pos < recv_pos) {
memcpy(dst, &buffer[read_pos], len);
read_pos += len;
} else if (read_pos > recv_pos) {
if (MAX_BUFF_SIZE – read_pos < len) {
memcpy(dst, &buffer[read_pos], MAX_BUFF_SIZE – read_pos);
len -= ( MAX_BUFF_SIZE – read_pos);
memcpy(&dst[ MAX_BUFF_SIZE – read_pos], buffer, len);
read_pos = len;
} else {
memcpy(dst, &buffer[read_pos], len);
read_pos = (read_pos+len) % MAX_BUFF_SIZE);
}
}
return len;
}

//read with, won't block.
int read(uchar* dst, int len) {
if (len > MAX_READABLE_SIZE) return -1;
if (len > effectiveSize()) len = effectiveSize();
return readFull(dst, len);
}

Saturday, June 30, 2012

output redirection

0 means stdin, 1 means stdout, 2 means stderr

$ echo 'X' 1>/dev/null    #output nothing
$ echo 'X' 2>/dev/null    #output  X
$ echo 'X' 1>&2             #output  X
$ echo 'X'  >/dev/null    2>&1 #output nothing stdout is redirected to /dev/null, stderr is also redirected what stdout is going to(that is /dev/null), this is equivlent to following red-colored command:
$ echo 'X' 1>dev/null 2>/dev/null

development tools

bash debugger:    sudo apt-get install bashdb

ophcrack install directory


 Install directory: /usr/local

 GUI: no
 Graph display: no
 Debugging: no

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

Now enter 'make' to compile
followed by 'make install' to install

Friday, June 29, 2012

qt install directory on ubuntu

/opt/

Comparison of Internet forum software


This is a comparison of the features of various Internet forum packages written in PHP.

Contents

  [hide

[edit]General information

Basic general information about the forums: creator/company, license/price etc.
CreatorLatest release dateCurrent stable versionLicenseAutomatic Updates (for security)
Beehive ForumProject Beehive Forum Developers2011-02-141.0.1GPLmanually
bbPressAutomattic2011-11-282.0.2GPLYes
Discuz!Comsenz Technology Co., Ltd.2012-05-18X2.5Discuz! EULA (proprietary,gratis for non-commercial use)manually
PHPWindHangzhou Detian Information Technology Co. Ltd2011-11-118.7PHPWind EULA (proprietary,gratis for non-commercial use)manually
e107e107 (CMS)2011-04-060.7.25GPLmanually
FluxBBFluxBB.org Developers2011-09-131.4.7GPLmanually
FUDforumIlia Alshanetsky and community2011-07-173.0.3RC1GPLmanually
IceBBXAOS Interactive2010-12-021.0GPLmanually
Invision Power BoardInvision Power Services Inc.2012-06-123.3.3Proprietarymanually
LayerBulletinLayerBulletin Project Community2012-03-281.1.5Artistic License 2.0manually
MercuryBoardJason Warner, Mark Elliot, and Jonathan West2008-02-241.1.7GPLmanually
miniBBPaul Puzyrev & Sergei Larionov2010-10-052.5aGPLmanually
MyBBMyBB Group2012-05-271.6.8LGPLmanually
NextBBSChris FR & Team2008-02-180.5.3GPLmanually
NinkoBBNijikokun2010-03-161.3 RC5GPLmanually
OvBBJonathon Freeman & Brian Otto2007-04-06V0.16aMITmanually
PhorumBrian Moon2011-09-095.2.18Phorum License 2 (BSD like)manually
phpBBphpBB Ltd.2012-01-023.0.10GPLmanually
PunBBRickard Andersson2011-12-011.4.1GPLmanually
Quicksilver ForumsGeoff Dunn, Roger Libiez (Samson) & The Quicksilver Forums Development Team2011-01-161.5GPLmanually
Simple Machines 1Simple Machines2011-12-221.1.16Proprietary[1]manually
Simple Machines 2Simple Machines2011-12-222.0.2BSD License[1]manually
Simply AJAX Forum System (SAFS)Fast Track Sites2010-05-203.10.05.20Proprietary[2]manually
Tritanium Bulletin BoardTritanium Scripts2012-03-161.6.1CC 3.0 by-nc-samanually
UBB.threadsMindraven, Inc.2010-08-217.5.6Proprietarymanually
UseBBDietrich Moerman2012-05-031.0.15GPLmanually
VanillaVanilla Forums Inc.2012-03-262.0.18.4GPLmanually
vBulletinvBulletin Solutions Inc.2012-05-224.2.0Proprietarymanually
WhiteBoardEli White & SaroSoftware2010-05-300.1.30GPLmanually
Woltlab Burning BoardWoltLab GmbH2011-08-263.1.6proprietarymanually
XenForoXenForo Ltd.2012-06-201.1.3Proprietarymanually
XMBXMB Software2009-03-011.9.11GPLmanually
Launch.ForumViralAge Inc ?1.0 AlphaProprietaryAutomatic
NitPick2011 ?2011-09-181.0 ?manually
CreatorLatest release dateCurrent stable versionLicenseAutomatic Updates (for security)

[edit]Features

FlatThreadedUser-selectable themesCalendarImage attachmentUnread message trackingWYSIWYG Editor
Beehive ForumYesYesYesNoYesFullYes (usingTinyMCE)
bbPressYesPluginYesNoPluginPluginPlugin
Discuz!YesYesYesYesYesYesYes
e107YesYesYesYesYesYesYes
FluxBBYesNoYesNoNoSessionNo
FUDforumYesYesYesYesYesFullYes
IceBBYesNoYesNoYesFullNo
Invision Power BoardYesYesYesYesYesFullYes
LayerBulletinYesNoYesNoYesFullNo
kusabaYesYesYesNoYes ?No
MercuryBoardYesNoYesNoYesSessionNo
miniBBYesNoYesYesYesYesNo
MyBBYesYesYesYesYesFullPlugin
NextBBSYesYesYesYesYesFullYes
NinkoBBYesNoNoNoNo ?No
PhorumYesYesYesNoYesFullNo
phpBBYesNoYesPluginYesFullPlanned
PunBBYesPluginYesPluginPluginSessionPlugin
Quicksilver ForumsYesNoYesNoYesFullNo
Simple Machines 1YesNoYesYesYesFullNo
Simple Machines 2YesNoYesYesYesFullYes
Simply AJAX Forum System (SAFS)YesYesYesNoYesFullNo
Tritanium Bulletin BoardYesNoYesPlannedYesFullNo
UBB.threadsYesYesYesYesYesFullPlanned (Version 8.0)
UseBBYesNoYesNoNoSessionNo
VanillaYesPluginPluginPluginPluginPluginPlugin
vBulletinYesYesYesYesYesFullYes
WhiteBoardYesNoPlannedYesNoFullNo
Woltlab Burning BoardYesYesYesYesYesFullYes
XenForoYesYesYesNoYesFullYes
XMBYesYesYesNoYes ?No
NitPick2011YesYesNoNoYesNoNo
FlatThreadedUser-selectable themesCalendarImage attachmentUnread message trackingWYSIWYG Editor

[edit]Data storage

Information about what data storage system can be used.
MySQLPostgreSQLMSSQLOracleSQLiteFirebird
Beehive ForumYesNoNoNoNoNo
bbPressYesNoNoNoNoNo
Discuz!YesYesNoNoNoNo
e107YesNoNoNoNoNo
FluxBBYesYesNoNoYesNo
FUDforumYesYesYesYesYesYes
IceBBYesNoNoNoNoNo
Invision Power BoardYesNoYes ($75)NoNoNo
LayerBulletinYesNoNoNoNoNo
kusabaYesNoNoNoNoNo
MercuryBoardYesNoNoNoNoNo
miniBBYesNoNoNoNoNo
MyBBYesYesNoNoYesNo
NextBBSYesYesNoNoYesNo
NinkoBBYesNoNoNoNoNo
PhorumYesYesNoNoNoNo
phpBBYesYesYesYesYesYes
PunBBYesYesNoNoYesNo
Quicksilver ForumsYesNoNoNoNoNo
Simple Machines 1YesNoNoNoNoNo
Simple Machines 2YesYesNoNoYesNo
Simply AJAX Forum System (SAFS)YesNoNoNoNoNo
Tritanium Bulletin BoardPlanned (Version 2.0)NoNoNoNoNo
UBB.threadsYesNoNoNoNoNo
UseBBYesNoNoNoNoNo
VanillaYesNoNoNoNoNo
vBulletinYesNoNoNoNoNo
WhiteBoardYesNoNoNoNoNo
WoltLab Burning BoardYesNoNoNoNoNo
XenForoYesNoNoNoNoNo
XMBYesNoNoNoNoNo
NitPick2011NoNoNoNoNoNo
MySQLPostgreSQLMSSQLOracleSQLiteFirebird

[edit]