Skip to content

Instantly share code, notes, and snippets.

@singularperturbation
Last active August 29, 2015 14:07
Show Gist options
  • Save singularperturbation/b48c2c6234a17c188aaa to your computer and use it in GitHub Desktop.
Save singularperturbation/b48c2c6234a17c188aaa to your computer and use it in GitHub Desktop.
Nimrod using libmarkdown
import os
type
MKD_FLAG = cuint
PMMIOT = pointer
proc mkd_in(inputFile: TFile,
mflag: MKD_FLAG) :
PMMIOT {.importc: "mkd_in",
dynlib: "libmarkdown.so(.2|.2.1.3)".}
proc markdown(doc : PMMIOT,
output: TFile,
flag: MKD_FLAG) : cint
{.importc: "markdown",
dynlib: "libmarkdown.so(.2|.2.1.3)".}
proc cleanup(fileOpen: bool, fileVar: TFile) =
if fileOpen:
close(fileVar)
proc main() =
var
inFile: TFile = nil
fName: string = os.getCurrentDir() / "sample.md"
mkProcessed: PMMIOT
opened: bool
opened = open(inFile, fName)
if not opened:
echo("Could not open file.")
cleanup(opened, inFile)
return
mkProcessed = mkd_in(inFile,cuint(0))
if mkProcessed != nil:
discard markdown(mkProcessed,stdout,cuint(0))
cleanup(opened,inFile)
for i in 1..40:
main()
@singularperturbation
Copy link
Author

Reminder: don't use passL and dynlib together, nodecl is right out.

@singularperturbation
Copy link
Author

Put main in loop to test memory allocation

valgrind complains about extra frees:
total heap usage: 2,245 allocs, 2,240 frees

Edit: Nothing to worry about - dynlib library allocs currently not freed until program exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment