char *readLine() { int bufSize = 1024; static char *buf = malloc(sizeof(char) * (1 + bufSize)); static int bytesAfterNewLine = 0; // number of bytes left from last call static int lastpos = 0; // where the leftover starts int totalBytes = 0; // total number of bytes in the array int foundLineBreak = 0; if (bytesAfterNewLine != 0) { memmove(buf, buf + lastpos, bytesAfterNewLine); totalBytes = bytesAfterNewLine; lastpos = 0; } while (true) { for (; lastpos < totalBytes; ++lastpos) { if (buf[lastpos] == '\n') { buf[lastpos] == '\0'; bytesAfterNewLine = totalBytes - lastpos - 1; foundLineBreak = 1; lastpos = (last + 1) % bufSize; break; } } if (foundLineBreak) break; if (bufSize == totalBytes) { bufSize *= 2; buf = realloc(buf, bufSize + 1); } int bytes = 0; while (true) { bytes = read(buf + totalBytes, bufSize - totalBytes); if (bytes != 0) break; } if (bytes == -1) { buf[totalBytes] = '\0'; break; } totalBytes += bytes; } if (totalBytes == 0 && foundLineBreak == 0) { return NULL: } else { return buf; } }
Sunday, February 19, 2012
implement readLine using read
Saturday, February 18, 2012
How to delete the blank page after a table in Microsoft Word
This stupid and simple problem has been bugging me for ages!!! There is a simple workaround for me.
Select the blank paragraph, go to Font dialog, in tab "Font", select "Hidden", then the annoying blank page just disappears.
Note: if you turn on "Show paragraph marks and other hidden formatting symbols", the hidden text will be shown.
Monday, December 12, 2011
RabbIT
Configure values for sector rabbit.filter.ReverseProxy.
httpinfilters=rabbit.filter.ReverseProxy,rabbit.filter.HttpBaseFilter,rabbit.filter.DontFilterFilter,rabbit.filter.BlockFilter,rabbit.filter.RevalidateFilter
......
[rabbit.filter.ReverseProxy]
# This filter is not enabled by default, add it to httpinfilters if you want it.
# This Filter makes rabbit work as an accellerator for one web site.
# Change requests
transformMatch=^/(.*)
transformTo=http://<target_host>:<port>/$1
# Deny proxy requests, you probably want this.
# deny=^http(s?)://.*
deny=
# If we want to allow admin access.
allowMeta=true
java -jar jars/rabbit4.jar -f conf/rabbit.conf
Resource: http://www.khelekore.org/rabbit/
Friday, December 02, 2011
Memory allocation settings in Hadoop
Edit file conf/mapred-site.xml to change amount of memory allocated to sorting:
<property>
<name>io.sort.mb</name>
<value>300</value>
</property>
Edit file conf/mapred-site.xml to change amount of memory allocated to each map/reduce task:
<property>
<name>mapred.child.java.opts</name>
<value>-Xmx800m</value>
</property>
Edit file conf/hadoop-env.sh to change amount of memory allocated to Hadoop daemons:
export HADOOP_HEAPSIZE=1000
Change ports used by Hadoop
Edit file conf/hdfs-site.xml to change ports used by HDFS
<property> <name>dfs.secondary.http.address</name> <value>0.0.0.0:51090</value> </property> <property> <name>dfs.datanode.address</name> <value>0.0.0.0:51010</value> </property> <property> <name>dfs.datanode.http.address</name> <value>0.0.0.0:51075</value> </property> <property> <name>dfs.datanode.https.address</name> <value>0.0.0.0:51475</value> </property> <property> <name>dfs.datanode.ipc.address</name> <value>0.0.0.0:51020</value> </property> <property> <name>dfs.http.address</name> <value>0.0.0.0:51070</value> </property> <property> <name>dfs.https.address</name> <value>0.0.0.0:51470</value> </property>
Edit file conf/mapred-site.xml o change ports used by MapReduce
<property> <name>mapred.job.tracker.http.address</name> <value>0.0.0.0:51030</value> </property> <property> <name>mapred.task.tracker.http.address</name> <value>0.0.0.0:51060</value> </property>
exclude directories when using GNU tar
tar zvcf name.tar.gz --exclude path/to/dir1 --exclude path/to/dir2 path/to/tar
Note:
- Do not include a trailing '/' in the path of excluded directories. Otherwise, it won't work.
- Put --exclude before the directory/file to be tarred.
Friday, November 18, 2011
Install RPM packages with non-root account
Recently, I need to install RPM to RedHat Linux, but I don't have root access. I found this post: http://ajaya.name/?p=6353. However, some commands in the command are not correct or need more clarification. So I wrote down my experience below.
- create file ~/.rpmmacros and add following line
%_rpmlock_path lib/rpm/__db.000
- Initialize the database by running command:
rpm --initdb \
--root /home/<user_name>/rpm-local/ \
--dbpath /home/<user_name>/rpm-local/lib/rpm
- Check the dependence
rpm --root /home/<user_name>/rpm-local/ \
--dbpath /home/<user_name>/rpm-local/lib/rpm \
-ivh package.rpm
- Install package
rpm --root /home/<user_name>/rpm-local \
--dbpath /home/<user_name>/rpm-local/lib/rpm \ --relocate /usr=/home/<user_name>/rpm-local \
--nodeps \
-ivh package.rpm
Resources
Thursday, October 27, 2011
Add "Edit with Vim" item to context-menu in Windows for Vim
This post shows how to add "Edit with Vim" and "Tab Edit with Vim" items to context menu (pop out when you right click a file) in Windows.
Run regedit.exe, go to HKEY_LOCAL_MACHINE/SOFTWARE/Classes/*/shell/
- Create new key "Tab Edit with &Vim" (Right click parent entry -> New -> Key)
- Create new key "Edit with Vim"
- Create new key "command" under "Tab Edit with &Vim"
- Edit the entry with name "(Default)", change its data to
<vim_dir>\gvim.exe" -p --remote-tab-silent "%1" "%*" - Create new key "command" under "Edit with Vim".
- Edit the entry with name "(Default)", change its data to
<vim_dir>\gvim.exe "%1"
It seems that latest versions of vim automatically create the registry entry:
- HKEY_LOCAL_MACHINE\SOFTWARE\Vim\Gvim and the data of entry path points to the vim executable.
- HKEY_CLASSES_ROOT\*\shellex\ContextmenuHandlers\gvim. Data of the entry "(Default)" is the CLSID (51EEE242-AD87-11d3-9C1E-0090278BBD99).
You can delete this entry because we have already added menu item "Edit with Vim". - HKEY_CLASSES_ROOT\Applications\gvim.exe\shell\edit\command. Data of the entry "(Default)" is "<vim_dir>\gvim.exe" "%1"
- HKEY_CLASSES_ROOT\CLSID\{51EEE242-AD87-11d3-9C1E-0090278BBD99}
It has a key named InProcServer32. The data of "(Default)" is <vim_dir>\gvimext.dll - Some other keys
Monday, October 17, 2011
Logging in Hadoop
Hadoop uses log4j via Apache common logging. The config file is conf/log4j.properties.
Some important variables are set in the command line. Following is a snippet cut from the whole command line used to launch HDFS name node.
-Dhadoop.root.logger=INFO,DRFA
-Dhadoop.log.dir=/N/u/hdfs/programs/hadoop-0.21.0/bin/../logs
-Dhadoop.log.file=hadoop-hdfs-namenode-b009.log
-Dhadoop.home.dir=/N/u/hdfs/programs/hadoop-0.21.0/bin/..
-Dhadoop.id.str=hdfs
You can see that log dir, log file, log level, logger are set. DRFA is defined in conf/log4j.properties.
Sunday, October 09, 2011
pdf to eps conversion
Currently, I need to convert pdf files to eps so that they can be included in latex files.
-
Use Acrobat Pro open the pdf file.
Click File -> export -> PostScript -> Encapsulated PostScript or use "Save As" and change "Save As Type"
However, the bounding box is NOT correctly calculated.
You can use gsview to correct it. Use gsview open the eps file, click "File -> PS to EPS", select "Automatically calculate Bounding Box" and save the output file. -
Use ghostscript. Execute following command:
gswin32 -sDEVICE=epswrite -sOutputFile=<filename>.eps <filename>.pdf
This works well and bounding box is correctly calculated. - Use Xpdf (http://www.foolabs.com/xpdf/download.html)
pdftops -eps <filename>.pdf <filename>.eps
However, the bounding box is NOT correctly calculated.