-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoo_reader.erl
50 lines (43 loc) · 1.51 KB
/
coo_reader.erl
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
-module(coo_reader).
-export([read_Pattern/1, start/1]).
start(NAME) ->
{ok, File2} = file:open(NAME, [read]),
_COO =
read_Pattern(File2). %%io:format( "I've read ~w~n",[COO])
%%%% get_coordinates(LINE) %%%%%%%%%%
%% given as input a Line containing indefinete number of colons, reads the coordinates a puts them in a List
%%
%% + returns a List of coordinates
%%%%%% read_Pattern(File)
%% reads a new line (a string) form the file descriptor File, separates the substring separated by " ", and converts them into a list of numbers %%
%% (integesr or float)
%%
%% + returns: a list of values
%% + calls: convert(Line)
%%
read_Pattern(File) ->
case io:get_line(File, "") of
eof -> file:close(File);
Line ->
io:format("I've read ~s~n", [Line]),
Pattern = string:tokens(Line, "\t"),
%%X=lists:nth(1,Line),
Numbers = convert(Pattern),
io:format("READ : Pattern: ~s Numbers :~w~n, ",
[Pattern, Numbers])
end.
%%%% convert(LINE) %%%%%%%%%%
%% given as input a List of Tokens (String) containing the BitString form of a floating point or a integer, returns a List of corrisponding integers or %%% float
%%
%% es: convert(["22.2","1","33.241235"]) -> [22.2, 1, 33.241235]
%%
%% + returns a List of coordinates
convert(P) -> convert(P, []).
convert([], TEMP) -> TEMP;
convert([HEAD | TAIL], TEMP) ->
case string:to_float(HEAD) of
{error, _} ->
convert(TAIL,
TEMP ++ [element(1, string:to_integer(HEAD))]);
{Number, _Empty} -> convert(TAIL, TEMP ++ [Number])
end.