Monday, November 5, 2012

html2hamljs

New mini project. html2hamljs. It creates haml from html to use with haml-js

Friday, August 3, 2012

Detecting memory leaks in C/C++ executable or libs with vallgrind

This is an awesome tool. It tells u where and how your memory leaks happen. Don't forget to compile with the -g to get debug symbols....anyway the tool is called

Valgrind

on Ubuntu i installed using
sudo apt-get install valgrind

Invoking native C/C++ libs using python

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html...here is what you have to do with python

This one is super ez!
from ctypes import cdll
from ctypes import c_char_p
from ctypes import c_float

lib = cdll.LoadLibrary('libmylib.so')
helloWorld = lib.MYLIB_helloWorld

helloWorld()

Invoking native C/C++ libs using ruby

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html...here is what you have to do with ruby

install ffi. linux distros have packages.....so on ubuntu apt-get install ffi...

then here is the module code as well as sample script

module LibMylib
        extend FFI::Library
        ffi_lib "libmylib.so"
        attach_function :MYLIB_helloWorld, [], :void
end
then your script. make sure libmylib is replaced with whatever u named the module file above
require 'ffi'
require 'libmylib'

LibMylib.MYLIB_helloWorld()

Invoking native C/C++ libs using clojure and JNA

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html....here is how you do it in clojure download jna.jar and platform.jar write clojures codes!
(defn helloworld ( []
                ( let [ lib  ( com.sun.jna.NativeLibrary/getInstance "mylib" ) ]
                ( let [ helloWorld ( .getFunction lib  "MYLIB_helloWorld" ) ]
                ( println ( .invokeVoid helloWorld null ) ) ) ) ) )

( helloworld )


then run with
java -cp /usr/share/java/clojure.jar:jna.jar:. -Djna.nosys=true clojure.main helloworld.clj


Invoking native C/C++ lib using groovy and JNA

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html....here is how to invoke using groovy again make sure you get the jna.jar and platform.jar from the interwebs!

here is the groovy code:

import com.sun.jna.*;

def lib = NativeLibrary.getInstance("mylib")
def helloWorld = lib.getFunction( "MYLIB_helloWorld" )

/*
if args do this
Object[] args = new Object[2];
args[0] = "arg1"
args[1] = "arg2"
*/

helloWorld.invokeVoid(null)

then run with

groovy -cp jna.jar:.  -Djna.nosys=true helloworld.groovy

for more invoke options see http://jna.java.net/javadoc/com/sun/jna/Function.html

Invoking native C/C++ lib from C# and mono

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html......here is how u connect using c# and mono (haven't tried .net windows but I'm guessing it's the same)

this one is super ez. here is the full example
using System;
using System.Runtime.InteropServices;

public class HelloWorld
{

        static public void Main( string[] args )
        {
                
MYLIB_helloWorld();

        }

        [DllImport("libmylib.so")]
        private static extern void MYLIB_helloWorld();




}

then run with
gmcs HelloWorld.cs
mono HelloWorld.exe

Invoking native C/C++ lib from Java and JNA

