Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(var).
- -compile(export_all).
- start(Pref,FileName)->
- {ok, Bin}=file:read_file(FileName),
- case ets:info(dic) of
- undefined->ok;
- _->ets:delete(dic),ok
- end,
- ets:new(dic,[ordered_set,named_table]),
- ets:insert(dic,{iterator,0}),
- FileTpl=lists:foldl(
- fun(B, Acc) ->
- R = case re:run(B,"^([^=]+=)(.+)$",[global]) of
- {match,LVal}->
- re_to_var(Pref,B,LVal,<<"">>)
- %%io:format("~p~n",[LL]),
- %%{LCh,LVal}
- ;
- _LVal->B
- end,
- <<Acc/binary, R/binary >>
- end,<<"">>, binary:split(Bin,<<"\n">>,[global])),
- file:write_file(FileName ++ ".vars",
- <<<<Var/binary,"=",Val/binary,"\r">>||{Var,Val}<-ets:tab2list(dic),Var=/=iterator>>
- ),
- file:write_file(FileName ++ ".tpl", FileTpl)
- .
- %%%%%%%%%%%
- re_to_var(Pref,B,[],Acc)->Acc;
- re_to_var(Pref,B,[[{S1,S2},{E1,E2},{V1,V2}]|T],Acc)->
- Var = binary:part(B,E1,E2),
- Val = binary:replace(binary:part(B,V1,V2),<<"\r">>, <<"">>),
- Env=binary:replace(binary:replace(Var, <<"][">>, <<"_">>,[global]), <<"$_ENV">>, <<"">>,[global]),
- Env1=erlang:list_to_binary([
- Pref|
- string:to_upper([X||<<X>><=Env,(X>=$A andalso X=<$Z) orelse (X>=$a andalso X=<$z) orelse (X>=$0 andalso X=<$9) orelse (X=:=$_)])
- ]),
- II = case ets:lookup(dic,Env1) of
- [{_,_}]->erlang:list_to_binary(erlang:integer_to_list(ets:update_counter(dic, iterator, 1)));
- _-><<"">>
- end,
- {Val1,Dot}= case binary:last(Val) of
- $;-> {binary:part(Val,0,byte_size(Val)-1),<<";\n">>};
- _->{Val,<<"\n">>}
- end,
- ets:insert(dic,{<<Env1/binary,II/binary>>,<<Val1/binary>>}),
- re_to_var(Pref,B,T,<<Acc/binary, Var/binary, Env1/binary,II/binary, Dot/binary>>).
- %%var:start("CRON_","config.php").
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement