more modules updated

This commit is contained in:
Araq
2014-08-28 01:39:45 +02:00
parent bae9a0ceac
commit 12d2a37519
6 changed files with 71 additions and 71 deletions

View File

@@ -16,29 +16,29 @@ type
{.deprecated: [TLibHandle: LibHandle].}
proc loadLib*(path: string, global_symbols=false): TLibHandle
proc loadLib*(path: string, global_symbols=false): LibHandle
## loads a library from `path`. Returns nil if the library could not
## be loaded.
proc loadLib*(): TLibHandle
proc loadLib*(): LibHandle
## gets the handle from the current executable. Returns nil if the
## library could not be loaded.
proc unloadLib*(lib: TLibHandle)
proc unloadLib*(lib: LibHandle)
## unloads the library `lib`
proc raiseInvalidLibrary*(name: cstring) {.noinline, noreturn.} =
## raises an `EInvalidLibrary` exception.
var e: ref EInvalidLibrary
var e: ref LibraryError
new(e)
e.msg = "could not find symbol: " & $name
raise e
proc symAddr*(lib: TLibHandle, name: cstring): pointer
proc symAddr*(lib: LibHandle, name: cstring): pointer
## retrieves the address of a procedure/variable from `lib`. Returns nil
## if the symbol could not be found.
proc checkedSymAddr*(lib: TLibHandle, name: cstring): pointer =
proc checkedSymAddr*(lib: LibHandle, name: cstring): pointer =
## retrieves the address of a procedure/variable from `lib`. Raises
## `EInvalidLibrary` if the symbol could not be found.
result = symAddr(lib, name)
@@ -87,13 +87,13 @@ elif defined(windows) or defined(dos):
proc getProcAddress(lib: THINSTANCE, name: cstring): pointer {.
importc: "GetProcAddress", header: "<windows.h>", stdcall.}
proc loadLib(path: string, global_symbols=false): TLibHandle =
result = cast[TLibHandle](winLoadLibrary(path))
proc loadLib(): TLibHandle =
result = cast[TLibHandle](winLoadLibrary(nil))
proc unloadLib(lib: TLibHandle) = FreeLibrary(cast[THINSTANCE](lib))
proc loadLib(path: string, global_symbols=false): LibHandle =
result = cast[LibHandle](winLoadLibrary(path))
proc loadLib(): LibHandle =
result = cast[LibHandle](winLoadLibrary(nil))
proc unloadLib(lib: LibHandle) = FreeLibrary(cast[THINSTANCE](lib))
proc symAddr(lib: TLibHandle, name: cstring): pointer =
proc symAddr(lib: LibHandle, name: cstring): pointer =
result = getProcAddress(cast[THINSTANCE](lib), name)
else:

View File

@@ -36,7 +36,7 @@ when defined(windows):
while i < a.len and j < b.len:
if a[i] in {'-', '_'}: inc i
if b[j] in {'-', '_'}: inc j
if a[i].tolower != b[j].tolower: return false
if a[i].toLower != b[j].toLower: return false
inc i
inc j
result = i == a.len and j == b.len
@@ -295,11 +295,11 @@ else:
proc getCurrentEncoding*(): string =
## retrieves the current encoding. On Unix, always "UTF-8" is returned.
when defined(windows):
result = codePageToName(GetACP())
result = codePageToName(getACP())
else:
result = "UTF-8"
proc open*(destEncoding = "UTF-8", srcEncoding = "CP1252"): PConverter =
proc open*(destEncoding = "UTF-8", srcEncoding = "CP1252"): EncodingConverter =
## opens a converter that can convert from `srcEncoding` to `destEncoding`.
## Raises `EIO` if it cannot fullfill the request.
when not defined(windows):
@@ -312,19 +312,19 @@ proc open*(destEncoding = "UTF-8", srcEncoding = "CP1252"): PConverter =
result.dest = nameToCodePage(destEncoding)
result.src = nameToCodePage(srcEncoding)
if int(result.dest) == -1:
raise newException(EInvalidEncoding,
raise newException(EncodingError,
"cannot find encoding " & destEncoding)
if int(result.src) == -1:
raise newException(EInvalidEncoding,
raise newException(EncodingError,
"cannot find encoding " & srcEncoding)
proc close*(c: PConverter) =
proc close*(c: EncodingConverter) =
## frees the resources the converter `c` holds.
when not defined(windows):
iconvClose(c)
when defined(windows):
proc convert*(c: PConverter, s: string): string =
proc convert*(c: EncodingConverter, s: string): string =
## converts `s` to `destEncoding` that was given to the converter `c`. It
## assumed that `s` is in `srcEncoding`.
@@ -354,7 +354,7 @@ when defined(windows):
cbMultiByte = cint(s.len),
lpWideCharStr = cstring(result),
cchWideChar = cint(cap))
if m == 0: osError(osLastError())
if m == 0: raiseOSError(osLastError())
setLen(result, m*2)
elif m <= cap:
setLen(result, m*2)
@@ -391,7 +391,7 @@ when defined(windows):
cchWideChar = cint(result.len div 2),
lpMultiByteStr = cstring(res),
cbMultiByte = cap.cint)
if m == 0: osError(osLastError())
if m == 0: raiseOSError(osLastError())
setLen(res, m)
result = res
elif m <= cap:

