Skip to content

Commit

Permalink
Merge pull request #72 from cpp-lln-lab/bids_matlab
Browse files Browse the repository at this point in the history
[FIX] cast `spec.entities` as structure before printing
  • Loading branch information
Remi-Gau authored Apr 8, 2024
2 parents f1ac5e9 + bb4f7e4 commit 6af4f13
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 63 deletions.
103 changes: 42 additions & 61 deletions src/Mapping.m
Original file line number Diff line number Diff line change
Expand Up @@ -170,39 +170,36 @@ function print_mapping(obj, filename)

obj = flatten_mapping(obj);

% print to screen be default;
output_is_json = false;
if nargin > 1 && strcmp(bids.internal.file_utils(filename, 'ext'), 'json')
output_is_json = true;
content = {};
for i = 1:size(obj.mapping, 1)
input = obj.mapping(i);
input = rmfield(input, 'name_spec');
content{i, 1} = struct('input', input, ...
'output', obj.mapping(i).name_spec);
end
bids.util.jsonencode(filename, content);
return
end

% print to screen by default
fid = 1;

% what to separate input and output with
left = ' ';
separator = ' --> ';
right = ' ';

if nargin < 2

fid = 1;

elseif ~strcmp(bids.internal.file_utils(filename, 'ext'), 'json')

if nargin > 1
fid = fopen(filename, 'Wt');
if fid == -1
error('Unable to write file %s.', filename);
end

% markdown table separators
left = '| ';
separator = ' | ';
right = ' |';

elseif strcmp(bids.internal.file_utils(filename, 'ext'), 'json')

output_is_json = true;

content = {};

else

fid = 1;

end

header = ['<!--\n', ...
Expand All @@ -212,60 +209,44 @@ function print_mapping(obj, filename)
'# Mapping\n\n', ...
left, 'input', separator, 'output', right, '\n', ...
left, '-', separator, '-', right, '\n'];
if ~output_is_json
fprintf(fid, '\n');
if fid ~= 1
fprintf(fid, header);
end

fprintf(fid, '\n');
if fid ~= 1
fprintf(fid, header);
end

for i = 1:size(obj.mapping, 1)

if ~output_is_json
%%
input = obj.mapping(i);
input = prepare_for_printing(input);
%%
input = obj.mapping(i);
input = prepare_for_printing(input);

input_filename = input.filename;
input_filename = input.filename;

%%
output = obj.mapping(i).name_spec;
output = prepare_for_printing(output);
%%
output = obj.mapping(i).name_spec;
output = prepare_for_printing(output);

output_filename = output.filename;
output_filename = output.filename;

output_filename = ['*' output_filename];
output_filename = strrep(output_filename, 'add-star', '*');

if fid ~= 1
input_filename = strrep(input_filename, '*', '\*');
input_filename = strrep(input_filename, '_', '\_');
output_filename = strrep(output_filename, '*', '\*');
output_filename = strrep(output_filename, '_', '\_');
end

fprintf(fid, '%s%s%s%s%s\n', ...
left, input_filename, separator, output_filename, right);

else

input = obj.mapping(i);
input = rmfield(input, 'name_spec');
content{i, 1} = struct('input', input, ...
'output', obj.mapping(i).name_spec);
output_filename = ['*' output_filename];
output_filename = strrep(output_filename, 'add-star', '*');

if fid ~= 1
input_filename = strrep(input_filename, '*', '\*');
input_filename = strrep(input_filename, '_', '\_');
output_filename = strrep(output_filename, '*', '\*');
output_filename = strrep(output_filename, '_', '\_');
end

end
fprintf(fid, '%s%s%s%s%s\n', ...
left, input_filename, separator, output_filename, right);

if ~output_is_json
fprintf(fid, '\n');
if fid ~= 1
fclose(fid);
end
else
bids.util.jsonencode(filename, content);
end

fprintf(fid, '\n');
if fid ~= 1
fclose(fid);
end

end
Expand Down
8 changes: 6 additions & 2 deletions src/utils/prepare_for_printing.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
%
% (C) Copyright 2022 spm_2_bids developers

if isfield(spec, 'entities') && strcmp(spec.entities, '*')
spec.entities = struct('add', 'joker');
if isfield(spec, 'entities')
if strcmp(spec.entities, '*')
spec.entities = struct('add', 'joker');
elseif strcmp(spec.entities, '')
spec.entities = struct();
end
end
bf = bids.File(spec, 'tolerant', true);

Expand Down

0 comments on commit 6af4f13

Please sign in to comment.