= attrdb: database of attribute-value pairs include "bufio.m"; include "attrdb.m"; attrdb := load Attrdb Attrdb->PATH; Attr: adt { attr: string; val: string; tag: int; # application-defined data, initially 0 }; Tuples: adt { n: int; pairs: list of ref Attr; hasattr: fn(t: self ref Tuples, attr: string): int; haspair: fn(t: self ref Tuples, attr: string, value: string): int; find: fn(t: self ref Tuples, attr: string): list of ref Attr; findbyattr: fn(t: self ref Tuples, attr: string, value: string, rattr: string): list of ref Attr; }; Dbentry: adt { n: int; lines: list of ref Tuples; find: fn(e: self ref Dbentry, attr: string): list of (ref Tuples, list of ref Attr); findfirst: fn(e: self ref Dbentry, attr: string): string; findpair: fn(e: self ref Dbentry, attr: string, value: string): list of ref Tuples; findbyattr: fn(e: self ref Dbentry, attr: string, value: string, rattr: string): list of (ref Tuples, list of ref Attr); }; Db: adt { open: fn(path: string): ref Db; sopen: fn(data: string): ref Db; changed: fn(db: self ref Db): int; reopen: fn(db: self ref Db): int; append: fn(db1: self ref Db, db2: ref Db): ref Db; find: fn(db: self ref Db, start: ref Dbptr, attr: string): (ref Dbentry, ref Dbptr); findpair: fn(db: self ref Db, start: ref Dbptr, attr: string, value: string): (ref Dbentry, ref Dbptr); findbyattr: fn(db: self ref Db, start: ref Dbptr, attr: string, value: string, rattr: string): (ref Dbentry, ref Dbptr); }; init: fn(): string; parseentry: fn(s: string, lno: int): (ref Dbentry, int, string); parseline: fn(s: string, lno: int): (ref Tuples, string); Attrdb fetches data from textual databases that containgroups of attribute-value pairs. The format is defined byattrdb(6). Init must be called before any other function in the module. Each logical database is represented by a Db value. It canspan several physical files, named in the body of a databaseattribute in the primary file of the database. (If no suchattribute appears, there is just the one physical file in thedatabase.) Db.open(path) Opens path as a database, and returns a (reference to a) Db value that represents it. On an error, it returns nil and the system error string contains a diagnostic. If path contains a database attribute with associated attributes of the form file=filename, the logical database is formed by (logically) concate‐ nating the contents of each filename in the order listed. See attrdb(6) for details. Db.sopen(data) Treat the contents of the string data as a database, and return a Db value representing it. db1.append(db2) Return a Db value that represents the result of logi‐ cally appending the contents of database db2 to db1. db.changed() Return true iff one or more of the underlying files have changed since they were last read. db.reopen() Discard any cached data, and reopen the database files. Return 0 on success but -1 if any file could not be reopened. db.find(ptr,attr) Starting at ptr, look in db for the next entry that contains an attribute attr and return a tuple (e,ptr) where e is a Dbentry value representing the whole entry, and ptr is a database pointer for the next entry. If attr cannot be found, e is nil. db.findpair(ptr, attr, value) Starting at ptr, look in db for the next entry that contains the pair attr=value, and return a tuple (e,ptr) where e is a Dbentry value representing the whole entry, and ptr is a database pointer for the next entry. If the given pair cannot be found, e is nil. db.findbyattr(ptr, attr, value, rattr) Starting at ptr in db, look for the next entry con‐ taining both the pair attr=value and a pair with attribute rattr; return a tuple (e,ptr) where e is a Dbentry value representing the whole entry, and ptr is a database pointer for the next entry. If no such entry can be found, e is nil. Parseline takes a line containing a set of space-separatedattribute=value pairs, and returns a tuple (ts,err). If theline's syntax is correct, ts is a Tuples value that repre‐sents the pairs as a list of Attr values. If the syntax iswrong (eg, unmatched quote), ts is nil and err contains adiagnostic. == source/appl/lib/attrdb.b == see ALSOcfg(2), attrdb(6), ndb(6)
= attrdb: database of attribute-value pairs
include "bufio.m";
include "attrdb.m";
attrdb := load Attrdb Attrdb->PATH;
Attr: adt {
attr: string;
val: string;
tag: int; # application-defined data, initially 0
};
Tuples: adt {
n: int;
pairs: list of ref Attr;
hasattr: fn(t: self ref Tuples, attr: string): int;
haspair: fn(t: self ref Tuples,
attr: string, value: string): int;
find: fn(t: self ref Tuples, attr: string): list of ref Attr;
findbyattr: fn(t: self ref Tuples,
attr: string, value: string, rattr: string):
list of ref Attr;
};
Dbentry: adt {
n: int;
lines: list of ref Tuples;
find: fn(e: self ref Dbentry, attr: string):
list of (ref Tuples, list of ref Attr);
findfirst: fn(e: self ref Dbentry, attr: string): string;
findpair: fn(e: self ref Dbentry,
attr: string, value: string):
list of ref Tuples;
findbyattr: fn(e: self ref Dbentry,
attr: string, value: string, rattr: string):
list of (ref Tuples, list of ref Attr);
};
Db: adt {
open: fn(path: string): ref Db;
sopen: fn(data: string): ref Db;
changed: fn(db: self ref Db): int;
reopen: fn(db: self ref Db): int;
append: fn(db1: self ref Db, db2: ref Db): ref Db;
find: fn(db: self ref Db, start: ref Dbptr,
attr: string): (ref Dbentry, ref Dbptr);
findpair: fn(db: self ref Db, start: ref Dbptr,
attr: string, value: string):
(ref Dbentry, ref Dbptr);
findbyattr: fn(db: self ref Db, start: ref Dbptr,
attr: string, value: string, rattr: string):
(ref Dbentry, ref Dbptr);
};
init: fn(): string;
parseentry: fn(s: string, lno: int): (ref Dbentry, int, string);
parseline: fn(s: string, lno: int): (ref Tuples, string);
Attrdb fetches data from textual databases that contain
groups of attribute-value pairs. The format is defined by
attrdb(6).
Init must be called before any other function in the module.
Each logical database is represented by a Db value. It can
span several physical files, named in the body of a database
attribute in the primary file of the database. (If no such
attribute appears, there is just the one physical file in the
database.)
Db.open(path)
Opens path as a database, and returns a (reference to
a) Db value that represents it. On an error, it
returns nil and the system error string contains a
diagnostic. If path contains a database attribute
with associated attributes of the form file=filename,
the logical database is formed by (logically) concate‐
nating the contents of each filename in the order
listed. See attrdb(6) for details.
Db.sopen(data)
Treat the contents of the string data as a database,
and return a Db value representing it.
db1.append(db2)
Return a Db value that represents the result of logi‐
cally appending the contents of database db2 to db1.
db.changed()
Return true iff one or more of the underlying files
have changed since they were last read.
db.reopen()
Discard any cached data, and reopen the database
files. Return 0 on success but -1 if any file could
not be reopened.
db.find(ptr,attr)
Starting at ptr, look in db for the next entry that
contains an attribute attr and return a tuple (e,ptr)
where e is a Dbentry value representing the whole
entry, and ptr is a database pointer for the next
entry. If attr cannot be found, e is nil.
db.findpair(ptr, attr, value)
Starting at ptr, look in db for the next entry that
contains the pair attr=value, and return a tuple
(e,ptr) where e is a Dbentry value representing the
whole entry, and ptr is a database pointer for the
next entry. If the given pair cannot be found, e is
nil.
db.findbyattr(ptr, attr, value, rattr)
Starting at ptr in db, look for the next entry con‐
taining both the pair attr=value and a pair with
attribute rattr; return a tuple (e,ptr) where e is a
Dbentry value representing the whole entry, and ptr is
a database pointer for the next entry. If no such
entry can be found, e is nil.
Parseline takes a line containing a set of space-separated
attribute=value pairs, and returns a tuple (ts,err). If the
line's syntax is correct, ts is a Tuples value that repre‐
sents the pairs as a list of Attr values. If the syntax is
wrong (eg, unmatched quote), ts is nil and err contains a
diagnostic.
== source
/appl/lib/attrdb.b
== see ALSO
cfg(2), attrdb(6), ndb(6)