jueves, 8 de mayo de 2014

A simple way to determine if a String could be an integer

There are a lot of ways to check if a String could represent an integer value. I will show you two very simple and yet fast methods:

Method 1

    public final static boolean isInteger(String in)
    {
        char c;
        int length = in.length();
        boolean ret = length > 0;
        int i = ret && in.charAt(0) == '-' ? 1 : 0;
        for (; ret && i < length; i++)
        {
            c = in.charAt(i);
            ret = (c >= '0' && c <= '9');
        }
        return ret;
    }


Method 2 (modified version of method 1)

    public static boolean isInteger(String in)
    {
        if (in != null)
        {
            char c;
            int i = 0;
            int l = in.length();
            if (l > 0 && in.charAt(0) == '-')
            {
                i = 1;
            }
            if (l > i)
            {
                for (; i < l; i++)
                {
                    c = in.charAt(i);
                    if (c < '0' || c > '9')
                        return false;
                }
                return true;
            }
        }
        return false;
    }


Method 3 

    static final byte[] b = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    public final static boolean isInteger(String in)
    {
        char c;
        int length = in.length();
        int i = 0;
        if (length > 0 && in.charAt(0) == '-')
            i = 1;
        if (i >= length)
        {
            return false;
        }
        for (; i < length; i++)
        {
            c = in.charAt(i);
            if (b[c & 0xff] == 0)
                return false;
        }
        return true;
    }



I hope this information is useful.

PS. Be aware that all of these will say that very large strings (with valid big integers) are true, so if you just try to convert them to Integers, an Exception could be thrown.

domingo, 4 de mayo de 2014

Multiple column list using HTML (ul and li)

I know that you could find a lot of ways to show columns using HTML,  but sometimes for most of those ways you need a "deep"  knowledge of CSS, if you just know the very basic stuff of CSS and HTML, then this is for you. Fast and easy.

The idea is having a multicolumn list, using ul and li tags (and just a little bit of CSS).
Let's suppose you have the following data and you want them to show as a column for every category:

id  name         category
--------------------------
1   Antena       Exterior
2   Emblema      Exterior
3   Llantas      Exterior
4   Luces        Exterior
5   Estéreo      Interior
6   Tapetes      Interior
7   Encendedor   Interior
8   Gato         Accesorio
9   Extintor     Accesorio
10  Herramientas Accesorio
11  Claxon       Cofre
12  Batería      Cofre
13  Filtro aire  Cofre


You want them to look like this:

Exterior   Interior    Accesorio     Cofre
---------  ----------  ------------  -----------
Antena     Estéreo     Gato          Claxon
Emblema    Tapetes     Extintor      Batería
Llantas    Encendedor  Herramientas  Filtro aire
Luces

So, the HTML code could be as easy as this:

<ul style="float:left;list-style-type: none;">Exterior<hr>
    <li>Antena</li>
    <li>Emblema</li>
    <li>Llantas</li>
    <li>Luces</li>
</ul>

<ul style="float:left;list-style-type: none;">Interior<hr>
    <li>Estéreo</li>
    <li>Tapetes</li>
    <li>Encendedor</li>
</ul>

<ul style="float:left;list-style-type: none;">Accesorio<hr>
    <li>Gato</li>
    <li>Extintor</li>
    <li>Herramientas</li>
</ul>

<ul style="float:left;list-style-type: none;">Cofre<hr>
    <li>Claxon</li>
    <li>Batería</li>
    <li>Filtro aire</li>
</ul>




There you have It!.

I hope this is useful for you.

martes, 1 de abril de 2014

Signing Qt5 (5.2.0) Applications in Mac OSX Mavericks

I was trying to sign an application that We developed with Qt5 (medicalmanik.com) and due to the not so clear documentation you find out there, I decided to publish the way I could complete this task (I tried some procedures I found on different sites without success -that's why I am publishing this).

First of all let me tell you that I just basically modified one script I found on Internet (and use another one as is) and adapted it to my needs, so you just have to edit some lines in It and that's it!

What It is important to notice is that if you want to distribute your application out of the AppStore you will need a Developer ID Application certificate (not the Mac Developer -this is just for testing).

OK, let's begin...

1) Copy, paste and save to a file the following code, or download it  from  https://gist.github.com/kainjow/8059407 (macdeployqt_fix_frameworks.rb). There is not need to modify it.


#!/usr/bin/env ruby

# Copies missing Info.plist files for a .app's Qt frameworks installed
# from macdeployqt. Without the plists, 'codesign' fails on 10.9 with
# "bundle format unrecognized, invalid, or unsuitable".
#
# Example usage: 
# ruby macdeployqt_fix_frameworks.rb /Users/me/Qt5.2.0/5.2.0/clang_64/ MyProgram.app
#
# Links:
# https://bugreports.qt-project.org/browse/QTBUG-23268
# http://stackoverflow.com/questions/19637131/sign-a-framework-for-osx-10-9
# http://qt-project.org/forums/viewthread/35312

require 'fileutils'

qtdir = ARGV.shift
dotapp = ARGV.shift

abort "Missing args." if !qtdir || !dotapp
abort "\"#{qtdir}\" invalid" if !File.exists?(qtdir) || !File.directory?(qtdir)
abort "\"#{dotapp}\" invalid" if !File.exists?(dotapp) || !File.directory?(dotapp)

