From acd0ee9d1301a96f6f2fe1b19c12b3639c77c59e Mon Sep 17 00:00:00 2001 From: Hans Bolinder Date: Tue, 15 Jan 2013 08:41:05 +0100 Subject: [stdlib] Introduce new functions epp:read_encoding_from_binary/1,2 --- lib/stdlib/doc/src/epp.xml | 19 +++++++++++++++++-- lib/stdlib/src/epp.erl | 37 +++++++++++++++++++++++++++++++++---- lib/stdlib/test/epp_SUITE.erl | 9 +++++++-- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/lib/stdlib/doc/src/epp.xml b/lib/stdlib/doc/src/epp.xml index 3e8aba2e5f..df7bf883fc 100644 --- a/lib/stdlib/doc/src/epp.xml +++ b/lib/stdlib/doc/src/epp.xml @@ -4,7 +4,7 @@
- 19962012 + 19962013 Ericsson AB. All Rights Reserved. @@ -109,7 +109,8 @@ Return a string representation of an encoding

Returns a string representation of an encoding. The string - is recognized by read_encoding/1,2 and + is recognized by read_encoding/1,2, + read_encoding_from_binary/1,2, and set_encoding/1 as a valid encoding.

@@ -127,6 +128,20 @@ occur in a comment.

+ + + + Read the encoding from a binary + +

Read the encoding from + a binary. Returns the read encoding, or none if no + valid encoding was found.

+

The option in_comment_only is true by + default, which is correct for Erlang source files. If set to + false the encoding string does not necessarily have to + occur in a comment.

+
+
Read and set the encoding of an IO device diff --git a/lib/stdlib/src/epp.erl b/lib/stdlib/src/epp.erl index 475b1bf933..afa39c3fb9 100644 --- a/lib/stdlib/src/epp.erl +++ b/lib/stdlib/src/epp.erl @@ -24,7 +24,8 @@ -export([scan_erl_form/1,parse_erl_form/1,macro_defs/1]). -export([parse_file/1, parse_file/3]). -export([default_encoding/0, encoding_to_string/1, - read_encoding/1, read_encoding/2, set_encoding/1]). + read_encoding_from_binary/1, read_encoding_from_binary/2, + set_encoding/1, read_encoding/1, read_encoding/2]). -export([interpret_file_attribute/1]). -export([normalize_typed_record_fields/1,restore_typed_record_fields/1]). @@ -265,13 +266,41 @@ set_encoding(File) -> ok = io:setopts(File, [{encoding, Enc}]), Encoding. --spec read_encoding_from_file(File, InComment) -> source_encoding() | none when - File :: io:device(), - InComment :: boolean(). +-spec read_encoding_from_binary(Binary) -> source_encoding() | none when + Binary :: binary(). -define(ENC_CHUNK, 32). -define(N_ENC_CHUNK, 16). % a total of 512 bytes +read_encoding_from_binary(Binary) -> + read_encoding_from_binary(Binary, []). + +-spec read_encoding_from_binary(Binary, Options) -> + source_encoding() | none when + Binary :: binary(), + Options :: [Option], + Option :: {in_comment_only, boolean()}. + +read_encoding_from_binary(Binary, Options) -> + InComment = proplists:get_value(in_comment_only, Options, true), + try + com_nl(Binary, fake_reader(0), 0, InComment) + catch + throw:no -> + none + end. + +fake_reader(N) -> + fun() when N =:= ?N_ENC_CHUNK -> + throw(no); + () -> + {<<>>, fake_reader(N+1)} + end. + +-spec read_encoding_from_file(File, InComment) -> source_encoding() | none when + File :: io:device(), + InComment :: boolean(). + read_encoding_from_file(File, InComment) -> {ok, Pos0} = file:position(File, cur), Opts = io:getopts(File), diff --git a/lib/stdlib/test/epp_SUITE.erl b/lib/stdlib/test/epp_SUITE.erl index 606bbbcbb2..041d521514 100644 --- a/lib/stdlib/test/epp_SUITE.erl +++ b/lib/stdlib/test/epp_SUITE.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 1998-2012. All Rights Reserved. +%% Copyright Ericsson AB 1998-2013. All Rights Reserved. %% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in @@ -1342,6 +1342,8 @@ encoding(Enc, File) -> E = encoding_nocom(Enc, File). encoding_com(Enc, File) -> + B = list_to_binary(Enc), + E = epp:read_encoding_from_binary(B), ok = file:write_file(File, Enc), {ok, Fd} = file:open(File, [read]), E = epp:set_encoding(Fd), @@ -1349,10 +1351,13 @@ encoding_com(Enc, File) -> E = epp:read_encoding(File). encoding_nocom(Enc, File) -> + Options = [{in_comment_only, false}], + B = list_to_binary(Enc), + E = epp:read_encoding_from_binary(B, Options), ok = file:write_file(File, Enc), {ok, Fd} = file:open(File, [read]), ok = file:close(Fd), - epp:read_encoding(File, [{in_comment_only, false}]). + E = epp:read_encoding(File, Options). check(Config, Tests) -> eval_tests(Config, fun check_test/2, Tests). -- cgit v1.2.3