Home > marsbar > @marmoire > do_save.m

do_save

PURPOSE ^

method to save data for item

SYNOPSIS ^

function [res, o] = do_save(o, item, flags, filename)

DESCRIPTION ^

 method  to save data for item
 FORMAT [res, o] = do_save(o, item, flags, filename)
 
 o        - object
 item     - item name
 flags    - flags for save (see save_item_data.m for details)
 filename - (maybe) filename for save
 
 Returns
 saved_f  - flag set to 1 if save done, 0 not done, -1 if cancel
 o        - possibly modified object 

 The function is written like this so that, in the future, we can use
 callbacks in this code to manipulate all the objects in the armoire
 
 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [res, o] = do_save(o, item, flags, filename)
0002 % method  to save data for item
0003 % FORMAT [res, o] = do_save(o, item, flags, filename)
0004 %
0005 % o        - object
0006 % item     - item name
0007 % flags    - flags for save (see save_item_data.m for details)
0008 % filename - (maybe) filename for save
0009 %
0010 % Returns
0011 % saved_f  - flag set to 1 if save done, 0 not done, -1 if cancel
0012 % o        - possibly modified object
0013 %
0014 % The function is written like this so that, in the future, we can use
0015 % callbacks in this code to manipulate all the objects in the armoire
0016 %
0017 % $Id$
0018   
0019 if nargin < 2
0020   error('Need item');
0021 end
0022 if nargin < 3
0023   flags = NaN;
0024 end
0025 if nargin < 4
0026   filename = NaN;
0027 end
0028 
0029 % Get item
0030 item_struct = get_item_struct(o, item);
0031 
0032 % process flags
0033 if ~isstruct(flags), flags = []; end
0034 if pr_is_nix(filename), filename = item_struct.file_name; end
0035 if pr_is_nix(filename), filename = item_struct.default_file_name; end
0036 
0037 if pr_needs_save(item_struct) || isfield(flags, 'force') % force flag
0038   % prompt for filename if UI
0039   if isfield(flags, 'ui')
0040     % warn if empty, and warn_empty flag (we must be forcing to get here)
0041     if pr_isempty(item_struct)
0042       if isfield(flags, 'warn_empty')
0043     msgbox('Nothing to save', ...
0044            [item_struct.title ' is not set'], 'warn');
0045       end
0046       res = 0;
0047       return
0048     end
0049     % Work out prompt
0050     if isfield(flags, 'prompt')
0051       prompt = flags.prompt;
0052     else 
0053       prompt = item_struct.title;
0054     end
0055     if isfield(flags, 'prompt_prefix')
0056       prompt = [flags.prompt_prefix prompt];
0057     end
0058     if isfield(flags, 'prompt_suffix')
0059       prompt = [prompt flags.prompt_suffix];
0060     end
0061     if isfield(flags, 'ync')
0062       save_yn = questdlg(['Save ' prompt '?'],...
0063              'Save', 'Yes', 'No', 'Cancel', 'Yes');
0064       if strcmp(save_yn, 'Cancel'), res = -1; return, end      
0065       if strcmp(save_yn, 'No')
0066     if isfield(flags, 'no_no_save')
0067       o  = set_item_param(o, item, 'has_changed', 0);
0068     end
0069     res = 0; 
0070     return
0071       end
0072     end
0073     pr = ['Filename to save ' prompt]; 
0074     [f p] = mars_uifile('put', item_struct.filter_spec, pr, filename);
0075     if all(f==0), res = -1; return, end
0076     filename = fullfile(p, f);
0077   end
0078   savestruct(item_struct.data, filename);
0079   if item_struct.verbose
0080     fprintf('%s saved to %s\n', item_struct.title, filename);
0081   end
0082   item_struct.file_name = filename;
0083   item_struct.has_changed = 0;
0084   if item_struct.leave_as_file
0085     % maintain only on file, as it has beed saved
0086     item_struct.data = [];
0087   end
0088   o  = set_item_struct(o, item, item_struct);
0089   res = 1;
0090 else
0091   res = 0;
0092 end
0093 return

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