more modules updated
This commit is contained in:
parent
bae9a0ceac
commit
12d2a37519
6 changed files with 71 additions and 71 deletions
|
|
@ -16,29 +16,29 @@ type
|
||||||
|
|
||||||
{.deprecated: [TLibHandle: LibHandle].}
|
{.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
|
## loads a library from `path`. Returns nil if the library could not
|
||||||
## be loaded.
|
## be loaded.
|
||||||
|
|
||||||
proc loadLib*(): TLibHandle
|
proc loadLib*(): LibHandle
|
||||||
## gets the handle from the current executable. Returns nil if the
|
## gets the handle from the current executable. Returns nil if the
|
||||||
## library could not be loaded.
|
## library could not be loaded.
|
||||||
|
|
||||||
proc unloadLib*(lib: TLibHandle)
|
proc unloadLib*(lib: LibHandle)
|
||||||
## unloads the library `lib`
|
## unloads the library `lib`
|
||||||
|
|
||||||
proc raiseInvalidLibrary*(name: cstring) {.noinline, noreturn.} =
|
proc raiseInvalidLibrary*(name: cstring) {.noinline, noreturn.} =
|
||||||
## raises an `EInvalidLibrary` exception.
|
## raises an `EInvalidLibrary` exception.
|
||||||
var e: ref EInvalidLibrary
|
var e: ref LibraryError
|
||||||
new(e)
|
new(e)
|
||||||
e.msg = "could not find symbol: " & $name
|
e.msg = "could not find symbol: " & $name
|
||||||
raise e
|
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
|
## retrieves the address of a procedure/variable from `lib`. Returns nil
|
||||||
## if the symbol could not be found.
|
## 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
|
## retrieves the address of a procedure/variable from `lib`. Raises
|
||||||
## `EInvalidLibrary` if the symbol could not be found.
|
## `EInvalidLibrary` if the symbol could not be found.
|
||||||
result = symAddr(lib, name)
|
result = symAddr(lib, name)
|
||||||
|
|
@ -87,13 +87,13 @@ elif defined(windows) or defined(dos):
|
||||||
proc getProcAddress(lib: THINSTANCE, name: cstring): pointer {.
|
proc getProcAddress(lib: THINSTANCE, name: cstring): pointer {.
|
||||||
importc: "GetProcAddress", header: "<windows.h>", stdcall.}
|
importc: "GetProcAddress", header: "<windows.h>", stdcall.}
|
||||||
|
|
||||||
proc loadLib(path: string, global_symbols=false): TLibHandle =
|
proc loadLib(path: string, global_symbols=false): LibHandle =
|
||||||
result = cast[TLibHandle](winLoadLibrary(path))
|
result = cast[LibHandle](winLoadLibrary(path))
|
||||||
proc loadLib(): TLibHandle =
|
proc loadLib(): LibHandle =
|
||||||
result = cast[TLibHandle](winLoadLibrary(nil))
|
result = cast[LibHandle](winLoadLibrary(nil))
|
||||||
proc unloadLib(lib: TLibHandle) = FreeLibrary(cast[THINSTANCE](lib))
|
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)
|
result = getProcAddress(cast[THINSTANCE](lib), name)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ when defined(windows):
|
||||||
while i < a.len and j < b.len:
|
while i < a.len and j < b.len:
|
||||||
if a[i] in {'-', '_'}: inc i
|
if a[i] in {'-', '_'}: inc i
|
||||||
if b[j] in {'-', '_'}: inc j
|
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 i
|
||||||
inc j
|
inc j
|
||||||
result = i == a.len and j == b.len
|
result = i == a.len and j == b.len
|
||||||
|
|
@ -295,11 +295,11 @@ else:
|
||||||
proc getCurrentEncoding*(): string =
|
proc getCurrentEncoding*(): string =
|
||||||
## retrieves the current encoding. On Unix, always "UTF-8" is returned.
|
## retrieves the current encoding. On Unix, always "UTF-8" is returned.
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
result = codePageToName(GetACP())
|
result = codePageToName(getACP())
|
||||||
else:
|
else:
|
||||||
result = "UTF-8"
|
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`.
|
## opens a converter that can convert from `srcEncoding` to `destEncoding`.
|
||||||
## Raises `EIO` if it cannot fullfill the request.
|
## Raises `EIO` if it cannot fullfill the request.
|
||||||
when not defined(windows):
|
when not defined(windows):
|
||||||
|
|
@ -312,19 +312,19 @@ proc open*(destEncoding = "UTF-8", srcEncoding = "CP1252"): PConverter =
|
||||||
result.dest = nameToCodePage(destEncoding)
|
result.dest = nameToCodePage(destEncoding)
|
||||||
result.src = nameToCodePage(srcEncoding)
|
result.src = nameToCodePage(srcEncoding)
|
||||||
if int(result.dest) == -1:
|
if int(result.dest) == -1:
|
||||||
raise newException(EInvalidEncoding,
|
raise newException(EncodingError,
|
||||||
"cannot find encoding " & destEncoding)
|
"cannot find encoding " & destEncoding)
|
||||||
if int(result.src) == -1:
|
if int(result.src) == -1:
|
||||||
raise newException(EInvalidEncoding,
|
raise newException(EncodingError,
|
||||||
"cannot find encoding " & srcEncoding)
|
"cannot find encoding " & srcEncoding)
|
||||||
|
|
||||||
proc close*(c: PConverter) =
|
proc close*(c: EncodingConverter) =
|
||||||
## frees the resources the converter `c` holds.
|
## frees the resources the converter `c` holds.
|
||||||
when not defined(windows):
|
when not defined(windows):
|
||||||
iconvClose(c)
|
iconvClose(c)
|
||||||
|
|
||||||
when defined(windows):
|
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
|
## converts `s` to `destEncoding` that was given to the converter `c`. It
|
||||||
## assumed that `s` is in `srcEncoding`.
|
## assumed that `s` is in `srcEncoding`.
|
||||||
|
|
||||||
|
|
@ -354,7 +354,7 @@ when defined(windows):
|
||||||
cbMultiByte = cint(s.len),
|
cbMultiByte = cint(s.len),
|
||||||
lpWideCharStr = cstring(result),
|
lpWideCharStr = cstring(result),
|
||||||
cchWideChar = cint(cap))
|
cchWideChar = cint(cap))
|
||||||
if m == 0: osError(osLastError())
|
if m == 0: raiseOSError(osLastError())
|
||||||
setLen(result, m*2)
|
setLen(result, m*2)
|
||||||
elif m <= cap:
|
elif m <= cap:
|
||||||
setLen(result, m*2)
|
setLen(result, m*2)
|
||||||
|
|
@ -391,7 +391,7 @@ when defined(windows):
|
||||||
cchWideChar = cint(result.len div 2),
|
cchWideChar = cint(result.len div 2),
|
||||||
lpMultiByteStr = cstring(res),
|
lpMultiByteStr = cstring(res),
|
||||||
cbMultiByte = cap.cint)
|
cbMultiByte = cap.cint)
|
||||||
if m == 0: osError(osLastError())
|
if m == 0: raiseOSError(osLastError())
|
||||||
setLen(res, m)
|
setLen(res, m)
|
||||||
result = res
|
result = res
|
||||||
elif m <= cap:
|
elif m <= cap:
|
||||||
|
|
|
||||||
|
|
@ -46,37 +46,37 @@ type
|
||||||
{.deprecated: [TEventArgs: EventArgs, TEventHandler: EventHandler,
|
{.deprecated: [TEventArgs: EventArgs, TEventHandler: EventHandler,
|
||||||
TEventEmitter: EventEmitter, EInvalidEvent: EventError].}
|
TEventEmitter: EventEmitter, EInvalidEvent: EventError].}
|
||||||
|
|
||||||
proc initEventHandler*(name: string): TEventHandler =
|
proc initEventHandler*(name: string): EventHandler =
|
||||||
## Initializes an EventHandler with the specified name and returns it.
|
## Initializes an EventHandler with the specified name and returns it.
|
||||||
result.handlers = @[]
|
result.handlers = @[]
|
||||||
result.name = name
|
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.
|
## Adds the callback to the specified event handler.
|
||||||
handler.handlers.add(func)
|
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.
|
## Removes the callback from the specified event handler.
|
||||||
for i in countup(0, len(handler.handlers) -1):
|
for i in countup(0, len(handler.handlers) -1):
|
||||||
if func == handler.handlers[i]:
|
if func == handler.handlers[i]:
|
||||||
handler.handlers.del(i)
|
handler.handlers.del(i)
|
||||||
break
|
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.
|
## Checks if a callback is registered to this event handler.
|
||||||
return handler.handlers.contains(func)
|
return handler.handlers.contains(func)
|
||||||
|
|
||||||
|
|
||||||
proc clearHandlers*(handler: var TEventHandler) =
|
proc clearHandlers*(handler: var EventHandler) =
|
||||||
## Clears all of the callbacks from the event handler.
|
## Clears all of the callbacks from the event handler.
|
||||||
setLen(handler.handlers, 0)
|
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):
|
for k in 0..high(emitter.s):
|
||||||
if emitter.s[k].name == event: return k
|
if emitter.s[k].name == event: return k
|
||||||
return -1
|
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
|
## Assigns a event handler with the specified callback. If the event
|
||||||
## doesn't exist, it will be created.
|
## doesn't exist, it will be created.
|
||||||
var i = getEventHandler(emitter, event)
|
var i = getEventHandler(emitter, event)
|
||||||
|
|
@ -87,17 +87,17 @@ proc on*(emitter: var TEventEmitter, event: string, func: proc(e: TEventArgs) {.
|
||||||
else:
|
else:
|
||||||
addHandler(emitter.s[i], func)
|
addHandler(emitter.s[i], func)
|
||||||
|
|
||||||
proc emit*(emitter: var TEventEmitter, eventhandler: var TEventHandler,
|
proc emit*(emitter: var EventEmitter, eventhandler: var EventHandler,
|
||||||
args: TEventArgs) =
|
args: EventArgs) =
|
||||||
## Fires an event handler with specified event arguments.
|
## Fires an event handler with specified event arguments.
|
||||||
for func in items(eventhandler.handlers): func(args)
|
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.
|
## Fires an event handler with specified event arguments.
|
||||||
var i = getEventHandler(emitter, event)
|
var i = getEventHandler(emitter, event)
|
||||||
if i >= 0:
|
if i >= 0:
|
||||||
emit(emitter, emitter.s[i], args)
|
emit(emitter, emitter.s[i], args)
|
||||||
|
|
||||||
proc initEventEmitter*(): TEventEmitter =
|
proc initEventEmitter*(): EventEmitter =
|
||||||
## Creates and returns a new EventEmitter.
|
## Creates and returns a new EventEmitter.
|
||||||
result.s = @[]
|
result.s = @[]
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ type
|
||||||
|
|
||||||
TGenKeyValuePair[T] = tuple[key: string, val: T]
|
TGenKeyValuePair[T] = tuple[key: string, val: T]
|
||||||
TGenKeyValuePairSeq[T] = seq[TGenKeyValuePair[T]]
|
TGenKeyValuePairSeq[T] = seq[TGenKeyValuePair[T]]
|
||||||
TGenTable*[T] = object of TObject
|
TGenTable*[T] = object of RootObj
|
||||||
counter: int
|
counter: int
|
||||||
data: TGenKeyValuePairSeq[T]
|
data: TGenKeyValuePairSeq[T]
|
||||||
mode: TGenTableMode
|
mode: TGenTableMode
|
||||||
|
|
@ -78,7 +78,7 @@ proc RawGet[T](tbl: PGenTable[T], key: string): int =
|
||||||
var h: THash
|
var h: THash
|
||||||
h = myhash(tbl, key) and high(tbl.data) # start with real hash value
|
h = myhash(tbl, key) and high(tbl.data) # start with real hash value
|
||||||
while not isNil(tbl.data[h].key):
|
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
|
return h
|
||||||
h = nextTry(h, high(tbl.data))
|
h = nextTry(h, high(tbl.data))
|
||||||
result = - 1
|
result = - 1
|
||||||
|
|
|
||||||
|
|
@ -98,11 +98,11 @@ type
|
||||||
filename: string
|
filename: string
|
||||||
options: set[XmlParseOption]
|
options: set[XmlParseOption]
|
||||||
|
|
||||||
{.deprecated: [TXmlParser: XmlParser, TXmlParseOptions: XmlParseOptions,
|
{.deprecated: [TXmlParser: XmlParser, TXmlParseOptions: XmlParseOption,
|
||||||
TXmlError: XmlErrorKind, TXmlEventKind: XmlEventKind].}
|
TXmlError: XmlErrorKind, TXmlEventKind: XmlEventKind].}
|
||||||
|
|
||||||
const
|
const
|
||||||
errorMessages: array[XmlError, string] = [
|
errorMessages: array[XmlErrorKind, string] = [
|
||||||
"no error",
|
"no error",
|
||||||
"']]>' expected",
|
"']]>' expected",
|
||||||
"name expected",
|
"name expected",
|
||||||
|
|
@ -235,11 +235,11 @@ proc parseCDATA(my: var TXMLParser) =
|
||||||
markError(my, errEndOfCDataExpected)
|
markError(my, errEndOfCDataExpected)
|
||||||
break
|
break
|
||||||
of '\c':
|
of '\c':
|
||||||
pos = lexbase.HandleCR(my, pos)
|
pos = lexbase.handleCR(my, pos)
|
||||||
buf = my.buf
|
buf = my.buf
|
||||||
add(my.a, '\L')
|
add(my.a, '\L')
|
||||||
of '\L':
|
of '\L':
|
||||||
pos = lexbase.HandleLF(my, pos)
|
pos = lexbase.handleLF(my, pos)
|
||||||
buf = my.buf
|
buf = my.buf
|
||||||
add(my.a, '\L')
|
add(my.a, '\L')
|
||||||
else:
|
else:
|
||||||
|
|
@ -263,11 +263,11 @@ proc parseComment(my: var TXMLParser) =
|
||||||
markError(my, errEndOfCommentExpected)
|
markError(my, errEndOfCommentExpected)
|
||||||
break
|
break
|
||||||
of '\c':
|
of '\c':
|
||||||
pos = lexbase.HandleCR(my, pos)
|
pos = lexbase.handleCR(my, pos)
|
||||||
buf = my.buf
|
buf = my.buf
|
||||||
if my.options.contains(reportComments): add(my.a, '\L')
|
if my.options.contains(reportComments): add(my.a, '\L')
|
||||||
of '\L':
|
of '\L':
|
||||||
pos = lexbase.HandleLF(my, pos)
|
pos = lexbase.handleLF(my, pos)
|
||||||
buf = my.buf
|
buf = my.buf
|
||||||
if my.options.contains(reportComments): add(my.a, '\L')
|
if my.options.contains(reportComments): add(my.a, '\L')
|
||||||
else:
|
else:
|
||||||
|
|
@ -286,11 +286,11 @@ proc parseWhitespace(my: var TXmlParser, skip=False) =
|
||||||
Inc(pos)
|
Inc(pos)
|
||||||
of '\c':
|
of '\c':
|
||||||
# the specification says that CR-LF, CR are to be transformed to LF
|
# 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
|
buf = my.buf
|
||||||
if not skip: add(my.a, '\L')
|
if not skip: add(my.a, '\L')
|
||||||
of '\L':
|
of '\L':
|
||||||
pos = lexbase.HandleLF(my, pos)
|
pos = lexbase.handleLF(my, pos)
|
||||||
buf = my.buf
|
buf = my.buf
|
||||||
if not skip: add(my.a, '\L')
|
if not skip: add(my.a, '\L')
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
import macros, strtabs
|
import macros, strtabs
|
||||||
|
|
||||||
type
|
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
|
XmlNodeKind* = enum ## different kinds of ``PXmlNode``'s
|
||||||
xnText, ## a text element
|
xnText, ## a text element
|
||||||
|
|
@ -48,43 +48,43 @@ proc newElement*(tag: string): XmlNode =
|
||||||
result.s = @[]
|
result.s = @[]
|
||||||
# init attributes lazily to safe memory
|
# 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`.
|
## creates a new ``PXmlNode`` of kind ``xnText`` with the text `text`.
|
||||||
result = newXmlNode(xnText)
|
result = newXmlNode(xnText)
|
||||||
result.fText = text
|
result.fText = text
|
||||||
|
|
||||||
proc newComment*(comment: string): PXmlNode =
|
proc newComment*(comment: string): XmlNode =
|
||||||
## creates a new ``PXmlNode`` of kind ``xnComment`` with the text `comment`.
|
## creates a new ``PXmlNode`` of kind ``xnComment`` with the text `comment`.
|
||||||
result = newXmlNode(xnComment)
|
result = newXmlNode(xnComment)
|
||||||
result.fText = comment
|
result.fText = comment
|
||||||
|
|
||||||
proc newCData*(cdata: string): PXmlNode =
|
proc newCData*(cdata: string): XmlNode =
|
||||||
## creates a new ``PXmlNode`` of kind ``xnComment`` with the text `cdata`.
|
## creates a new ``PXmlNode`` of kind ``xnComment`` with the text `cdata`.
|
||||||
result = newXmlNode(xnCData)
|
result = newXmlNode(xnCData)
|
||||||
result.fText = cdata
|
result.fText = cdata
|
||||||
|
|
||||||
proc newEntity*(entity: string): PXmlNode =
|
proc newEntity*(entity: string): XmlNode =
|
||||||
## creates a new ``PXmlNode`` of kind ``xnEntity`` with the text `entity`.
|
## creates a new ``PXmlNode`` of kind ``xnEntity`` with the text `entity`.
|
||||||
result = newXmlNode(xnCData)
|
result = newXmlNode(xnCData)
|
||||||
result.fText = entity
|
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,
|
## gets the associated text with the node `n`. `n` can be a CDATA, Text,
|
||||||
## comment, or entity node.
|
## comment, or entity node.
|
||||||
assert n.k in {xnText, xnComment, xnCData, xnEntity}
|
assert n.k in {xnText, xnComment, xnCData, xnEntity}
|
||||||
result = n.fText
|
result = n.fText
|
||||||
|
|
||||||
proc rawText*(n: PXmlNode): string {.inline.} =
|
proc rawText*(n: XmlNode): string {.inline.} =
|
||||||
## returns the underlying 'text' string by reference.
|
## returns the underlying 'text' string by reference.
|
||||||
## This is only used for speed hacks.
|
## This is only used for speed hacks.
|
||||||
shallowCopy(result, n.fText)
|
shallowCopy(result, n.fText)
|
||||||
|
|
||||||
proc rawTag*(n: PXmlNode): string {.inline.} =
|
proc rawTag*(n: XmlNode): string {.inline.} =
|
||||||
## returns the underlying 'tag' string by reference.
|
## returns the underlying 'tag' string by reference.
|
||||||
## This is only used for speed hacks.
|
## This is only used for speed hacks.
|
||||||
shallowCopy(result, n.fTag)
|
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
|
## 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,
|
## ``xnText`` and ``xnEntity`` nodes are considered part of `n`'s inner text,
|
||||||
## other child nodes are silently ignored.
|
## other child nodes are silently ignored.
|
||||||
|
|
@ -93,55 +93,55 @@ proc innerText*(n: PXmlNode): string =
|
||||||
for i in 0 .. n.s.len-1:
|
for i in 0 .. n.s.len-1:
|
||||||
if n.s[i].k in {xnText, xnEntity}: result.add(n.s[i].fText)
|
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.
|
## gets the tag name of `n`. `n` has to be an ``xnElement`` node.
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
result = n.fTag
|
result = n.fTag
|
||||||
|
|
||||||
proc add*(father, son: PXmlNode) {.inline.} =
|
proc add*(father, son: XmlNode) {.inline.} =
|
||||||
## adds the child `son` to `father`.
|
## adds the child `son` to `father`.
|
||||||
add(father.s, son)
|
add(father.s, son)
|
||||||
|
|
||||||
proc len*(n: PXmlNode): int {.inline.} =
|
proc len*(n: XmlNode): int {.inline.} =
|
||||||
## returns the number `n`'s children.
|
## returns the number `n`'s children.
|
||||||
if n.k == xnElement: result = len(n.s)
|
if n.k == xnElement: result = len(n.s)
|
||||||
|
|
||||||
proc kind*(n: PXmlNode): TXmlNodeKind {.inline.} =
|
proc kind*(n: XmlNode): XmlNodeKind {.inline.} =
|
||||||
## returns `n`'s kind.
|
## returns `n`'s kind.
|
||||||
result = n.k
|
result = n.k
|
||||||
|
|
||||||
proc `[]`* (n: PXmlNode, i: int): PXmlNode {.inline.} =
|
proc `[]`* (n: XmlNode, i: int): XmlNode {.inline.} =
|
||||||
## returns the `i`'th child of `n`.
|
## returns the `i`'th child of `n`.
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
result = n.s[i]
|
result = n.s[i]
|
||||||
|
|
||||||
iterator items*(n: PXmlNode): PXmlNode {.inline.} =
|
iterator items*(n: XmlNode): XmlNode {.inline.} =
|
||||||
## iterates over any child of `n`.
|
## iterates over any child of `n`.
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
for i in 0 .. n.len-1: yield n[i]
|
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`.
|
## gets the attributes belonging to `n`.
|
||||||
## Returns `nil` if attributes have not been initialised for this node.
|
## Returns `nil` if attributes have not been initialised for this node.
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
result = n.fAttr
|
result = n.fAttr
|
||||||
|
|
||||||
proc `attrs=`*(n: PXmlNode, attr: PXmlAttributes) {.inline.} =
|
proc `attrs=`*(n: XmlNode, attr: XmlAttributes) {.inline.} =
|
||||||
## sets the attributes belonging to `n`.
|
## sets the attributes belonging to `n`.
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
n.fAttr = attr
|
n.fAttr = attr
|
||||||
|
|
||||||
proc attrsLen*(n: PXmlNode): int {.inline.} =
|
proc attrsLen*(n: XmlNode): int {.inline.} =
|
||||||
## returns the number of `n`'s attributes.
|
## returns the number of `n`'s attributes.
|
||||||
assert n.k == xnElement
|
assert n.k == xnElement
|
||||||
if not isNil(n.fAttr): result = len(n.fAttr)
|
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
|
## gets the client data of `n`. The client data field is used by the HTML
|
||||||
## parser and generator.
|
## parser and generator.
|
||||||
result = n.fClientData
|
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
|
## sets the client data of `n`. The client data field is used by the HTML
|
||||||
## parser and generator.
|
## parser and generator.
|
||||||
n.fClientData = data
|
n.fClientData = data
|
||||||
|
|
@ -175,13 +175,13 @@ proc addIndent(result: var string, indent: int) =
|
||||||
result.add("\n")
|
result.add("\n")
|
||||||
for i in 1..indent: result.add(' ')
|
for i in 1..indent: result.add(' ')
|
||||||
|
|
||||||
proc noWhitespace(n: PXmlNode): bool =
|
proc noWhitespace(n: XmlNode): bool =
|
||||||
#for i in 1..n.len-1:
|
#for i in 1..n.len-1:
|
||||||
# if n[i].kind != n[0].kind: return true
|
# if n[i].kind != n[0].kind: return true
|
||||||
for i in 0..n.len-1:
|
for i in 0..n.len-1:
|
||||||
if n[i].kind in {xnText, xnEntity}: return true
|
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`.
|
## adds the textual representation of `n` to `result`.
|
||||||
if n == nil: return
|
if n == nil: return
|
||||||
case n.k
|
case n.k
|
||||||
|
|
@ -234,14 +234,14 @@ const
|
||||||
xmlHeader* = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
|
xmlHeader* = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
|
||||||
## header to use for complete XML output
|
## header to use for complete XML output
|
||||||
|
|
||||||
proc `$`*(n: PXmlNode): string =
|
proc `$`*(n: XmlNode): string =
|
||||||
## converts `n` into its string representation. No ``<$xml ...$>`` declaration
|
## converts `n` into its string representation. No ``<$xml ...$>`` declaration
|
||||||
## is produced, so that the produced XML fragments are composable.
|
## is produced, so that the produced XML fragments are composable.
|
||||||
result = ""
|
result = ""
|
||||||
result.add(n)
|
result.add(n)
|
||||||
|
|
||||||
proc newXmlTree*(tag: string, children: openArray[PXmlNode],
|
proc newXmlTree*(tag: string, children: openArray[XmlNode],
|
||||||
attributes: PXmlAttributes = nil): PXmlNode =
|
attributes: XmlAttributes = nil): XmlNode =
|
||||||
## creates a new XML tree with `tag`, `children` and `attributes`
|
## creates a new XML tree with `tag`, `children` and `attributes`
|
||||||
result = newXmlNode(xnElement)
|
result = newXmlNode(xnElement)
|
||||||
result.fTag = tag
|
result.fTag = tag
|
||||||
|
|
@ -285,7 +285,7 @@ macro `<>`*(x: expr): expr {.immediate.} =
|
||||||
let x = callsite()
|
let x = callsite()
|
||||||
result = xmlConstructor(x)
|
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`.
|
## Finds the first child element of `n` with a name of `name`.
|
||||||
## Returns `nil` on failure.
|
## Returns `nil` on failure.
|
||||||
assert n.kind == xnElement
|
assert n.kind == xnElement
|
||||||
|
|
@ -294,14 +294,14 @@ proc child*(n: PXmlNode, name: string): PXmlNode =
|
||||||
if i.tag == name:
|
if i.tag == name:
|
||||||
return i
|
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`.
|
## Finds the first attribute of `n` with a name of `name`.
|
||||||
## Returns "" on failure.
|
## Returns "" on failure.
|
||||||
assert n.kind == xnElement
|
assert n.kind == xnElement
|
||||||
if n.attrs == nil: return ""
|
if n.attrs == nil: return ""
|
||||||
return n.attrs[name]
|
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`.
|
## Iterates over all the children of `n` returning those matching `tag`.
|
||||||
##
|
##
|
||||||
## Found nodes satisfying the condition will be appended to the `result`
|
## 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:
|
elif child.k == xnElement:
|
||||||
child.findAll(tag, result)
|
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:
|
## Shortcut version to assign in let blocks. Example:
|
||||||
##
|
##
|
||||||
## .. code-block::
|
## .. code-block::
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue