eini.erl 6.98 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
%% Licensed to the Apache Software Foundation (ASF) under one
%% or more contributor license agreements.  See the NOTICE file
%% distributed with this work for additional information
%% regarding copyright ownership.  The ASF licenses this file
%% to you under the Apache License, Version 2.0 (the
%% "License"); you may not use this file except in compliance
%% with the License.  You may obtain a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.

Ryosuke Nakai's avatar
Ryosuke Nakai committed
18
-module(eini).
Devin Torres's avatar
Devin Torres committed
19

20
-author('shino@accense.com').
Ryosuke Nakai's avatar
Ryosuke Nakai committed
21
-author('nakai@accense.com').
Shunichi Shinohara's avatar
Shunichi Shinohara committed
22

23
-export([parse/1]).
Shunichi Shinohara's avatar
Shunichi Shinohara committed
24

Ryosuke Nakai's avatar
Ryosuke Nakai committed
25
26
27
28
29
30
31
32
33
34
35
36
37
-export([lookup_value/3,
         register/2,
         register/4,
         is_section/2]).

-export([start_link/0]).
-export([init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         terminate/2,
         code_change/3]).

38
39
%% for debug use
-export([lex/1, parse_tokens/1]).
Devin Torres's avatar
Devin Torres committed
40

Ryosuke Nakai's avatar
Ryosuke Nakai committed
41
42
-define(EINI_TABLE, eini_table).

43
-type sections() :: [section()].
44
45
-type section() :: {Title::atom(), [property()]}.
-type property() :: {Key::atom(), Value::binary()}.
46
47
48
49
50
51

-type reason() :: {illegal_character, Line::integer(), Reason::string()}
                | {syntax_error, Line::integer(), Reason::string()}
                | {duplicate_title, Title::binary()}
                | {duplicate_key, Title::binary(), Key::binary()}.

52
53
54
55
56
57
-spec parse(Content:: string() | binary()) -> {ok, sections()}
                                            | {error, reason()}.
parse(Content) when is_binary(Content) ->
  parse(binary_to_list(Content));
parse(Content) when is_list(Content) ->
  case lex(Content) of
58
    {ok, Tokens} ->
Shunichi Shinohara's avatar
Shunichi Shinohara committed
59
      parse_and_validate(Tokens);
Shunichi Shinohara's avatar
Shunichi Shinohara committed
60
61
62
63
    {error, Reason} ->
      {error, Reason}
  end.

Shunichi Shinohara's avatar
Shunichi Shinohara committed
64
parse_and_validate(Tokens) ->
Shunichi Shinohara's avatar
Shunichi Shinohara committed
65
66
  case parse_tokens(Tokens) of
    {ok, Parsed} ->
Shunichi Shinohara's avatar
Shunichi Shinohara committed
67
      validate(Parsed);
68
69
70
    {error, Reason} ->
      {error, Reason}
  end.
Devin Torres's avatar
Devin Torres committed
71

72
73
-spec lex(string()) -> {ok, list(Token::tuple())}
                     | {error, {illegal_character, Line::integer(), Reason::string()}}.
74
lex(String) when is_list(String) ->
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  %% Add \n char at the end if does NOT end by \n
  %% TOD(shino): more simple logic?
  String2 = case String of
              "" ->
                "\n";
              _NotEmpty ->
                case lists:last(String) of
                  $\n ->
                    String;
                  _ ->
                    String ++ "\n"
                end
            end,
  case eini_lexer:string(String2) of
89
90
91
92
    {ok, [{break, _Line}|RestTokens], _EndLine} ->
      {ok, RestTokens};
    {ok, Tokens, _EndLine} ->
      {ok, Tokens};
Shunichi Shinohara's avatar
Shunichi Shinohara committed
93
    {error, {ErrorLine, Mod, Reason}, _EndLine} ->
94
      {error, {illegal_character, ErrorLine, Mod:format_error(Reason)}}
95
96
  end.
  
97
98
99
-spec parse_tokens(Token::tuple()) ->
                      {ok, sections()}
                    | {error, {syntax_error, Line::integer(), Reason::string()}}.
100
101
102
103
104
parse_tokens(Tokens) ->
  case eini_parser:parse(Tokens) of
    {ok, Res} ->
      {ok, Res};
    {error, {Line, Mod, Reason}} ->
105
      {error, {syntax_error, Line, Mod:format_error(Reason)}}
106
  end.
Shunichi Shinohara's avatar
Shunichi Shinohara committed
107

108
109
110
111
-spec validate(sections()) ->
                      {ok, sections()}
                    | {error, {duplicate_title, Title::binary()}}
                    | {error, {duplicate_key, Title::binary(), Key::binary()}}.
Shunichi Shinohara's avatar
Shunichi Shinohara committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
validate(Sections) ->
  validate(Sections, [], []).

validate([], _AccTitles, AccSections) ->
  {ok, lists:reverse(AccSections)};
validate([{Title, Properties} = Section | Sections], AccTitles, AccSections) ->
  case lists:member(Title, AccTitles) of
    true ->
      {error, {duplicate_title, Title}};
    false ->
      validate(Sections, [Title|AccTitles], [Section|AccSections], Properties, [])
  end.

