diff options
author | Magnus Feuer <mfeuer@jaguarlandrover.com> | 2015-07-13 14:14:49 -0700 |
---|---|---|
committer | Magnus Feuer <mfeuer@jaguarlandrover.com> | 2015-07-13 14:14:49 -0700 |
commit | a7e9f03d7d0854ae90305ad8d36dee9965794b87 (patch) | |
tree | 081b1709bce9412f2d39e89d229351e1c5b293ae /components/rvi_common | |
parent | 1d4578df76a11dc6887c6e37e97a8ac4dac5df2c (diff) | |
download | rvi_core-a7e9f03d7d0854ae90305ad8d36dee9965794b87.tar.gz |
Reintroduced code deleted by mistake
Diffstat (limited to 'components/rvi_common')
-rw-r--r-- | components/rvi_common/src/rvi_common.erl | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/components/rvi_common/src/rvi_common.erl b/components/rvi_common/src/rvi_common.erl index 73e67bb..111566f 100644 --- a/components/rvi_common/src/rvi_common.erl +++ b/components/rvi_common/src/rvi_common.erl @@ -42,10 +42,18 @@ -export([utc_timestamp/0]). -export([start_json_rpc_server/3]). +-export([extract_json/2]). -define(NODE_SERVICE_PREFIX, node_service_prefix). -define(NODE_ADDRESS, node_address). +-record(pst, { + buffer = [], + balance = start, + in_string = false, + escaped = false + }). + json_rpc_status(0) -> ok; @@ -667,3 +675,127 @@ utc_timestamp() -> seconds_jan_1970() -> %% calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}). 62167219200. + + + +count_brackets([], + #pst { + buffer = [], + balance = start } = PSt) -> + { incomplete, PSt#pst {}}; + +count_brackets([], + #pst { + buffer = Buffer, + balance = start } = PSt) -> + count_brackets(Buffer, + PSt#pst { + buffer = [], + balance = start } ); +count_brackets([${ | Rem], + #pst { + buffer = Buffer, + balance = start } = PSt) -> + count_brackets(Rem, + PSt#pst{ + buffer = [ ${ | Buffer ], + balance = 1}); + +%% Drop any initial characters prior to opening bracket +count_brackets([_ | Rem], + #pst { balance = start } = PSt) -> + count_brackets(Rem, PSt ); + +%% If balance is back to zero, we have completed a JSON +%% element. +count_brackets(Rem, + #pst { + buffer = Buffer, + balance = 0 } = PSt) -> + + { complete, lists:reverse(Buffer), + PSt#pst { + buffer = Rem, + balance = start + } + }; + +%% If we still have balance, but no more input +%% we have an incomplete element.x +count_brackets([], PSt) -> + { incomplete, PSt }; + + +%% We have a string start or end, and we are not esacped +%% Flip our in-string state +count_brackets([$" | Rem], + #pst { + buffer = Buffer, + in_string = InString, + escaped = false} = PSt) -> + + count_brackets(Rem, PSt#pst { + buffer = [ $" | Buffer ], + in_string = not InString }); + + +%% We have an escape character, and we are in a string. Turn on our escape state +count_brackets([$\\ | Rem], + #pst { + buffer = Buffer, + in_string = true, + escaped = false } = PSt) -> + + count_brackets(Rem, PSt#pst { + buffer = [ $\\ | Buffer ], + escaped = true}); + +%% We have an opening bracket and we are not in a string +count_brackets([${ | Rem], + #pst { + buffer = Buffer, + balance = Balance, + in_string = false } = PSt) -> + + count_brackets(Rem, + PSt#pst { + buffer = [ ${ | Buffer ], + balance = Balance + 1}); + +%% We have an closing bracket and we are not in a string +count_brackets([$} | Rem], + #pst { + buffer = Buffer, + balance = Balance, + in_string = false } = PSt) -> + + count_brackets(Rem, + PSt#pst { + buffer = [ $} | Buffer ], + balance = Balance - 1}); + +%% We have just regular data to feed over. +%% Make sure to clear the escape state. +count_brackets([C | Rem], + #pst { buffer = Buffer } = PSt) -> + + count_brackets(Rem, PSt#pst { + buffer = [ C | Buffer ], + escaped = false + } ). + +extract_json(Buf, PST, Acc) -> + case count_brackets(Buf, PST) of + { complete, Processed, NPST} -> + extract_json([], NPST, [ Processed | Acc]); + + + { incomplete, NPST} -> + { Acc, NPST } + end. + +extract_json(Buf, undefined) -> + extract_json(Buf, #pst {},[]); + +extract_json(Buf, PST) -> + extract_json(Buf, PST,[]). |