2024-10-21 23:07:26 -06:00
|
|
|
use fmt;
|
2024-10-22 11:17:02 -06:00
|
|
|
use io;
|
2024-10-04 22:40:43 -06:00
|
|
|
use locale;
|
|
|
|
use strings;
|
|
|
|
|
2024-10-03 17:52:17 -06:00
|
|
|
// A line in the file, which can be a comment (or a blank line), an entry, or
|
|
|
|
// a localized entry.
|
2024-10-19 14:15:23 -06:00
|
|
|
export type line = (blank | comment | group_header | entry);
|
2024-10-12 20:12:43 -06:00
|
|
|
|
|
|
|
// A blank line.
|
|
|
|
// Specification: §3.1
|
|
|
|
export type blank = void;
|
2024-10-03 17:52:17 -06:00
|
|
|
|
|
|
|
// A comment.
|
|
|
|
// Specification: §3.1
|
2024-10-04 22:40:43 -06:00
|
|
|
export type comment = str;
|
2024-10-03 17:52:17 -06:00
|
|
|
|
2024-10-19 13:23:28 -06:00
|
|
|
// A group header.
|
|
|
|
// Specification: §3.2
|
|
|
|
export type group_header = str;
|
|
|
|
|
2024-10-19 14:23:13 -06:00
|
|
|
// An entry in a desktop file. Entries without an explicitly stated locale are
|
|
|
|
// assigned [[locale::c]].
|
2024-10-19 14:15:23 -06:00
|
|
|
// Specification: §3.3, §5
|
|
|
|
export type entry = struct {
|
|
|
|
group: str,
|
|
|
|
key: str,
|
|
|
|
value: str,
|
|
|
|
locale: locale::locale,
|
|
|
|
};
|
2024-10-19 13:23:28 -06:00
|
|
|
|
|
|
|
// Duplicates an [[entry]]. Use [[entry_finish]] to get rid of it.
|
2024-10-19 14:15:23 -06:00
|
|
|
export fn entry_dup(entr: entry) entry = entry {
|
|
|
|
group = strings::dup(entr.group),
|
|
|
|
key = strings::dup(entr.key),
|
|
|
|
value = strings::dup(entr.value),
|
|
|
|
locale = locale::dup(entr.locale),
|
|
|
|
};
|
2024-10-19 13:23:28 -06:00
|
|
|
|
|
|
|
// Frees memory associated with an [[entry]].
|
|
|
|
export fn entry_finish(entr: entry) void = {
|
2024-10-19 14:15:23 -06:00
|
|
|
free(entr.group);
|
|
|
|
free(entr.key);
|
|
|
|
free(entr.value);
|
|
|
|
locale::finish(entr.locale);
|
2024-10-04 22:40:43 -06:00
|
|
|
};
|
2024-10-21 23:07:26 -06:00
|
|
|
|
|
|
|
// Formats a [[line]] and writes it to an [[io::handle]].
|
|
|
|
export fn fprint(output: io::handle, lin: line) (size | io::error) = match (lin) {
|
2024-10-22 11:17:02 -06:00
|
|
|
case blank => yield fmt::fprintln(output);
|
|
|
|
case let lin: comment => yield fmt::fprintf(output, "#{}\n", lin: str);
|
|
|
|
case let lin: group_header => yield fmt::fprintf(output, "[{}]\n", lin: str);
|
2024-10-21 23:07:26 -06:00
|
|
|
case let lin: entry =>
|
|
|
|
let wrote = fmt::print(lin.key)?;
|
|
|
|
if (!locale::equal(locale::c, lin.locale)) {
|
|
|
|
let localestr = locale::format(lin.locale);
|
|
|
|
defer free(localestr);
|
|
|
|
wrote += fmt::printf("[{}]", localestr)?;
|
|
|
|
};
|
|
|
|
wrote += fmt::printf("={}\n", lin.value)?;
|
|
|
|
yield wrote;
|
|
|
|
};
|