validate(Sections, AccTitles, AccSections, [], _AccKeys) ->
  validate(Sections, AccTitles, AccSections);
validate(Sections, AccTitles, AccSections, [{Key, _Value}|Properties], AccKeys) ->
  case lists:member(Key, AccKeys) of
    true ->
      {error, {duplicate_key, hd(AccTitles), Key}};
    false ->
      validate(Sections, AccTitles, AccSections, Properties, [Key|AccKeys])
  end.
Ryosuke Nakai's avatar
Ryosuke Nakai committed
134
135

-spec lookup_value(file:name(), atom(), atom()) -> not_found | binary().
Ryosuke Nakai's avatar
Ryosuke Nakai committed
136
137
lookup_value(Filename, Section, Key) ->
  case ets:lookup(?EINI_TABLE, {Filename, Section, Key}) of
Ryosuke Nakai's avatar
Ryosuke Nakai committed
138
139
140
141
142
143
144
145
146
147
148
    [] ->
      not_found;
    [{_, Value}] ->
      Value
  end.

-spec register(file:name(), binary()) -> ok | {error, reason()}.
register(Filename, Binary) ->
  gen_server:call(?MODULE, {register, Filename, Binary}).  

-spec register(file:name(), atom(), atom(), any()) -> ok | {error, duplicate_key}.
Ryosuke Nakai's avatar
Ryosuke Nakai committed
149
150
register(Filename, Section, Key, Value) when is_atom(Section) andalso is_atom(Key) ->
  gen_server:call(?MODULE, {register, Filename, Section, Key, Value}).  
Ryosuke Nakai's avatar
Ryosuke Nakai committed
151
152
153
154
155
156
157
158
159
160

start_link() ->
  gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init(_Args) ->
  process_flag(trap_exit, true),
  Options = [set, protected, named_table, {read_concurrency, true}],
  _Tid = ets:new(?EINI_TABLE, Options),
  {ok, {}}.

Ryosuke Nakai's avatar
Ryosuke Nakai committed
161
162
handle_call({register, Filename, Section, Key, Value}, _From, State) ->
  case ets:insert_new(?EINI_TABLE, {{Filename, Section, Key}, Value}) of
Ryosuke Nakai's avatar
Ryosuke Nakai committed
163
164
165
    true ->
      {reply, ok, State};
    false ->
Ryosuke Nakai's avatar
Ryosuke Nakai committed
166
      {reply, {error, {duplicate_key, Section, Key}}, State}
Ryosuke Nakai's avatar
Ryosuke Nakai committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  end;
handle_call({register, Filename, Binary}, _From, State) ->
  case eini:parse(Binary) of
    {ok, Sections} ->
      case insert_sections(Filename, Sections) of
        ok ->
          true = ets:insert_new(?EINI_TABLE, {Filename, Binary}),
          {reply, ok, State};
        {error, Reason} ->
          {reply, {error, Reason}, State}
      end;
    {error, Reason} ->
      {reply, {error, Reason}, State}
  end;
handle_call(_Request, _From, State) ->
  {noreply, State}.

handle_cast(_Request, State) ->
  {noreply, State}.

handle_info(_Request, State) ->
  {noreply, State}.

terminate(_Reason, _State) ->
  ok.

code_change(_OldVsn, State, _Extra) ->
  {ok, State}.

-spec is_section(file:name(), atom()) -> boolean().
is_section(Filename, Section) ->
  case ets:match_object(?EINI_TABLE, {{Filename, Section, '_'}, '_'}) of
    [] ->
      false;
    _ ->
      true
  end.

-spec insert_sections(file:name(), [{atom(), [property()]}]) -> ok.
insert_sections(_Filename, []) ->
  ok;
Ryosuke Nakai's avatar
Ryosuke Nakai committed
208
209
insert_sections(Filename, [{Section, ListOfProperty}|ListOfSection]) ->
  insert_section(Filename, ListOfSection, Section, ListOfProperty).
Ryosuke Nakai's avatar
Ryosuke Nakai committed
210
211
212
213

-spec insert_section(file:name(), sections(), atom(), [property()]) -> ok.
insert_section(Filename, ListOfSection, _Section, []) ->
  insert_sections(Filename, ListOfSection);
Ryosuke Nakai's avatar
Ryosuke Nakai committed
214
215
insert_section(Filename, ListOfSection, Section, [{Key, Value}|ListOfProperty]) ->
  case ets:insert_new(?EINI_TABLE, {{Filename, Section, Key}, Value}) of
Ryosuke Nakai's avatar
Ryosuke Nakai committed
216
    true ->
Ryosuke Nakai's avatar
Ryosuke Nakai committed
217
      insert_section(Filename, ListOfSection, Section, ListOfProperty);
Ryosuke Nakai's avatar
Ryosuke Nakai committed
218
    false ->
Ryosuke Nakai's avatar
Ryosuke Nakai committed
219
      {error, {duplicate_key, Section, Key}}
Ryosuke Nakai's avatar
Ryosuke Nakai committed
220
  end.