Skip to content

Instantly share code, notes, and snippets.

@Araq
Created January 22, 2012 13:58
Show Gist options
  • Save Araq/1657152 to your computer and use it in GitHub Desktop.
Save Araq/1657152 to your computer and use it in GitHub Desktop.
More procs for dealing with CStringArray
proc allocCStringArray*(a: openArray[string]): cstringArray =
## creates a NULL terminated cstringArray from `a`. The result has to
## be freed with `deallocCStringArray` after it's not needed anymore.
result = cast[cstringArray](alloc0((a.len+1) * sizeof(cstring)))
for i in 0 .. a.high:
# XXX get rid of this string copy here:
var x = a[i]
result[i] = cast[cstring](alloc0(x.len+1))
copyMem(result[i], addr(x[0]), x.len)
proc deallocCStringArray*(a: cstringArray) =
## frees a NULL terminated cstringArray.
var i = 0
while a[i] != nil:
dealloc(a[i])
inc(i)
dealloc(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment