1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2008-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%% Some utilities
21
22-module(gen_util).
23-compile(export_all).
24
25lowercase([F|R]) when F >= $A, F =< $Z ->   [F+($a-$A)|R];
26lowercase(Str) when is_list(Str) ->   Str.
27
28lowercase_all([F|R]) when F >= $A, F =< $Z -> [F+($a-$A)|lowercase_all(R)];
29lowercase_all([F|R])  ->               [F|lowercase_all(R)];
30lowercase_all([]) ->                   [].
31
32uppercase([F|R]) when F >= $a, F =< $z ->    [F+($A-$a)|R];
33uppercase(Str) when is_list(Str) ->   Str.
34
35uppercase_all([F|R]) when F >= $a, F =< $z -> [F+($A-$a)|uppercase_all(R)];
36uppercase_all([A|R]) ->             [A|uppercase_all(R)];
37uppercase_all([]) ->                [].
38
39
40strip_name([H|R1],[H|R2]) ->
41    strip_name(R1,R2);
42strip_name(String,[]) -> String.
43
44
45get_hook(_Type, undefined) -> ignore;
46get_hook(Type, List) -> proplists:get_value(Type, List, ignore).
47
48get_taylor_made(Str, Name) ->
49    re:run(Str, "<<"++Name++"(.*)"++Name++">>",
50	   [dotall, {capture, all_but_first, list}]).
51
52open_write(File) ->
53    open_write(File, []).
54open_write(File, Opts) ->
55    %% io:format("Generating ~s~n",[File]),
56    {ok, Fd} = file:open(File++".temp", [write|Opts]),
57    put(current_file, {Fd,File}).
58
59
60close() ->
61    case get(current_file) of
62	undefined ->
63	    ok;
64	{closed, File} ->
65	    io:format("Closing twice ~s~n",[File]);
66	{Fd,File} ->
67	    file:close(Fd),
68	    case os:cmd("diff " ++ File ++ " " ++ File ++ ".temp" ++ "| head -30") of
69		[] ->
70		    ok = file:delete(File ++ ".temp"),
71		    %% So that make understands that we have made this
72		    %% case os:getenv("CLEARCASE_ROOT") of
73		    %% 	false -> os:cmd("touch " ++ File);
74		    %% 	_ ->  ignore
75		    %% end,
76		    ok;
77		Diff ->
78		    case check_diff(Diff) of
79			copyright -> %% We ignore copyright changes only
80			    ok = file:delete(File ++ ".temp");
81			_ ->
82			    io:format("Diff in ~s~n~s ~n", [File, Diff]),
83			    case file:rename(File ++ ".temp", File) of
84				ok -> ok;
85				_ ->
86				    io:format("*****  Failed to save file ~p ~n",[File])
87			    end
88		    end
89	    end,
90	    put(current_file, {closed, File})
91    end.
92
93
94check_diff(Diff) ->
95    try
96	[_,D1,_,D2|Tail] = re:split(Diff, "\n"),
97	case Tail of
98	    [] -> ok;
99	    [<<>>] -> ok;
100	    _ -> throw(diff)
101	end,
102	copyright(D1),
103	copyright(D2),
104	copyright
105    catch
106	throw:_ ->  diff;
107	error:{badmatch,_} ->
108	    diff;
109	_:What:Stacktrace ->
110	    io:format("~p:~p: ~p ~p~n", [?MODULE,?LINE, What, Stacktrace]),
111	    diff
112    end.
113
114copyright(<<_, _, "%% Copyright", _/binary>>) -> ok;
115copyright(<<_, _, " * Copyright", _/binary>>) -> ok;
116copyright(_) -> throw(diff).
117
118w(Str) ->
119    w(Str, []).
120w(Str,Args) ->
121    {Fd,_} = get(current_file),
122    io:format(Fd, Str, Args).
123
124args(Fun, Limit, List) ->
125    args(Fun, Limit, List, infinity, 0).
126
127args(Fun, Limit, List, Max) ->
128    args(Fun, Limit, List, Max, 0).
129
130args(_Fun, _Limit, [], _Max, _) -> "";  %% No args
131args(Fun, _Limit, [Last], _Max, _Pos) ->
132    case Fun(Last) of
133	skip -> ""; %% FIXME bug if last skips
134	Str  -> Str
135    end;
136args(Fun, Limit, [H|R], Max, Pos) ->
137    case Fun(H) of
138	skip -> args(Fun,Limit,R, Max, Pos);
139	Str  ->
140	    {NL, NewPos} =
141		case length(Str) + Pos of
142		    Curr when Curr > Max ->
143			{"\n  ", 0};
144		    Curr ->
145			{"", Curr}
146		end,
147	    case args(Fun,Limit,R, Max, NewPos) of
148		"" -> Str;
149		End -> Str ++ Limit ++ NL ++ End
150	    end
151    end.
152
153
154
155
156tokens(S) ->
157    tokens1(S, " \t\r\n(){}*;,@", []).
158tokens(S,Seps) ->
159    tokens1(S, Seps, []).
160
161tokens1([C|S], Seps, Toks) ->
162    case lists:member(C, Seps) of
163        true -> tokens1(S, Seps, [C|Toks]);
164        false -> tokens2(S, Seps, Toks, [C])
165    end;
166tokens1([], _Seps, Toks) ->
167    replace_and_remove(Toks, []).
168
169tokens2([C|S], Seps, Toks, Cs) ->
170    case lists:member(C, Seps) of
171        true -> tokens1(S, Seps, [C, lists:reverse(Cs) |Toks]);
172        false -> tokens2(S, Seps, Toks, [C|Cs])
173    end;
174tokens2([], _Seps, Toks, Cs) ->
175    replace_and_remove([lists:reverse(Cs)|Toks], []).
176
177replace_and_remove([E|R], Acc) when is_list(E) -> %% Keep everything that is a word
178    replace_and_remove(R, [E|Acc]);
179replace_and_remove([$\n | R], Acc) ->   %% It is semi line oriented so keep eol
180    replace_and_remove(R, [eol|Acc]);
181replace_and_remove([$( | R], Acc) ->
182    replace_and_remove(R, ["("|Acc]);
183replace_and_remove([$) | R], Acc) ->
184    replace_and_remove(R, [")"|Acc]);
185replace_and_remove([${ | R], Acc) ->
186    replace_and_remove(R, ["{"|Acc]);
187replace_and_remove([$} | R], Acc) ->
188    replace_and_remove(R, ["}"|Acc]);
189replace_and_remove([$| | R], Acc) ->
190    replace_and_remove(R, ["|"|Acc]);
191replace_and_remove([$* | R], Acc) ->
192    replace_and_remove(R, ["*"|Acc]);
193replace_and_remove([$+ | R], Acc) ->
194    replace_and_remove(R, ["+"|Acc]);
195replace_and_remove([$& | R], Acc) ->
196    replace_and_remove(R, [$&|Acc]);
197replace_and_remove([$<,$< | R], Acc) ->
198    replace_and_remove(R, ["<<"|Acc]);
199replace_and_remove([$, | R], Acc) ->
200    replace_and_remove(R, [cont|Acc]);
201replace_and_remove([$; | R], Acc) ->
202    replace_and_remove(R, [eoe|Acc]);
203replace_and_remove([$@ | R], Acc) ->
204    replace_and_remove(R, [directive|Acc]);
205
206replace_and_remove([_E|R], Acc) ->       %% Ignore everything else
207    replace_and_remove(R, Acc);
208replace_and_remove([], Acc) ->
209    Acc.
210
211halt(Reason) ->
212    case process_info(group_leader(), status) of
213	{_,waiting} ->
214	    %% Now all output data is down in the driver.
215	    %% Give the driver some extra time before halting.
216	    receive after 10 -> ok end,
217	    erlang:halt(Reason);
218	_ ->
219	    %% Probably still processing I/O requests.
220	    receive after 20 -> ok end,
221	    gen_util:halt(Reason)
222    end.
223
224erl_copyright() ->
225    StartYear = start_year(get(current_class)),
226    {CurrentYear,_,_}   = erlang:date(),
227    w("%%~n",[]),
228    w("%% %CopyrightBegin%~n",[]),
229    w("%%~n",[]),
230    w("%% Copyright Ericsson AB ~p-~p. All Rights Reserved.~n",
231      [StartYear, CurrentYear]),
232    w("%%~n",[]),
233    w("%% Licensed under the Apache License, Version 2.0 (the \"License\");~n",[]),
234    w("%% you may not use this file except in compliance with the License.~n",[]),
235    w("%% You may obtain a copy of the License at~n",[]),
236    w("%%~n",[]),
237    w("%%     http://www.apache.org/licenses/LICENSE-2.0~n",[]),
238    w("%%~n",[]),
239    w("%% Unless required by applicable law or agreed to in writing, software~n",[]),
240    w("%% distributed under the License is distributed on an \"AS IS\" BASIS,~n",[]),
241    w("%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.~n",[]),
242    w("%% See the License for the specific language governing permissions and~n",[]),
243    w("%% limitations under the License.~n",[]),
244    w("%%~n",[]),
245    w("%% %CopyrightEnd%~n",[]).
246
247c_copyright() ->
248    {CurrentYear,_,_}   = erlang:date(),
249    w("/*~n",[]),
250    w(" * %CopyrightBegin%~n",[]),
251    w(" *~n",[]),
252    w(" * Copyright Ericsson AB 2008-~p. All Rights Reserved.~n",[CurrentYear]),
253    w(" *~n",[]),
254    w(" * Licensed under the Apache License, Version 2.0 (the \"License\");~n",[]),
255    w(" * you may not use this file except in compliance with the License.~n",[]),
256    w(" * You may obtain a copy of the License at~n",[]),
257    w(" *~n",[]),
258    w(" *     http://www.apache.org/licenses/LICENSE-2.0~n",[]),
259    w(" *~n",[]),
260    w(" * Unless required by applicable law or agreed to in writing, software~n",[]),
261    w(" * distributed under the License is distributed on an \"AS IS\" BASIS,~n",[]),
262    w(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.~n",[]),
263    w(" * See the License for the specific language governing permissions and~n",[]),
264    w(" * limitations under the License.~n",[]),
265    w(" *~n",[]),
266    w(" * %CopyrightEnd% ~n",[]),
267    w("*/~n",[]).
268
269start_year("wxAuiManagerEvent") -> 2009;
270start_year("wxAuiNotebookEvent") -> 2009;
271start_year("wxChoicebook") -> 2009;
272start_year("wxGridCellBoolEditor") -> 2009;
273start_year("wxGridCellBoolRenderer") -> 2009;
274start_year("wxGridCellChoiceEditor") -> 2009;
275start_year("wxGridCellFloatEditor") -> 2009;
276start_year("wxGridCellFloatRenderer") -> 2009;
277start_year("wxGridCellNumberEditor") -> 2009;
278start_year("wxGridCellNumberRenderer") -> 2009;
279start_year("wxGridCellStringRenderer") -> 2009;
280start_year("wxGridCellTextEditor") -> 2009;
281start_year("wxHtmlLinkEvent") -> 2009;
282start_year("wxHtmlWindow") -> 2009;
283start_year("wxListbook") -> 2009;
284start_year("wxLogNull") -> 2009;
285start_year("wxSpinEvent") -> 2009;
286start_year("wxSplitterEvent") -> 2009;
287start_year("wxSplitterWindow") -> 2009;
288start_year("wxToolbook") -> 2009;
289start_year("wxTreebook") -> 2009;
290start_year(_) -> 2008.
291