Created
January 22, 2012 13:58
-
-
Save Araq/1657152 to your computer and use it in GitHub Desktop.
More procs for dealing with CStringArray
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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