Monday, December 15, 2014

Small C code to convert german text to old ASCII-DE encoding


I had a task to convert a UTF-8 or ISO-8859-2 encoding german text (names, addresses,...) into a 7bit ISO-646-DE (ASCII-DE) encoding. Since this encoding almost is non-existent, it was a bit difficult to find on Google search any example of such a conversion. I decided to be that sucker to write it :) I know this is not really the best code ever, but does its job. To see it in action, you can check it here (in case you see in a code pane [object Object], just refresh the page, and it will show the code) :)
#include 
#include 
#include 
#define NUM_OF_CONV_TABLE 8

/*
compile it with: 
gcc -o convert convert.c
run it with:
convert input.txt output.txt && hexdump -ve '1/1 "%.2X"' output.txt

*/

int main ( int argc, char *argv[] )
{

 static unsigned char utfconv[NUM_OF_CONV_TABLE] = 
     {0x84, 0x96, 0x9C, 0xA4, 0xB6, 0xBC, 0x9F}; 
 static unsigned char conv[NUM_OF_CONV_TABLE] = 
     {0xC4, 0xD6, 0xDC, 0xE4, 0xF6, 0xFC, 0xDF}; 
     //Ä, Ö, Ü, ä, ö , ü, ß
 static unsigned char values[NUM_OF_CONV_TABLE] = 
     { 0x5B, 0x5C, 0x5D, 0x7B, 0x7C, 0x7D, 0x7E }; 


    if ( argc != 3 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "Text Encoding Convertor: From UTF-8/ISO-8859-2, to ISO-646-DE (ASCII-DE)");
        printf( "usage: , ");
    }
    else 
    {
        // We assume argv[1] is a filename to open
        FILE *fileIn = fopen( argv[1], "r" );
        FILE *fileOut = fopen( argv[2], "w");

        /* fopen returns 0, the NULL pointer, on failure */
        if ( fileIn == 0 || fileOut == 0)
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            unsigned char x;
/*            unsigned char size;
            if (strcmp(covertToUpper(argv[3]),"UTF-8") != 0) {
                size=2;
            } else {
                size=1;
            }*/
            /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file.  Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
            unsigned char isUTF=0;
            unsigned char wasC3=0;
            while  ( fread(&x,1,1,fileIn)==1 )
            {
                int i=0;
                int o=x;
             while (i Reached EOF
      If nrd <  0 --> Some kind of error */






        }
    }
}

Tuesday, December 9, 2014

Getting list of files from a folder in a text file

I keep always forgetting the exact parameters to use, and need to write this down somewhere :) It might also be helpful for others: In Windows:
dir /b >folderlist.txt
and if you also want to see the subfolders with their full paths: In Windows:
dir /b >folderlist.txt
Edit: shell version will be added later.

Tuesday, March 4, 2014

How to convert Integer into String with leading zeros in Java

This is pretty easy task, but I keep on forgetting it:
String.format("%05d", num);

Tuesday, February 18, 2014

Batch Resize Images in Mac OS

To resize all the images to maximum height of 640px
sips -Z 640 *.jpg
To resize all the images to maximum width of 400px
sips --resampleWidth 400 *.jpg

To resize SVG files in Mac OS, you can use the rsvg library install it with:
brew install -v librsvg
and then run it with the following shell line:
for i in *; do rsvg-convert $i -w 100 -h 100 -f svg -o `echo new/$i`; done
This takes all the svg files in the folder, resizes it and copies the result into new/ folder

Thursday, February 13, 2014

jBPM bpmn designer validation errors

Ok, you must agree with me on this one. It is not nice to develop in environment where you get an errors on unknown location. Well, errors are fine, it is part of software development, but when you are not able to see any details of where the error comes from, it gets to waste enormous amount of time to dig out why such error happens.

I got such errors when I try to use jBPM eclipse environment to try implementing BPM process using an external bpmn file. Since bpm is created in one of those graphical eye-candy oriented BPM modelling software (with little or no support for bpmn validation), I could imagine there are some  pieces (details) missing within bpmn.

No Interface found - According to BPMN standard, every Service Task needs to be defined with a proper service operations. If such is not defined for any of the service tasks within your bpm file, you will get this error.

No Item definitions found - tbd.

If I come up to some more of these errors, I will add them here...

Resize of svg icons from command line

This post would probably only useful to me, but, it is possible to change the size of svg files using XSLT (why would one need to change size of vector graphics??? Well, there are cases where there are no means to scale the images)

I had a bunch of files which I needed to resize into a proper size (in my case, 16px x 16px icons), and was looking for a easy solution. Since I knew svg is xml-based vector graphics, I was hoping to find example similar to

I used the xsl file from the post on https://www.ruby-forum.com/topic/166757#732031, with adding the 16x16px as a target scale.
Command line to run:
xsltproc --stringparam scale 2.5 bigger.xsl in.svg >out.svg
<?xml version="1.0"?>
<!-- bigger.xsl $Id$
     Author: Terry Brown
     Created: Thu Sep 25 2008
-->
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:svg="http://www.w3.org/2000/svg"
  xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
  >
  <xsl:output method="xml"/>
  <xsl:template match="svg:svg">
    <xsl:copy>  <!-- copying the svg element -->
      <xsl:copy-of select="svg:metadata|svg:defs|sodipodi:namedview"/>
      <xsl:element name="svg:g">
 <xsl:attribute name="transform">
   <xsl:value-of select="concat(concat('scale(',$scale),')')"/>
 </xsl:attribute>
 <xsl:copy-of
     select="*[not(svg:metadata|svg:defs|sodipodi:namedview)]"/>
      </xsl:element>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Thanks Terry Brown!  

Wednesday, February 12, 2014

Create a new Custom Form Field in Vaadin

I am writing this post because of multiple solutions shown on web, which due to multiple drastic implementational changes during Vaadin development (most notably Vaadin 6 to Vaadin 7).

How to create a new custom form field, so that it can easily be integrated into FormLayout.

Simplest way so far is to extend CustomField class.

Here is the example
import com.vaadin.ui.AbstractField;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.CustomField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Button.ClickEvent;

public class ExampleCustomField extends CustomField<String> {
    private static final long serialVersionUID = 8376102707918932019L; 
    private static final String = "Default Text";
    private TextField field;
    private Button delButton=new Button("Reset");
    private HorizontalLayout layout=new HorizontalLayout();

    public ExampleCustomField() {
        super();
        layout.addComponent(field);
        layout.addComponent(delButton);

        delButton.addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = -5711434206619225940L;

            public void buttonClick(ClickEvent event) {
                field.setValue(RESET_VALUE);
                Notification
                    .show("Reset Button pressed");
                }
        });
    }

    
    @Override
    public String getValue(){
        return field.getValue();
    }
    
    @Override
    public Object getType() {
        return field.getType();
    }

    @Override
    protected Component initContent() {
        return layout;
    }
}