-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage.m
136 lines (123 loc) · 3.62 KB
/
package.m
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
:- module package.
:- interface.
:- import_module list, io.
:- type licenses
---> gpl
; bsd
; mit
; other.
:- type vcs
---> git
; fossil.
:- type releases
---> head
; tag.
:- type foreign
---> c
; erlang
; java
; csharp.
:- type package
---> package(
name :: string,
license :: licenses,
vcs :: vcs,
release :: releases,
url :: string,
tags :: list(string),
libs :: list(string),
apps :: list(string),
deps :: list(string),
foreign :: list(foreign),
description :: string,
comment :: string
).
:- type valid_result
---> ok
; error(string).
% validate(Package, Result)
%
% presently this just rejects some special characters
%
:- pred validate(package::in, valid_result::out) is det.
% tidy(Package0) = Package
%
% trims strings and removes zero-length strings from lists.
% TODO: normalize UTF8
:- func tidy(package) = package.
% load_packages(PackagesResult, Filename, !IO)
%
% Produce a list of packages from a file containing one package term on
% each line.
%
:- pred load_packages(io.res(list(package))::out, string::in,
io::di, io::uo) is det.
% load_packages_from_stream(PackagesResult, Stream, !IO)
:- pred load_packages_from_stream(io.res(list(package))::out,
io.text_input_stream::in,
io::di, io::uo) is det.
%----------------------------------------------------------------------%
:- implementation.
:- import_module string, char, int.
load_packages(Res, Filename, !IO) :-
io.open_input(Filename, Res0, !IO),
(
Res0 = ok(Stream),
load_packages_from_stream(Res, Stream, !IO),
io.close_input(Stream, !IO)
;
Res0 = error(E),
Res = error(E)
).
load_packages_from_stream(Res, Stream, !IO) :-
load_packages_from_stream([], 1, Res, Stream, !IO).
:- pred load_packages_from_stream(
list(package)::in,
int::in,
io.res(list(package))::out,
io.text_input_stream::in,
io::di, io::uo).
load_packages_from_stream(Acc, LineNo, Res, Stream, !IO) :-
io.read_line_as_string(Stream, Res0, !IO),
(
Res0 = ok(Line),
Term = chomp(Line),
io.read_from_string("packages file", Term, length(Term),
Res1, io.posn(LineNo, 0, 0), _),
(
Res1 = ok(Package),
load_packages_from_stream([Package | Acc], LineNo + 1, Res, Stream, !IO)
;
Res1 = eof,
Res = ok(Acc)
;
Res1 = error(Reason, N),
Error = string.format("%s on line %d",
[s(Reason), i(N)]),
Res = error(make_io_error(Error))
)
;
Res0 = eof,
Res = ok(Acc)
;
Res0 = error(E),
Res = error(E)
).
validate(P, R) :-
( if not all_match(valid_char, string(P)) then
R = error("package term contains control chars (newline, ESC, NUL, etc.)")
else
R = ok
).
:- pred valid_char(char::in) is semidet.
valid_char(C) :-
not (
to_int(C) < to_int(' ') % control chars, \n, etc.
).
tidy(package(Name, L, V, R, Url, Tags, Libs, Apps, Deps, F, Desc, Comment)) =
package(strip(Name), L, V, R, strip(Url),
nz(Tags), nz(Libs), nz(Apps), nz(Deps),
F, strip(Desc), strip(Comment)).
% remove zero-length strings from lists of strings
:- func nz(list(string)) = list(string).
nz(L) = negated_filter(unify(""), L).