Merge branch 'master' of github.com:Araq/Nimrod into upstream
This commit is contained in:
commit
c1ad05c4c5
32 changed files with 12023 additions and 459 deletions
213
lib/impure/db_mongo.nim
Normal file
213
lib/impure/db_mongo.nim
Normal file
|
|
@ -0,0 +1,213 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module implements a higher level wrapper for `mongodb`:idx:. Example:
|
||||
##
|
||||
## .. code-block:: nimrod
|
||||
##
|
||||
## import mongo, db_mongo, oids, json
|
||||
##
|
||||
## var conn = db_mongo.open()
|
||||
##
|
||||
## # construct JSON data:
|
||||
## var data = %{"a": %13, "b": %"my string value",
|
||||
## "inner": %{"i": %71} }
|
||||
##
|
||||
## var id = insertID(conn, "test.test", data)
|
||||
##
|
||||
## for v in find(conn, "test.test", "this.a == 13"):
|
||||
## print v
|
||||
##
|
||||
## delete(conn, "test.test", id)
|
||||
## close(conn)
|
||||
|
||||
import mongo, oids, json
|
||||
|
||||
type
|
||||
EDb* = object of EIO ## exception that is raised if a database error occurs
|
||||
TDbConn* = TMongo ## a database connection; alias for ``TMongo``
|
||||
|
||||
proc dbError*(db: TDbConn, msg: string) {.noreturn.} =
|
||||
## raises an EDb exception with message `msg`.
|
||||
var e: ref EDb
|
||||
new(e)
|
||||
if db.errstr[0] != '\0':
|
||||
e.msg = $db.errstr
|
||||
else:
|
||||
e.msg = $db.err & " " & msg
|
||||
raise e
|
||||
|
||||
proc Close*(db: var TDbConn) =
|
||||
## closes the database connection.
|
||||
disconnect(db)
|
||||
destroy(db)
|
||||
|
||||
proc Open*(host: string = defaultHost, port: int = defaultPort): TDbConn =
|
||||
## opens a database connection. Raises `EDb` if the connection could not
|
||||
## be established.
|
||||
init(result)
|
||||
|
||||
let x = connect(result, host, port.cint)
|
||||
if x != 0'i32:
|
||||
dbError(result, "cannot open: " & host)
|
||||
|
||||
proc jsonToBSon(b: var TBSon, key: string, j: PJsonNode) =
|
||||
case j.kind
|
||||
of JString:
|
||||
add(b, key, j.str)
|
||||
of JInt:
|
||||
add(b, key, j.num)
|
||||
of JFloat:
|
||||
add(b, key, j.fnum)
|
||||
of JBool:
|
||||
addBool(b, key, ord(j.bval))
|
||||
of JNull:
|
||||
addNull(b, key)
|
||||
of JObject:
|
||||
addStartObject(b, key)
|
||||
for k, v in items(j.fields):
|
||||
jsonToBSon(b, k, v)
|
||||
addFinishObject(b)
|
||||
of JArray:
|
||||
addStartArray(b, key)
|
||||
for i, e in pairs(j.elems):
|
||||
jsonToBSon(b, $i, e)
|
||||
addFinishArray(b)
|
||||
|
||||
proc jsonToBSon*(j: PJsonNode, oid: TOid): TBSon =
|
||||
## converts a JSON value into the BSON format. The result must be
|
||||
## ``destroyed`` explicitely!
|
||||
init(result)
|
||||
assert j.kind == JObject
|
||||
add(result, "_id", oid)
|
||||
for key, val in items(j.fields):
|
||||
jsonToBSon(result, key, val)
|
||||
finish(result)
|
||||
|
||||
proc `[]`*(obj: var TBSon, fieldname: cstring): TBSon =
|
||||
## retrieves the value belonging to `fieldname`. Raises `EInvalidKey` if
|
||||
## the attribute does not exist.
|
||||
var it = initIter(obj)
|
||||
let res = find(it, result, fieldname)
|
||||
if res == bkEOO:
|
||||
raise newException(EInvalidIndex, "key not in object")
|
||||
|
||||
proc getId*(obj: var TBSon): TOid =
|
||||
## retrieves the ``_id`` attribute of `obj`.
|
||||
var it = initIter(obj)
|
||||
var b: TBSon
|
||||
let res = find(it, b, "_id")
|
||||
if res == bkOID:
|
||||
result = oidVal(it)[]
|
||||
else:
|
||||
raise newException(EInvalidIndex, "_id not in object")
|
||||
|
||||
proc insertID*(db: var TDbConn, namespace: string, data: PJsonNode): TOid =
|
||||
## converts `data` to BSON format and inserts it in `namespace`. Returns
|
||||
## the generated OID for the ``_id`` field.
|
||||
result = genOid()
|
||||
var x = jsonToBSon(data, result)
|
||||
insert(db, namespace, x)
|
||||
destroy(x)
|
||||
|
||||
proc insert*(db: var TDbConn, namespace: string, data: PJsonNode) =
|
||||
## converts `data` to BSON format and inserts it in `namespace`.
|
||||
discard InsertID(db, namespace, data)
|
||||
|
||||
proc update*(db: var TDbConn, namespace: string, obj: var TBSon) =
|
||||
## updates `obj` in `namespace`.
|
||||
var cond: TBson
|
||||
init(cond)
|
||||
cond.add("_id", getId(obj))
|
||||
finish(cond)
|
||||
update(db, namespace, cond, obj, ord(UPDATE_UPSERT))
|
||||
destroy(cond)
|
||||
|
||||
proc update*(db: var TDbConn, namespace: string, oid: TOid, obj: PJsonNode) =
|
||||
## updates the data with `oid` to have the new data `obj`.
|
||||
var a = jsonToBSon(obj, oid)
|
||||
Update(db, namespace, a)
|
||||
destroy(a)
|
||||
|
||||
proc delete*(db: var TDbConn, namespace: string, oid: TOid) =
|
||||
## Deletes the object belonging to `oid`.
|
||||
var cond: TBson
|
||||
init(cond)
|
||||
cond.add("_id", oid)
|
||||
finish(cond)
|
||||
discard remove(db, namespace, cond)
|
||||
destroy(cond)
|
||||
|
||||
proc delete*(db: var TDbConn, namespace: string, obj: var TBSon) =
|
||||
## Deletes the object `obj`.
|
||||
delete(db, namespace, getId(obj))
|
||||
|
||||
iterator find*(db: var TDbConn, namespace: string): var TBSon =
|
||||
## iterates over any object in `namespace`.
|
||||
var cursor: TCursor
|
||||
init(cursor, db, namespace)
|
||||
while next(cursor) == mongo.OK:
|
||||
yield bson(cursor)[]
|
||||
destroy(cursor)
|
||||
|
||||
iterator find*(db: var TDbConn, namespace: string,
|
||||
query, fields: var TBSon): var TBSon =
|
||||
## yields the `fields` of any document that suffices `query`.
|
||||
var cursor = find(db, namespace, query, fields, 0'i32, 0'i32, 0'i32)
|
||||
if cursor != nil:
|
||||
while next(cursor[]) == mongo.OK:
|
||||
yield bson(cursor[])[]
|
||||
destroy(cursor[])
|
||||
|
||||
proc setupFieldnames(fields: openArray[string]): TBSon =
|
||||
init(result)
|
||||
for x in fields: add(result, x, 1'i32)
|
||||
finish(result)
|
||||
|
||||
iterator find*(db: var TDbConn, namespace: string,
|
||||
query: var TBSon, fields: openArray[string]): var TBSon =
|
||||
## yields the `fields` of any document that suffices `query`. If `fields`
|
||||
## is ``[]`` the whole document is yielded.
|
||||
var f = setupFieldnames(fields)
|
||||
var cursor = find(db, namespace, query, f, 0'i32, 0'i32, 0'i32)
|
||||
if cursor != nil:
|
||||
while next(cursor[]) == mongo.OK:
|
||||
yield bson(cursor[])[]
|
||||
destroy(cursor[])
|
||||
destroy(f)
|
||||
|
||||
proc setupQuery(query: string): TBSon =
|
||||
init(result)
|
||||
add(result, "$where", query)
|
||||
finish(result)
|
||||
|
||||
iterator find*(db: var TDbConn, namespace: string,
|
||||
query: string, fields: openArray[string]): var TBSon =
|
||||
## yields the `fields` of any document that suffices `query`. If `fields`
|
||||
## is ``[]`` the whole document is yielded.
|
||||
var f = setupFieldnames(fields)
|
||||
var q = setupQuery(query)
|
||||
var cursor = find(db, namespace, q, f, 0'i32, 0'i32, 0'i32)
|
||||
if cursor != nil:
|
||||
while next(cursor[]) == mongo.OK:
|
||||
yield bson(cursor[])[]
|
||||
destroy(cursor[])
|
||||
destroy(q)
|
||||
destroy(f)
|
||||
|
||||
when false:
|
||||
# this doesn't work this way; would require low level hacking
|
||||
iterator fieldPairs*(obj: var TBSon): tuple[key: cstring, value: TBSon] =
|
||||
## iterates over `obj` and yields all (key, value)-Pairs.
|
||||
var it = initIter(obj)
|
||||
var v: TBSon
|
||||
while next(it) != bkEOO:
|
||||
let key = key(it)
|
||||
discard init(v, value(it))
|
||||
yield (key, v)
|
||||
47
lib/pure/endians.nim
Normal file
47
lib/pure/endians.nim
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module contains helpers that deal with different byte orders
|
||||
## (`endian`:idx:).
|
||||
|
||||
proc swapEndian64*(outp, inp: pointer) =
|
||||
## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
|
||||
## contain at least 8 bytes.
|
||||
var i = cast[cstring](inp)
|
||||
var o = cast[cstring](outp)
|
||||
o[0] = i[7]
|
||||
o[1] = i[6]
|
||||
o[2] = i[5]
|
||||
o[3] = i[4]
|
||||
o[4] = i[3]
|
||||
o[5] = i[2]
|
||||
o[6] = i[1]
|
||||
o[7] = i[0]
|
||||
|
||||
proc swapEndian32*(outp, inp: pointer) =
|
||||
## copies `inp` to `outp` swapping bytes. Both buffers are supposed to
|
||||
## contain at least 4 bytes.
|
||||
var i = cast[cstring](inp)
|
||||
var o = cast[cstring](outp)
|
||||
o[0] = i[3]
|
||||
o[1] = i[2]
|
||||
o[2] = i[1]
|
||||
o[3] = i[0]
|
||||
|
||||
when system.cpuEndian == bigEndian:
|
||||
proc littleEndian64*(outp, inp: pointer) {.inline.} = swapEndian64(outp, inp)
|
||||
proc littleEndian32*(outp, inp: pointer) {.inline.} = swapEndian32(outp, inp)
|
||||
proc bigEndian64*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 8)
|
||||
proc bigEndian32*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 4)
|
||||
else:
|
||||
proc littleEndian64*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 8)
|
||||
proc littleEndian32*(outp, inp: pointer) {.inline.} = copyMem(outp, inp, 4)
|
||||
proc bigEndian64*(outp, inp: pointer) {.inline.} = swapEndian64(outp, inp)
|
||||
proc bigEndian32*(outp, inp: pointer) {.inline.} = swapEndian32(outp, inp)
|
||||
|
||||
|
|
@ -495,16 +495,16 @@ type
|
|||
JArray
|
||||
|
||||
PJsonNode* = ref TJsonNode ## JSON node
|
||||
TJsonNode* {.final, pure.} = object
|
||||
TJsonNode* {.final, pure, acyclic.} = object
|
||||
case kind*: TJsonNodeKind
|
||||
of JString:
|
||||
str*: String
|
||||
str*: string
|
||||
of JInt:
|
||||
num*: biggestInt
|
||||
of JFloat:
|
||||
fnum: Float
|
||||
fnum*: float
|
||||
of JBool:
|
||||
bval*: Bool
|
||||
bval*: bool
|
||||
of JNull:
|
||||
nil
|
||||
of JObject:
|
||||
|
|
@ -558,6 +558,45 @@ proc newJArray*(): PJsonNode =
|
|||
result.kind = JArray
|
||||
result.elems = @[]
|
||||
|
||||
|
||||
proc `%`*(s: string): PJsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JString PJsonNode`.
|
||||
new(result)
|
||||
result.kind = JString
|
||||
result.str = s
|
||||
|
||||
proc `%`*(n: biggestInt): PJsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JInt PJsonNode`.
|
||||
new(result)
|
||||
result.kind = JInt
|
||||
result.num = n
|
||||
|
||||
proc `%`*(n: float): PJsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JFloat PJsonNode`.
|
||||
new(result)
|
||||
result.kind = JFloat
|
||||
result.fnum = n
|
||||
|
||||
proc `%`*(b: bool): PJsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JBool PJsonNode`.
|
||||
new(result)
|
||||
result.kind = JBool
|
||||
result.bval = b
|
||||
|
||||
proc `%`*(keyVals: openArray[tuple[key: string, val: PJsonNode]]): PJsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JObject PJsonNode`
|
||||
new(result)
|
||||
result.kind = JObject
|
||||
newSeq(result.fields, keyVals.len)
|
||||
for i, p in pairs(keyVals): result.fields[i] = p
|
||||
|
||||
proc `%`*(elements: openArray[PJSonNode]): PJsonNode =
|
||||
## Generic constructor for JSON data. Creates a new `JArray PJsonNode`
|
||||
new(result)
|
||||
result.kind = JArray
|
||||
newSeq(result.elems, elements.len)
|
||||
for i, p in pairs(elements): result.elems[i] = p
|
||||
|
||||
proc len*(n: PJsonNode): int =
|
||||
## If `n` is a `JArray`, it returns the number of elements.
|
||||
## If `n` is a `JObject`, it returns the number of pairs.
|
||||
|
|
|
|||
86
lib/pure/oids.nim
Normal file
86
lib/pure/oids.nim
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## Nimrod OID support. An OID is a global ID that consists of a timestamp,
|
||||
## a unique counter and a random value. This combination should suffice to
|
||||
## produce a globally distributed unique ID. This implementation was extracted
|
||||
## from the Mongodb interface and it thus binary compatible with a Mongo OID.
|
||||
##
|
||||
## This implementation calls ``math.randomize()`` for the first call of
|
||||
## ``genOid``.
|
||||
|
||||
import times, endians
|
||||
|
||||
type
|
||||
Toid* {.pure, final.} = object ## an OID
|
||||
time: int32 ##
|
||||
fuzz: int32 ##
|
||||
count: int32 ##
|
||||
|
||||
proc hexbyte*(hex: char): int =
|
||||
case hex
|
||||
of '0'..'9': result = (ord(hex) - ord('0'))
|
||||
of 'a'..'f': result = (ord(hex) - ord('a') + 10)
|
||||
of 'A'..'F': result = (ord(hex) - ord('A') + 10)
|
||||
else: nil
|
||||
|
||||
proc parseOid*(str: cstring): TOid =
|
||||
## parses an OID.
|
||||
var bytes = cast[cstring](addr(result.time))
|
||||
var i = 0
|
||||
while i < 12:
|
||||
bytes[i] = chr((hexbyte(str[2 * i]) shl 4) or hexbyte(str[2 * i + 1]))
|
||||
inc(i)
|
||||
|
||||
proc oidToString*(oid: TOid, str: cstring) =
|
||||
const hex = "0123456789abcdef"
|
||||
# work around a compiler bug:
|
||||
var str = str
|
||||
var o = oid
|
||||
var bytes = cast[cstring](addr(o))
|
||||
var i = 0
|
||||
while i < 12:
|
||||
let b = bytes[i].ord
|
||||
str[2 * i] = hex[(b and 0xF0) shr 4]
|
||||
str[2 * i + 1] = hex[b and 0xF]
|
||||
inc(i)
|
||||
str[24] = '\0'
|
||||
|
||||
var
|
||||
incr, fuzz: int
|
||||
|
||||
proc genOid*(): TOid =
|
||||
## generates a new OID.
|
||||
proc rand(): cint {.importc: "rand", nodecl.}
|
||||
proc gettime(dummy: ptr cint): cint {.importc: "time", header: "<time.h>".}
|
||||
proc srand(seed: cint) {.importc: "srand", nodecl.}
|
||||
|
||||
var t = gettime(nil)
|
||||
|
||||
var i = int32(incr)
|
||||
atomicInc(incr)
|
||||
|
||||
if fuzz == 0:
|
||||
# racy, but fine semantically:
|
||||
srand(t)
|
||||
fuzz = rand()
|
||||
bigEndian32(addr result.time, addr(t))
|
||||
result.fuzz = fuzz
|
||||
bigEndian32(addr result.count, addr(i))
|
||||
|
||||
proc generatedTime*(oid: TOid): TTime =
|
||||
## returns the generated timestamp of the OID.
|
||||
var tmp: int32
|
||||
var dummy = oid.time
|
||||
bigEndian32(addr(tmp), addr(dummy))
|
||||
result = TTime(tmp)
|
||||
|
||||
when isMainModule:
|
||||
let xo = genOID()
|
||||
echo xo.generatedTime
|
||||
|
|
@ -151,7 +151,7 @@ else: # UNIX-like operating system
|
|||
ScriptExt* = ""
|
||||
DynlibFormat* = "lib$1.so"
|
||||
|
||||
when defined(macosx):
|
||||
when defined(macosx) or defined(bsd):
|
||||
var
|
||||
pathMax {.importc: "PATH_MAX", header: "<stdlib.h>".}: cint
|
||||
|
||||
|
|
@ -580,7 +580,7 @@ proc expandFilename*(filename: string): string {.rtl, extern: "nos$1".} =
|
|||
var L = GetFullPathNameA(filename, bufsize, result, unused)
|
||||
if L <= 0'i32 or L >= bufsize: OSError()
|
||||
setLen(result, L)
|
||||
elif defined(macosx):
|
||||
elif defined(macosx) or defined(bsd):
|
||||
# On Mac OS X 10.5, realpath does not allocate the buffer on its own
|
||||
var pathBuffer: cstring = newString(pathMax)
|
||||
var resultBuffer = realpath(filename, pathBuffer)
|
||||
|
|
@ -1355,6 +1355,7 @@ when defined(macosx):
|
|||
|
||||
proc getAppFilename*(): string {.rtl, extern: "nos$1".} =
|
||||
## Returns the filename of the application's executable.
|
||||
## **Note**: This does not work reliably on BSD.
|
||||
|
||||
# Linux: /proc/<pid>/exe
|
||||
# Solaris:
|
||||
|
|
@ -1410,6 +1411,7 @@ proc getApplicationDir*(): string {.rtl, extern: "nos$1", deprecated.} =
|
|||
|
||||
proc getAppDir*(): string {.rtl, extern: "nos$1".} =
|
||||
## Returns the directory of the application's executable.
|
||||
## **Note**: This does not work reliably on BSD.
|
||||
result = splitFile(getAppFilename()).dir
|
||||
|
||||
proc sleep*(milsecs: int) {.rtl, extern: "nos$1".} =
|
||||
|
|
|
|||
|
|
@ -1486,10 +1486,10 @@ proc primary(p: var TPegParser): TPeg =
|
|||
return !primary(p)
|
||||
of tkAt:
|
||||
getTok(p)
|
||||
return @primary(p)
|
||||
return @(primary(p))
|
||||
of tkCurlyAt:
|
||||
getTok(p)
|
||||
return @@primary(p).token(p)
|
||||
return @@(primary(p).token(p))
|
||||
else: nil
|
||||
case p.tok.kind
|
||||
of tkIdentifier:
|
||||
|
|
|
|||
|
|
@ -378,6 +378,22 @@ proc parseBool*(s: string): bool =
|
|||
of "y", "yes", "true", "1", "on": result = true
|
||||
of "n", "no", "false", "0", "off": result = false
|
||||
else: raise newException(EInvalidValue, "cannot interpret as a bool: " & s)
|
||||
|
||||
proc parseEnum*[T: enum](s: string): T =
|
||||
## parses an enum ``T``. Raises ``EInvalidValue`` for an invalid value in
|
||||
## `s`. The comparison is done in a style insensitive way.
|
||||
for e in low(T)..high(T):
|
||||
if cmpIgnoreStyle(s, $e) == 0:
|
||||
return e
|
||||
raise newException(EInvalidValue, "invalid enum value: " & s)
|
||||
|
||||
proc parseEnum*[T: enum](s: string, default: T): T =
|
||||
## parses an enum ``T``. Uses `default` for an invalid value in
|
||||
## `s`. The comparison is done in a style insensitive way.
|
||||
for e in low(T)..high(T):
|
||||
if cmpIgnoreStyle(s, $e) == 0:
|
||||
return e
|
||||
result = default
|
||||
|
||||
proc repeatChar*(count: int, c: Char = ' '): string {.noSideEffect,
|
||||
rtl, extern: "nsuRepeatChar".} =
|
||||
|
|
@ -1105,5 +1121,8 @@ when isMainModule:
|
|||
|
||||
doAssert "-ld a-ldz -ld".replaceWord("-ld") == " a-ldz "
|
||||
doAssert "-lda-ldz -ld abc".replaceWord("-ld") == "-lda-ldz abc"
|
||||
|
||||
|
||||
|
||||
type TMyEnum = enum enA, enB, enC, enuD, enE
|
||||
doAssert parseEnum[TMyEnum]("enu_D") == enuD
|
||||
|
||||
doAssert parseEnum("invalid enum value", enC) == enC
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2009 Andreas Rumpf
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
|
|
@ -307,6 +307,17 @@ proc setBackgroundColor*(bg: TBackgroundColor, bright=false) =
|
|||
gBG = ord(bg)
|
||||
if bright: inc(gBG, 60)
|
||||
stdout.write("\e[" & $gBG & 'm')
|
||||
|
||||
proc isatty*(f: TFile): bool =
|
||||
## returns true if `f` is associated with a terminal device.
|
||||
when defined(posix):
|
||||
proc isatty(fildes: TFileHandle): cint {.
|
||||
importc: "isatty", header: "<unistd.h>".}
|
||||
else:
|
||||
proc isatty(fildes: TFileHandle): cint {.
|
||||
importc: "_isatty", header: "<io.h>".}
|
||||
|
||||
result = isatty(fileHandle(f)) != 0'i32
|
||||
|
||||
# XXX:
|
||||
# These should be private, but there is no yet
|
||||
|
|
@ -317,7 +328,7 @@ proc styledEchoProcessArg*(style: set[TStyle]) = setStyle style
|
|||
proc styledEchoProcessArg*(color: TForegroundColor) = setForeGroundColor color
|
||||
proc styledEchoProcessArg*(color: TBackgroundColor) = setBackGroundColor color
|
||||
|
||||
macro styledEcho*(m: stmt): stmt =
|
||||
macro styledEcho*(m: stmt): stmt =
|
||||
result = newNimNode(nnkStmtList)
|
||||
|
||||
for i in countup(1, m.len - 1):
|
||||
|
|
|
|||
|
|
@ -2254,4 +2254,3 @@ template eval*(blk: stmt): stmt =
|
|||
|
||||
when defined(initDebugger):
|
||||
initDebugger()
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ type
|
|||
LONG* = int
|
||||
WINBOOL* = int32
|
||||
DWORD* = int32
|
||||
HDC* = THandle
|
||||
HGLRC* = THandle
|
||||
|
||||
TSECURITY_ATTRIBUTES* {.final, pure.} = object
|
||||
nLength*: int32
|
||||
|
|
|
|||
116
lib/wrappers/libsvm.nim
Normal file
116
lib/wrappers/libsvm.nim
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# (c) Copyright 2012 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module is a low level wrapper for `libsvm`:idx:.
|
||||
|
||||
{.deadCodeElim: on.}
|
||||
const
|
||||
LIBSVM_VERSION* = 312
|
||||
|
||||
when defined(windows):
|
||||
const svmdll* = "libsvm.dll"
|
||||
elif defined(macosx):
|
||||
const svmdll* = "libsvm.dylib"
|
||||
else:
|
||||
const svmdll* = "libsvm.so"
|
||||
|
||||
type
|
||||
Tnode*{.pure, final.} = object
|
||||
index*: cint
|
||||
value*: cdouble
|
||||
|
||||
Tproblem*{.pure, final.} = object
|
||||
L*: cint
|
||||
y*: ptr cdouble
|
||||
x*: ptr ptr Tnode
|
||||
|
||||
Ttype*{.size: sizeof(cint).} = enum
|
||||
C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR
|
||||
|
||||
TKernelType*{.size: sizeof(cint).} = enum
|
||||
LINEAR, POLY, RBF, SIGMOID, PRECOMPUTED
|
||||
|
||||
Tparameter*{.pure, final.} = object
|
||||
typ*: TType
|
||||
kernelType*: TKernelType
|
||||
degree*: cint # for poly
|
||||
gamma*: cdouble # for poly/rbf/sigmoid
|
||||
coef0*: cdouble # for poly/sigmoid
|
||||
# these are for training only
|
||||
cache_size*: cdouble # in MB
|
||||
eps*: cdouble # stopping criteria
|
||||
C*: cdouble # for C_SVC, EPSILON_SVR and NU_SVR
|
||||
nr_weight*: cint # for C_SVC
|
||||
weight_label*: ptr cint # for C_SVC
|
||||
weight*: ptr cdouble # for C_SVC
|
||||
nu*: cdouble # for NU_SVC, ONE_CLASS, and NU_SVR
|
||||
p*: cdouble # for EPSILON_SVR
|
||||
shrinking*: cint # use the shrinking heuristics
|
||||
probability*: cint # do probability estimates
|
||||
|
||||
|
||||
#
|
||||
# svm_model
|
||||
#
|
||||
|
||||
type
|
||||
TModel*{.pure, final.} = object
|
||||
param*: Tparameter # parameter
|
||||
nr_class*: cint # number of classes, = 2 in regression/one class svm
|
||||
L*: cint # total #SV
|
||||
SV*: ptr ptr Tnode # SVs (SV[l])
|
||||
sv_coef*: ptr ptr cdouble # coefficients for SVs in decision functions (sv_coef[k-1][l])
|
||||
rho*: ptr cdouble # constants in decision functions (rho[k*(k-1)/2])
|
||||
probA*: ptr cdouble # pariwise probability information
|
||||
probB*: ptr cdouble # for classification only
|
||||
label*: ptr cint # label of each class (label[k])
|
||||
nSV*: ptr cint # number of SVs for each class (nSV[k])
|
||||
# nSV[0] + nSV[1] + ... + nSV[k-1] = l
|
||||
# XXX
|
||||
free_sv*: cint # 1 if svm_model is created by svm_load_model
|
||||
# 0 if svm_model is created by svm_train
|
||||
|
||||
|
||||
proc train*(prob: ptr Tproblem, param: ptr Tparameter): ptr Tmodel{.cdecl,
|
||||
importc: "svm_train", dynlib: svmdll.}
|
||||
proc cross_validation*(prob: ptr Tproblem, param: ptr Tparameter, nr_fold: cint,
|
||||
target: ptr cdouble){.cdecl,
|
||||
importc: "svm_cross_validation", dynlib: svmdll.}
|
||||
proc save_model*(model_file_name: cstring, model: ptr Tmodel): cint{.cdecl,
|
||||
importc: "svm_save_model", dynlib: svmdll.}
|
||||
proc load_model*(model_file_name: cstring): ptr Tmodel{.cdecl,
|
||||
importc: "svm_load_model", dynlib: svmdll.}
|
||||
proc get_svm_type*(model: ptr Tmodel): cint{.cdecl, importc: "svm_get_svm_type",
|
||||
dynlib: svmdll.}
|
||||
proc get_nr_class*(model: ptr Tmodel): cint{.cdecl, importc: "svm_get_nr_class",
|
||||
dynlib: svmdll.}
|
||||
proc get_labels*(model: ptr Tmodel, label: ptr cint){.cdecl,
|
||||
importc: "svm_get_labels", dynlib: svmdll.}
|
||||
proc get_svr_probability*(model: ptr Tmodel): cdouble{.cdecl,
|
||||
importc: "svm_get_svr_probability", dynlib: svmdll.}
|
||||
proc predict_values*(model: ptr Tmodel, x: ptr Tnode, dec_values: ptr cdouble): cdouble{.
|
||||
cdecl, importc: "svm_predict_values", dynlib: svmdll.}
|
||||
proc predict*(model: ptr Tmodel, x: ptr Tnode): cdouble{.cdecl,
|
||||
importc: "svm_predict", dynlib: svmdll.}
|
||||
proc predict_probability*(model: ptr Tmodel, x: ptr Tnode,
|
||||
prob_estimates: ptr cdouble): cdouble{.cdecl,
|
||||
importc: "svm_predict_probability", dynlib: svmdll.}
|
||||
proc free_model_content*(model_ptr: ptr Tmodel){.cdecl,
|
||||
importc: "svm_free_model_content", dynlib: svmdll.}
|
||||
proc free_and_destroy_model*(model_ptr_ptr: ptr ptr Tmodel){.cdecl,
|
||||
importc: "svm_free_and_destroy_model", dynlib: svmdll.}
|
||||
proc destroy_param*(param: ptr Tparameter){.cdecl, importc: "svm_destroy_param",
|
||||
dynlib: svmdll.}
|
||||
proc check_parameter*(prob: ptr Tproblem, param: ptr Tparameter): cstring{.
|
||||
cdecl, importc: "svm_check_parameter", dynlib: svmdll.}
|
||||
proc check_probability_model*(model: ptr Tmodel): cint{.cdecl,
|
||||
importc: "svm_check_probability_model", dynlib: svmdll.}
|
||||
|
||||
proc set_print_string_function*(print_func: proc (arg: cstring) {.cdecl.}){.
|
||||
cdecl, importc: "svm_set_print_string_function", dynlib: svmdll.}
|
||||
1186
lib/wrappers/mongo.nim
Normal file
1186
lib/wrappers/mongo.nim
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -27,9 +27,7 @@ elif defined(macosx):
|
|||
else:
|
||||
const
|
||||
dllname = "libglut.so.3"
|
||||
type
|
||||
PInteger* = ptr int
|
||||
PPChar* = ptr cstring
|
||||
type
|
||||
TGlutVoidCallback* = proc (){.cdecl.}
|
||||
TGlut1IntCallback* = proc (value: cint){.cdecl.}
|
||||
TGlut2IntCallback* = proc (v1, v2: cint){.cdecl.}
|
||||
|
|
@ -231,8 +229,16 @@ const # glutGet parameters.
|
|||
GLUT_GAME_MODE_REFRESH_RATE* = 5
|
||||
GLUT_GAME_MODE_DISPLAY_CHANGED* = 6 # GLUT initialization sub-API.
|
||||
|
||||
proc glutInit*(argcp: PInteger, argv: PPChar){.dynlib: dllname,
|
||||
proc glutInit*(argcp: ptr cint, argv: pointer){.dynlib: dllname,
|
||||
importc: "glutInit".}
|
||||
|
||||
proc glutInit*() =
|
||||
## version that passes `argc` and `argc` implicitely.
|
||||
var
|
||||
cmdLine {.importc: "cmdLine".}: array[0..255, cstring]
|
||||
cmdCount {.importc: "cmdCount".}: cint
|
||||
glutInit(addr(cmdCount), addr(cmdLine))
|
||||
|
||||
proc glutInitDisplayMode*(mode: int16){.dynlib: dllname,
|
||||
importc: "glutInitDisplayMode".}
|
||||
proc glutInitDisplayString*(str: cstring){.dynlib: dllname,
|
||||
|
|
|
|||
9633
lib/wrappers/opengl/opengl.nim
Normal file
9633
lib/wrappers/opengl/opengl.nim
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue