1
%% ``The contents of this file are subject to the Erlang Public License,
2
%% Version 1.1, (the "License"); you may not use this file except in
3
%% compliance with the License. You should have received a copy of the
4
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
7
%% Software distributed under the License is distributed on an "AS IS"
8
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
%% the License for the specific language governing rights and limitations
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
16
%% $Id: asn1ct_name.erl,v 1.1 2008/12/17 09:53:30 mikpe Exp $
20
%%-compile(export_all).
21
-export([name_server_loop/1,
36
start_server(asn1_ns, asn1ct_name,name_server_loop,[[]]).
38
stop() -> stop_server(asn1_ns).
40
name_server_loop(Vars) ->
41
%% io:format("name -- ~w~n",[Vars]),
43
{From,{current,Variable}} ->
44
From ! {asn1_ns,get_curr(Vars,Variable)},
45
name_server_loop(Vars);
46
{From,{pop,Variable}} ->
47
From ! {asn1_ns,done},
48
name_server_loop(pop_var(Vars,Variable));
49
{From,{push,Variable}} ->
50
From ! {asn1_ns,done},
51
name_server_loop(push_var(Vars,Variable));
52
{From,{delete,Variable}} ->
53
From ! {asn1_ns,done},
54
name_server_loop(delete_var(Vars,Variable));
55
{From,{new,Variable}} ->
56
From ! {asn1_ns,done},
57
name_server_loop(new_var(Vars,Variable));
58
{From,{prev,Variable}} ->
59
From ! {asn1_ns,get_prev(Vars,Variable)},
60
name_server_loop(Vars);
61
{From,{next,Variable}} ->
62
From ! {asn1_ns,get_next(Vars,Variable)},
63
name_server_loop(Vars);
65
From ! {asn1_ns,stopped},
76
asn1_ns ! {self(), Req},
77
receive {asn1_ns, Reply} -> Reply end.
79
pop(V) -> req({pop,V}).
80
push(V) -> req({push,V}).
81
clear() -> req(stop), start().
82
curr(V) -> req({current,V}).
83
new(V) -> req({new,V}).
84
delete(V) -> req({delete,V}).
88
exit('cant get prev of none');
95
exit('cant get next of none');
103
lists:reverse(generate(V,last(Curr),[],0))
106
generate(V,Number,Res,Pos) ->
112
generate(V,Number,[list_to_atom(lists:concat([V,Ell]))|Res],Ell)
116
last2(lists:reverse(atom_to_list(V))).
119
list_to_integer(lists:reverse(get_digs(RevL))).
131
push_var(Vars,Variable) ->
132
case lists:keysearch(Variable,1,Vars) of
134
[{Variable,[0]}|Vars];
135
{value,{Variable,[Digit|Drest]}} ->
136
NewVars = lists:keydelete(Variable,1,Vars),
137
[{Variable,[Digit,Digit|Drest]}|NewVars]
140
pop_var(Vars,Variable) ->
141
case lists:keysearch(Variable,1,Vars) of
144
{value,{Variable,[_Dig]}} ->
145
lists:keydelete(Variable,1,Vars);
146
{value,{Variable,[_Dig|Digits]}} ->
147
NewVars = lists:keydelete(Variable,1,Vars),
148
[{Variable,Digits}|NewVars]
151
get_curr([],Variable) ->
153
get_curr([{Variable,[0|_Drest]}|_Tail],Variable) ->
155
get_curr([{Variable,[Digit|_Drest]}|_Tail],Variable) ->
156
list_to_atom(lists:concat([Variable,integer_to_list(Digit)]));
158
get_curr([_|Tail],Variable) ->
159
get_curr(Tail,Variable).
161
new_var(Vars,Variable) ->
162
case lists:keysearch(Variable,1,Vars) of
164
[{Variable,[1]}|Vars];
165
{value,{Variable,[Digit|Drest]}} ->
166
NewVars = lists:keydelete(Variable,1,Vars),
167
[{Variable,[Digit+1|Drest]}|NewVars]
170
delete_var(Vars,Variable) ->
171
case lists:keysearch(Variable,1,Vars) of
174
{value,{Variable,[N]}} when N =< 1 ->
175
lists:keydelete(Variable,1,Vars);
176
{value,{Variable,[Digit|Drest]}} ->
181
NewVars = lists:keydelete(Variable,1,Vars),
182
[{Variable,[Digit-1|Drest]}|NewVars]
186
get_prev(Vars,Variable) ->
187
case lists:keysearch(Variable,1,Vars) of
190
{value,{Variable,[Digit|_]}} when Digit =< 1 ->
192
{value,{Variable,[Digit|_]}} when Digit > 1 ->
193
list_to_atom(lists:concat([Variable,
194
integer_to_list(Digit-1)]));
199
get_next(Vars,Variable) ->
200
case lists:keysearch(Variable,1,Vars) of
202
list_to_atom(lists:concat([Variable,"1"]));
203
{value,{Variable,[Digit|_]}} when Digit >= 0 ->
204
list_to_atom(lists:concat([Variable,
205
integer_to_list(Digit+1)]));
212
stop_server(Name, whereis(Name)).
213
stop_server(_Name, undefined) -> stopped;
214
stop_server(Name, _Pid) ->
215
Name ! {self(), stop},
216
receive {Name, _} -> stopped end.
219
start_server(Name,Mod,Fun,Args) ->
220
case whereis(Name) of
222
register(Name, spawn(Mod,Fun, Args));