Home > atmlab > handy > qtool.m

qtool

PURPOSE ^

QTOOL Creation of control files from a template.

SYNOPSIS ^

function qtool(S,outfile,Q,dbg)

DESCRIPTION ^

 QTOOL   Creation of control files from a template.

       The data for the control file are given by the structure Q. 

       The Qtool has three mechanisms to create control files from
       templates. See the file examples/sample.tmplt for a practical 
       example on a template file. Such a file is read by the
       function *file2strs* and is then passed to *qtool* as a
       string array. The string array can also be defined inside
       a function, or a local sub-functions, and thus a seperate
       template file can be avoided.

       1. Variable substition.
         The Qtool tries to replace everything specified between
         $-signs by the value of the corresponding variable in the 
         workspace. If the variable does not exist, there will be an
         error.
         As the data structure given to the function will have
         locally the name Q, this name must also be used
         in the control file template, e.g. $Q.PLAT_ALT$.

       2. If statements
         The keywords IF, ELSE and END are valid. These keywords
         must be in uppercase and be placed in column 1.
         Nested if-statements are not valid.
         All logical expressions of Matlab can be used.

       3. Inline functions
         If a @-sign is found in the first column, the rest of the
         line is treated to be the name of a function writing text
         to the control file.
         This function will then be called with Q and the control 
         file identifier as input. For example "@some_fun" will 
         result in a function call as "some_fun(Q,fid)". 

       A field with name FILESEP will be added to Q. This field is 
       set to the file seperator for the platform (a string). A 
       folder and a file name of Q are thus combined in the template
    as: $Q.CALCGRIDS_DIR$$Q.FILESEP$$Q.F_MONO$

 FORMAT   qtool(S,outfile,Q[,dbg])
        
 IN    S         String array containing the template file.
       outfile   Name of control file to create.
       Q         Structure with data to paste into the template.
 OPT   dbg       Debugging mode. Default is 0. If set to 1, the input
                 strings are displayed on the screen. The last displayed
                 string is the one causing problems (if there are any).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

qtool.m

SOURCE CODE ^

