-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetab
executable file
·48 lines (42 loc) · 1.27 KB
/
detab
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
#!/usr/bin/env escript
%% settabs -- set initial tab stops
%%
%% We can't generate a list/array because we don't have a MAXLINE.
%% settabs returns the TabSpace value instead.
settabs() ->
TabSpace = 4,
TabSpace.
%% tabpos -- return true if col is a tab stop
%%
%% Doesn't make sense in using MAXLINE since Erlang handle
%% the EOF and buffer size for us, unless we're using
%% io:get_chars/2 because the second parameter of the said
%% function is exactly a limiter.
tabpos(Col) ->
TabSpace = settabs(),
TabSpace - Col rem TabSpace.
%% detab - convert tabs to blanks
%%
%% It doesn't work exactely as the Pascal version.
%% See settabs/0 and tappos/1 for details.
%%
%% BUGS
%% Special characters other than 9, 10, 32 might transform
%% the return string in numbers and screw everything.
detab() ->
Input = ststd:getf(),
detab(Input, 0, []).
detab([], _, Acc) ->
ststd:putf("~p~n", [Acc]);
detab([9|T], Col, Acc) ->
Feed = tabpos(Col),
Blanks = lists:map(fun(_) -> 32 end, lists:seq(1, Feed)),
detab(T, Col+Feed, lists:concat([Acc, Blanks]));
detab([H|T], Col, Acc) ->
detab(T, Col+1, lists:concat([Acc, [H]])).
main([]) ->
detab();
main(Arg) ->
{ok, Bin} = file:read_file(Arg),
File = erlang:binary_to_list(Bin),
detab(File, 0, []).