You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi
I try implement string key/value storage and found this to hard for me
If i right understand - sophia not store strings as cstring, but as array of bytes
If i right code in errorExit - will not work, (cast to cstring in Nim will fail with segfault)
I found the strange workarround how set string and get it with casting to array[0,char]
It's work, but i dont understand why) May you explain how do it right?
My code is:
import sophia,strutils
proc free(obj: pointer) {.importc: "free", header: "<stdio.h>"}
proc errorExit() =
var size: cint
var error = env.getstring("sophia.error", addr size);
#cast to csting not working?
var msg = $(cast[ptr cstring](error)[])
echo("Error: " & msg)
free(error)
discard env.destroy()
# Create a Sophia environment
var env = env()
# Set directory and add a db named test
discard env.setstring("sophia.path", "sophia3".cstring, 0)
discard env.setstring("db", "db".cstring, 0)
# Get the db
var db = env.getobject("db.db")
# Open the environment
var rc = env.open()
if (rc == -1): errorExit()
echo "Opened Sophia env"
# Store strings
var o:pointer
var key:string = "hello"
var value:string = "hello world"
o = document(db)
discard o.setstring("key".cstring, addr key[0], (key.len).cint)
discard o.setstring("value".cstring, addr value[0], (value.len).cint)
rc = db.set(o);
if (rc == -1): errorExit()
o = document(db)
discard o.setstring("key".cstring, addr key[0], (key.len).cint)
o = db.get(o)
if (o!=nil):
var valPointer = o.getstring("value".cstring, nil)
#very strange but work
var returnVal = $(cast[ptr array[0,char]](valPointer)[])
echo returnVal
echo returnVal.len
else:
echo "not found"
echo "Yup, it's gone, all good"
# Clean up
discard destroy(env)
The text was updated successfully, but these errors were encountered:
Hi
I try implement string key/value storage and found this to hard for me
If i right understand - sophia not store strings as cstring, but as array of bytes
If i right code in errorExit - will not work, (cast to cstring in Nim will fail with segfault)
I found the strange workarround how set string and get it with casting to array[0,char]
It's work, but i dont understand why) May you explain how do it right?
My code is:
The text was updated successfully, but these errors were encountered: