-
Notifications
You must be signed in to change notification settings - Fork 9
Generic modules IniFile
This generic module helps you to create lenses for INI files.
http://en.wikipedia.org/wiki/INI_file
let settings = IniFile.entry "allow_non-us_software" | IniFile.entry "allow_unsigned_uploads" | IniFile.entry "check_version" | IniFile.entry "default_host_main" | IniFile.entry "default_host_non-us" | IniFile.entry "fqdn" | IniFile.entry "hash" | IniFile.entry "incoming" | IniFile.entry "login" | IniFile.entry "method" | IniFile.entry "passive_ftp" | IniFile.entry "post_upload_command" | IniFile.entry "pre_upload_command" | IniFile.entry "run_dinstall" | IniFile.entry "run_lintian" | IniFile.entry "scp_compress" let record = IniFile.record "target" settings let lns = IniFile.lns record let filter = (incl "/etc/dput.cf") . (incl "~/.dput.cf") . Util.stdexcl let xfm = transform lns filter
This class provides several useful types for INI files.
All these ypes can be referred to by using:
IniFile.$type $type_argument*
General usage:
let mycomment = IniFile.comment
The module currently provides 3 types of comments.
Definition:
let comment_generic (pattern:regexp) = [ label "comment" . del pattern "; " . value_to_eol . eol ]
This is the most generic type of comment, which takes a pattern as an argument. Use this if you need to refine the pattern for comments.
Definition:
let comment = comment_generic /[ \t]*(#|;)[ \t]*/
This is the default kind of comment. In can be indented and the comment character might be either "#" or ";".
Definition:
let comment_nosharp = comment_generic /[ \t]*;[ \t]*/
Some implementations of INI files do not support "#" as a valid comment character. If it is your case, you can use this definition to support only ";".
General usage:
let myentry = IniFile.entry
The module currently supports 5 types of entries.
Definition:
let entry_generic (kw:regexp) (sep:lens) (comment:lens) = [ key kw . sep . value_to_comment? . (comment|eol) ]
This is the most generic definition of an entry. It takes 3 arguments: the keyword (regexp), the separator (lens) and the type of comment (lens).
You can use this definition if none of the other definitions fit your needs.
Definition:
let entry (kw:regexp) = entry_generic kw value_sepwithcolon comment
This is the default definition of an entry. It takes a keyword (regexp) as argument. It supports both "=" and ":" as separators and trailing comments using both ";" and "#".
Definition:
let entry_setcomment (kw:regexp) (comment:lens) = entry_generic kw value_sepwithcolon comment
This definition allows you to choose the type of comment to use. You might want to use this if you are using comment_nosharp or if your format does not support trailing comments.
Definition:
let entry_nocolon (kw:regexp) = entry_generic kw value_sep comment
This is the same as the default entry definition, but it does not allow ":" as a separator.
Definition:
let entry_nocolon_setcomment (kw:regexp) (comment:lens) = entry_generic kw value_sep comment
Same as entry_nocolon, but lets you set the type of comment to use.
IniFile.record represents an Ini file record, e.g. "[record]\nfield = value\n"
General usage:
let myrecord = IniFile.record "record_label" myentry
There are currently 4 types of it.
Definition:
let record (label_name:string) (entry:lens) = [ label label_name . title . (entry | comment | empty)* ]
This is a default record. It takes two arguments: the label name (string) and the entry lens (lens).
Definition:
let record_setcomment (label_name:string) (entry:lens) (comment:lens) = [ label label_name . title . (entry | comment | empty)* ]
Same as record, but allows you to choose the type of comment to use. This is useful if you wish to use comment_nosharp or to make up your own kind of comment lens.
Definition:
let record_noempty (label_name:string) (entry:lens) = [ label label_name . title . (entry | comment)* ]
Some implementations of INI File do not allow empty lines. This is the default record type implementing this behavior.
Definition:
let record_noempty_setcomment (label_name:string) (entry:lens) (comment:lens) = [ label label_name . title . (entry | comment)* ]
Same as record_noempty, but lets you set the type of comment to use.
General usage:
let lns = IniFile.lns myrecord
IniFile.lns represents an Ini file lens.
In effect, it implements '( comment | empty | record )*', using the IniFile definition of empty and comment.
Note: It might be interesting to make other optional lns types, such as lns_nocomment which would remove comments from the tree.
There are currently 4 types of lns types.
Definition:
let lns (record:lens) = ( comment | empty )* . record*
This is the default type of lns. It takes one argument: the record lens.
Definition:
let lns_setcomment (record:lens) (comment:lens) = ( comment | empty )* . record*
Same as lns, but lets you choose the type of comment to use.
Definition:
let lns_noempty (record:lens) = comment* . record*
Same as lns, but does not support empty lines.
Definition:
let lns_noempty_setcomment (record:lens) (comment:lens) = comment* . record*
Same as lns_noempty, but lets you choose the type of comment to use.
- Support double quotes in values (e.g. 'field1 = "value2" ). This is allowed by some implementations of INI files.