Advertisement
krot

var.erl

Oct 8th, 2020
4,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.66 KB | None | 0 0
  1. -module(var).
  2.  -compile(export_all).
  3.  
  4. start(Pref,FileName)->
  5.  
  6.     {ok, Bin}=file:read_file(FileName),
  7.  
  8.     case ets:info(dic) of
  9.     undefined->ok;
  10.     _->ets:delete(dic),ok
  11.     end,
  12.  
  13.     ets:new(dic,[ordered_set,named_table]),
  14.     ets:insert(dic,{iterator,0}),
  15.  
  16.     FileTpl=lists:foldl(
  17.     fun(B, Acc) ->
  18.  
  19.     R = case re:run(B,"^([^=]+=)(.+)$",[global]) of
  20.     {match,LVal}->
  21.     re_to_var(Pref,B,LVal,<<"">>)
  22.     %%io:format("~p~n",[LL]),
  23.     %%{LCh,LVal}
  24.     ;
  25.  
  26.     _LVal->B
  27.     end,
  28.     <<Acc/binary, R/binary >>
  29.     end,<<"">>, binary:split(Bin,<<"\n">>,[global])),
  30.    
  31.    
  32.  
  33.     file:write_file(FileName ++ ".vars",
  34.                 <<<<Var/binary,"=",Val/binary,"\r">>||{Var,Val}<-ets:tab2list(dic),Var=/=iterator>>
  35.                 ),
  36.  
  37.     file:write_file(FileName ++ ".tpl", FileTpl)
  38. .
  39. %%%%%%%%%%%
  40. re_to_var(Pref,B,[],Acc)->Acc;
  41.  
  42. re_to_var(Pref,B,[[{S1,S2},{E1,E2},{V1,V2}]|T],Acc)->
  43.      
  44.     Var = binary:part(B,E1,E2),
  45.     Val = binary:replace(binary:part(B,V1,V2),<<"\r">>, <<"">>),
  46.     Env=binary:replace(binary:replace(Var, <<"][">>, <<"_">>,[global]), <<"$_ENV">>, <<"">>,[global]),
  47.      
  48.     Env1=erlang:list_to_binary([
  49.     Pref|
  50.     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=:=$_)])
  51.     ]),
  52.    
  53.     II = case ets:lookup(dic,Env1) of
  54.     [{_,_}]->erlang:list_to_binary(erlang:integer_to_list(ets:update_counter(dic, iterator, 1)));
  55.     _-><<"">>
  56.     end,
  57.  
  58.     {Val1,Dot}= case binary:last(Val) of
  59.     $;-> {binary:part(Val,0,byte_size(Val)-1),<<";\n">>};
  60.     _->{Val,<<"\n">>}
  61.     end,
  62.      
  63.  
  64.     ets:insert(dic,{<<Env1/binary,II/binary>>,<<Val1/binary>>}),
  65.  
  66.     re_to_var(Pref,B,T,<<Acc/binary, Var/binary, Env1/binary,II/binary, Dot/binary>>).
  67.  
  68. %%var:start("CRON_","config.php").
  69.  
  70.  
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement