Calling Euphoria routines from C routines
- Posted by Robert Craig <rds at ATTCANADA.NET> Jul 16, 1999
- 471 views
I discovered today that calling Euphoria routines from C under Linux already works. I thought there might be some complicated work to do, but it seems to work fine using exactly the same mechanism as WIN32 call-backs. Here's a demo program you can try on Linux. The C library has a general-purpose quick sort routine, qsort(). You pass it an array of items. Each item is a certain number of bytes in size. You also pass it a pointer to your own comparison routine, that will compare any two items and return -1, 0, or +1. qsort() calls your routine whenever it compares two items. It passes you pointers to the two items. The demo passes the call-back address of a Euphoria function to do the compares. qsort() calls the Euphoria function for each compare. At the end, the array of random numbers has been sorted. -- Calling a Euphoria routine from a C routine under Linux include dll.e include machine.e atom libc integer qid, nitems, item_size, qsort atom qaddr, array function qcompare(atom pa, atom pb) return compare(peek(pa), peek(pb)) end function libc = open_dll("/usr/lib/libc.so") -- standard C library qid = routine_id("qcompare") qaddr = call_back(qid) qsort = define_c_proc(libc, "qsort", {C_POINTER, C_INT, C_INT, C_POINTER}) array = allocate(50) poke(array, rand(repeat(255, 50))) nitems = 50 item_size = 1 c_proc(qsort, {array, nitems, item_size, qaddr}) ?peek({array,50}) You can stick a trace(1) in qcompare() and see all the calls taking place. Regards, Rob Craig Rapid Deployment Software http://members.aol.com/FilesEu/