View File

@@ -46,37 +46,37 @@ type
{.deprecated: [TEventArgs: EventArgs, TEventHandler: EventHandler,
TEventEmitter: EventEmitter, EInvalidEvent: EventError].}
proc initEventHandler*(name: string): TEventHandler =
proc initEventHandler*(name: string): EventHandler =
## Initializes an EventHandler with the specified name and returns it.
result.handlers = @[]
result.name = name
proc addHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}) =
proc addHandler*(handler: var EventHandler, func: proc(e: EventArgs) {.closure.}) =
## Adds the callback to the specified event handler.
handler.handlers.add(func)
proc removeHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}) =
proc removeHandler*(handler: var EventHandler, func: proc(e: EventArgs) {.closure.}) =
## Removes the callback from the specified event handler.
for i in countup(0, len(handler.handlers) -1):
if func == handler.handlers[i]:
handler.handlers.del(i)
break
proc containsHandler*(handler: var TEventHandler, func: proc(e: TEventArgs) {.closure.}): bool =
proc containsHandler*(handler: var EventHandler, func: proc(e: EventArgs) {.closure.}): bool =
## Checks if a callback is registered to this event handler.
return handler.handlers.contains(func)
proc clearHandlers*(handler: var TEventHandler) =
proc clearHandlers*(handler: var EventHandler) =
## Clears all of the callbacks from the event handler.
setLen(handler.handlers, 0)
proc getEventHandler(emitter: var TEventEmitter, event: string): int =
proc getEventHandler(emitter: var EventEmitter, event: string): int =
for k in 0..high(emitter.s):
if emitter.s[k].name == event: return k
return -1
proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs) {.closure.}) =
proc on*(emitter: var EventEmitter, event: string, func: proc(e: EventArgs) {.closure.}) =
## Assigns a event handler with the specified callback. If the event
## doesn't exist, it will be created.
var i = getEventHandler(emitter, event)
@@ -87,17 +87,17 @@ proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs) {.
else:
addHandler(emitter.s[i], func)
proc emit*(emitter: var TEventEmitter, eventhandler: var TEventHandler,
args: TEventArgs) =
proc emit*(emitter: var EventEmitter, eventhandler: var EventHandler,
args: EventArgs) =
## Fires an event handler with specified event arguments.
for func in items(eventhandler.handlers): func(args)
proc emit*(emitter: var TEventEmitter, event: string, args: TEventArgs) =
proc emit*(emitter: var EventEmitter, event: string, args: EventArgs) =
## Fires an event handler with specified event arguments.
var i = getEventHandler(emitter, event)
if i >= 0:
emit(emitter, emitter.s[i], args)
proc initEventEmitter*(): TEventEmitter =
proc initEventEmitter*(): EventEmitter =
## Creates and returns a new EventEmitter.
result.s = @[]

View File

@@ -25,7 +25,7 @@ type
TGenKeyValuePair[T] = tuple[key: string, val: T]
TGenKeyValuePairSeq[T] = seq[TGenKeyValuePair[T]]
TGenTable*[T] = object of TObject
TGenTable*[T] = object of RootObj
counter: int
data: TGenKeyValuePairSeq[T]
mode: TGenTableMode
@@ -78,7 +78,7 @@ proc RawGet[T](tbl: PGenTable[T], key: string): int =
var h: THash
h = myhash(tbl, key) and high(tbl.data) # start with real hash value
while not isNil(tbl.data[h].key):
if mycmp(tbl, tbl.data[h].key, key):
if myCmp(tbl, tbl.data[h].key, key):
return h
h = nextTry(h, high(tbl.data))
result = - 1

View File

@@ -98,11 +98,11 @@ type
filename: string
options: set[XmlParseOption]
{.deprecated: [TXmlParser: XmlParser, TXmlParseOptions: XmlParseOptions,
{.deprecated: [TXmlParser: XmlParser, TXmlParseOptions: XmlParseOption,
TXmlError: XmlErrorKind, TXmlEventKind: XmlEventKind].}
const
errorMessages: array[XmlError, string] = [
errorMessages: array[XmlErrorKind, string] = [
"no error",
"']]>' expected",
"name expected",
@@ -235,11 +235,11 @@ proc parseCDATA(my: var TXMLParser) =
markError(my, errEndOfCDataExpected)
break
of '\c':
pos = lexbase.HandleCR(my, pos)
pos = lexbase.handleCR(my, pos)
buf = my.buf
add(my.a, '\L')
of '\L':
pos = lexbase.HandleLF(my, pos)
pos = lexbase.handleLF(my, pos)
buf = my.buf
add(my.a, '\L')
else:
@@ -263,11 +263,11 @@ proc parseComment(my: var TXMLParser) =
markError(my, errEndOfCommentExpected)
break
of '\c':
pos = lexbase.HandleCR(my, pos)
pos = lexbase.handleCR(my, pos)
buf = my.buf
if my.options.contains(reportComments): add(my.a, '\L')
of '\L':
pos = lexbase.HandleLF(my, pos)
pos = lexbase.handleLF(my, pos)
buf = my.buf
if my.options.contains(reportComments): add(my.a, '\L')
else:
@@ -286,11 +286,11 @@ proc parseWhitespace(my: var TXmlParser, skip=False) =
Inc(pos)
of '\c':
# the specification says that CR-LF, CR are to be transformed to LF
pos = lexbase.HandleCR(my, pos)
pos = lexbase.handleCR(my, pos)
buf = my.buf
if not skip: add(my.a, '\L')
of '\L':
pos = lexbase.HandleLF(my, pos)
pos = lexbase.handleLF(my, pos)
buf = my.buf
if not skip: add(my.a, '\L')
else:

View File

@@ -12,7 +12,7 @@
import macros, strtabs
type
XmlNode* = ref TXmlNode ## an XML tree consists of ``PXmlNode``'s.
XmlNode* = ref XmlNodeObj ## an XML tree consists of ``PXmlNode``'s.
XmlNodeKind* = enum ## different kinds of ``PXmlNode``'s
xnText, ## a text element
@@ -48,43 +48,43 @@ proc newElement*(tag: string): XmlNode =
result.s = @[]
# init attributes lazily to safe memory
proc newText*(text: string): PXmlNode =
proc newText*(text: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnText`` with the text `text`.
result = newXmlNode(xnText)
result.fText = text
proc newComment*(comment: string): PXmlNode =
proc newComment*(comment: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnComment`` with the text `comment`.
result = newXmlNode(xnComment)
result.fText = comment
proc newCData*(cdata: string): PXmlNode =
proc newCData*(cdata: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnComment`` with the text `cdata`.
result = newXmlNode(xnCData)
result.fText = cdata
proc newEntity*(entity: string): PXmlNode =
proc newEntity*(entity: string): XmlNode =
## creates a new ``PXmlNode`` of kind ``xnEntity`` with the text `entity`.
result = newXmlNode(xnCData)
result.fText = entity
proc text*(n: PXmlNode): string {.inline.} =
proc text*(n: XmlNode): string {.inline.} =
## gets the associated text with the node `n`. `n` can be a CDATA, Text,
## comment, or entity node.
assert n.k in {xnText, xnComment, xnCData, xnEntity}
result = n.fText
proc rawText*(n: PXmlNode): string {.inline.} =
proc rawText*(n: XmlNode): string {.inline.} =
## returns the underlying 'text' string by reference.
## This is only used for speed hacks.
shallowCopy(result, n.fText)
proc rawTag*(n: PXmlNode): string {.inline.} =
proc rawTag*(n: XmlNode): string {.inline.} =
## returns the underlying 'tag' string by reference.
## This is only used for speed hacks.
shallowCopy(result, n.fTag)
proc innerText*(n: PXmlNode): string =
proc innerText*(n: XmlNode): string =
## gets the inner text of `n`. `n` has to be an ``xnElement`` node. Only
## ``xnText`` and ``xnEntity`` nodes are considered part of `n`'s inner text,
## other child nodes are silently ignored.
@@ -93,55 +93,55 @@ proc innerText*(n: PXmlNode): string =
for i in 0 .. n.s.len-1:
if n.s[i].k in {xnText, xnEntity}: result.add(n.s[i].fText)
proc tag*(n: PXmlNode): string {.inline.} =
proc tag*(n: XmlNode): string {.inline.} =
## gets the tag name of `n`. `n` has to be an ``xnElement`` node.
assert n.k == xnElement
result = n.fTag
proc add*(father, son: PXmlNode) {.inline.} =
proc add*(father, son: XmlNode) {.inline.} =
## adds the child `son` to `father`.
add(father.s, son)
proc len*(n: PXmlNode): int {.inline.} =
proc len*(n: XmlNode): int {.inline.} =
## returns the number `n`'s children.
if n.k == xnElement: result = len(n.s)
proc kind*(n: PXmlNode): TXmlNodeKind {.inline.} =
proc kind*(n: XmlNode): XmlNodeKind {.inline.} =
## returns `n`'s kind.
result = n.k
proc `[]`* (n: PXmlNode, i: int): PXmlNode {.inline.} =
proc `[]`* (n: XmlNode, i: int): XmlNode {.inline.} =
## returns the `i`'th child of `n`.
assert n.k == xnElement
result = n.s[i]
iterator items*(n: PXmlNode): PXmlNode {.inline.} =
iterator items*(n: XmlNode): XmlNode {.inline.} =
## iterates over any child of `n`.
assert n.k == xnElement
for i in 0 .. n.len-1: yield n[i]
proc attrs*(n: PXmlNode): PXmlAttributes {.inline.} =
proc attrs*(n: XmlNode): XmlAttributes {.inline.} =
## gets the attributes belonging to `n`.
## Returns `nil` if attributes have not been initialised for this node.
assert n.k == xnElement
result = n.fAttr
proc `attrs=`*(n: PXmlNode, attr: PXmlAttributes) {.inline.} =
proc `attrs=`*(n: XmlNode, attr: XmlAttributes) {.inline.} =
## sets the attributes belonging to `n`.
assert n.k == xnElement
n.fAttr = attr
proc attrsLen*(n: PXmlNode): int {.inline.} =
proc attrsLen*(n: XmlNode): int {.inline.} =
## returns the number of `n`'s attributes.
assert n.k == xnElement
if not isNil(n.fAttr): result = len(n.fAttr)
proc clientData*(n: PXmlNode): int {.inline.} =
proc clientData*(n: XmlNode): int {.inline.} =
## gets the client data of `n`. The client data field is used by the HTML
## parser and generator.
result = n.fClientData
proc `clientData=`*(n: PXmlNode, data: int) {.inline.} =
proc `clientData=`*(n: XmlNode, data: int) {.inline.} =
## sets the client data of `n`. The client data field is used by the HTML
## parser and generator.
n.fClientData = data
@@ -175,13 +175,13 @@ proc addIndent(result: var string, indent: int) =
result.add("\n")
for i in 1..indent: result.add(' ')
proc noWhitespace(n: PXmlNode): bool =
proc noWhitespace(n: XmlNode): bool =
#for i in 1..n.len-1:
# if n[i].kind != n[0].kind: return true
for i in 0..n.len-1:
if n[i].kind in {xnText, xnEntity}: return true
proc add*(result: var string, n: PXmlNode, indent = 0, indWidth = 2) =
proc add*(result: var string, n: XmlNode, indent = 0, indWidth = 2) =
## adds the textual representation of `n` to `result`.
if n == nil: return
case n.k
@@ -234,14 +234,14 @@ const
xmlHeader* = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
## header to use for complete XML output
proc `$`*(n: PXmlNode): string =
proc `$`*(n: XmlNode): string =
## converts `n` into its string representation. No ``<$xml ...$>`` declaration
## is produced, so that the produced XML fragments are composable.
result = ""
result.add(n)
proc newXmlTree*(tag: string, children: openArray[PXmlNode],
attributes: PXmlAttributes = nil): PXmlNode =
proc newXmlTree*(tag: string, children: openArray[XmlNode],
attributes: XmlAttributes = nil): XmlNode =
## creates a new XML tree with `tag`, `children` and `attributes`
result = newXmlNode(xnElement)
result.fTag = tag
@@ -285,7 +285,7 @@ macro `<>`*(x: expr): expr {.immediate.} =
let x = callsite()
result = xmlConstructor(x)
proc child*(n: PXmlNode, name: string): PXmlNode =
proc child*(n: XmlNode, name: string): XmlNode =
## Finds the first child element of `n` with a name of `name`.
## Returns `nil` on failure.
assert n.kind == xnElement
@@ -294,14 +294,14 @@ proc child*(n: PXmlNode, name: string): PXmlNode =
if i.tag == name:
return i
proc attr*(n: PXmlNode, name: string): string =
proc attr*(n: XmlNode, name: string): string =
## Finds the first attribute of `n` with a name of `name`.
## Returns "" on failure.
assert n.kind == xnElement
if n.attrs == nil: return ""
return n.attrs[name]
proc findAll*(n: PXmlNode, tag: string, result: var seq[PXmlNode]) =
proc findAll*(n: XmlNode, tag: string, result: var seq[XmlNode]) =
## Iterates over all the children of `n` returning those matching `tag`.
##
## Found nodes satisfying the condition will be appended to the `result`
@@ -326,7 +326,7 @@ proc findAll*(n: PXmlNode, tag: string, result: var seq[PXmlNode]) =
elif child.k == xnElement:
child.findAll(tag, result)
proc findAll*(n: PXmlNode, tag: string): seq[PXmlNode] =
proc findAll*(n: XmlNode, tag: string): seq[XmlNode] =
## Shortcut version to assign in let blocks. Example:
##
## .. code-block::