@@ 59,6 59,10 @@ using dbm: DBM
type CppString {.importcpp: "std::string".} = object
proc data(s: CppString): pointer {.importcpp: "#.data()".}
proc len(s: CppString): csize_t {.importcpp: "#.length()".}
+proc `$`(cpp: CppString): string =
+ result.setLen(cpp.len)
+ if result.len > 0:
+ copyMem(addr result[0], cpp.data, result.len)
type CppStringView {.importcpp: "std::string_view".} = object
proc toStringView(s: pointer; count: int): CppStringView {.
@@ 72,7 76,7 @@ proc toStringView(buf: openarray[byte]): CppStringView {.inline.} =
type Status {.importcpp: "tkrzw::Status".} = object
proc IsOK(st: Status): bool {.importcpp: "#.IsOK()".}
-proc GetMessage(st: Status): cstring {.importcpp: "#.GetMessage().c_str()".}
+proc GetMessage(st: Status): CppString {.importcpp: "#.GetMessage()".}
template check(st: Status) =
if not IsOK(st): raise newException(IOError, $GetMessage(st))
@@ 80,8 84,8 @@ type Iterator {.
importcpp: "std::unique_ptr<tkrzw::DBM::Iterator>".} = object
proc First(iter: Iterator): Status {.importcpp: "#->First()".}
proc Next(iter: Iterator): Status {.importcpp: "#->Next()".}
-proc GetKey(iter: Iterator): cstring {.importcpp: "#->GetKey().c_str()".}
-proc GetValue(iter: Iterator): cstring {.importcpp: "#->GetValue().c_str()".}
+proc GetKey(iter: Iterator): CppString {.importcpp: "#->GetKey()".}
+proc GetValue(iter: Iterator): CppString {.importcpp: "#->GetValue()".}
template importDBM(T: untyped): untyped =
proc `=destroy`(obj: var `T Obj`) {.importcpp: "#.~" & $T & "()".}
@@ 175,13 179,13 @@ iterator pairs*(dbm): (string, string) =
var
iter = dbm.MakeIterator()
status = iter.First()
- key = iter.GetKey()
- val = iter.GetValue()
+ key = $iter.GetKey()
+ val = $iter.GetValue()
while status.IsOK and key != "" and val != "":
- yield($key, $val)
+ yield(key, val)
status = iter.Next()
- key = iter.GetKey()
- val = iter.GetValue()
+ key = $iter.GetKey()
+ val = $iter.GetValue()
proc synchronize*(dbm; hard: bool) =
## Synchronizes the content of the database to the file system.