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