Home > marsbar > @marmoire > do_set.m

do_set

PURPOSE ^

private function to set data into item

SYNOPSIS ^

function [o, errf] = do_set(o, item, flags, data, filename)

DESCRIPTION ^

 private function to set data into item
 FORMAT [o, errf] = do_set(o, item, flags, data, filename)

 o           - object
 item        - name of item to set to
 flags       - containing fields:
                 action: one of: 'set' 'set_ui' 'get' 'clear' 'update'
 data        - the data to set into this item
 filename    - (possibly) filename for these data

 Returns
 o           - returned object, probably modified
 errf        - flag set to 1 if error, meaning object was not modified

 The flags argument at the moment is a bit redundant, as it only
 contains one field, but allows for future expansion, and is more
 compatible with the do_save method.

 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [o, errf] = do_set(o, item, flags, data, filename)
0002 % private function to set data into item
0003 % FORMAT [o, errf] = do_set(o, item, flags, data, filename)
0004 %
0005 % o           - object
0006 % item        - name of item to set to
0007 % flags       - containing fields:
0008 %                 action: one of: 'set' 'set_ui' 'get' 'clear' 'update'
0009 % data        - the data to set into this item
0010 % filename    - (possibly) filename for these data
0011 %
0012 % Returns
0013 % o           - returned object, probably modified
0014 % errf        - flag set to 1 if error, meaning object was not modified
0015 %
0016 % The flags argument at the moment is a bit redundant, as it only
0017 % contains one field, but allows for future expansion, and is more
0018 % compatible with the do_save method.
0019 %
0020 % $Id$
0021 
0022 if nargin < 2
0023   error('Need item');
0024 end
0025 if nargin < 3
0026   error('Need calling flags');
0027 end
0028 if nargin < 4
0029   data = NaN;
0030 end
0031 if nargin < 5
0032   filename = NaN;
0033 end
0034 
0035 % Errf for return
0036 errf = 0;
0037 
0038 % process flags
0039 if ischar(flags)  % can be string, with action
0040   if ~isempty(flags)
0041     flags = struct('action', flags);
0042   end
0043 end
0044 if ~isstruct(flags), flags = []; end
0045 if ~isfield(flags, 'action'), flags.action = 'set'; end
0046 action = flags.action;
0047 
0048 % get item to work on
0049 item_struct = get_item_struct(o, item);
0050 
0051 % get filename for data if set_ui
0052 if strcmp(action, 'set_ui')
0053   [fn pn] = mars_uifile('get', ...
0054             item_struct.filter_spec, ...
0055             ['Select ' item_struct.title '...']);
0056   if isequal(fn,0) | isequal(pn,0), errf = 1;, return, end
0057   filename = fullfile(pn, fn);
0058   data = [];
0059 end
0060 
0061 % Keep copy of passed filename for set_action call
0062 passed_filename = filename;
0063   
0064 % optionally, treat char data as filename
0065 % but passed filename overrides char data
0066 if item_struct.char_is_filename & ischar(data)
0067   if ~pr_is_nix(filename)
0068     warning(sprintf(...
0069     'Passed filename %s overrides data filename %s\n',...
0070     filename, data));
0071   else
0072     filename = data;
0073   end
0074   data = [];
0075 end
0076 
0077 if pr_is_nix(filename) % may need to save if no associated filename
0078   item_struct.has_changed = 1;
0079 else % don't need to save, but may need to load from file
0080   item_struct.has_changed = 0;
0081   if isempty(data)
0082     data = load(filename, ['-' item_struct.file_type]);
0083   end
0084 end
0085 item_struct.data = data;
0086 
0087 % If no filename passed:
0088 % if new set, filename is empty
0089 % if an update, filename stays
0090 is_update = strcmp(action, 'update');
0091 if pr_is_nan(filename)
0092   if ~is_update
0093     filename = '';
0094   end
0095 end  
0096 item_struct.file_name = filename;
0097 
0098 % If this was a clear, don't flag for save
0099 if pr_isempty(item_struct), item_struct.has_changed = 0; end
0100 
0101 % Put processed stuff into object, and copy old object
0102 % This so we can pass the candidate new object to the set_action routines
0103 % for checking and/or changing, but still roll back if we need to.
0104 old_o = o; 
0105 o = set_item_struct(o, item, item_struct);
0106 
0107 % and here is where we do the rules stuff
0108 is_clear = strcmp(action, 'clear');
0109 if ~isempty(item_struct.set_action) & ...
0110       (ismember(action, {'get','set','set_ui'}) | ...
0111        (is_update & item_struct.set_action_if_update) | ...
0112        (is_clear & item_struct.set_action_if_clear))  
0113   [tmp errf msg] = eval(item_struct.set_action);
0114   if errf
0115     o = old_o;
0116     warning(['Data not set: ' msg]);
0117     return
0118   end
0119   % work out what has been returned.  It can be:
0120   % object, item_struct, or data; we much prefer the object, to be
0121   % consistent
0122   if isa(tmp, 'marmoire')   % object
0123     o = tmp;
0124     item_struct = get_item_struct(o, item);
0125   elseif isstruct(tmp) & isfield(tmp, 'set_action') % item struct
0126     item_struct = tmp;
0127   else % it's just the data
0128     item_struct.data = tmp;
0129   end
0130 end
0131 
0132 % set has_changed, if update
0133 if strcmp(action, 'update')
0134   item_struct.has_changed = 1;
0135 end
0136 
0137 % possibly remove data from structure
0138 if ~item_struct.has_changed & item_struct.leave_as_file
0139   item_struct.data = [];
0140 end
0141 
0142 % return object with data set
0143 o = set_item_struct(o, item, item_struct);
0144 
0145 return

Generated on Wed 11-May-2022 16:26:09 by m2html © 2003-2019