hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

system.ha (1184B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bufio;
      5 use encoding::utf8;
      6 use fs;
      7 use io;
      8 use os;
      9 use strings;
     10 
     11 @init fn init() void = {
     12 	// Done in a separate function so we can discard errors here
     13 	load_systemdb(): void;
     14 };
     15 
     16 fn load_systemdb() (void | fs::error | io::error | utf8::invalid) = {
     17 	const file = os::open(SYSTEM_DB)?;
     18 	defer io::close(file)!;
     19 
     20 	let sc = bufio::newscanner(file);
     21 	defer bufio::finish(&sc);
     22 
     23 	for (let line => bufio::scan_line(&sc)?) {
     24 		line = strings::trim(line);
     25 		if (strings::hasprefix(line, "#") || len(line) == 0) {
     26 			continue;
     27 		};
     28 
     29 		const items = strings::cut(line, "\t");
     30 		const mime = strings::trim(items.0),
     31 			exts = strings::trim(items.1);
     32 		if (len(exts) == 0) {
     33 			continue;
     34 		};
     35 
     36 		const tok = strings::tokenize(exts, " ");
     37 		let entry = alloc(mimetype {
     38 			mime = strings::dup(mime),
     39 			exts = [],
     40 		})!;
     41 		for (let ext => strings::next_token(&tok)) {
     42 			append(entry.exts, strings::dup(ext))!;
     43 		};
     44 		register_heap(entry);
     45 	};
     46 };
     47 
     48 fn register_heap(mime: *mimetype) void = {
     49 	let i = len(heap_db);
     50 	append(heap_db, mime)!;
     51 	for (i < len(heap_db); i += 1) {
     52 		hashtable_insert(heap_db[i]);
     53 	};
     54 };