1. free_string()

As I was working with an include library that was calling various C
routines, I realized that Euphoria has no routine in machine.e to complement
allocate_string().

So, after a bit of work, I now submit free_string() to the list for anyone
who may need it. I played with several variations, and this was by far the
fastest one.

 --------
global function free_string(atom mem)
 -- retrieve a C-style null-terminated string from memory
    atom x
    sequence s

    x = mem
    while peek(x) do
        x += 1
    end while
    s = peek({mem, x - mem})
    free(mem)
    return s
end function
 --------


BTW, what I usually do with these little "additions" to the standard
Euphoria libraries is:

1) Create an include file with a the *same* name, but with a .g
   extension (because my name starts with a "G", see...)
      EXAMPLE: machine.e --> machine.g
2) Include the .e file in the .g file.
3) Then add my "additions" to the .g file. Now, instead of including
   the .e file in my programs, I can include the .g file.

Just in case that explanation flew over some people's heads, here's an
example of my machine.g include file:

 --------
include machine.e

global function free_string(atom mem)
 -- ...and so on
 --------

This way, my programs can simply include machine.g, to obtain all the
standard Euphoria machine.e routines in addition to my extra ones. A program
example:

 --------
include machine.g

atom addr
addr = allocate_string("This is a test")

sequence str
str = free_string(addr)
 -- str now contains "This is a test"
 --------

This is one instance where Euphoria's handling of include files works *very*
nicely. :)



Be seeing you,
   Gabriel Boehme

 ------
We only have what we give away.

Robert Fripp
 ------

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu