hare-xdg/format/desktop_entry/scan.ha

112 lines
2.9 KiB
Hare
Raw Normal View History

2024-10-12 20:12:43 -06:00
use bufio;
2024-10-12 21:34:57 -06:00
use encoding::utf8;
2024-10-12 21:43:07 -06:00
use errors;
2024-10-12 20:12:43 -06:00
use io;
2024-10-12 21:34:57 -06:00
use locale;
2024-10-12 20:12:43 -06:00
use memio;
2024-10-12 21:34:57 -06:00
use strings;
2024-10-12 20:12:43 -06:00
export type scanner = struct {
scanner: bufio::scanner,
entry: entry,
localized_entry: localized_entry,
};
// Creates a desktop entry file scanner. Use [[next]] to read lines. The caller
// must call [[finish]] once they're done with this object.
export fn scan(input: io::handle) scanner = scanner {
scanner = bufio::newscanner(input),
...
};
2024-10-12 20:12:43 -06:00
// Returns the next entry from a desktop entry file. The return value is
// borrowed from the [[scanner]] use [[line_dup]] to retain a copy.
export fn next(this: *scanner) (line | io::EOF | error) = {
let text = match (bufio::scan_line(&this.scanner)) {
case let text: const str =>
yield text;
case let err: (io::error | utf8::invalid) =>
return err;
case io::EOF =>
return io::EOF;
};
2024-10-12 20:12:43 -06:00
if (text == "") {
// blank line
return blank;
} else if (strings::hasprefix(text, '#')) {
// comment
return strings::ltrim(text, '#'): comment;
} else if (strings::hasprefix(text, '[')) {
// group header
return parse_group_header(text)?;
2024-10-12 20:12:43 -06:00
} else {
// key/value pair
let (key, valu, local) = parse_entry(text)?;
match (local) {
case let local: locale::locale =>
localized_entry_finish(this.localized_entry);
this.localized_entry = (key, valu, local);
return this.localized_entry;
case void =>
entry_finish(this.entry);
this.entry = (key, valu);
return this.entry;
2024-10-12 20:12:43 -06:00
};
};
2024-10-12 21:43:07 -06:00
};
// Frees resources associated with a [[scanner]].
export fn finish(this: *scanner) void = {
bufio::finish(&this.scanner);
entry_finish(this.entry);
2024-10-12 20:12:43 -06:00
};
// memory is borrowed from the input
fn parse_group_header(text: str) (group_header | error) = {
2024-10-12 21:34:57 -06:00
if (!strings::hasprefix(text, '[') || !strings::hassuffix(text, ']')) {
return invalid_group_header;
2024-10-12 20:12:43 -06:00
};
text = strings::rtrim(strings::ltrim(text, '['), ']');
2024-10-12 21:34:57 -06:00
if (strings::contains(text, '[', ']')) {
return invalid_group_header;
2024-10-12 20:12:43 -06:00
};
return text: group_header;
2024-10-12 20:12:43 -06:00
};
// memory must be freed by the caller
fn parse_entry(line: str) ((str, str, (locale::locale | void)) | error) = {
2024-10-12 21:34:57 -06:00
if (!strings::contains(line, '=')) return invalid_entry;
let (key, valu) = strings::cut(line, "=");
2024-10-12 20:12:43 -06:00
key = strings::ltrim(strings::rtrim(key));
2024-10-12 21:34:57 -06:00
if (!validate_entry_key(key)) return invalid_entry;
2024-10-12 20:12:43 -06:00
2024-10-12 21:34:57 -06:00
let local: (locale::locale | void) = void;
let (key, local_string) = strings::cut(key, "[");
2024-10-12 20:12:43 -06:00
if (local_string != "") {
2024-10-12 21:43:07 -06:00
local_string = strings::rtrim(local_string, ']');
2024-10-12 21:34:57 -06:00
if (!validate_entry_locale(local_string)) return invalid_entry;
2024-10-12 20:12:43 -06:00
2024-10-12 21:43:07 -06:00
local = match(locale::parse(local_string)) {
2024-10-12 21:34:57 -06:00
case let local: locale::locale => yield local;
2024-10-12 21:51:52 -06:00
case locale::invalid => return invalid_entry;
2024-10-12 20:12:43 -06:00
};
};
2024-10-12 21:34:57 -06:00
return (strings::dup(key), strings::dup(valu), local);
2024-10-12 20:12:43 -06:00
};
fn validate_entry_key(key: str) bool = {
return strings::contains(key, '[', ']', '=');
};
fn validate_entry_locale(key: str) bool = {
return strings::contains(key, '[', ']', '=');
};