0001 % QTOOL   Creation of control files from a template.
0002 %
0003 %       The data for the control file are given by the structure Q.
0004 %
0005 %       The Qtool has three mechanisms to create control files from
0006 %       templates. See the file examples/sample.tmplt for a practical
0007 %       example on a template file. Such a file is read by the
0008 %       function *file2strs* and is then passed to *qtool* as a
0009 %       string array. The string array can also be defined inside
0010 %       a function, or a local sub-functions, and thus a seperate
0011 %       template file can be avoided.
0012 %
0013 %       1. Variable substition.
0014 %         The Qtool tries to replace everything specified between
0015 %         $-signs by the value of the corresponding variable in the
0016 %         workspace. If the variable does not exist, there will be an
0017 %         error.
0018 %         As the data structure given to the function will have
0019 %         locally the name Q, this name must also be used
0020 %         in the control file template, e.g. $Q.PLAT_ALT$.
0021 %
0022 %       2. If statements
0023 %         The keywords IF, ELSE and END are valid. These keywords
0024 %         must be in uppercase and be placed in column 1.
0025 %         Nested if-statements are not valid.
0026 %         All logical expressions of Matlab can be used.
0027 %
0028 %       3. Inline functions
0029 %         If a @-sign is found in the first column, the rest of the
0030 %         line is treated to be the name of a function writing text
0031 %         to the control file.
0032 %         This function will then be called with Q and the control
0033 %         file identifier as input. For example "@some_fun" will
0034 %         result in a function call as "some_fun(Q,fid)".
0035 %
0036 %       A field with name FILESEP will be added to Q. This field is
0037 %       set to the file seperator for the platform (a string). A
0038 %       folder and a file name of Q are thus combined in the template
0039 %    as: $Q.CALCGRIDS_DIR$$Q.FILESEP$$Q.F_MONO$
0040 %
0041 % FORMAT   qtool(S,outfile,Q[,dbg])
0042 %
0043 % IN    S         String array containing the template file.
0044 %       outfile   Name of control file to create.
0045 %       Q         Structure with data to paste into the template.
0046 % OPT   dbg       Debugging mode. Default is 0. If set to 1, the input
0047 %                 strings are displayed on the screen. The last displayed
0048 %                 string is the one causing problems (if there are any).
0049 
0050 % 2002-12-20   Created by Patrick Eriksson, based on the function with same
0051 %              name in AMI.
0052 
0053 
0054 function qtool(S,outfile,Q,dbg)
0055 
0056 
0057 %= Input
0058 %
0059 rqre_nargin( 3, nargin );
0060 %
0061 if nargin < 4
0062   dbg = 0;
0063 end 
0064 
0065 
0066 %=== Set file sperator for the computer platfom ===============================
0067 %
0068 Q.FILESEP = filesep;
0069 
0070 
0071 %=== Open output file for writing
0072 %
0073 fid = fileopen( outfile, 'w' );
0074 
0075 
0076 %=== Read from string array, replace keywords etc. and write to cfile
0077 line    = 0;
0078 in_if   = 0;
0079 in_else = 0;
0080 do      = 1;
0081 do_this = 1;
0082 
0083 for line = 1 : length( S )
0084 
0085   s    = S{line};
0086 
0087   if dbg
0088     fprintf('%s\n',s);
0089   end
0090 
0091   if ~ischar(s)
0092     error('The array *S* contains non-string elements.');
0093   end
0094 
0095   if length(s) & double( tail( s, 1 ) ) == 10
0096     error('The strings shall not include the newline character.');
0097   end
0098 
0099   do_this = 1;
0100 
0101   if (length(s)>=3) & strcmp(s(1:3),'IF ')
0102 
0103     if in_if | in_else
0104       error(sprintf('Nested IFs found on line %d.',line));
0105     end
0106 
0107     in_if   = 1;
0108     in_else = 0;
0109     s       = deblank( s(4:length(s)) );
0110     if isempty(s)
0111       error(sprintf('IF statement without variable found on line %d.',...
0112                                                                     line));
0113     end
0114     if eval(s)
0115       do = 1;
0116     else
0117       do = 0;
0118     end         
0119     do_this = 0;
0120 
0121   end %if
0122 
0123   if (length(s)>=4) & strcmp(s(1:4),'ELSE')
0124 
0125     if ~in_if | in_else
0126       error(sprintf('Not allowed placement of ELSE at line %d.',line));
0127     end
0128 
0129     in_if   = 0;
0130     in_else = 1;
0131     do      = ~do;
0132     do_this = 0;
0133 
0134   end %else
0135 
0136   if (length(s)>=3) & strcmp(s(1:3),'END')
0137 
0138     if ~in_if & ~in_else 
0139       error(sprintf('Not allowed placement of END at line %d.',line));
0140     end
0141 
0142     in_if   = 0;
0143     in_else = 0;
0144     do      = 1;
0145     do_this = 0;
0146 
0147   end %else
0148 
0149   if do_this & do
0150 
0151     %= Check first if any "inline" function shall be called
0152     if length(s) & s(1) == '@'
0153       %
0154       eval([ s(2:length(s)), '(Q,fid);' ])
0155 
0156     %= Replace variables (marked by $$) and move text to cfile
0157     else
0158 
0159       if ~isempty( s )
0160           dollars = find( s == '$' );
0161           if ~isempty(dollars)
0162             if isodd(length(dollars))
0163               error(sprintf(...
0164                      'An odd number of $-signs was found on line %d.',line));
0165             end
0166             while ~isempty(dollars)
0167               i1 = dollars(1);
0168               i2 = dollars(2);
0169               name = s((i1+1):(i2-1));
0170               if isstr(eval(name))
0171                 s = [s(1:(i1-1)),eval(name),s((i2+1):length(s))];
0172               else
0173                 s = [s(1:(i1-1)),num2str(eval(name)),s((i2+1):length(s))];
0174               end
0175               dollars = find( s == '$' );
0176             end
0177           end
0178       end
0179 
0180       fprintf(fid,'%s\n',s);
0181 
0182     end % else
0183   end % if do...
0184 end
0185 
0186 
0187 
0188 fileclose( fid );
0189 
0190 
0191 
0192 if in_if | in_else
0193   error('EOF reached inside IF or ELSE statement.');
0194 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005