tAdd function to print multiple records - cdb - Constant database manipulation utility
(HTM) git clone git://git.z3bra.org/cdb.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 2a0d87cc4eda76a89b64fd3aed83e49765b46cf2
(DIR) parent e0826edd95958d320872f63bd0163b3b9cc8b66c
(HTM) Author: Willy Goiffon <contact@z3bra.org>
Date: Wed, 7 Jun 2023 14:27:05 +0200
Add function to print multiple records
Diffstat:
M README | 1 +
M cdb.1 | 8 ++++++--
M cdb.go | 20 ++++++++++++--------
3 files changed, 19 insertions(+), 10 deletions(-)
---
(DIR) diff --git a/README b/README
t@@ -12,6 +12,7 @@ Features
- Create a new cdb from encoded records
- Dump existing cdb as encoded records
- Query a key to retrieve its value(s)
+- Skip and/or print multiple records
Usage
-----
(DIR) diff --git a/cdb.1 b/cdb.1
t@@ -7,7 +7,7 @@
.Sh SYNOPSIS
.Nm cdb
.Op Fl hdm
-.Op Fl k Ar key Op Fl s Ar n
+.Op Fl k Ar key Op Fl cs Ar n
.Ar file
.Sh DESCRIPTION
.Nm
t@@ -34,10 +34,14 @@ for record
.Ar key .
Only the first match is returned. To search subsequent match, see
.Fl s .
+.It Fl c Ar n
+Print
+.Ar n
+records from the database (default: 1).
.It Fl s Ar n
Skip the first
.Ar n
-records from the database, and display the next one.
+records from the database, and display the next one (default: 0).
.El
.Ss RECORD FORMAT
A dump record is encoded as
(DIR) diff --git a/cdb.go b/cdb.go
t@@ -42,7 +42,7 @@ func makedb(db string, tmp *os.File) {
exitOnErr(os.Rename(tmpname, db))
}
-func get(db string, key string, skip int) {
+func get(db string, key string, skip int, count int) {
var r *io.SectionReader
c, err := cdb.Open(db)
t@@ -51,7 +51,7 @@ func get(db string, key string, skip int) {
}
defer c.Close()
- for n := 0; n <= skip; n++ {
+ for n := 0; n < skip + count; n++ {
r, err = c.FindNext([]byte(key))
if err == io.EOF {
os.Exit(100)
t@@ -59,12 +59,14 @@ func get(db string, key string, skip int) {
if err != nil {
log.Fatal(err)
}
+ if n >= skip {
+ _, err = io.Copy(os.Stdout, r)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Printf("\n")
+ }
}
- _, err = io.Copy(os.Stdout, r)
- if err != nil {
- log.Fatal(err)
- }
- fmt.Printf("\n")
}
func main() {
t@@ -74,11 +76,13 @@ func main() {
var key string
var db string
var skip int
+ var count int
var dump bool
var make bool
flag.StringVar(&key, "k", "", "Print record with key k")
flag.IntVar(&skip, "s", 0, "Skip the first s matching records")
+ flag.IntVar(&count, "c", 1, "Print c matching records")
flag.BoolVar(&dump, "d", false, "Print records suitable for cbdmake")
flag.BoolVar(&make, "m", false, "Writes a constant database to f")
flag.Parse()
t@@ -111,5 +115,5 @@ func main() {
os.Exit(0)
}
- get(db, key, skip)
+ get(db, key, skip, count)
}