-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_flow.m
56 lines (49 loc) · 2.22 KB
/
parse_flow.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
function flow = parse_flow(filename)
flow = struct('title', '', ...
'stamp', '', ...
'steps', struct('name',{}, 'started',{}, 'actions', {}), ...
'messages', struct('text',{}, 'stamp',{}), ...
'answers', containers.Map);
fid = fopen(filename);
line = fgetl(fid);
while ischar(line)
line = strtrim(line);
if ~isempty(line)
tokens = regexp(line, '^(.*?) \[([^\]]+)\]$', 'tokens');
text = tokens{1}{1};
stamp = datestr(str2double(tokens{1}{2})/86400 + datenum(1970,1,1));
if isempty(flow.title) % haven't read title yet: must be here
flow.title = text;
flow.stamp = stamp;
elseif text(1) == '-' % beginning of a step
i = length(flow.steps) + 1;
flow.steps(i).name = text(3:end);
flow.steps(i).started = stamp;
flow.steps(i).actions = struct('name',{}, 'started',{});
elseif text(1) == '>' % it's an answered question
% question without range
tokens = regexp(text, '^> "([^"]+)" \["([^"]+)"\]$', 'tokens');
if isempty(tokens)
% question with range
tokens = regexp(text, '^> "([^"]+)" \([^)]+\) \[([^\]]+)\]$', 'tokens');
if isempty(tokens)
% question with range (buggy format)
tokens = regexp(text, '^> "([^"]+) \([^)]+\)" \[([^\]]+)\]$', 'tokens');
end
end
flow.answers(tokens{1}{1}) = struct('text', tokens{1}{2}, 'stamp', stamp);
elseif text(1) == '"' % it's a message
i = length(flow.messages) + 1;
flow.messages(i).text = text(2:end-1);
flow.messages(i).stamp = stamp;
else % must be an action
i = length(flow.steps);
j = length(flow.steps(i).actions) + 1;
flow.steps(i).actions(j).name = text;
flow.steps(i).actions(j).started = stamp;
end
end
line = fgetl(fid);
end
fclose(fid);
end