Skip to content

Commit

Permalink
jload load data to struct, test if loadbj input is buffer, update err…
Browse files Browse the repository at this point in the history
…or msg
  • Loading branch information
fangq committed Jun 13, 2020
1 parent 55db193 commit 4904155
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
31 changes: 17 additions & 14 deletions jload.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
% jload
% or
% jload(fname)
% varlist=jload(fname)
% [varlist, header]=jload(fname)
% varlist=jload(fname,'param1',value1,'param2',value2,...)
%
% Load variables from a JSON or binary JSON file to a workspace
Expand Down Expand Up @@ -33,13 +35,16 @@
% can be used to adjust the parsing options
%
% output:
% varlist: a list of variables loaded
% varlist: a struct with each subfield a variable stored in the file,
% if output is ignored, the variables will be loaded to the
% workspace specified by the 'ws' option, which by default
% load the variables to the current workspace ('caller')
%
% examples:
% jload % load all variables in jamdata.jamm to the 'caller' workspace
% jload mydat.jamm
% jload('mydat.jamm','vars', {'v1','v2',...}) % load selected variables
% jload('mydat.jamm','simplifycell',1)
% varlist=jload('mydat.jamm','simplifycell',1)
%
% license:
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
Expand All @@ -48,7 +53,7 @@
%

if(nargin==0)
filename='jamdata.jamm';
filename=[pwd filesep 'jamdata.jamm'];
end

opt=varargin2struct(varargin{:});
Expand All @@ -75,11 +80,6 @@
header=loadfun(filename,'ObjectID',1, varargin{:});
end

if(jsonopt('Header',0,opt))
varargout{1}=header;
return;
end

allvar=fieldnames(header.WorkspaceHeader);

varlist=jsonopt('vars',allvar,opt);
Expand All @@ -96,10 +96,13 @@
body=loadfun(filename,'ObjectID',2, varargin{:});
end

for i=1:length(varlist)
assignin(ws, varlist{i}, body.WorkspaceData.(varlist{i}));
end

if(nargout>1)
varargout{1}=varlist;
if(nargout==0)
for i=1:length(varlist)
assignin(ws, varlist{i}, body.WorkspaceData.(varlist{i}));
end
else
varargout{1}=rmfield(body.WorkspaceData,setdiff(fieldnames(body.WorkspaceData),varlist));
if(nargout>1)
varargout{2}=header;
end
end
2 changes: 1 addition & 1 deletion jsave.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
%

if(nargin==0)
filename='jamdata.jamm';
filename=[pwd filesep 'jamdata.jamm'];
end

opt=varargin2struct(varargin{:});
Expand Down
8 changes: 5 additions & 3 deletions loadbj.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@
fid = fopen(fname,'rb');
string = fread(fid,inf,'uint8=>char')';
fclose(fid);
else
elseif(regexp(fname, '^\s*[\[\{SCHiUIulmLMhdDTFZN]'))
string=fname;
else
error_pos('input file does not exist or buffer is invalid');
end

pos = 1; inputlen = length(string); inputstr = string;
Expand Down Expand Up @@ -156,7 +158,7 @@
[cc,pos]=next_char(inputstr,pos);
if(cc=='[')
if(isfield(varargin{1},'noembedding_') && varargin{1}.noembedding_==1)
error('ND array size specifier does not support embedding');
error_pos('ND array size specifier does not support embedding');
end
varargin{1}.noembedding_=1;
[dim, pos]=parse_array(inputstr, pos, varargin{:});
Expand Down Expand Up @@ -342,7 +344,7 @@
end
msg = [sprintf(msg, pos) ': ' ...
inputstr(poShow(1):poShow(2)) '<error>' inputstr(poShow(3):poShow(4)) ];
error( ['JSONLAB:InvalidFormat: ' msg] );
error( ['JSONLAB:BJData:InvalidFormat: ' msg] );
end

%%-------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions loadjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
end
end
else
error('input file does not exist');
error_pos('input file does not exist');
end

pos = 1; inputlen = length(string); inputstr = string;
Expand Down Expand Up @@ -489,7 +489,7 @@
end
msg = [sprintf(msg, pos) ': ' ...
inputstr(poShow(1):poShow(2)) '<error>' inputstr(poShow(3):poShow(4)) ];
error( ['JSONLAB:InvalidFormat: ' msg] );
error( ['JSONLAB:JSON:InvalidFormat: ' msg] );
end

%%-------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion loadmsgpack.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
len = double(bytes2scalar(bytes(idx+1:idx+4), 'uint32'));
[obj, idx] = parsemap(len, bytes, idx+5, varargin{:});
otherwise
error('transplant:parsemsgpack:unknowntype', ...
error('JSONLAB:MSGPACK:InvalidFormat', ...
['Unknown type "' dec2bin(currentbyte) '"']);
end
end
Expand Down

0 comments on commit 4904155

Please sign in to comment.