Created
May 5, 2015 11:28
-
-
Save hcs42/1caf91ce186748e39992 to your computer and use it in GitHub Desktop.
test_term: a script that checks if a file contains only valid Erlang terms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env escript | |
% test_term is a script that checks if a file contains only valid Erlang terms. | |
% | |
% Example: | |
% | |
% $ echo '{1,2}.' > f | |
% $ test_term f | |
% f:The file contains 1 valid Erlang term. | |
% | |
% $ echo '{1,2x}' > f | |
% $ test_term f | |
% f:1: syntax error before: x | |
main([]) -> | |
io:format("You need to specify a file.\n"); | |
main(FileNames) -> | |
analyze_files(FileNames). | |
analyze_files([]) -> | |
ok; | |
analyze_files([FileName|FileNames]) -> | |
io:format("~s:", [FileName]), | |
analyze_file(FileName), | |
analyze_files(FileNames). | |
analyze_file(FileName) -> | |
case file:consult(FileName) of | |
{ok, Terms} -> | |
Length = length(Terms), | |
Plural = | |
case Length >= 2 of | |
true -> "s"; | |
false -> "" | |
end, | |
io:format("The file contains ~p valid Erlang term~s.~n", | |
[Length, Plural]); | |
{error, Reason} -> | |
Error = file:format_error(Reason), | |
io:format("~s~n", [Error]) | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment