Close opened file handles from the vCard lexer.
This commit is contained in:
@@ -65,17 +65,27 @@ proc readVCard*(p: var VCardParser, validate = true): VCard =
|
|||||||
p.error(exc.msg)
|
p.error(exc.msg)
|
||||||
|
|
||||||
proc initVCardParser*(input: Stream, filename = "input"): VCardParser =
|
proc initVCardParser*(input: Stream, filename = "input"): VCardParser =
|
||||||
|
## Note: the returned VCardParser object will have an open file handle. Make
|
||||||
|
## sure to call `close` on the returned VCardParser to avoid file handle
|
||||||
|
## leaks.
|
||||||
result.filename = filename
|
result.filename = filename
|
||||||
lexer.open(result, input)
|
lexer.open(result, input)
|
||||||
|
|
||||||
proc initVCardParser*(content: string, filename = "input"): VCardParser =
|
proc initVCardParser*(content: string, filename = "input"): VCardParser =
|
||||||
|
## Note: the returned VCardParser object will have an open file handle. Make
|
||||||
|
## sure to call `close` on the returned VCardParser to avoid file handle
|
||||||
|
## leaks.
|
||||||
initVCardParser(newStringStream(content), filename)
|
initVCardParser(newStringStream(content), filename)
|
||||||
|
|
||||||
proc initVCardParserFromFile*(filepath: string): VCardParser =
|
proc initVCardParserFromFile*(filepath: string): VCardParser =
|
||||||
|
## Note: the returned VCardParser object will have an open file handle. Make
|
||||||
|
## sure to call `close` on the returned VCardParser to avoid file handle
|
||||||
|
## leaks.
|
||||||
initVCardParser(newFileStream(filepath, fmRead), filepath)
|
initVCardParser(newFileStream(filepath, fmRead), filepath)
|
||||||
|
|
||||||
proc parseVCards*(input: Stream, filename = "input", validate = true): seq[VCard] =
|
proc parseVCards*(input: Stream, filename = "input", validate = true): seq[VCard] =
|
||||||
var p = initVCardParser(input, filename)
|
var p = initVCardParser(input, filename)
|
||||||
|
defer: p.close()
|
||||||
while p.peek != '\0': result.add(p.readVCard(validate))
|
while p.peek != '\0': result.add(p.readVCard(validate))
|
||||||
|
|
||||||
proc parseVCards*(content: string, filename = "input", validate = true): seq[VCard] =
|
proc parseVCards*(content: string, filename = "input", validate = true): seq[VCard] =
|
||||||
|
|||||||
Reference in New Issue
Block a user