Sunday, July 13, 2014

printf command could handle special character easily

Just found a better solution on handling special character in a string. In a given scenario where I have a loop to process each file from a given file list as shown below:
 while read file
 do
  newfile=$(echo "${file}" | sed 's, ,\\ ,g')
  ...
  ...

 done < ${TEMP_FILE}
Assuming ${TEMP_FILE} containing a list of files. The file is validate to see is there any space within the file name. If there is a space, prefix it with backslash. In this context, I was using sed command to handle this for me. But there is a problem where this command does not able to handle special character, such as single quote (‘) because this command only search for space and then prefix it with backslash.

I've been thinking to have all sed command for every single special characters but it seems not a wise move to me. The expert share me a though that printf command should help me out as shown in the code snippet below:

newfile=$(printf "%q" "${file}")

This solution not only fix the single quote but also space within a string. Really appreciate the help from expert.

Tuesday, July 8, 2014

Beware of -Dfile option when using mvn in Cygwin

Cygwin is my favorite tool that could allow me to execute Linux command on Windows. Sometimes I find it weird when I’m navigating to the root path of each partition where primary partition on Windows, which C:, were prefix with /cygdrive. At first I’m not so comfort with this approach but then later I started to adopt this culture in my daily routine job.

Here come to a problem when I was using Maven to install a third party dependency into my local Maven repository. When the following command was fired:

mvn install:install-file -Dfile=/cygdrive/c/Users/kok.hoe.loh/workspace/ProjectA/lib/AuditUtil.jar -DgroupId=org.huahsin.eai -DartifactId=AuditUtil -Dversion=0.0.1 -Dpackaging=jar
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building ProjectA 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) @ ProjectA ---
[INFO] Installing C:\cygdrive\c\Users\kok.hoe.loh\workspace\ProjectA\lib\AuditUtil.jar to C:\Users\kok.hoe.loh\.m2\repository\org\huahsin\eai\AuditUtil\0.0.1\AuditUtil-0.0.1.jar
[INFO] Installing C:\Users\kok.hoe.loh\tool\Cygwin\tmp\mvninstall6980818442128433518.pom to C:\Users\kok.hoe.loh\.m2\repository\org\huahsin\eai\AuditUtil\0.0.1\AuditUtil-0.0.1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.764s
[INFO] Finished at: Tue Jul 08 18:00:24 SGT 2014
[INFO] Final Memory: 5M/76M
[INFO] ------------------------------------------------------------------------
The build was success but the dependency was not install in my local Maven repository. There trace information has already show me the problem but I have overlook it. Notice that the dependency was install to C:\cygdrive\c\..., which is not what I want. It seems to me -Dfile option doesn't recognize Cygwin file system.

This fix was easy, just ignore the root when dealing with -Dfile, which is /cygdrive/c for my case. Following sample shows the real work.

mvn install:install-file -Dfile=/Users/kok.hoe.loh/workspace/ProjectA/lib/AuditUtil.jar -DgroupId=org.huahsin.eai -DartifactId=AuditUtil -Dversion=0.0.1 -Dpackaging=jar

Sunday, July 6, 2014

Resolving GLEW library when compile Cocos2d-x in Windows 8

This is my second try on compiling Cocos2d-x on Windows platform. Since I have successfully compile for Linux, I'm so eager to try it on Windows. Unfortunately the build were failed. This was happened in few months ago.

In my previous attempt to compile Cocos2d-x on Windows platform happened in few months ago, I was so frustrate that I'm not able to resolve the GLEW library during run-time. I know the root cause, it is either:

  1. the GLEW DLL wasn't there, or
  2. an incompatible GLEW version has been loaded.

A check on the build directory, found out there already have the GLEW DLL, so option 1 is invalid. I downloaded the GLEW library and replace the existing GLEW library and my fate still the same. Failed! Thus option 2 wasn't out. I was running out of clue after many times of try and error, ever wonder which GLEW library did Cocos2d-x were using?

Now a few months later, when I'm looking back on the same problem, I think I'm almost there, just that I have miss out some steps left to be done. As of this writing, I'm using Cocos2d-x 3.1.1 and VS Desktop 2012 Express Edition on Windows 8. This is what I did:

  1. Right click on the project > Properties > Linker tab > General tab > Add the new GLEW library path where the library is downloaded into Additional Library Directories field.
  2. On Linker page again > Input tab > Add glew32.lib into Additional Dependencies field.
  3. Replace the new GLEW DLL with the one located in build path.

