Friday, January 25, 2013

Stupid scanner tricks

This is a great piece of code I found here and it's so useful that I decided to write a post for it.
If you need to convert a stream to a String you can use this simple line of code:
 String text = new Scanner(new InputStream()).useDelimiter("\\A").next();  
Remember that Scanner takes in input any class implementing Readable: InputStream, File, Channel and so on. For example, if you need to fetch a big InputStream in a Servlet you can do it without using any loop. Also remember that regex "\A" matches the beginning of the input, and since there is only one begin in one input, Scanner will fetch the whole stream in one move! If you need a specific charset, you can specify it as a second parameter in Scanner constructor.

Good coding!

Monday, December 31, 2012

Building QtMobility 1.2 on Mac OS X Lion

After a lot of pain and time I found that under Lion, QtMobility can't be compiled because it reverts to LLVM instead of GCC, giving the following error:
 ld: file not found: QtDBus.framework/Versions/4/QtDBus for architecture x86_64.  
I don't know why exactly, but I found a workaround to use GCC and it works fine. What I saw is that if you want to compile only a part of QtMobility, instead of the whole framework, it gives you this error with other Qt modules too, not just with QtDBus. Normally you should run the following steps to do a build:
  • Open a console and type: git clone git://gitorious.org/qt-mobility/qt-mobility.git
  • Once download has completed, you can build it using ./configure, make and make install (don't do it now)
  • Even if after the configure script has done its work you do qmake -spec /usr/local/Qt4.8/mkspecs/macx-g++, it just changes the compiler in the parent Makefile, and not in the rest of children projects (Bearer, Contacts, Multimedia ...). You should repeat it manually for each QtMobility module, or..
To do it in one move, open the configure.sh script, locate the following lines:
 checkostype(){  
   match="Darwin"  
   if [ `uname -s` = "${match}" ]; then  
     OS="darwin"  
     QMKSPEC="-spec macx-g++"  
     if [[ `gcc --version` =~ .*llvm.* ]]; then  
       QMKSPEC="-spec macx-llvm"  
     else  
       QMKSPEC="-spec macx-g++"  
     fi  
     echo "QMAKESPEC = "$QMKSPEC >> "$CONFIG_IN"  
   fi  
 }  
 checkostype  
and change them to:
 checkostype(){  
   match="Darwin"  
   if [ `uname -s` = "${match}" ]; then  
     OS="darwin"  
     QMKSPEC="-spec macx-g++"  
     #if [[ `gcc --version` =~ .*llvm.* ]]; then  
     #  QMKSPEC="-spec macx-llvm"  
     #else  
     #  QMKSPEC="-spec macx-g++"  
     #fi  
     echo "QMAKESPEC = "$QMKSPEC >> "$CONFIG_IN"  
   fi  
 }  
 checkostype  

Good coding!

Sunday, December 30, 2012

MinGW-gcc-4.4.0

Since until now Qt under Windows is binary compatible with minGW-4.4, and this version seems disappeared from mingw site, I decided to share it as a free download. I grabbed it from trolltech ftp, so I'm just mirroring it for easy finding on the web.



Good coding!!