-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.ab
More file actions
40 lines (33 loc) · 1.23 KB
/
string.ab
File metadata and controls
40 lines (33 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Amber-NAR © Karim Vergnes <me@thesola.io>
// Licensed under GNU GPLv2
// NARString extraction utility
import { parse_int } from "std/text"
/// Calculate the 8-byte aligned size of a NAR string from its effective size.
pub fun get_padded_length(length: Num): Num
{
let padding = 8 - (length % 8)
if padding < 8:
return length + padding
else:
return length
}
/// Assuming the file descriptor is at the start of a NARString, returns
/// the length of the NARString leaving the file descriptor at the end of the
/// length block.
///
/// Can be used to skip unwanted NARStrings.
pub fun read_nar_length(file: Text = "/dev/stdin", offset: Num = 0): Num?
{
let length_raw = $tail -c -{offset} < {file} | od -t d8 -N8 -An$?
return parse_int(length_raw)?
}
/// Assuming the file descriptor is at the start of a NARString, reads and
/// returns the value of that NARString leaving the file descriptor at the
/// start of the next one.
pub fun read_nar_string(file: Text = "/dev/stdin", offset: Num = 0): Text?
{
let length = read_nar_length(file, offset)?
let padded_length = get_padded_length(length)
let padding = padded_length - length
return $tail -c -{offset + 8} < {file} | head -c {length}$?
}