Charles.Forsyth20060303-partialeea662d19 years agoHistory
159 lines · 3.3 KB · Text
RawDownload
.TH CFG 2.SH NAMECfg, Record, Tuple, Attr \- configuration file parser.SH SYNOPSIS.EXinclude "cfg.m";cfg := load Cfg Cfg->PATH; Attr: adt {	name: string;	value: string;}; Tuple: adt {	lnum: int;	attrs: list of Attr;	lookup: fn(t: self ref Tuple, name: string): string;}; Record: adt {	tuples: list of ref Tuple;	lookup: fn(r: self ref Record, name: string)		: (string, ref Tuple);}; init: fn(path: string): string;lookup: fn(name: string): list of (string, ref Record);getkeys: fn(): list of string; .EE.SH DESCRIPTION.B Cfgparses its configuration file format into a set of.BR Record s..PPEach line of the configuration file is comprised of a tuple of attributes.Comments are introduced by the.RB ' # 'character and run to the end of the input line.Empty lines and comments are ignored..PPAn attribute has a name followed by an optional value.The value is specified by separating the name and valueby an.RB ' = 'character.Attribute names and values are input.IR words .A word is delimited by spacing characters and the.RB ' = 'character.If a word needs to include any of these characters then the word may bequoted using single or double quotes.The start and end quotes must be the same.The quoting character may be included in the word by appearing twice..PPExamples:.EX	'a b c'	yields	a b c	"a b c"	yields	a b c	'a " c'	yields	a " c	'a '' c'	yields	a ' c.EE.PPThe name of the first attribute of a tuple is its.IR key .The.I primary tuple valueis the value of its first attribute..PPTuples whose first attribute name appears at the start of a line (having nopreceeding spacing characters)are treated as the start of a new record.A record incorporates all tuples up to the start of the next record.The.I record keyis defined to be the name of its first attribute.The.I primary record valueis the value of the first attribute..PPThe.B adttypes.BR Attr ,.B Tupleand.B Recordare direct analogues of the constructs defined above..P.TP.BI init( path ).B Initinitialises the.B Cfgmodule, causing it to open and parse the configuration filegiven by the.I pathargument.If an error is encountered in processing the file thenan error string is returned.If there are no errors.B initreturns.BR nil ..TP.BI lookup( name ).B Lookupreturnsthe set of.BR Record swhose.I keymatches the.I nameargument.The return value is a list of.RI ( "primary record value" , " record" )pairs..TP.B getkeys().B Getkeysreturns a list of the record keys that appear in the configuration file.Note that more than one record can have the same key.Duplicate key names are not returned by.BR getkeys() ..TP.IB record .lookup( name )Returns the first tuple in.I recordwhose key matches.IR name .The return value is.RI ( "primary tuple value" , " tuple" ).If no matching tuple is found then the value.B (nil, nil)is returned.Note that more than one tuple of the record could have a.I keythat matches.IR name .Only the first matching tuple is returned.If an application makes use ofmultiple tuples with the same.I keythen the.IB record .tuplelist will have to be handled explicitly by the application..TP.IB tuple .lookup( name )Returns the first attribute in.I tuplewhose name matches.IR name .The return value is the value of the attribute or.B nilif no matching attribute was found..SH SOURCE.B /appl/lib/cfg.b