Remember not to make a clean build as this will wipe off all the stuff in build directory. If a clean build has accidentally been fired, just replace the GLEW DLL will do.

Thursday, July 3, 2014

Hacking on LibreOffice to bypass proxy during build

It was a relax week for me while having all my tasks commit. Since I’m quite free at the moment, I grab a copy of LibreOffice source code to play around with. When I just started to build on it, the make command return me error due to the build process unable to proceed. The reason being is wget command which require the proxy information is not provided. I know that I’m require to provide proxy information but where? Do a deep search in the source code, found out Makefile.fetch contain following code:

&& $(WGET) --progress=dot:mega -Q 0 -P "." -l 0 -nd -nH -N $1/$2 2>&1 | tee -a  $(fetch_LOGFILE) && [ $$PIPESTATUS -eq 0 ]

I think this should be the one that cause the build failed. I wasn't sure whether it works or not if I pass in the proxy information? Just give it a try:

&& $(WGET) -e use_proxy=yes -e http_proxy=xxx.xxx.xxx.xx:8080 --proxy-user=username --proxy-password=password --progress=dot:mega -Q 0 -P "." -l 0 -nd -nH -N $1/$2 2>&1 | tee -a $(fetch_LOGFILE) && [ $$PIPESTATUS -eq 0 ]

Yes! I’m rite. The build able proceed as usual.

Wednesday, July 2, 2014

ls command doesn’t work but find works!

Oh no! The ls command does not work as expected! I have the following code in my bash script that will count the total number of txt and pdf file extension.

FILECOUNT=`ls /source_file_path/${1}/*.[Tt][Xx][Tt] /source_file_path/${1}/*.[Pp][Dd][Ff] | wc -l`

Somehow this statement will not work if I schedule it in the cronjob. But it will work if I manually execute the script. Feeling so weird. I was lucky that someone from the forum giving me an advise saying that find command would be a better choice for my use case. Here comes the new code.

FILECOUNT=$(find /source_file_path/${1}/ -maxdepth 1 -name "*.[Pp][Dd][Ff]" -o -name "*.[Tt][Xx][Tt]" | wc -l)

Beware of the maxdepth option, I made a mistake that if I miss this option in the find command, it will return me all files located in the subfolder as well.

Tuesday, July 1, 2014

WinSCP namespace could not be found.

Now my file transfer program has evolve to transfer files from one server to another server. This may involve WinSCP API using C# programming language in the project. Frankly speaking, I'm a noob in C#. In order to work with WinSCP API, the first thing to do is to have WinSCPnet.dll downloaded and put it at the same level with my executable path. Next is to add a reference of this module into the project. Following steps shows how this could be done.

  1. Go to project properties > Reference Paths > Fill in the Folder field with the path where the WinSCPnet.dll is locate. 
  2. Right click on project > Add Reference > Choose Extensions under Reference Manager > Select WinSCPnet. 
Failed to do this will cause an error at following statement:

using WinSCP; 

And this error message could be seen.

The type or namespace name 'WinSCP' could not be found (are you missing a using directive or an assembly reference?)

Tuesday, June 24, 2014

Test command on file size in bash script

My objective was to capture the files locate in a particular path into a file, this is what I did in my bash script.
 # find out all available file
 files=$(ls -1 ${SRC_PATH}/*.txt)
 echo "${files}" > ${TEMP_FILE} 

 # convert the space between each file into newline
 sed 's/ /\n/g' ${TEMP_FILE} > ${PARSE_FILE}
 
 # loop through each file and check the file size is greater than zero
 for file in `cat ${PARSE_FILE}`; do

  file_size=`ls -lah ${file} | awk '{ print $5 }'`
  if [ $file_size -ne 0 ]; then
   echo $file >> "${NICE_FILE}"
  fi
 done
After a few round of review and unit testing on my code, I realize there is a problem where sed command was unable to handle file name with space within it. After a few round of rework, finally I came out a so called "perfect" solution.
 # find out all available file
 files=$(ls -1 ${SRC_PATH}/*.txt)
 echo "${files}" > ${TEMP_FILE} 

 # loop through each file and check the file size is greater than zero
 while read file 
 do
  if [ -s "${file}" ]
  then
   newfile=$(echo "${file}" | sed 's, ,\\ ,g')
    
   echo "${newfile}" >> "${NICE_FILE}"
  fi
 done < ${TEMP_FILE}
No, this is not yet the perfect solution, I'm sure there is always a better one beyond this universe.