0001 function [form_id_const, form_name_id_const_map, form_name_list] = make_form_name_defs(params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 close_conn = 0;
0033
0034 if nargin < 1
0035 error('Must pass in params structure with database information')
0036 end
0037
0038 params_struct_names = {'mysql','ensemble'};
0039 params_struct_idx = find(isfield(params,params_struct_names));
0040 if isempty(params_struct_idx)
0041 error('%s: Do not have sufficient database connection information', mfilename);
0042 elseif length(params_struct_idx > 1)
0043 fprintf('WARNING %s: both ensemble and mysql structures present. Using %s\n', mfilename, params_struct_names{params_struct_idx(1)});
0044 params_struct_idx = params_struct_idx(1);
0045 end
0046
0047 try
0048 CONN_ID = params.(params_struct_names{params_struct_idx}).conn_id;
0049 catch
0050 CONN_ID = 0;
0051 params.(params_struct_names{params_struct_idx}).conn_id = CONN_ID;
0052 close_conn = 1;
0053 end
0054 try DATABASE_SCRIPT_PATH = params.paths.project_root; catch DATABASE_SCRIPT_PATH = '.'; end
0055 try write2file = params.write2file; catch write2file = 0; end
0056
0057 CONN_ID = mysql_make_conn(params.(params_struct_names{params_struct_idx(1)}));
0058
0059 mysql_str = sprintf('SELECT form_name, form_id FROM form');
0060 [names, ids] = mysql(CONN_ID, mysql_str);
0061
0062 if write2file
0063 fname = fullfile(DATABASE_SCRIPT_PATH, 'form_name_defs.m');
0064
0065
0066 if exist(fname)
0067 unix_str = sprintf('mv form_name_defs.m form_name_defs.m.%s', datestr(datenum(now),29));
0068 unix(unix_str);
0069 end
0070
0071 fid = fopen(fname, 'wt');
0072 if fid == -1
0073 error(sprintf('Could not open file: %s\n', fname))
0074 end
0075
0076 fprintf('Writing %s\n', fname);
0077
0078 fprintf(fid, ...
0079 ['%% Automatically generated mapping of form names to form numbers.\n%%\n%%'...
0080 ' form_name_defs.m\n%%\n%% Generated on %s by make_form_name_defs.m\n\n'], datestr(datenum(now)));
0081 end
0082
0083 nforms = length(ids);
0084
0085 form_name_list = cell(nforms,2);
0086 for iform = 1:nforms
0087 orig_name = names{iform};
0088
0089
0090
0091 curr_name = regexprep(orig_name, {'-',' ','/',','}, '_');
0092 curr_name = regexprep(curr_name, {'''','(',')','\.'}, '');
0093 new_name = upper(curr_name);
0094
0095 form_name_list{iform,1} = orig_name;
0096 form_name_list{iform,2} = new_name;
0097 if write2file
0098 fprintf(fid,'form_id_const.%s = %d;\n', new_name, ids(iform));
0099 end
0100 form_id_const.(new_name) = ids(iform);
0101 end
0102
0103
0104 if write2file
0105 fprintf(fid, '\n\nform_name_id_const_map = { ...\n');
0106 end
0107
0108 for iform = 1:nforms
0109 if write2file
0110 fprintf(fid,'''%s'', ''%s''; ...\n', strrep(form_name_list{iform,1},'''',''''''), form_name_list{iform,2});
0111 end
0112 form_name_id_const_map{iform,1} = ...
0113 strrep(form_name_list{iform,1},'''','''''');
0114 form_name_id_const_map{iform,2} = form_name_list{iform,2};
0115 end
0116 if write2file
0117 fprintf(fid, '};\n');
0118 end
0119
0120
0121 if write2file
0122 fprintf(fid, '\n\nform_name_list = { ...\n');
0123 end
0124 for iform = 1:nforms
0125 if write2file
0126 fprintf(fid,'''%s''; ...\n', strrep(form_name_list{iform,1},'''',''''''));
0127 end
0128 new_form_name_list{iform,1} = strrep(form_name_list{iform,1},'''','''''');
0129 end
0130
0131 if write2file
0132 fprintf(fid, '};\n');
0133 fclose(fid);
0134 end
0135
0136 form_name_list = new_form_name_list;
0137
0138
0139
0140 if(close_conn)
0141 mysql('close');
0142 end