So as a continuation of the last post http://orphanware.blogspot.com/2012/08/making-c-available-through-native-calls.html......here is how u connect using JNA:
  1. compile your native lib to so or dll and place it in your LIB_LIBRARY_PATH on linux or in your PATH in windows (IMPORTANT needs to start with "lib". So if it's mylib it should be libmylib
  2. download jna.jar and platform.jar either manually or with a tool like maven
  3. create an interface that extends com.sun.jna.Native
  4. create class to use (not implement) the interface
here is sampe interface


import com.sun.jna.Library;
import com.sun.jna.Native;


interface MyLib extends Library {

        MyLib INSTANCE = (MyLib) Native.loadLibrary("mylib", MyLib.class );
        String MYLIB_helloWorld();
        
}

and a class that uses it


import com.sun.jna.Library;
import com.sun.jna.Native;

public class MyLibUser{


        public static void main( String[] args ) {

                MyLib lib = MyLib.INSTANCE;

                lib.MYLIB_helloWorld();
        }


}

now compile with
javac -cp jna.jar:. -Djna.nosys=true  MyLibUser
and run with
java -cp jna.jar:. -Djna.nosys=true  MyLibUser
Gaaaangsta! No?

Making C++ available through native calls

So if you need some raw of performance in your application what do you do? You drop down into a native development language like C. Well what if you want to code with OOP goodness and use C++. Regular calls from Java, Ruby, etc. will not get to your C++ functions because C++ adds all sorts of template madness when it exports your programs symbols.

So what do you do?


extern "C"

so if your want to wrap your c++ classes in a single method called helloWorld (or MYLIB_helloWorld to not be confused with other helloWorlds!):
extern "C" MYLIB_helloWorld();
in your header file.....then in your cpp file you can just do the normal
MYLIB_helloWorld();
Yay! Now you can make native calls from higher level languages like java or even ruby without them getting pissed off at the c++ template symbols. Will post examples soon.

Cygwin and SSH server

To setup SSH on windows using cygwin....first install cygwin! Make sure you check off the ssh package. Then open up the cygwin shell and do the following:

ssh-host-config -y

when prompted with the CYGWIN=
tty ntsec
and then finally
cygrunsrv -S sshd
You might hit ERRORS wither on first start or 2 years later! Most common that I've run into are:
  • Win32 error 1069: The service did not start due to a logon failure.
  • Error starting a service: QueryServiceStatus: Win32 error 1062: The service has not been started.
For the first one go to services in windows and lookup CYGWINsshd. In properties under "log on" tab make sure your user (usually cyg_server) AND password match the on in your cygwin. So type
passwd cyg_server
in your cygwin shell to change it ... For the second problem just repeat the steps from STEP one to reconfigure.. Cheers!

Wednesday, July 18, 2012

String Palindrome Algorithm

The last of these old school algorithms. Funny how trivial they should be after all these years but they still trip me up. Hence these posts and practices!

class Pal {



 public static void main(String args[]) {

  //System.out.println(findPalIter(args[0]));
  System.out.println(findPalRec(args[0]));


 }



 public static boolean findPalIter(String str) {

  char[] orig = str.toCharArray();
  char[] backward = new char[orig.length];

  int j=0;
  for( int i=orig.length-1; i >=0; i-- ) {


   backward[j++] = orig[i];



  }

  if( str.equals( new String(backward)))
   return true;


  return false;




 }


 private static boolean findPalRec(String str) {

  char[] chars = str.toCharArray();

  if( chars.length == 0 || chars.length == 1 ) 
   return true;

  if( chars[0] == chars[chars.length-1])
   return findPalRec( str.substring( 1, chars.length-1 ) );
  else
   return false;

 }







}


https://github.com/asharif/StringPalindrome

String Anagram Algorithm

As a continuation of the last post, here is an algorithm for finding anagrams from a string
public class Anag {



        public static void main(String args[]){

                char[] anag = new char[args[0].length()];
                findAnag(args[0], new String(anag), 0);




        }



        public static void findAnag(String orig, String anag, int index) {
                if ( orig.toCharArray().length == 0 ) {

                        System.out.println( anag );
                        return;


                }

                char[] origChars = orig.toCharArray();
                char[] anagChars = anag.toCharArray();

                for( int i=0; i < origChars.length; i++ ) {

                        anagChars[index] = origChars[i];
                        index++;
                        String strStillLeft = orig.substring(0, i) + orig.substring(i+1, orig.length());
                        findAnag( strStillLeft, new String(anagChars), index);
                        index--;

                }




        }











}

https://github.com/asharif/StringAnag

String Combination Algorithm

Just needed to brush up some old but important algorithms so I thought I'd post them here and on github for my own future reference and anyone elses. This example I did in Java:

class Combo {



        public static void main ( String args[] ) {

                char[] combos = new char[args[0].length()];
                findCombos ( args[0], new String(combos), 0 );

        }



        private static void findCombos( String str, String combos, int index ) {

                char[] comboChars = combos.toCharArray();


                if ( index == str.length()) {

                        System.out.println( new String(comboChars) );

                        return;

                }

                for ( int i = 0; i < str.length(); i++ ) {
                        char[] strChars = str.toCharArray();
                        comboChars[index] = strChars[i];
                        index++;
                        findCombos(str, new String(comboChars), index);
                        index--;

                }




        }


}

Here it is on github: https://github.com/asharif/StringCombo

Monday, July 16, 2012

packit4me.com (2D/3D Bin Packing)

(UPDATE 06/04/2013) This project is now available @ http://www.packit4me.com/
3D/2D bin packing. Given multiple bins and items to be packed into the bins, the algorithm attempts to find a good solutions of how to pack. I got so many requests for this that I made it into a real life project. Check out the site. It has comprehensive documentation on how to call the API with all the popular languages (Java, php, C#, ruby, python). I hope it works for you! If not just email me or make some posts here. I'll try to setup some ticket system soon to take requests.

Friday, March 2, 2012