aboutsummaryrefslogtreecommitdiffstats
path: root/lib/asn1/src/asn1ct_name.erl
blob: c0c2ed302cfb6d46a811a9aaed153e4dbeffc43b (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2012. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
%%
-module(asn1ct_name).

%%-compile(export_all).
-export([start/0,
	 curr/1,
	 clear/0,
	 prev/1,
	 next/1,
	 all/1,
	 new/1]).

start() ->
    Parent = self(),
    case get(?MODULE) of
	undefined ->
            put(?MODULE, spawn_link(fun() ->
                            Ref = monitor(process, Parent),
                            name_server_loop({Ref,Parent},[])
                    end)),
            ok;
	_Pid ->
	    %% Already started. Clear the variables.
	    clear()
    end.

name_server_loop({Ref, Parent} = Monitor,Vars) ->
%%    io:format("name -- ~w~n",[Vars]),
    receive
	{_From,clear} ->
	    name_server_loop(Monitor, []);
	{From,{current,Variable}} ->
	    From ! {?MODULE,get_curr(Vars,Variable)},
	    name_server_loop(Monitor,Vars);
	{_From,{new,Variable}} ->
	    name_server_loop(Monitor,new_var(Vars,Variable));
	{From,{prev,Variable}} ->
	    From ! {?MODULE,get_prev(Vars,Variable)},
	    name_server_loop(Monitor,Vars);
	{From,{next,Variable}} ->
	    From ! {?MODULE,get_next(Vars,Variable)},
	    name_server_loop(Monitor,Vars);
	{'DOWN', Ref, process, Parent, Reason} ->
	    exit(Reason)
    end.

req(Req) ->
    Pid = get(?MODULE),
    Ref = monitor(process, Pid),
    Pid ! {self(), Req},
    receive
        {?MODULE, Reply} ->
	    Reply;
	{'DOWN', Ref, process, Pid, Reason} ->
            error({name_server_died,Reason})
    end.

cast(Req) ->
    get(?MODULE) ! {self(), Req},
    ok.

clear() ->     cast(clear).
curr(V) ->     req({current,V}).
new(V) ->      cast({new,V}).

prev(V) ->
    case req({prev,V}) of
	none ->
	    exit('cant get prev of none');
	Rep -> Rep
    end.

next(V) ->
    req({next,V}).
    
all(V) ->
    Curr = curr(V),
    if Curr == V -> [];
	true ->
	    lists:reverse(generate(V,last(Curr),[],0))
    end.

generate(V,Number,Res,Pos) ->
    Ell = Pos+1,
    if
	Ell > Number ->
	    Res;
	true ->
	    generate(V,Number,[list_to_atom(lists:concat([V,Ell]))|Res],Ell)
    end.
		
last(V) ->
    last2(lists:reverse(atom_to_list(V))).

last2(RevL) ->
    list_to_integer(lists:reverse(get_digs(RevL))).
    
    
get_digs([H|T]) ->
    if
	H < $9+1,
	H > $0-1 ->
	    [H|get_digs(T)];
	true ->
	    []
    end.

get_curr([], Variable) ->
    Variable;
get_curr([{Variable,Digit}|_Tail], Variable) ->
    list_to_atom(lists:concat([Variable,Digit]));
get_curr([_|Tail], Variable) ->
    get_curr(Tail, Variable).

new_var(Vars, Variable) ->
    case lists:keyfind(Variable, 1, Vars) of
	false ->
	    [{Variable,1}|Vars];
	{Variable,Digit} ->
	    NewVars = lists:keydelete(Variable, 1, Vars),
	    [{Variable,Digit+1}|NewVars]
    end.

get_prev(Vars, Variable) ->
    case lists:keyfind(Variable, 1, Vars) of
	false ->
	    none;
	{Variable,Digit} when Digit =< 1 ->
	    Variable;
	{Variable,Digit} when Digit > 1 ->
	    list_to_atom(lists:concat([Variable,Digit-1]))
    end.

get_next(Vars, Variable) ->
    case lists:keyfind(Variable, 1, Vars) of
	false ->
	    list_to_atom(lists:concat([Variable,"1"]));
	{Variable,Digit} when Digit >= 0 ->
	    list_to_atom(lists:concat([Variable,Digit+1]))
    end.