%
% Author: Stanislaw Adaszewski, 2014
% http://algoholic.eu
%
% Find proper subs in Octave for accessing field in inherited classes using builtin()
%
% Example:
% x = cfg_branch
% a = oct_findfield(x, 'tag')
%
% tag = builtin('subsref', x, a)
%

function out = oct_findfield(obj, name)
    if isjava(obj) || ischar(obj) || iscell(obj)
	out = struct('type', '.', 'subs', name);
	return
    end
    % disp(class(obj));
    names = builtin('fieldnames', obj);
    % disp(names);
    if ~isempty(cell2mat(strfind(name, names)))
	out = struct('type', '.', 'subs', name);
    else
	for i=1:numel(names)
	    if exist(names{i}) == 2
		subs = struct('type', '.', 'subs', names{i});
		val = builtin('subsref', obj, subs);
		ret = oct_findfield(val, name);
		if numel(ret) > 0
		    out = [subs ret];
		    return
		end
	    end
	end
	out = [];
    end
end
