parsercmd/src/clconvert.cxx
// ===========================================================================
// Parses an option token, e.g. /Tc, -w, -I, --bool
// Returns true if successful, in which case 'opt' is set to one or more
// translation actions.  Multiple actions indicate flag options which do not
// deal with arguments.  A single action may involve arguments.
//
Parses an option token, e.g. /Tc, -w, -I, --bool
Returns true if successful, in which case 'opt' is set to one or more
translation actions.  Multiple actions indicate flag options which do not
deal with arguments.  A single action may involve arguments.
Parses an option token e . g . / Tc - w - I -- bool Returns true if successful in which case ' opt ' is set to one or more translation actions . Multiple actions indicate flag options which do not deal with arguments . A single action may involve arguments .
bool CommandLineConverter::option_token(queue<const OptionInterpreter *> &opt) {
    if (fault) return false;
    const string *flagintro = interp->flagintro();
    bool maybe_flag = flagintro != NULL;
    int n;
    for (n = 0; n < num_nameintros; n += 1) {
	maybe_name[n] = true;
    }
    bool is_flag = false; 
    int is_name_eg; 
    for (n = 0; n < num_nameintros; n += 1) {
        is_name[n] = false; 
    }
    int i = 0;
    int fil = maybe_flag ? (int)flagintro->length() : -1;
    for (;;) {
        for (n = 0; n < num_nameintros; n += 1) {
	    if (maybe_name[n] && intro_len[n] == i) {
		is_name[n] = true;
	    }
	}
	if (maybe_flag && fil == i) {
	    is_flag = true;
	}
	bool need_more_look = maybe_flag && !is_flag;
        for (n = 0; n < num_nameintros; n += 1) {
	    if (maybe_name[n] && !is_name[n]) {
		need_more_look = true;
	    }
	}
	is_name_eg = -1;
	for (n = 0; n < num_nameintros; n += 1) {
	    if (is_name[n]) {
		is_name_eg = n;
	    }
	}
    	if (!need_more_look && (is_flag || is_name_eg >= 0)) {
	    break;
	}
	if (istrm == NULL) {
	    return false;
	}
	char ch;
	istrm->get(ch);
	if (istrm->eof()) {
	    if (is_flag || is_name_eg >= 0) {
		break;
	    }
	    if (i != 0) {
		fault = true;
	    }
	    return false;
	}
	bool some_maybe_name = false;
	for (n = 0; n < num_nameintros; n += 1) {
	    maybe_name[n] = maybe_name[n] && i < intro_len[n] && (nameintros[n].at(i) == ch);
	    if (maybe_name[n]) {
		some_maybe_name = true;
	    }
	}
	maybe_flag = maybe_flag && i < fil && (flagintro->at(i) == ch);
	i += 1;
	if (!(maybe_flag || some_maybe_name)) {
	    istrm->putback(ch);
	    i -= 1;
	    if (is_name_eg >= 0 || is_flag) {
		break;
	    }
	    if (i != 0) {
		fault = true;
	    }
	    return false;
	}
	for (n = 0; n < num_nameintros; n += 1) {
	    is_name[n] = false;
	}
	is_flag = false;
    }
    string optname;
    bool flag_found = false;
    bool non_flag_found = false;
    for (;;) {
        for (n = 0; n < num_nameintros; n += 1) {
	    if (is_name[n]) {
		const OptionInterpreter *optp = name_selectors[n]->named_option(optname);
		if (optp && optp->immediate_arg() && at_argument()) {
		    opt = queue<const OptionInterpreter *>(); 
		    opt.push(optp);
		    return true;
		}
		if (optp && (optp->separate_arg() || optp->no_arg() || optp->swallow_remaining())
			 && at_whitespace()) {
		    opt = queue<const OptionInterpreter *>(); 
		    opt.push(optp);
		    return true;
		}
	    }
	}
	if (at_whitespace()) {
	    if (flag_found && !non_flag_found) {
		return true;
	    }
	    string opt = is_flag ? *flagintro : nameintros[is_name_eg];
	    opt.append(optname);
	    return unrecognized_option(opt);
	}
	char ch;
	istrm->get(ch);
	optname.append(1, ch);
	if (is_flag) {
	    const OptionInterpreter *optp = interp->flag_option(ch);
	    if (optp && optp->no_arg()) {
		opt.push(optp);
		flag_found = true;
	    }
	    else {
		non_flag_found = true;
	    }
	}
    }
}
bool CommandLineConverter :: option_token ( queue < const OptionInterpreter * > & opt ) { if ( fault ) return false ; const string * flagintro = interp -> flagintro ( ) ; bool maybe_flag = flagintro != NULL ; int n ; for ( n = 0 ; n < num_nameintros ; n += 1 ) { maybe_name [ n ] = true ; } bool is_flag = false ; int is_name_eg ; for ( n = 0 ; n < num_nameintros ; n += 1 ) { is_name [ n ] = false ; } int i = 0 ; int fil = maybe_flag ? ( int ) flagintro -> length ( ) : -1 ; for ( ; ; ) { for ( n = 0 ; n < num_nameintros ; n += 1 ) { if ( maybe_name [ n ] && intro_len [ n ] == i ) { is_name [ n ] = true ; } } if ( maybe_flag && fil == i ) { is_flag = true ; } bool need_more_look = maybe_flag && ! is_flag ; for ( n = 0 ; n < num_nameintros ; n += 1 ) { if ( maybe_name [ n ] && ! is_name [ n ] ) { need_more_look = true ; } } is_name_eg = -1 ; for ( n = 0 ; n < num_nameintros ; n += 1 ) { if ( is_name [ n ] ) { is_name_eg = n ; } } if ( ! need_more_look && ( is_flag || is_name_eg >= 0 ) ) { break ; } if ( istrm == NULL ) { return false ; } char ch ; istrm -> get ( ch ) ; if ( istrm -> eof ( ) ) { if ( is_flag || is_name_eg >= 0 ) { break ; } if ( i != 0 ) { fault = true ; } return false ; } bool some_maybe_name = false ; for ( n = 0 ; n < num_nameintros ; n += 1 ) { maybe_name [ n ] = maybe_name [ n ] && i < intro_len [ n ] && ( nameintros [ n ] . at ( i ) == ch ) ; if ( maybe_name [ n ] ) { some_maybe_name = true ; } } maybe_flag = maybe_flag && i < fil && ( flagintro -> at ( i ) == ch ) ; i += 1 ; if ( ! ( maybe_flag || some_maybe_name ) ) { istrm -> putback ( ch ) ; i -= 1 ; if ( is_name_eg >= 0 || is_flag ) { break ; } if ( i != 0 ) { fault = true ; } return false ; } for ( n = 0 ; n < num_nameintros ; n += 1 ) { is_name [ n ] = false ; } is_flag = false ; } string optname ; bool flag_found = false ; bool non_flag_found = false ; for ( ; ; ) { for ( n = 0 ; n < num_nameintros ; n += 1 ) { if ( is_name [ n ] ) { const OptionInterpreter * optp = name_selectors [ n ] -> named_option ( optname ) ; if ( optp && optp -> immediate_arg ( ) && at_argument ( ) ) { opt = queue < const OptionInterpreter * > ( ) ; opt . push ( optp ) ; return true ; } if ( optp && ( optp -> separate_arg ( ) || optp -> no_arg ( ) || optp -> swallow_remaining ( ) ) && at_whitespace ( ) ) { opt = queue < const OptionInterpreter * > ( ) ; opt . push ( optp ) ; return true ; } } } if ( at_whitespace ( ) ) { if ( flag_found && ! non_flag_found ) { return true ; } string opt = is_flag ? * flagintro : nameintros [ is_name_eg ] ; opt . append ( optname ) ; return unrecognized_option ( opt ) ; } char ch ; istrm -> get ( ch ) ; optname . append ( 1 , ch ) ; if ( is_flag ) { const OptionInterpreter * optp = interp -> flag_option ( ch ) ; if ( optp && optp -> no_arg ( ) ) { opt . push ( optp ) ; flag_found = true ; } else { non_flag_found = true ; } } } }
Parses an option token, e.g.
Parses an option token e . g .
// true if flagintro detected // -1, or an example nameintro if any detected // true if nameintros[n] detected // nameintro and flagintro are the character sequences which introduce // named options and flag options respectively.  For example,  // *flagintro == "-" and *nameintro == "--" is possible, or else // *nameintro == "/" and flagintro == NULL would be possible. // Test whether we have a name or a flag. // Determine whether further examination of the option is needed. // We're trying just to read characters until a nameintro or flagintro // is recognized. The tricky case occurs if one is the prefix of the other. // In that case, we might have to read a character to check on the longer // possibility, and then back up. // Look at the next character. // We have a valid prefix. // We can't back up to restore the parse state to where we started. // Determine whether, given the character ch, it is still possible  // to find the name prefix or the flag prefix. // Neither is possible; read no more characters. // We have backed up to a valid prefix. // We can't back up to restore the parse state to where we started. // We have found the name prefix, or the flag prefix, or both. // Advance through the option name. At each character, check //   1. If a name option is complete, done. //   2. If at the end of the option name, done. //   3. If the character is a flag option, record it. // Clear out flag options. // Clear out flag options. // Don't need to check for istrm->eof() because at_whitespace did it.