frameworksDir = File.join(dotapp, 'Contents', 'Frameworks')
Dir.foreach(frameworksDir) do |framework|
  next if !framework.match(/^Qt.*.framework$/)
  fullPath = File.join(frameworksDir, framework)
  destPlist = File.join(fullPath, 'Resources', 'Info.plist')
  next if File.exists?(destPlist)
  srcPlist = File.join(qtdir, 'lib', framework, 'Contents', 'Info.plist')
  abort "Source plist not found: \"#{srcPlist}\"" if !File.exists?(srcPlist)
  FileUtils.cp(srcPlist, destPlist)
end

2) Create a file named macdeployqt.sh (this will call macdeployqt_fix_frameworks.rb and macdeployqt ) and copy, paste and edit the following code (posted by Sarah Jane Smith on https://bugreports.qt-project.org/browse/QTBUG-23268). Modify It according to your needs (take a look at the PATHS).


# Create a temporary copy of the app for deploy & signing
# This must not be previously signed/sandboxed
# Include the .app suffix

APP_NAME=$1

QT5_CLANG=/Users/hectorsuarez/Aplicaciones/Qt5.2.0/5.2.0/clang_64
QT5_BIN=$QT5_CLANG/bin


#EXT_SIGN_CERT="Mac Developer: Hector Suarez Barenca"
EXT_SIGN_CERT="Developer ID Application: Barenca Networks"

# This will create a temporary directory
BUILD_TMP=~/tmp
if [ -d $BUID_TMP ]
then
echo
echo "###"
echo "### Cleaning directory... $BUILD_TMP/$APP_NAME ###"
echo "###"
echo
### Borrar versión previa
rm -rf $BUILD_TMP/$APP_NAME
else
    mkdir -p $BUILD_TMP
fi

echo "### Copy $APP_NAME to $BUILD_TMP"
echo

### Copy the .app to a temporary location
cp -R $APP_NAME $BUILD_TMP

TMP_APP=$BUILD_TMP/$APP_NAME

QT_FMWK_VERSION="5"
QT_FMWKS="QtCore QtGui QtPrintSupport QtWidgets QtMultimedia QtMultimediaWidgets QtNetwork QtSql QtScript QtWebKitWidgets QtOpenGL QtPositioning QtQml QtQuick QtSensors QtWebKit"

echo "### Deploying Qt ${QT_FMWK_VERSION}..."
echo 
$QT5_BIN/macdeployqt $TMP_APP $VERBOSE_MACDEPLOY_QT

echo "Patching Qt frameworks..."

## Set the right PATH 
~/Aplicaciones/macdeployqt_fix_frameworks.rb $QT5_CLANG $TMP_APP

for FMWK in $QT_FMWKS; do
    FMWK_PATH="${TMP_APP}/Contents/Frameworks/${FMWK}.framework"
    mv -v "${FMWK_PATH}/Resources" "${FMWK_PATH}/Versions/${QT_FMWK_VERSION}/."
    (cd "${FMWK_PATH}" && ln -s "Versions/${QT_FMWK_VERSION}/Resources" "Resources")
    (cd "${FMWK_PATH}/Versions" && ln -s "${QT_FMWK_VERSION}" "Current")
    perl -pi -e "s/${FMWK}_debug/${FMWK}/" "${FMWK_PATH}/Resources/Info.plist"
done


echo "Codesigning..."
for FMWK in $QT_FMWKS; do
    FMWK_PATH="${TMP_APP}/Contents/Frameworks/${FMWK}.framework"
    /usr/bin/codesign --verbose \
        --sign "$EXT_SIGN_CERT" \
        "${FMWK_PATH}/Versions/5"
done

for PLGN in $(find $TMP_APP -name "*.dylib"); do
    /usr/bin/codesign --verbose \
        --sign "$EXT_SIGN_CERT" \
        ${PLGN}
done

echo
echo "### Trying to sign $APP_NAME"
echo
/usr/bin/codesign --verify --verbose \
    --sign "$EXT_SIGN_CERT" \
    $TMP_APP

echo ""
echo "### Confirming \"Registered Developer\" codesiging of app"
codesign --verify --verbose=4 $TMP_APP

echo ""
echo "### Confirming codesigning app, frameworks & plugins for GateKeeper"
spctl --verbose=4 --assess --type execute $TMP_APP



3) Execute macdeployqt.sh  and enjoy It!



I hope this works for you.  Best regards.


PS. I tested these scripts with Qt 5.2.0
      Thanks to those that take time to help others


References:
https://gist.github.com/kainjow/8059407
https://bugreports.qt-project.org/browse/QTBUG-23268

https://developer.apple.com/library/mac/documentation/IDEs/Conceptual/AppDistributionGuide/DistributingApplicationsOutside/DistributingApplicationsOutside.html

http://stackoverflow.com/questions/19637131/sign-a-framework-for-osx-10-9

http://stackoverflow.com/questions/7697508/how-do-you-codesign-framework-bundles-for-the-mac-app-store

http://www.copyquery.com/productsigned-mac-app-not-installing-in-computers-that-are-not-mine/