Programmer's Blog

Programmer's reference

Monthly Archives: March 2014

[C++] Calling object’s member function in struct

In short, we can't do

obj->obj->function();

but

(*obj).obj->function();

To call the next layer of function using a pointer
Advertisements

[C++] variable type conversion

Here I will demonstrate how to exchange each type of variables into one another, this post may be enriched in the future to provide more methods

//convert int i to char[] cbuf
1. snprintf(cbuf, sizeof(cbuf), "%d", i);

//convert int i to string sbuf
1. sstream stream;
   stream << i;
   sbuf = stream.str();

//char[] cbuf to stringstream
1. istringstream sstr(static_cast<char*>(cbuf));

[Linux] Repeatly execute commands

use a for loop in the following format in the promt

$> for i in {1..10} ; do <command>; done

OR

$> for in in `seq 10`; do <command>; done

[Linux] kill multiple processes

Using awk ‘{print $2}’ to extract the process number on the second column of ps-grep and pipeline into kill

$> kill -9 `ps -ef | grep processname | awk '{print $2}'`

[Linux] skipping incompatible lib when compile

This phenomenon happens may caused by compiling 32bit sources ( with -m32 flag) on 64 bit machine

/usr/bin/ld: skipping incompatible /usr/local/lib/libzmq.so when searching for -lzmq

The method of solving this problem is to remove the -m32 flag from the makefile

yum install encounter “Public key not found”

One method is to skip the public key checking of rpm, note that there’s danger of doing so for installing untrusted packages

yum install --nogpgcheck xxxx.rpm

[Oracle] Count number of records for all Tables

SELECT TABLE_NAME,NUM_ROWS FROM USER_TABLES;

[Mysql] Check number of records for all Tables

SELECT table_name, table_rows 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = '<database name>';

[Shell] Repeat execution at certain time interval

SCRIPT_START_TIME=`date +%s`
TOTAL_RUNTIME=2  //how many times you want to run your script once start execution
SLEEP_TIME=10   //time interval you want to run the script

EXECUTION_END_TIME=0
 TOTAL_ELAPSED_TIME=0
function startExecution()
{
 //Things you want to do here
 }
until [ x = "true" ]
 do
startExecution
 echo “script running since : $TOTAL_ELAPSED_TIME seconds”
 sleep $SLEEP_TIME
 EXECUTION_END_TIME=`date +%s`
 TOTAL_ELAPSED_TIME=`expr $EXECUTION_END_TIME – $SCRIPT_START_TIME`
if [ $TOTAL_RUNTIME -lt $TOTAL_ELAPSED_TIME ]
 then
 break
 fi
done
echo “script execution complete”

[Android] Horizontal Progress Bar

The following code is to create a Horizontal progress bar in the Activity. By default, the loading bar is a circle, but through some modification it can be changed to horizontal.

-----fragment_main.xml-------
<ProgressBar 
        android:id="@+id/progress_bar1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal" />

-----MainActivity.Java-------
ProgressBar mProgress = (ProgressBar) findViewById(R.id.progress_bar1);
mProgress.setMax(100);      //set the maximum value of the bar
mProgress.setProgress(50);  //50% this statement can be replaced
                            //with loop or comparisons