I took the code at https://www.unixuser.org/~euske/doc/cdbinternals/pycdb.py.html and made it Python 3 compatible, then I converted it to Swift.
Some links for you:
- https://cr.yp.to/cdb/cdb.txt and https://cr.yp.to/cdb.html
- http://www.cse.yorku.ca/~oz/hash.html
- https://www.unixuser.org/~euske/doc/cdbinternals/index.html
The original C version of cdb
(written by DJB) is only necessary if you want to prove to
yourself that this Swift library is compatible:
brew install cdb
The Swift unit tests need cdbmake
from this library.
Use the C library to make a test.cdb
file from /etc/services
:
cdbmake-sv test.cdb test.tmp < /etc/services
Query this cdb using the C version of `cdbget:
# result should be 25
cdbget smtp/tcp < test.cdb && echo ''
See Usage
below for how to perform the same cdbget
in swift.
Swift's package system doesn't appear to have a good way to deal with namespace conflicts, so the classes are manually
prefixed with CDB
. Example: CDBReader
. get
is also a reserved keyword, so we have to live with triply redundant
cdbget()
.
// swift-tools-version:5.5
import PackageDescription
let package = Package(
name: "MyProject",
dependencies: [
.package(url: "https://github.com/createthis/swift-cdb.git", from: "1.0.0"),
],
targets: [
.target(
name: "MyProject",
dependencies: [
.product(name: "cdb", package: "swift-cdb")
]
),
]
)
Query it using this Swift library:
import cdb
let cdbReader = try CDBReader(filePath: "test.cdb", posHeader: 0)
let valueFromCdb = try cdbReader.cdbget(key: "smtp/tcp")
print("valueFromCdb=\(String(data: valueFromCdb, encoding: .utf8)!)")
Alternatively, to create a cdb using swift, do something like this:
import cdb
let data = ["key1": "value1", "key2": "value2", "key3": "value3"]
let cdbWriter = try CDBWriter(filePath: "my.cdb")
for (key, value) in data {
try cdbWriter.put(key: key, value: value)
}
try cdbWriter.finalize();
The Swift unit tests need cdbmake
from the original C library, so install that first (see above).
Finally, you can test that the output of this Swift library matches the output of the original C version:
swift test