strfstruct

PURPOSE ^

STRFSTRUCT Format structure as string.

SYNOPSIS ^

function str = strfstruct(pattern, repstruct)

DESCRIPTION ^

STRFSTRUCT  Format structure as string.

  Syntax:
    STR = STRFSTRUCT(PATTERN, REPSTRUCT)

  Description:
    STR = STRFSTRUCT(PATTERN, REPSTRUCT) replaces structure field specifiers in
    string PATTERN with the corresponding field values in struct REPSTRUCT.
    Recognized field specifier keys match the fields in structure REPSTRUCT.
    They are names of fields of REPSTRUCT in capital letters, enclosed in curly
    braces and prefixed with a dollar sign. Specifiers may also include a comma
    separated list of modifiers, separated from the specifier key by a comma. 
    These modifiers are intended to allow transformations affecting the format 
    of the replacement value. The transformations are applied sequentially in 
    the same order as they appear in the specifier, from left to right.
    Each transformation is applied to the output of the previous one, starting
    from the value of the field of REPSTRUCT matching the specifier key,
    or the empty string if there is no such field. Finally, if the resulting 
    replacement value is a numeric value, it is converted to string by function
    NUM2STRING before applying the replacement. Recognized modifiers are:
      %...: string conversion with desired format using SPRINTF.
        The current replacement value is passed to function SPRINTF using the
        modifier value as format string.
        Example:
          '${DEPLOYMENT_ID,%04d}' with REPSTRUCT.DEPLOYMENT_ID=2 
           is replaced by 0002.
      l: lower case conversion.
        The current replacement value is converted to lower case by passing it
        to function LOWER.
        Example:
          '${GLIDER_NAME,l}' with REPSTRUCT.GLIDER_NAME='DEEPY' 
          is replaced by 'deepy'.
      U: upper case conversion.
        The current replacement value is converted to upper case by passing it
        to function UPPER.
        Example:
          '${GLIDER_NAME,U}' with REPSTRUCT.GLIDER_NAME='deepy' 
           is replaced by 'DEEPY'.
      T...: time string representation.
        The current replacement value is passed to function DATESTR using the
        the modifier value as format string with leading T removed.
        Example:
          ${DEPLOYMENT_END,Tyyyymmmdd} 
          with REPSTRUCT.DEPLOYMENT_END=datenum([2001 01 17 0 0 0]) 
          is replaced by '2001Jan17'.
      s/.../...: regular expression replacement.
        The current replacement value is passed to function REGEXPREP to
        replace the occurrences of a pattern subexpression by a replacement,
        both specified in the modifier as substrings separated by a delimiter.
        The delimiter is the second character in the modifier (here is '/',
        but any other character may be used). The pattern is the substring
        between the first and the second occurrence of the delimiter in the 
        modifier. The replacement is the substring starting right after thes
        second occurence of the delimiter until the end of the modifier.
        If the replacement is the null string, the second delimiter is optional
        and subexpressions in the current replacement value matching the
        pattern are deleted.
        Example:
          ${GLIDER_NAME,s/(-|\s*)/_}
          with REPSTRUCT.GLIDER_NAME='complex-compound  glider name'
          is replaced by 'complex_compound_glider_name'.
      @...: named conversion function.
        The current replacement value is passed to the function named by the
        modifier value with the leading @ removed. This is useful to specify 
        custom formatters for more advanced conversions.
        Example:
          ${DEPLOYMENT_ID,@dec2bin} 
          with REPSTRUCT.DEPLOYMENT_ID=2 
          is replaced by 'C'.
    
  Notes:
    This function is inspired by the C function STRFTIME, the shell 
    command DATE, and Bash parameter expansion.

  Examples:
    deployment.deployment_id = 2;
    deployment.deployment_name = 'funnymission';
    deployment.glider_name = 'happyglider';
    deployment.glider_deployment_code = '0001';
    deployment.deployment_start = datenum([2000 1 1 0 0 0]);
    deployment.deployment_end =  datenum([2001 1 1 0 0 0]);
    pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_NAME}'
    str = strfstruct(pattern, deployment)
    nums_pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_ID,%04d}'
    nums_str = strfstruct(nums_pattern, deployment)
    case_pattern = '/base/path/${GLIDER_NAME,U}/${DEPLOYMENT_NAME}'
    case_str = strfstruct(case_pattern, deployment)
    date_pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_START,Tyyyy-mm-dd}'
    date_str = strfstruct(date_pattern, deployment)
    subs_pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_NAME,s/funny/boring}'
    subs_str = strfstruct(subs_pattern, deployment)
    func_pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_ID,@dec2bin}'
    func_str = strfstruct(func_pattern, deployment)

  See also:
    SPRINTF
    LOWER
    UPPER
    DATESTR
    REGEXPREP
    STR2FUNC

  Authors:
    Joan Pau Beltran  <joanpau.beltran@socib.cat>

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

strfstruct.m

SOURCE CODE ^

0001 function str = strfstruct(pattern, repstruct)
0002 %STRFSTRUCT  Format structure as string.
0003 %
0004 %  Syntax:
0005 %    STR = STRFSTRUCT(PATTERN, REPSTRUCT)
0006 %
0007 %  Description:
0008 %    STR = STRFSTRUCT(PATTERN, REPSTRUCT) replaces structure field specifiers in
0009 %    string PATTERN with the corresponding field values in struct REPSTRUCT.
0010 %    Recognized field specifier keys match the fields in structure REPSTRUCT.
0011 %    They are names of fields of REPSTRUCT in capital letters, enclosed in curly
0012 %    braces and prefixed with a dollar sign. Specifiers may also include a comma
0013 %    separated list of modifiers, separated from the specifier key by a comma.
0014 %    These modifiers are intended to allow transformations affecting the format
0015 %    of the replacement value. The transformations are applied sequentially in
0016 %    the same order as they appear in the specifier, from left to right.
0017 %    Each transformation is applied to the output of the previous one, starting
0018 %    from the value of the field of REPSTRUCT matching the specifier key,
0019 %    or the empty string if there is no such field. Finally, if the resulting
0020 %    replacement value is a numeric value, it is converted to string by function
0021 %    NUM2STRING before applying the replacement. Recognized modifiers are:
0022 %      %...: string conversion with desired format using SPRINTF.
0023 %        The current replacement value is passed to function SPRINTF using the
0024 %        modifier value as format string.
0025 %        Example:
0026 %          '${DEPLOYMENT_ID,%04d}' with REPSTRUCT.DEPLOYMENT_ID=2
0027 %           is replaced by 0002.
0028 %      l: lower case conversion.
0029 %        The current replacement value is converted to lower case by passing it
0030 %        to function LOWER.
0031 %        Example:
0032 %          '${GLIDER_NAME,l}' with REPSTRUCT.GLIDER_NAME='DEEPY'
0033 %          is replaced by 'deepy'.
0034 %      U: upper case conversion.
0035 %        The current replacement value is converted to upper case by passing it
0036 %        to function UPPER.
0037 %        Example:
0038 %          '${GLIDER_NAME,U}' with REPSTRUCT.GLIDER_NAME='deepy'
0039 %           is replaced by 'DEEPY'.
0040 %      T...: time string representation.
0041 %        The current replacement value is passed to function DATESTR using the
0042 %        the modifier value as format string with leading T removed.
0043 %        Example:
0044 %          ${DEPLOYMENT_END,Tyyyymmmdd}
0045 %          with REPSTRUCT.DEPLOYMENT_END=datenum([2001 01 17 0 0 0])
0046 %          is replaced by '2001Jan17'.
0047 %      s/.../...: regular expression replacement.
0048 %        The current replacement value is passed to function REGEXPREP to
0049 %        replace the occurrences of a pattern subexpression by a replacement,
0050 %        both specified in the modifier as substrings separated by a delimiter.
0051 %        The delimiter is the second character in the modifier (here is '/',
0052 %        but any other character may be used). The pattern is the substring
0053 %        between the first and the second occurrence of the delimiter in the
0054 %        modifier. The replacement is the substring starting right after thes
0055 %        second occurence of the delimiter until the end of the modifier.
0056 %        If the replacement is the null string, the second delimiter is optional
0057 %        and subexpressions in the current replacement value matching the
0058 %        pattern are deleted.
0059 %        Example:
0060 %          ${GLIDER_NAME,s/(-|\s*)/_}
0061 %          with REPSTRUCT.GLIDER_NAME='complex-compound  glider name'
0062 %          is replaced by 'complex_compound_glider_name'.
0063 %      @...: named conversion function.
0064 %        The current replacement value is passed to the function named by the
0065 %        modifier value with the leading @ removed. This is useful to specify
0066 %        custom formatters for more advanced conversions.
0067 %        Example:
0068 %          ${DEPLOYMENT_ID,@dec2bin}
0069 %          with REPSTRUCT.DEPLOYMENT_ID=2
0070 %          is replaced by 'C'.
0071 %
0072 %  Notes:
0073 %    This function is inspired by the C function STRFTIME, the shell
0074 %    command DATE, and Bash parameter expansion.
0075 %
0076 %  Examples:
0077 %    deployment.deployment_id = 2;
0078 %    deployment.deployment_name = 'funnymission';
0079 %    deployment.glider_name = 'happyglider';
0080 %    deployment.glider_deployment_code = '0001';
0081 %    deployment.deployment_start = datenum([2000 1 1 0 0 0]);
0082 %    deployment.deployment_end =  datenum([2001 1 1 0 0 0]);
0083 %    pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_NAME}'
0084 %    str = strfstruct(pattern, deployment)
0085 %    nums_pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_ID,%04d}'
0086 %    nums_str = strfstruct(nums_pattern, deployment)
0087 %    case_pattern = '/base/path/${GLIDER_NAME,U}/${DEPLOYMENT_NAME}'
0088 %    case_str = strfstruct(case_pattern, deployment)
0089 %    date_pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_START,Tyyyy-mm-dd}'
0090 %    date_str = strfstruct(date_pattern, deployment)
0091 %    subs_pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_NAME,s/funny/boring}'
0092 %    subs_str = strfstruct(subs_pattern, deployment)
0093 %    func_pattern = '/base/path/${GLIDER_NAME}/${DEPLOYMENT_ID,@dec2bin}'
0094 %    func_str = strfstruct(func_pattern, deployment)
0095 %
0096 %  See also:
0097 %    SPRINTF
0098 %    LOWER
0099 %    UPPER
0100 %    DATESTR
0101 %    REGEXPREP
0102 %    STR2FUNC
0103 %
0104 %  Authors:
0105 %    Joan Pau Beltran  <joanpau.beltran@socib.cat>
0106 
0107 %  Copyright (C) 2013-2016
0108 %  ICTS SOCIB - Servei d'observacio i prediccio costaner de les Illes Balears
0109 %  <http://www.socib.es>
0110 %
0111 %  This program is free software: you can redistribute it and/or modify
0112 %  it under the terms of the GNU General Public License as published by
0113 %  the Free Software Foundation, either version 3 of the License, or
0114 %  (at your option) any later version.
0115 %
0116 %  This program is distributed in the hope that it will be useful,
0117 %  but WITHOUT ANY WARRANTY; without even the implied warranty of
0118 %  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0119 %  GNU General Public License for more details.
0120 %
0121 %  You should have received a copy of the GNU General Public License
0122 %  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0123 
0124   error(nargchk(2, 2, nargin, 'struct'));
0125 
0126   repflds = fieldnames(repstruct);
0127   repvals = struct2cell(repstruct);
0128   repkeys = upper(repflds);
0129   specesc = regexptranslate('escape', '$');
0130   specbeg = regexptranslate('escape', '{');
0131   specend = regexptranslate('escape', '}');
0132   specsep = regexptranslate('escape', ',');
0133   specexp = ...
0134     [specesc specbeg ...
0135      '((?:[^' specesc specend ']|' specesc '[' specesc specsep specend '])*)' ...
0136      specend];
0137   specelemexp = ...
0138     ['(?:[^' specsep specesc specend ']|' specesc '[' specsep specesc specend '])*'];
0139   specescexp = [ specesc '([' specsep specesc specend '])' ];
0140   specescrep = '$1';
0141   [token_list, split_list] = regexp(pattern, specexp, 'tokens', 'split');
0142   for token_idx = 1:numel(token_list)
0143     token = token_list{token_idx}{1};
0144     subst = '';
0145     match_list = ...
0146       regexprep(regexp(token, specelemexp, 'match'), specescexp, specescrep);
0147     key = match_list{1};
0148     key_select = strcmp(key, repkeys);
0149     if any(key_select)
0150       subst = repvals{key_select};
0151     end
0152     for match_idx = 2:numel(match_list)
0153       modifier = match_list{match_idx};
0154       switch modifier(1)
0155         case '%'
0156           subst = sprintf(modifier, subst);
0157         case 'l'
0158           subst = lower(subst);
0159         case 'U'
0160           subst = upper(subst);
0161         case 'T'
0162           subst = datestr(subst, modifier(2:end));
0163         case 's'
0164           substdelim = modifier(2);
0165           substbound = 2 + find(modifier(3:end) == substdelim, 1, 'first');
0166           if isempty(substbound)
0167             substpatstr = modifier(3:end);
0168             substrepstr = '';
0169           else
0170             substpatstr = modifier(3:substbound(1)-1);
0171             substrepstr = modifier(substbound(1)+1:end);
0172           end
0173           subst = regexprep(subst, substpatstr, substrepstr);
0174         case '@'
0175           substfunc = str2func(modifier(2:end));
0176           subst = substfunc(subst);
0177       end
0178       if isnumeric(subst)
0179         subst = num2str(subst);
0180       end
0181     end
0182     split_list{2, token_idx} = subst;
0183   end
0184   str = [split_list{:}];
0185   
0186 end
0187

Generated on Fri 06-Oct-2017 10:47:42 by m2html © 2005